From 38779667c4ba286c80e0bb5c673c710ca4200cfa Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Tue, 22 Oct 2024 16:54:07 +0300 Subject: [PATCH] Adding groups of paths --- src/Assets/Asset.php | 45 +++++++++++++++++-- src/Assets/Config.php | 100 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 123 insertions(+), 22 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 4c5631a..a32500e 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -175,6 +175,21 @@ class Asset { */ protected string $root_path = ''; + /** + * The path group name for this asset. + * + * A path group is a group of assets that share the same path which could be different that the root path or the asset's path. + * + * The order of priority goes like this: + * + * 1. If a specific root path is set, that will be used. + * 2. If a path group is set, that will be used. + * 3. Otherwise, the root path will be used. + * + * @var string + */ + protected string $group_path_name = ''; + /** * Content or callable that should be printed after the asset. * @@ -285,6 +300,19 @@ public function __construct( string $slug, string $file, string $version = null, $this->infer_type(); } + /** + * Adds the asset to a group path. + * + * @since TBD + * + * @return static + */ + public function add_to_group_path( string $group_path_name ) { + $this->group_path_name = $group_path_name; + + return $this; + } + /** * Registers an asset. * @@ -630,7 +658,7 @@ public function clone_to( string $clone_type, ...$dependencies ) { $slug, str_replace( ".{$source_type}", ".{$clone_type}", $this->file ), $this->version, - $this->root_path + $this->get_root_path() ); $condition = $this->get_condition(); @@ -910,7 +938,8 @@ public function get_min_url(): string { */ public function get_path(): string { if ( $this->path === null ) { - return Config::get_relative_asset_path(); + $group_relative = $this->group_path_name ? Config::get_relative_path_of_group_path( $this->group_path_name ) : ''; + return $group_relative ? $group_relative : Config::get_relative_asset_path(); } return $this->path; @@ -922,7 +951,17 @@ public function get_path(): string { * @return ?string */ public function get_root_path(): ?string { - return $this->root_path; + if ( ! $this->group_path_name ) { + return $this->root_path; + } + + if ( $this->root_path !== Config::get_path() ) { + return $this->root_path; + } + + $group_path = Config::get_path_of_group_path( $this->group_path_name ); + + return $group_path ? $group_path : $this->root_path; } /** diff --git a/src/Assets/Config.php b/src/Assets/Config.php index 39291df..7257608 100644 --- a/src/Assets/Config.php +++ b/src/Assets/Config.php @@ -19,6 +19,11 @@ class Config { */ protected static string $root_path = ''; + /** + * @var array> + */ + protected static array $group_paths = []; + /** * @var string */ @@ -42,6 +47,55 @@ public static function get_hook_prefix(): string { return static::$hook_prefix; } + /** + * Gets the root path of a group. + * + * @since TBD + * + * @return string + */ + public static function get_path_of_group_path( string $group ): string { + return ( static::$group_paths[ $group ] ?? [] )['root'] ?? ''; + } + + /** + * Gets the relative path of a group. + * + * @since TBD + * + * @return string + */ + public static function get_relative_path_of_group_path( string $group ): string { + return ( static::$group_paths[ $group ] ?? [] )['relative'] ?? ''; + } + + /** + * Adds a group path. + * + * @since TBD + * + * @throws RuntimeException If the root or relative path is not specified. + * + * @param string $group_path_slug The slug of the group path. + * @param array $paths The paths of the group path. + * + * @return void + */ + public static function add_group_path( string $group_path_slug, array $paths ): void { + if ( empty( $paths['root'] ) || ! is_string( $paths['root'] ) ) { + throw new RuntimeException( 'You must specify a root path for the group path' ); + } + + if ( empty( $paths['relative'] ) || ! is_string( $paths['relative'] ) ) { + throw new RuntimeException( 'You must specify a relative path for the group path' ); + } + + $paths['root'] = self::normalize_path( $paths['root'] ); + $paths['relative'] = trailingslashit( $paths['relative'] ); + + static::$group_paths[ $group_path_slug ] = $paths; + } + /** * Gets the root path of the project. * @@ -131,16 +185,35 @@ public static function set_relative_asset_path( string $path ) { * @return void */ public static function set_path( string $path ) { - $plugin_dir = WP_PLUGIN_DIR; + static::$root_path = self::normalize_path( $path ); + } - if ( DIRECTORY_SEPARATOR !== '/' ) { - $plugin_dir = str_replace( DIRECTORY_SEPARATOR, '/', $plugin_dir ); - // Because the $path passed can be a constant like plugin_dir_path( __FILE__ ), we should check and replace the slash in the $path too. - $path = str_replace( DIRECTORY_SEPARATOR, '/', $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 TBD + * + * @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 = strpos( $path, $plugin_dir ); - $themes_content_dir_position = strpos( $path, get_theme_root() ); + $themes_content_dir_position = strpos( $path, wp_normalize_path( get_theme_root() ) ); if ( $plugins_content_dir_position === false @@ -154,17 +227,6 @@ public static function set_path( string $path ) { $path = substr( $path, $themes_content_dir_position ); } - static::$root_path = trailingslashit( $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; + return trailingslashit( $path ); } }