Skip to content

Commit

Permalink
Prevent assets from being added to assets array twice
Browse files Browse the repository at this point in the history
When adding assets, an asset would be added to the `Assets::$assets` array with the slug as the index and with a numeric index. There's no reason for the numeric index, as it just causes extra looping and attempts to enqueue.

WP must be outputting script tags with single quotes now. Updated tests to reflect that.
  • Loading branch information
borkweb committed Jan 19, 2025
1 parent 83b1982 commit 4e8ea4d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"description": "A library for managing asset registration and enqueuing in WordPress.",
"type": "library",
"license": "GPL-2.0",
"config": {
"platform": {
"php": "7.4"
}
},
"autoload": {
"psr-4": {
"StellarWP\\Assets\\": "src/Assets/"
Expand All @@ -24,7 +29,9 @@
}
],
"minimum-stability": "stable",
"require": {},
"require": {
"php": ">=7.4"
},
"require-dev": {
"codeception/module-asserts": "^1.0",
"codeception/module-cli": "^1.0",
Expand Down
9 changes: 8 additions & 1 deletion src/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,9 +688,16 @@ public function register_in_wp( $assets = null ) {
)
) {
// Registering the asset now would trigger a doing_it_wrong notice: queue the assets to be registered later.
if ( $assets === null ) {
return;
}

if ( ! is_array( $assets ) ) {
$assets = [ $assets ];
if ( ! $assets instanceof Asset ) {

Check failure on line 696 in src/Assets/Assets.php

View workflow job for this annotation

GitHub Actions / phpstan

Instanceof between StellarWP\Assets\Asset and StellarWP\Assets\Asset will always evaluate to true.
throw new \InvalidArgumentException( 'Assets in register_in_wp() must be of type Asset' );
}

$assets = [ $assets->get_slug() => $assets ];
}

// Register later, avoid the doing_it_wrong notice.
Expand Down
22 changes: 11 additions & 11 deletions tests/wpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,15 @@ public function should_localize_data_correctly(): void {
->register();

$this->assertEquals( <<< SCRIPT
<script id="my-first-script-js-extra">
<script id='my-first-script-js-extra'>
var boomshakalakaProjectFirstScriptData = {"animal":"cat","color":"orange"};
</script>
SCRIPT,
apply_filters( 'script_loader_tag', '', 'my-first-script' )
);
$this->assertEquals( <<< SCRIPT
<script id="my-second-script-js-extra">
<script id='my-second-script-js-extra'>
var boomshakalakaProjectSecondScriptData = {"animal":"dog","color":"green"};
</script>
Expand All @@ -422,7 +422,7 @@ public function should_localize_data_correctly(): void {
);

$this->assertEquals( <<< SCRIPT
<script id="my-second-script-mod-js-extra">
<script id='my-second-script-mod-js-extra'>
var boomshakalakaProjectSecondScriptModData = {"animal":"horse"};
</script>
Expand Down Expand Up @@ -504,7 +504,7 @@ public function should_allow_localizing_data_in_normal_and_namespaced_form_for_s

$apply_filters = apply_filters( 'script_loader_tag', '', 'my-test-script' );
$this->assertEquals( <<< SCRIPT
<script id="my-test-script-js-extra">
<script id='my-test-script-js-extra'>
var boomshakalakaProjectTestScriptData = {"animal":"cat","color":"orange"};
</script>
<script id="my-test-script-ns-extra">
Expand Down Expand Up @@ -552,7 +552,7 @@ public function should_allow_localizing_data_using_a_closure(): void {

$apply_filters = apply_filters( 'script_loader_tag', '', 'my-script-with-closure-data' );
$this->assertEquals( <<< SCRIPT
<script id="my-script-with-closure-data-js-extra">
<script id='my-script-with-closure-data-js-extra'>
var scriptWithClosureData = {"animal":"cat","color":"orange"};
</script>
<script id="my-script-with-closure-data-ns-extra">
Expand Down Expand Up @@ -587,9 +587,9 @@ public function should_allow_setting_dependencies_with_an_array(): void {
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 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()
Expand Down Expand Up @@ -625,9 +625,9 @@ public function should_allow_setting_dependencies_with_a_callable(): void {
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 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()
Expand Down

0 comments on commit 4e8ea4d

Please sign in to comment.