From d4a36e7cddde12a38b4e7932b50faded4855659c Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 25 Jan 2024 08:23:16 +0100 Subject: [PATCH 1/4] Remove `wp_script_modules` function --- src/wp-includes/class-wp-script-modules.php | 25 +++++++++++++++++++++ src/wp-includes/script-modules.php | 25 +++------------------ src/wp-settings.php | 2 ++ 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php index 7eebb79da1b32..5f06165be140f 100644 --- a/src/wp-includes/class-wp-script-modules.php +++ b/src/wp-includes/class-wp-script-modules.php @@ -30,6 +30,31 @@ class WP_Script_Modules { */ private $enqueued_before_registered = array(); + /** + * Container for the main instance of the class. + * + * @since 6.5.0 + * @var WP_Script_Modules|null + */ + private static $instance = null; + + /** + * Utility method to retrieve the main instance of the class. + * + * The instance will be created if it does not exist yet. + * + * @since 6.5.0 + * + * @return WP_Script_Modules The main instance. + */ + public static function get_instance(): WP_Script_Modules { + if ( null === self::$instance ) { + self::$instance = new self(); + } + + return self::$instance; + } + /** * Registers the script module if no script module with that script module * identifier has already been registered. diff --git a/src/wp-includes/script-modules.php b/src/wp-includes/script-modules.php index 205494f699386..8544079ffb8c0 100644 --- a/src/wp-includes/script-modules.php +++ b/src/wp-includes/script-modules.php @@ -8,25 +8,6 @@ * @subpackage Script Modules */ -/** - * Retrieves the main WP_Script_Modules instance. - * - * This function provides access to the WP_Script_Modules instance, creating one - * if it doesn't exist yet. - * - * @since 6.5.0 - * - * @return WP_Script_Modules The main WP_Script_Modules instance. - */ -function wp_script_modules(): WP_Script_Modules { - static $instance = null; - if ( is_null( $instance ) ) { - $instance = new WP_Script_Modules(); - $instance->add_hooks(); - } - return $instance; -} - /** * Registers the script module if no script module with that script module * identifier has already been registered. @@ -59,7 +40,7 @@ function wp_script_modules(): WP_Script_Modules { * If $version is set to null, no version is added. */ function wp_register_script_module( string $id, string $src, array $deps = array(), $version = false ) { - wp_script_modules()->register( $id, $src, $deps, $version ); + WP_Script_Modules::get_instance()->register( $id, $src, $deps, $version ); } /** @@ -96,7 +77,7 @@ function wp_register_script_module( string $id, string $src, array $deps = array * If $version is set to null, no version is added. */ function wp_enqueue_script_module( string $id, string $src = '', array $deps = array(), $version = false ) { - wp_script_modules()->enqueue( $id, $src, $deps, $version ); + WP_Script_Modules::get_instance()->enqueue( $id, $src, $deps, $version ); } /** @@ -107,5 +88,5 @@ function wp_enqueue_script_module( string $id, string $src = '', array $deps = a * @param string $id The identifier of the script module. */ function wp_dequeue_script_module( string $id ) { - wp_script_modules()->dequeue( $id ); + WP_Script_Modules::get_instance()->dequeue( $id ); } diff --git a/src/wp-settings.php b/src/wp-settings.php index 28bcdded99704..bb1577c0f2c4a 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -375,6 +375,8 @@ require ABSPATH . WPINC . '/class-wp-script-modules.php'; require ABSPATH . WPINC . '/script-modules.php'; +WP_Script_Modules::get_instance()->add_hooks(); + $GLOBALS['wp_embed'] = new WP_Embed(); /** From 96232f25ba854ac890a7a8e25f4bb838894459b0 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 31 Jan 2024 17:19:49 +0100 Subject: [PATCH 2/4] Add back the function --- src/wp-includes/script-modules.php | 21 ++++++++++++++++++--- src/wp-settings.php | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/script-modules.php b/src/wp-includes/script-modules.php index 8544079ffb8c0..677014ca1b839 100644 --- a/src/wp-includes/script-modules.php +++ b/src/wp-includes/script-modules.php @@ -8,6 +8,21 @@ * @subpackage Script Modules */ +/** + * Retrieves the main WP_Script_Modules instance. + * + * This function provides access to the WP_Script_Modules instance, creating one + * if it doesn't exist yet. + * + * @since 6.5.0 + * + * @return WP_Script_Modules The main WP_Script_Modules instance. + */ +function wp_script_modules(): WP_Script_Modules { + return WP_Script_Modules::get_instance(); +} + + /** * Registers the script module if no script module with that script module * identifier has already been registered. @@ -40,7 +55,7 @@ * If $version is set to null, no version is added. */ function wp_register_script_module( string $id, string $src, array $deps = array(), $version = false ) { - WP_Script_Modules::get_instance()->register( $id, $src, $deps, $version ); + wp_script_modules()->register( $id, $src, $deps, $version ); } /** @@ -77,7 +92,7 @@ function wp_register_script_module( string $id, string $src, array $deps = array * If $version is set to null, no version is added. */ function wp_enqueue_script_module( string $id, string $src = '', array $deps = array(), $version = false ) { - WP_Script_Modules::get_instance()->enqueue( $id, $src, $deps, $version ); + wp_script_modules()->enqueue( $id, $src, $deps, $version ); } /** @@ -88,5 +103,5 @@ function wp_enqueue_script_module( string $id, string $src = '', array $deps = a * @param string $id The identifier of the script module. */ function wp_dequeue_script_module( string $id ) { - WP_Script_Modules::get_instance()->dequeue( $id ); + wp_script_modules()->dequeue( $id ); } diff --git a/src/wp-settings.php b/src/wp-settings.php index 30722022082b6..87769333c59e5 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -378,7 +378,7 @@ require ABSPATH . WPINC . '/script-modules.php'; require ABSPATH . WPINC . '/interactivity-api.php'; -WP_Script_Modules::get_instance()->add_hooks(); +wp_script_modules()->add_hooks(); $GLOBALS['wp_embed'] = new WP_Embed(); From da3ac228ce276aaf201bd63bd9af4eebe82b2b31 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 31 Jan 2024 17:20:20 +0100 Subject: [PATCH 3/4] Remove double space --- src/wp-includes/script-modules.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wp-includes/script-modules.php b/src/wp-includes/script-modules.php index 677014ca1b839..5fffc23ef2e95 100644 --- a/src/wp-includes/script-modules.php +++ b/src/wp-includes/script-modules.php @@ -22,7 +22,6 @@ function wp_script_modules(): WP_Script_Modules { return WP_Script_Modules::get_instance(); } - /** * Registers the script module if no script module with that script module * identifier has already been registered. From 777bdc989b1cf56ba6911972199d3a6f62d6fa2d Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 31 Jan 2024 18:30:52 +0100 Subject: [PATCH 4/4] use global var --- src/wp-includes/class-wp-script-modules.php | 25 --------------------- src/wp-includes/script-modules.php | 10 ++++++++- 2 files changed, 9 insertions(+), 26 deletions(-) diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php index f9d50f40b914f..b2413d0f2442e 100644 --- a/src/wp-includes/class-wp-script-modules.php +++ b/src/wp-includes/class-wp-script-modules.php @@ -30,31 +30,6 @@ class WP_Script_Modules { */ private $enqueued_before_registered = array(); - /** - * Container for the main instance of the class. - * - * @since 6.5.0 - * @var WP_Script_Modules|null - */ - private static $instance = null; - - /** - * Utility method to retrieve the main instance of the class. - * - * The instance will be created if it does not exist yet. - * - * @since 6.5.0 - * - * @return WP_Script_Modules The main instance. - */ - public static function get_instance(): WP_Script_Modules { - if ( null === self::$instance ) { - self::$instance = new self(); - } - - return self::$instance; - } - /** * Registers the script module if no script module with that script module * identifier has already been registered. diff --git a/src/wp-includes/script-modules.php b/src/wp-includes/script-modules.php index 5fffc23ef2e95..2aff768bb3d1a 100644 --- a/src/wp-includes/script-modules.php +++ b/src/wp-includes/script-modules.php @@ -14,12 +14,20 @@ * This function provides access to the WP_Script_Modules instance, creating one * if it doesn't exist yet. * + * @global WP_Script_Modules $wp_script_modules + * * @since 6.5.0 * * @return WP_Script_Modules The main WP_Script_Modules instance. */ function wp_script_modules(): WP_Script_Modules { - return WP_Script_Modules::get_instance(); + global $wp_script_modules; + + if ( ! ( $wp_script_modules instanceof WP_Script_Modules ) ) { + $wp_script_modules = new WP_Script_Modules(); + } + + return $wp_script_modules; } /**