From 42a2f4e98c88588edc971fc9286c38c04a56d95b Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Mon, 11 Nov 2024 15:39:33 +0200 Subject: [PATCH 1/7] Support asset path prefixing based on group path --- src/Assets/Asset.php | 2 ++ src/Assets/Config.php | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 0e164f0..888cd7d 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -310,6 +310,8 @@ public function __construct( string $slug, string $file, string $version = null, public function add_to_group_path( string $group_path_name ) { $this->group_path_name = $group_path_name; + $this->prefix_asset_directory( Config::is_group_path_using_asset_directory_prefix( $this->group_path_name ) ); + return $this; } diff --git a/src/Assets/Config.php b/src/Assets/Config.php index 0811d75..d1260d7 100644 --- a/src/Assets/Config.php +++ b/src/Assets/Config.php @@ -69,23 +69,37 @@ public static function get_relative_path_of_group_path( string $group ): string return ( static::$group_paths[ $group ] ?? [] )['relative'] ?? ''; } + /** + * Gets whether the group is using the asset directory prefix. + * + * @since 1.4.2 + * + * @return bool + */ + public static function is_group_path_using_asset_directory_prefix( string $group ): bool { + return ( static::$group_paths[ $group ] ?? [] )['prefix'] ?? false; + } + /** * Adds a group path. * * @since 1.4.0 + * @since 1.4.2 Added the ability to specify whether the group path is using the asset directory prefix. * * @throws RuntimeException If the root or relative path is not specified. * - * @param string $group_path_slug The slug of the group path. - * @param string $root The root path of the group. - * @param string $relative The relative path of the group. + * @param string $group_path_slug The slug of the group path. + * @param string $root The root path of the group. + * @param string $relative The relative path of the group. + * @param bool $is_using_asset_directory_prefix Whether the group path is using the asset directory prefix. * * @return void */ - public static function add_group_path( string $group_path_slug, string $root, string $relative ): void { + public static function add_group_path( string $group_path_slug, string $root, string $relative, bool $is_using_asset_directory_prefix = false ): void { static::$group_paths[ $group_path_slug ] = [ 'root' => self::normalize_path( $root ), 'relative' => trailingslashit( $relative ), + 'prefix' => $is_using_asset_directory_prefix, ]; } From c84fe22acf6db9583201881bc72c8556dc3d92d3 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Mon, 11 Nov 2024 15:39:40 +0200 Subject: [PATCH 2/7] version bump --- assets.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets.php b/assets.php index 0fa8fb5..3f1553c 100644 --- a/assets.php +++ b/assets.php @@ -2,7 +2,7 @@ /** * Plugin Name: Assets * Description: Asset library with a plugin bootstrap file for automated testing. - * Version: 1.4.1 + * Version: 1.4.2 * Author: StellarWP * Author URI: https://stellarwp.com */ From 0d5194c19b140ab4e508ee2cfed427de678f9fff Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Mon, 11 Nov 2024 15:49:13 +0200 Subject: [PATCH 3/7] Added new test cases --- tests/wpunit/AssetsTest.php | 51 ++++++++++++++++++++++++++++++++++--- tests/wpunit/ConfigTest.php | 8 ++++-- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/tests/wpunit/AssetsTest.php b/tests/wpunit/AssetsTest.php index c8bedf4..27709dc 100644 --- a/tests/wpunit/AssetsTest.php +++ b/tests/wpunit/AssetsTest.php @@ -148,7 +148,7 @@ public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content Config::set_version( '1.1.0' ); Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/assets' ); Config::set_relative_asset_path( 'tests/_data/' ); - Config::add_group_path( 'fake-group-path', constant( 'WP_PLUGIN_DIR' ) . '/assets/tests', '_data/fake-feature' ); + Config::add_group_path( 'fake-group-path', constant( 'WP_PLUGIN_DIR' ) . '/assets/tests', '_data/fake-feature', true ); foreach ( array_keys( $slugs ) as $slug ) { Asset::add( $slug . '-script', $slug . '.js' )->add_to_group_path( 'fake-group-path' ); @@ -161,6 +161,47 @@ public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content } } + /** + * @test + * + * @dataProvider constantProvider + */ + public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content_url_are_diff_and_assets_are_in_asset_group_without_prefixing( $id, $constants ) { + $slugs = [ + 'fake1' => [ true, false ], + 'fake2' => [ false, false ], + 'fake3' => [ true, true ] + ]; + + foreach ( array_keys( $slugs ) as $slug ) { + Assets::init()->remove( $slug . '-script' ); + Assets::init()->remove( $slug . '-style' ); + } + + foreach ( $constants as $constant => $value ) { + $this->set_const_value( $constant, $value ); + $this->assertEquals( $value, constant( $constant ) ); + } + + Config::reset(); + + Config::set_hook_prefix( 'bork' ); + Config::set_version( '1.1.0' ); + Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/assets' ); + Config::set_relative_asset_path( 'tests/_data/' ); + Config::add_group_path( 'fake-group-path', constant( 'WP_PLUGIN_DIR' ) . '/assets/tests', '_data/fake-feature' ); + + foreach ( array_keys( $slugs ) as $slug ) { + Asset::add( $slug . '-script', $slug . '.js' )->add_to_group_path( 'fake-group-path' ); + Asset::add( $slug . '-style', $slug . '.css' )->add_to_group_path( 'fake-group-path' ); + } + + foreach ( $slugs as $slug => $data ) { + $this->assert_minified_found( $slug, true, $data['0'], $data['1'], $id, 'fake-group-path', '/assets/tests/_data/fake-feature/', false, false ); + $this->assert_minified_found( $slug, false, $data['0'], $data['1'], $id, 'fake-group-path', '/assets/tests/_data/fake-feature/', false, false ); + } + } + /** * @test * @@ -190,7 +231,7 @@ public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/assets' ); Config::set_relative_asset_path( 'tests/_data/' ); // Now our scripts are using a path that does not actually exist in the filesystem. So we can't expect it to figure out minified vs un-minified. So we are adding a new param. - Config::add_group_path( 'fake-group-path', constant( 'WP_PLUGIN_DIR' ) . '/another-plugin/ecp', 'random/feature' ); + Config::add_group_path( 'fake-group-path', constant( 'WP_PLUGIN_DIR' ) . '/another-plugin/ecp', 'random/feature', true ); foreach ( array_keys( $slugs ) as $slug ) { Asset::add( $slug . '-script', $slug . '.js' )->add_to_group_path( 'fake-group-path' ); @@ -902,11 +943,13 @@ protected function existence_assertions( $test_slug_prefix ) { * @param string $id * @param string $add_to_path_group * @param string $group_path_path + * @param bool $wont_figure_out_min_vs_unmin + * @param bool $should_prefix */ - protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min = true, $has_only_min = false, $id = '', $add_to_path_group = '', $group_path_path = '', $wont_figure_out_min_vs_unmin = false ) { + protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min = true, $has_only_min = false, $id = '', $add_to_path_group = '', $group_path_path = '', $wont_figure_out_min_vs_unmin = false, $should_prefix = true ) { $asset = Assets::init()->get( $slug_prefix . '-' . ( $is_js ? 'script' : 'style' ) ); - $url = plugins_url( ( $group_path_path ? $group_path_path : '/assets/tests/_data/' ) . ( $is_js ? 'js' : 'css' ) . '/' . $slug_prefix ); + $url = plugins_url( ( $group_path_path ? $group_path_path : '/assets/tests/_data/' ) . ( $should_prefix ? ( $is_js ? 'js' : 'css' ) : '' ) . '/' . $slug_prefix ); $urls = []; diff --git a/tests/wpunit/ConfigTest.php b/tests/wpunit/ConfigTest.php index abdaf6b..f56493c 100644 --- a/tests/wpunit/ConfigTest.php +++ b/tests/wpunit/ConfigTest.php @@ -107,13 +107,17 @@ public function should_reset() { * @test */ public function should_add_group_paths() { - Config::add_group_path( 'my-group-path-1', dirname( __DIR__, 2) . '/src/feature-1', 'app-1'); - Config::add_group_path( 'my-group-path-2', dirname( __DIR__, 2) . '/src/feature-2', 'app-2'); + Config::add_group_path( 'my-group-path-1', dirname( __DIR__, 2) . '/src/feature-1', 'app-1', true ); + Config::add_group_path( 'my-group-path-2', dirname( __DIR__, 2) . '/src/feature-2', 'app-2', false ); $this->assertEquals( WP_PLUGIN_DIR . '/assets/src/feature-1/', Config::get_path_of_group_path( 'my-group-path-1' ) ); $this->assertEquals( 'app-1/', Config::get_relative_path_of_group_path( 'my-group-path-1' ) ); + $this->assertTrue( Config::is_group_path_using_asset_directory_prefix( 'my-group-path-1' ) ); $this->assertEquals( WP_PLUGIN_DIR . '/assets/src/feature-2/', Config::get_path_of_group_path( 'my-group-path-2' ) ); $this->assertEquals( 'app-2/', Config::get_relative_path_of_group_path( 'my-group-path-2' ) ); + $this->assertFalse( Config::is_group_path_using_asset_directory_prefix( 'my-group-path-2' ) ); + + $this->assertFalse( Config::is_group_path_using_asset_directory_prefix( 'unknown-group' ) ); } /** From 5db1b1c4b070e33b46ebe3021bcc3d2164e20834 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Mon, 11 Nov 2024 15:49:23 +0200 Subject: [PATCH 4/7] Added fake assets to support test cases --- tests/_data/fake-feature/fake1.css | 0 tests/_data/fake-feature/fake1.js | 0 tests/_data/fake-feature/fake1.min.css | 0 tests/_data/fake-feature/fake1.min.js | 0 tests/_data/fake-feature/fake2.css | 0 tests/_data/fake-feature/fake2.js | 0 tests/_data/fake-feature/fake3.min.css | 0 tests/_data/fake-feature/fake3.min.js | 0 8 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/_data/fake-feature/fake1.css create mode 100644 tests/_data/fake-feature/fake1.js create mode 100644 tests/_data/fake-feature/fake1.min.css create mode 100644 tests/_data/fake-feature/fake1.min.js create mode 100644 tests/_data/fake-feature/fake2.css create mode 100644 tests/_data/fake-feature/fake2.js create mode 100644 tests/_data/fake-feature/fake3.min.css create mode 100644 tests/_data/fake-feature/fake3.min.js diff --git a/tests/_data/fake-feature/fake1.css b/tests/_data/fake-feature/fake1.css new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/fake-feature/fake1.js b/tests/_data/fake-feature/fake1.js new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/fake-feature/fake1.min.css b/tests/_data/fake-feature/fake1.min.css new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/fake-feature/fake1.min.js b/tests/_data/fake-feature/fake1.min.js new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/fake-feature/fake2.css b/tests/_data/fake-feature/fake2.css new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/fake-feature/fake2.js b/tests/_data/fake-feature/fake2.js new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/fake-feature/fake3.min.css b/tests/_data/fake-feature/fake3.min.css new file mode 100644 index 0000000..e69de29 diff --git a/tests/_data/fake-feature/fake3.min.js b/tests/_data/fake-feature/fake3.min.js new file mode 100644 index 0000000..e69de29 From 9001c0c3217e256f5de19b5c84b92c02ed1b25a9 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Mon, 11 Nov 2024 15:54:57 +0200 Subject: [PATCH 5/7] Updated Doc files --- README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0cd21da..2142986 100644 --- a/README.md +++ b/README.md @@ -82,9 +82,25 @@ add_action( 'plugins_loaded', function() { Now you can specify "group paths" in your application. This enables you to load assets which are stored in locations outside of your path set through `Config::set_path( PATH_TO_YOUR_PROJECT_ROOT );` ```php -Config::add_group_path( 'group-path-slug', GROUP_PATH_ROOT, 'group/path/relevant/path' ); +Config::add_group_path( 'group-path-slug', GROUP_PATH_ROOT, 'group/path/relevant/path', true ); ``` +**Note**: Specifying the 4th parameter of add_group_path method as true, means that all the assets that belong to the specified `group-path-slug` will have their paths prefixed with `css` or `js`. + +For example: + +```php +Config::add_group_path( 'group-path-slug', GROUP_PATH_ROOT, 'group/path/relevant/path', true ); + +Asset::add( 'another-style', 'css/another.css' ) + ->add_to_group_path( 'group-path-slug' ); + +// This asset's would be found in GROUP_PATH_ROOT . 'group/path/relevant/path' . '/css/css/another.css' +``` + +If you don't want the above to happen you would either specify false or leave the 4th parameter to its default state. Then the asset of the above example would be found in: +`GROUP_PATH_ROOT . 'group/path/relevant/path' . '/css/another.css'` + ## Register and enqueue assets There are a lot of options that are available for handling assets From 2f074d1c64cf45b75bd3b3cf3a7878a5ee61588d Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Mon, 11 Nov 2024 15:59:43 +0200 Subject: [PATCH 6/7] Updated doc comment --- src/Assets/Asset.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index 888cd7d..58e8516 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -304,6 +304,7 @@ public function __construct( string $slug, string $file, string $version = null, * Adds the asset to a group path. * * @since 1.4.0 + * @since 1.4.2 Also sets the usage of the Asset directory prefix based on the group path. * * @return static */ From 3fd3d0f562363e8c81fad7644607e1f880722b81 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Mon, 11 Nov 2024 16:40:55 +0200 Subject: [PATCH 7/7] Update README.md Co-authored-by: theAverageDev (Luca Tumedei) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2142986..13f5f99 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ Now you can specify "group paths" in your application. This enables you to load Config::add_group_path( 'group-path-slug', GROUP_PATH_ROOT, 'group/path/relevant/path', true ); ``` -**Note**: Specifying the 4th parameter of add_group_path method as true, means that all the assets that belong to the specified `group-path-slug` will have their paths prefixed with `css` or `js`. +**Note**: Specifying the 4th parameter of `add_group_path` method as `true`, means that all the assets that belong to the specified `group-path-slug` will have their paths prefixed with `css` or `js`. For example: