Skip to content

Commit

Permalink
Merge pull request #27 from stellarwp/feat/support-asset-path-prefixi…
Browse files Browse the repository at this point in the history
…ng-based-on-group-path

Feat/support asset path prefixing based on group path
  • Loading branch information
dpanta94 authored Nov 11, 2024
2 parents b6efe53 + 3fd3d0f commit 6c26d08
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 12 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
3 changes: 3 additions & 0 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,15 @@ 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
*/
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;
}

Expand Down
22 changes: 18 additions & 4 deletions src/Assets/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}

Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
51 changes: 47 additions & 4 deletions tests/wpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand All @@ -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
*
Expand Down Expand Up @@ -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' );
Expand Down Expand Up @@ -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 = [];

Expand Down
8 changes: 6 additions & 2 deletions tests/wpunit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}

/**
Expand Down

0 comments on commit 6c26d08

Please sign in to comment.