Skip to content

Commit

Permalink
Script Loader: Allow for wp_register_script() to be called after `w…
Browse files Browse the repository at this point in the history
…p_enqueue_script()`.

When a plugin registers styles/scripts on `wp_enqueue_scripts` (as plugin authors are encouraged to do), and conditionally enqueues their script/style on `the_content` filter, things "just work". In block themes, `the_content` is run prior to the header being processed, which results in the above scenario failing.

This change makes a `wp_enqueue_script( 'example' ); wp_register_script( 'example' );` work, where as currently the enqueue silently fails (no "doing it wrong" message) and the following register has no impact. Scripts can therefore be enqueued and dequeued (by "handle") before they are registered.

Fixes #54529.


git-svn-id: https://develop.svn.wordpress.org/trunk@52338 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
audrasjb committed Dec 7, 2021
1 parent 39d6d8c commit 6f546a0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/wp-includes/class.wp-dependencies.php
Expand Up @@ -94,6 +94,15 @@ class WP_Dependencies {
*/
private $all_queued_deps;

/**
* List of assets enqueued before details were registered.
*
* @since 5.9.0
*
* @var array
*/
private $queued_before_register = array();

/**
* Processes the items and dependencies.
*
Expand Down Expand Up @@ -248,6 +257,18 @@ public function add( $handle, $src, $deps = array(), $ver = false, $args = null
return false;
}
$this->registered[ $handle ] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );

// If the item was enqueued before the details were registered, enqueue it now.
if ( array_key_exists( $handle, $this->queued_before_register ) ) {
if ( ! is_null( $this->queued_before_register[ $handle ] ) ) {
$this->enqueue( $handle . '?' . $this->queued_before_register[ $handle ] );
} else {
$this->enqueue( $handle );
}

unset( $this->queued_before_register[ $handle ] );
}

return true;
}

Expand Down Expand Up @@ -334,6 +355,12 @@ public function enqueue( $handles ) {
if ( isset( $handle[1] ) ) {
$this->args[ $handle[0] ] = $handle[1];
}
} else if ( ! isset( $this->registered[ $handle[0] ] ) ) {
$this->queued_before_register[ $handle[0] ] = null; // $args

if ( isset( $handle[1] ) ) {
$this->queued_before_register[ $handle[0] ] = $handle[1];
}
}
}
}
Expand All @@ -360,6 +387,8 @@ public function dequeue( $handles ) {

unset( $this->queue[ $key ] );
unset( $this->args[ $handle[0] ] );
} else if ( array_key_exists( $handle[0], $this->queued_before_register ) ) {
unset( $this->queued_before_register[ $handle[0] ] );
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/phpunit/tests/dependencies.php
Expand Up @@ -136,4 +136,18 @@ public function test_query_and_registered_enqueued() {
$this->assertFalse( $dep->query( 'one' ) );

}

function test_enqueue_before_register() {
$dep = new WP_Dependencies;

$this->assertArrayNotHasKey( 'one', $dep->registered );

$dep->enqueue( 'one' );

$this->assertNotContains( 'one', $dep->queue );

$this->assertTrue( $dep->add( 'one', '' ) );

$this->assertContains( 'one', $dep->queue );
}
}

0 comments on commit 6f546a0

Please sign in to comment.