From cdafa716f7bd34b24f02f5f31a13faf5e3007614 Mon Sep 17 00:00:00 2001 From: audrasjb Date: Tue, 7 Dec 2021 17:46:01 +0000 Subject: [PATCH] Script Loader: Allow for `wp_register_script()` to be called after `wp_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. Built from https://develop.svn.wordpress.org/trunk@52338 git-svn-id: http://core.svn.wordpress.org/trunk@51930 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class.wp-dependencies.php | 29 +++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/wp-includes/class.wp-dependencies.php b/wp-includes/class.wp-dependencies.php index e9f701b727..649bf172c3 100644 --- a/wp-includes/class.wp-dependencies.php +++ b/wp-includes/class.wp-dependencies.php @@ -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. * @@ -248,6 +257,18 @@ class WP_Dependencies { 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; } @@ -334,6 +355,12 @@ class WP_Dependencies { 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]; + } } } } @@ -360,6 +387,8 @@ class WP_Dependencies { 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] ] ); } } } diff --git a/wp-includes/version.php b/wp-includes/version.php index edf7a35bf8..b91456ccde 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '5.9-beta1-52337'; +$wp_version = '5.9-beta1-52338'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.