Skip to content

Commit

Permalink
feat(Asset) support callable dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed May 1, 2024
1 parent 4ee3fa9 commit 2dfd2da
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 12 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ Asset::add( 'script-with-dependencies', 'js/something.js' )
->register();
```

You can also specify dependencies as a callable that returns an array of dependencies, like so:

```php
Asset::add( 'script-with-dependencies', 'js/something.js' )
->set_dependencies( function() {
return [ 'jquery', 'jquery-ui', 'some-other-thing' ];
} )
->register();
```

Note that the callable will be called when the asset is enqueued, not later, when the asset is printed.

#### Auto-enqueuing on an action
To specify when to enqueue the asset, you can indicate it like so:

Expand Down
20 changes: 10 additions & 10 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class Asset {
/**
* The asset dependencies.
*
* @var array
* @var array<string>|callable
*/
protected array $dependencies = [];
protected $dependencies = [];

/**
* The asset file path.
Expand Down Expand Up @@ -492,9 +492,9 @@ public function get_condition() {
/**
* Get the asset dependencies.
*
* @return array
* @return array<string>|callable
*/
public function get_dependencies(): array {
public function get_dependencies() {
return $this->dependencies;
}

Expand Down Expand Up @@ -1215,15 +1215,15 @@ public function set_condition( $condition ) {
/**
* @since 1.0.0
*
* @param string ...$dependencies
* @param string|callable ...$dependencies
*
* @return static
*/
public function set_dependencies( string ...$dependencies ) {
$this->dependencies = [];

foreach ( $dependencies as $dependency ) {
$this->add_dependency( $dependency );
public function set_dependencies( ...$dependencies ) {
if ( $dependencies[0] && is_callable( $dependencies[0] ) ) {
$this->dependencies = $dependencies[0];
} else {
$this->dependencies = $dependencies;
}

return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,8 @@ public function register_in_wp( $assets = null ) {

// If the asset is a callable, we call the function,
// passing it the asset and expecting back an array of dependencies.
if ( is_callable( $asset->get_dependencies() ) ) {
$dependencies = call_user_func( $asset->get_dependencies(), [ $asset ] );
if ( is_callable( $dependencies ) ) {
$dependencies = $dependencies( $asset );
}

wp_register_script( $asset->get_slug(), $asset->get_url(), $dependencies, $asset->get_version(), $asset->is_in_footer() );
Expand Down
66 changes: 66 additions & 0 deletions tests/wpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,70 @@ public function should_allow_localizing_data_using_a_closure(): void {
$this->assertTrue( $resolved_one );
$this->assertTrue( $resolved_two );
}

/**
* It should allow setting dependencies with an array
*
* @test
*/
public function should_allow_setting_dependencies_with_an_array(): void {
Asset::add( 'my-deps-base-script', 'base-script.js' )
->register();
Asset::add( 'my-deps-vendor-script', 'vendor-script.js' )
->register();
Asset::add( 'my-deps-dependent-script', 'dependent-script.js' )
->set_dependencies( 'my-deps-base-script', 'my-deps-vendor-script' )
->enqueue_on( 'test_action' )
->print()
->register();

ob_start();
do_action( 'test_action' );
$this->assertEquals( <<< SCRIPT
<script src="http://wordpress.test/wp-content/plugins/assets/tests/_data/js/base-script.js?ver=1.0.0" id="my-deps-base-script-js"></script>
<script src="http://wordpress.test/wp-content/plugins/assets/tests/_data/js/vendor-script.js?ver=1.0.0" id="my-deps-vendor-script-js"></script>
<script src="http://wordpress.test/wp-content/plugins/assets/tests/_data/js/dependent-script.js?ver=1.0.0" id="my-deps-dependent-script-js"></script>
SCRIPT,
ob_get_clean()
);
}

/**
* It should allow setting dependencies with a callable
*
* @test
*/
public function should_allow_setting_dependencies_with_a_callable(): void {
Asset::add( 'my-base-script-2', 'base-script-2.js' )
->register();
Asset::add( 'my-vendor-script-2', 'vendor-script-2.js' )
->register();
$resolved = false;
$asset = Asset::add( 'my-dependent-script-2', 'dependent-script-2.js' )
->set_dependencies( function () use ( &$resolved ) {
$resolved = true;

return [ 'my-base-script-2', 'my-vendor-script-2' ];
} )
->enqueue_on( 'test_action_2' )
->print();

$this->assertFalse( $resolved, 'The dependencies should not have been resolved yet.' );

$asset->register();

$this->assertTrue( $resolved );

ob_start();
do_action( 'test_action_2' );
$this->assertEquals( <<< SCRIPT
<script src="http://wordpress.test/wp-content/plugins/assets/tests/_data/js/base-script-2.js?ver=1.0.0" id="my-base-script-2-js"></script>
<script src="http://wordpress.test/wp-content/plugins/assets/tests/_data/js/vendor-script-2.js?ver=1.0.0" id="my-vendor-script-2-js"></script>
<script src="http://wordpress.test/wp-content/plugins/assets/tests/_data/js/dependent-script-2.js?ver=1.0.0" id="my-dependent-script-2-js"></script>
SCRIPT,
ob_get_clean()
);
}
}

0 comments on commit 2dfd2da

Please sign in to comment.