Skip to content

Commit

Permalink
fix(Assets) avoid doing-it-wrong with direct calls
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed May 3, 2024
1 parent c97e328 commit 370401a
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,8 @@ public function register_in_wp( $assets = null ) {

if ( 'js' === $asset->get_type() ) {
// Script is already registered.
if ( wp_script_is( $asset->get_slug(), 'registered' ) ) {

if ( (bool) wp_scripts()->query( $asset->get_slug(), 'registered' ) ) {
continue;
}

Expand All @@ -709,24 +710,28 @@ public function register_in_wp( $assets = null ) {
$dependencies = $dependencies( $asset );
}

wp_register_script( $asset->get_slug(), $asset->get_url(), $dependencies, $asset->get_version(), $asset->is_in_footer() );
$wp_scripts = wp_scripts();
$registered = $wp_scripts->add( $asset->get_slug(), $asset->get_url(), $dependencies, $asset->get_version() );
if ( $asset->is_in_footer() ) {
$wp_scripts->add_data( $asset->get_slug(), 'group', 1 );
}

// Register that this asset is actually registered on the WP methods.
// @phpstan-ignore-next-line
if ( wp_script_is( $asset->get_slug(), 'registered' ) ) {
if ( (bool) wp_scripts()->query( $asset->get_slug(), 'registered' ) ) {
$asset->set_as_registered();
}
} else {
// Style is already registered.
if ( wp_style_is( $asset->get_slug(), 'registered' ) ) {
if ( (bool) wp_styles()->query( $asset->get_slug(), 'registered' ) ) {
continue;
}

wp_register_style( $asset->get_slug(), $asset->get_url(), $asset->get_dependencies(), $asset->get_version(), $asset->get_media() );
wp_styles()->add( $asset->get_slug(), $asset->get_url(), $asset->get_dependencies(), $asset->get_version(), $asset->get_media() );

// Register that this asset is actually registered on the WP methods.
// @phpstan-ignore-next-line
if ( wp_style_is( $asset->get_slug(), 'registered' ) ) {
if ( (bool) wp_styles()->query( $asset->get_slug(), 'registered' ) ) {
$asset->set_as_registered();
}

Expand Down Expand Up @@ -811,20 +816,22 @@ public function print_group( $group, $echo = true ) {
return $acc;
}, [ 'css' => [], 'js' => [] ] );

$wp_scripts = wp_scripts();
$wp_styles = wp_styles();

// Make sure each script is registered.
foreach ( $to_print as $slug => $asset ) {
if ( $asset->is_registered() ) {
continue;
}
'js' === $asset->get_type()
? wp_register_script( $slug, $asset->get_file(), $asset->get_dependencies(), $asset->get_version() )
: wp_register_style( $slug, $asset->get_file(), $asset->get_dependencies(), $asset->get_version() );
? $wp_scripts->add( $asset->get_slug(), $asset->get_file(), $asset->get_dependencies(), $asset->get_version() )
: $wp_styles->add( $slug, $asset->get_file(), $asset->get_dependencies(), $asset->get_version() );
}

ob_start();
wp_scripts()->do_items( $by_type['js'] );
wp_styles()->do_items( $by_type['css'] );
$wp_scripts->do_items( $by_type['js'] );
$wp_styles->do_items( $by_type['css'] );
$tags = ob_get_clean();

if ( $echo ) {
Expand Down

0 comments on commit 370401a

Please sign in to comment.