-`. * * Derived from the kit version and from the copy's own namespace * (`__NAMESPACE__` is rewritten by a prefixer, and is a different string * in every plugin's copy), so two copies -- even at one version -- never * register the same handle with two different URLs, and a copy never * inherits another copy's URL or `ver=`. Deterministic: same copy, same * handle on every request. */ public static function handle(): string { return '404ops-ui-' . trim( (string) preg_replace( '/[^a-z0-9]+/i', '-', self::VERSION ), '-' ) . '-' . hash( 'crc32b', __NAMESPACE__ ); } /** * Register + enqueue this copy's stylesheet. * * The stylesheet is located relative to THIS class file, so it resolves * wherever the plugin bundles the kit (`vendor/`, `vendor-prefixed/`, a * dev checkout) -- `plugins_url()` alone cannot express every location, * so we map the on-disk path to a URL via WP_CONTENT_DIR -> content_url(). * * Loaded once per kit VERSION per request: when another copy at this exact * version has already enqueued its (byte-identical) stylesheet on this * request, this copy adds nothing. Different versions each load their own * sheet -- see docs/MENU-CONTRACT.md "Assets" for why that is safe. */ public static function enqueue(): void { if ( ! function_exists( 'wp_enqueue_style' ) ) { return; } $handle = self::handle(); $styles = $GLOBALS[ Contract::STYLES_KEY ] ?? array(); $taken = is_array( $styles ) ? ( $styles[ self::VERSION ] ?? null ) : null; if ( is_string( $taken ) && $taken !== $handle && ( wp_style_is( $taken, 'enqueued' ) || wp_style_is( $taken, 'done' ) ) ) { return; } if ( ! wp_style_is( $handle, 'registered' ) ) { // assets/404ops-ui.css sits one level up from this src/ directory. $css_url = self::path_to_url( dirname( __DIR__ ) . '/assets/404ops-ui.css' ); // Outside wp-content (a symlinked checkout): let WordPress derive // the URL from the plugin path. plugin_basename() is string-only, // so the named file need not exist. if ( '' === $css_url && function_exists( 'plugins_url' ) ) { $css_url = plugins_url( 'assets/404ops-ui.css', dirname( __DIR__ ) . '/ui-kit.php' ); } if ( '' !== $css_url ) { wp_register_style( $handle, $css_url, array(), self::VERSION ); } } wp_enqueue_style( $handle ); if ( ! is_array( $styles ) ) { $styles = array(); } $styles[ self::VERSION ] = $handle; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- the key is a contract constant, deliberately unprefixed so every copy agrees on it (see Contract's class docblock). $GLOBALS[ Contract::STYLES_KEY ] = $styles; } /** * Map an absolute on-disk path under wp-content to its public URL. * Works for plugins/, mu-plugins/, and vendored locations alike. * * @param string $path Absolute filesystem path. * @return string Public URL, or '' if the path is not under wp-content. */ private static function path_to_url( string $path ): string { if ( ! defined( 'WP_CONTENT_DIR' ) || ! function_exists( 'content_url' ) ) { return ''; } $content_dir = wp_normalize_path( WP_CONTENT_DIR ); $path = wp_normalize_path( $path ); if ( 0 !== strpos( $path, $content_dir ) ) { return ''; } return content_url( substr( $path, strlen( $content_dir ) ) ); } }