( static::$path_urls[ $key ] ) ) { $bases = Utils::get_bases(); $base_dirs = array_column( $bases, 'base_dir' ); $base_urls = array_column( $bases, 'base_url' ); uasort( $base_dirs, function( $a, $b ) { return strlen( $b ) <=> strlen( $a ); } ); foreach ( $base_dirs as $dir_type => $dir_path ) { if ( ! isset( $base_urls[ $dir_type ] ) ) { continue; } static::$path_urls[ $key ] = str_replace( $dir_path, $base_urls[ $dir_type ], $path ); if ( static::$path_urls[ $key ] !== $path ) { break; } } } return static::$path_urls[ $key ]; } /** * Gets the version of the project. * * @return string */ public static function get_version(): string { return static::$version; } /** * Resets this class back to the defaults. */ public static function reset() { static::$hook_prefix = ''; static::$relative_asset_path = 'src/assets/'; static::$root_path = ''; static::$path_urls = []; static::$version = ''; Utils::clear_runtime_cache(); } /** * Sets the hook prefix. * * @param string $prefix The prefix to add to hooks. * * @return void */ public static function set_hook_prefix( string $prefix ) { static::$hook_prefix = $prefix; } /** * Sets the relative asset path of the project. * * @param string $path The root path of the project. * * @return void */ public static function set_relative_asset_path( string $path ) { static::$relative_asset_path = trailingslashit( $path ); } /** * Sets the root path of the project. * * @param string $path The root path of the project. * * @return void */ public static function set_path( string $path ) { static::$root_path = self::normalize_path( $path ); } /** * Sets the version of the project. * * @param string $version The version of the project. * * @return void */ public static function set_version( string $version ) { static::$version = $version; } /** * Normalizes a path. * * @since 1.4.0 * @since 1.4.1 Allow for paths that are not in the plugin or theme directory. * * @param string $path The path to normalize. * * @return string */ protected static function normalize_path( string $path ): string { $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR ); $path = wp_normalize_path( $path ); $plugins_content_dir_position = $plugin_dir ? strpos( $path, $plugin_dir ) : false; $themes_content_dir_position = strpos( $path, wp_normalize_path( get_theme_root() ) ); if ( $plugins_content_dir_position === false && $themes_content_dir_position === false && strpos( $path, '/' ) !== 0 ) { // Default to plugins if a relative path is provided. $path = trailingslashit( $plugin_dir ) . $path; } elseif ( $plugins_content_dir_position !== false ) { $path = substr( $path, $plugins_content_dir_position ); } elseif ( $themes_content_dir_position !== false ) { $path = substr( $path, $themes_content_dir_position ); } return trailingslashit( $path ); } }