Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.10
* Version: 1.4.11
* Author: StellarWP
* Author URI: https://stellarwp.com
*/
11 changes: 8 additions & 3 deletions src/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,18 @@ public function enqueue_group( $groups, bool $should_enqueue_no_matter_what = fa
* useful where an asset is required in a situation not anticipated when it was originally
* registered.
*
* @param string|array $assets_to_enqueue Which assets will be enqueued.
* @param bool $should_enqueue_no_matter_what Whether to ignore conditional requirements when enqueuing.
* @param string|array $assets_to_enqueue Which assets will be enqueued.
* @param bool $should_enqueue_no_matter_what Whether to ignore conditional requirements when enqueuing.
*
* @since 1.0.0
*
* @since 1.4.11 - If explicitly passed an empty array, enqueue nothing.
*/
public function enqueue( $assets_to_enqueue = null, bool $should_enqueue_no_matter_what = false ) {
// If explicitly passed an empty array, enqueue nothing.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the expected behavior is if $assets_to_enqueue is empty (null - not an array) we should enqueue everything?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that keeps the library backwards compatible

if ( is_array( $assets_to_enqueue ) && empty( $assets_to_enqueue ) ) {
return;
}

$assets_to_enqueue = array_filter( (array) $assets_to_enqueue );
if ( ! empty( $assets_to_enqueue ) ) {
$assets = (array) $this->get( $assets_to_enqueue );
Expand Down
30 changes: 30 additions & 0 deletions tests/wpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,36 @@ static function ( $url ) {
$this->assertEquals( $path, $asset->get_full_resource_path() );
}

/**
* It should not enqueue any assets when an empty group is requested
*
* @test
*/
public function should_not_enqueue_any_assets_when_empty_group_is_requested(): void {
// Register multiple assets WITHOUT enqueue_on actions
// (we don't want them to auto-enqueue, we only want to test group enqueueing)
Asset::add( 'test-script-no-group-1', 'test-script-1.js' )
->register();
Asset::add( 'test-script-no-group-2', 'test-script-2.js' )
->register();
Asset::add( 'test-style-no-group', 'test-style.css' )
->register();

// Register assets with groups
Asset::add( 'test-script-with-group', 'test-script-with-group.js' )
->add_to_group( 'existing-group' )
->register();

// Call enqueue_group with a non-existent group (should enqueue nothing)
Assets::init()->enqueue_group( 'non-existent-group' );

// Verify that no assets were enqueued
$this->assertFalse( wp_script_is( 'test-script-no-group-1', 'enqueued' ), 'Script without group 1 should not be enqueued' );
$this->assertFalse( wp_script_is( 'test-script-no-group-2', 'enqueued' ), 'Script without group 2 should not be enqueued' );
$this->assertFalse( wp_style_is( 'test-style-no-group', 'enqueued' ), 'Style without group should not be enqueued' );
$this->assertFalse( wp_script_is( 'test-script-with-group', 'enqueued' ), 'Script with different group should not be enqueued' );
}

/**
* Set a constant value using uopz.
*
Expand Down
Loading