Prevent assets from being added to assets array twice#31
Merged
Conversation
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.
bordoni
approved these changes
Jan 19, 2025
dpanta94
reviewed
Jan 20, 2025
Comment on lines
+696
to
+700
| if ( ! $assets instanceof Asset ) { | ||
| throw new \InvalidArgumentException( 'Assets in register_in_wp() must be of type Asset' ); | ||
| } | ||
|
|
||
| $assets = [ $assets->get_slug() => $assets ]; |
Member
There was a problem hiding this comment.
What the check here is doing doesn't seem enough to be sure about what we want to achieve. We might pass an invalid array and the error would still be there and it could be even more serious.
It should be instead:
if ( ! is_array( $assets ) {
$assets = [ $assets ];
}
foreach ( $assets as $asset {
if ( ! $asset instanceof Asset ) {
throw
}
$this->assets[ $asset->get_slug() ] = $asset;
}
return;
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When adding assets, an asset would be added to the
Assets::$assetsarray 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.Before:
After