From 370f2e6e75d6a8b8a41f15d7495f9b60966f0e96 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 19 Sep 2024 13:38:07 +0200 Subject: [PATCH] Move script module registration out of experimental --- lib/client-assets.php | 49 ++++++++++++++++++++++++++++ lib/experimental/script-modules.php | 50 ----------------------------- 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 62e874d6b06c82..de344fe33f26c5 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -601,6 +601,55 @@ function gutenberg_register_vendor_scripts( $scripts ) { } add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' ); +/** + * Registers or re-registers Gutenberg Script Modules. + * + * Script modules that are registered by Core will be re-registered by Gutenberg. + */ +function gutenberg_register_script_modules() { + /* + * Expects multidimensional array like: + * + * 'interactivity/index.min.js' => array('dependencies' => array(…), 'version' => '…'), + * 'interactivity/debug.min.js' => array('dependencies' => array(…), 'version' => '…'), + * 'interactivity-router/index.min.js' => … + */ + $assets = include gutenberg_dir_path() . '/build-module/assets.production.php'; + + foreach ( $assets as $file_name => $script_module_data ) { + $package_name = dirname( $file_name ); + $package_sub_name = basename( $file_name, '.min.js' ); + + switch ( $package_name ) { + /* + * Interactivity exposes two entrypoints, `/index` and `/debug`. + * They're the production and development versions of the package. + */ + case 'interactivity': + if ( SCRIPT_DEBUG ) { + if ( 'index' === $package_sub_name ) { + continue 2; + } + } else { + if ( 'debug' === $package_sub_name ) { + continue 2; + } + } + $script_module_id = '@wordpress/interactivity'; + break; + + default: + $script_module_id = 'index' === $package_sub_name ? + "@wordpress/{$package_name}" : + "@wordpress/{$package_name}/{$package_sub_name}"; + } + + $path = gutenberg_url( "build-module/{$file_name}" ); + wp_deregister_script_module( $script_module_id ); + wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] ); + } +} +add_action( 'after_setup_theme', 'gutenberg_register_script_modules', 20 ); /* * Always remove the Core action hook while gutenberg_enqueue_stored_styles() exists to avoid styles being printed twice. diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index 9cc1c535316f8a..fe23786fc03621 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -239,55 +239,5 @@ function gutenberg_a11y_script_module_html() { . '
' . ''; } - -/** - * Registers Gutenberg Script Modules. - * - * @since 19.3 - */ -function gutenberg_register_script_modules() { - /* - * Expects multidimensional array like: - * - * 'interactivity/index.min.js' => array('dependencies' => array(…), 'version' => '…'), - * 'interactivity/debug.min.js' => array('dependencies' => array(…), 'version' => '…'), - * 'interactivity-router/index.min.js' => … - */ - $assets = include gutenberg_dir_path() . '/build-module/assets.production.php'; - - foreach ( $assets as $file_name => $script_module_data ) { - $package_name = dirname( $file_name ); - $package_sub_name = basename( $file_name, '.min.js' ); - - switch ( $package_name ) { - /* - * Interactivity exposes two entrypoints, `/index` and `/debug`. - * They're the production and development versions of the package. - */ - case 'interactivity': - if ( SCRIPT_DEBUG ) { - if ( 'index' === $package_sub_name ) { - continue 2; - } - } else { - if ( 'debug' === $package_sub_name ) { - continue 2; - } - } - $script_module_id = '@wordpress/interactivity'; - break; - - default: - $script_module_id = 'index' === $package_sub_name ? - "@wordpress/{$package_name}" : - "@wordpress/{$package_name}/{$package_sub_name}"; - } - - $path = gutenberg_url( "build-module/{$file_name}" ); - wp_deregister_script_module( $script_module_id ); - wp_register_script_module( $script_module_id, $path, $script_module_data['dependencies'], $script_module_data['version'] ); - } -} -add_action( 'after_setup_theme', 'gutenberg_register_script_modules', 20 ); add_action( 'wp_footer', 'gutenberg_a11y_script_module_html' ); add_action( 'admin_footer', 'gutenberg_a11y_script_module_html' );