Skip to content

Commit 3da0472

Browse files
authored
Merge pull request #32 from stellarwp/fix/accept-only-asset-or-array-of-assets-during-register-in-wp
Ensure reigster_in_wp accepts only null or Asset or Asset[]
2 parents 7f62d32 + da3792b commit 3da0472

File tree

2 files changed

+76
-9
lines changed

2 files changed

+76
-9
lines changed

src/Assets/Assets.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace StellarWP\Assets;
44

5+
use InvalidArgumentException;
6+
57
class Assets {
68
/**
79
* @var ?Assets
@@ -676,8 +678,9 @@ protected function do_enqueue( Asset $asset, bool $force_enqueue = false ): void
676678
* Register the Assets on the correct hooks.
677679
*
678680
* @since 1.0.0
681+
* @since 1.4.5 Ensure the method accepts only `null` or an `Asset` instance or an array of `Asset[]` instances.
679682
*
680-
* @param array|Asset|null $assets Array of asset objects, single asset object, or null.
683+
* @param Asset[]|Asset|null $assets Array of asset objects, single asset object, or null.
681684
*
682685
* @return void
683686
*/
@@ -688,33 +691,43 @@ public function register_in_wp( $assets = null ) {
688691
)
689692
) {
690693
// Registering the asset now would trigger a doing_it_wrong notice: queue the assets to be registered later.
691-
if ( $assets === null ) {
694+
if ( $assets === null || empty( $assets ) ) {
692695
return;
693696
}
694697

695698
if ( ! is_array( $assets ) ) {
696-
if ( ! $assets instanceof Asset ) {
697-
throw new \InvalidArgumentException( 'Assets in register_in_wp() must be of type Asset' );
699+
$assets = [ $assets ];
700+
}
701+
702+
foreach ( $assets as $asset ) {
703+
if ( ! $asset instanceof Asset ) {
704+
throw new InvalidArgumentException( 'Assets in register_in_wp() must be of type Asset' );
698705
}
699706

700-
$assets = [ $assets->get_slug() => $assets ];
707+
// Register later, avoid the doing_it_wrong notice.
708+
$this->assets[ $asset->get_slug() ] = $asset;
701709
}
702710

703-
// Register later, avoid the doing_it_wrong notice.
704-
$this->assets = array_merge( $this->assets, $assets );
705-
706711
return;
707712
}
708713

709-
if ( is_null( $assets ) ) {
714+
if ( null === $assets ) {
710715
$assets = $this->get();
711716
}
712717

713718
if ( ! is_array( $assets ) ) {
714719
$assets = [ $assets ];
715720
}
716721

722+
if ( empty( $assets ) ) {
723+
return;
724+
}
725+
717726
foreach ( $assets as $asset ) {
727+
if ( ! $asset instanceof Asset ) {
728+
throw new InvalidArgumentException( 'Assets in register_in_wp() must be of type Asset' );
729+
}
730+
718731
// Asset is already registered.
719732
if ( $asset->is_registered() ) {
720733
continue;

tests/wpunit/AssetsTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use StellarWP\Assets\Tests\AssetTestCase;
66
use PHPUnit\Framework\Assert;
7+
use stdClass;
8+
use Closure;
9+
use Generator;
10+
use InvalidArgumentException;
711

812
class AssetsTest extends AssetTestCase {
913
/**
@@ -43,6 +47,56 @@ public function unset_uopz_redefines() {
4347
self::$uopz_redefines = [];
4448
}
4549

50+
public function it_should_accept_instance_of_asset_or_array_of_assets_in_register_in_wp() {
51+
$asset_1 = Asset::add( 'fake1-script', 'fake1.js' );
52+
$asset_2 = Asset::add( 'fake1-style', 'fake1.css' );
53+
$asset_3 = Asset::add( 'fake2-script', 'fake2.js' );
54+
$asset_4 = Asset::add( 'fake2-style', 'fake2.css' );
55+
$asset_5 = Asset::add( 'fake3-script', 'fake3.js' );
56+
57+
$assets = Assets::init();
58+
59+
$assets->register_in_wp( null ); // No problemo... nothing happens though.
60+
$assets->register_in_wp( [] ); // No problemo... nothing happens though.
61+
62+
$this->assertFalse( $asset_1->is_registered() );
63+
$assets->register_in_wp( $asset_1 );
64+
$this->assertTrue( $asset_1->is_registered() );
65+
66+
$this->assertFalse( $asset_2->is_registered() );
67+
$this->assertFalse( $asset_3->is_registered() );
68+
$this->assertFalse( $asset_4->is_registered() );
69+
$this->assertFalse( $asset_5->is_registered() );
70+
$assets->register_in_wp( [ $asset_2, $asset_3, $asset_4, $asset_5 ] );
71+
$this->assertTrue( $asset_2->is_registered() );
72+
$this->assertTrue( $asset_3->is_registered() );
73+
$this->assertTrue( $asset_4->is_registered() );
74+
$this->assertTrue( $asset_5->is_registered() );
75+
}
76+
77+
public function invalid_params_for_register_in_wp_provider(): Generator {
78+
yield 'string' => [ fn() => 'string' ];
79+
yield 'int' => [ fn() => 1 ];
80+
yield 'float' => [ fn() => 1.1 ];
81+
yield 'bool - true' => [ fn() => true ];
82+
yield 'bool - false' => [ fn() => false ];
83+
yield 'object' => [ fn() => new stdClass() ];
84+
yield 'array - mixed' => [ fn () => [ Asset::add( 'fake1-script', 'fake1.js' ), 'string' ] ];
85+
}
86+
87+
/**
88+
* @test
89+
* @dataProvider invalid_params_for_register_in_wp_provider
90+
*/
91+
public function it_should_throw_exception_when_invalid_params_are_passed_to_register_in_wp( Closure $fixture ) {
92+
$assets = Assets::init();
93+
94+
$this->expectException( InvalidArgumentException::class );
95+
$this->expectExceptionMessage( 'Assets in register_in_wp() must be of type Asset' );
96+
97+
$assets->register_in_wp( $fixture() );
98+
}
99+
46100
/**
47101
* @test
48102
*/

0 commit comments

Comments
 (0)