diff --git a/assets.php b/assets.php index 6a9f281..445eab5 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.10 + * Version: 1.4.11 * Author: StellarWP * Author URI: https://stellarwp.com */ diff --git a/src/Assets/Assets.php b/src/Assets/Assets.php index f389b04..7061cff 100755 --- a/src/Assets/Assets.php +++ b/src/Assets/Assets.php @@ -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. + 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 ); diff --git a/tests/wpunit/AssetsTest.php b/tests/wpunit/AssetsTest.php index 155e139..f7b9229 100644 --- a/tests/wpunit/AssetsTest.php +++ b/tests/wpunit/AssetsTest.php @@ -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. *