| @@ -5,61 +5,45 @@ | ||
| 5 | 5 | * |
| 6 | 6 | * @package gutenberg |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | -/** | |
| 10 | - * Register all script modules. | |
| 11 | - */ | |
| 12 | -function gutenberg_register_script_modules() { | |
| 13 | - // Ensure this only runs once. wp_default_scripts can fire multiple times, | |
| 14 | - // and each wp_deregister_script_module() call also dequeues the module. | |
| 15 | - // If a module was enqueued between calls, repeated deregister/register | |
| 16 | - // cycles would lose the enqueue state. | |
| 17 | - static $already_registered = false; | |
| 18 | - if ( $already_registered ) { | |
| 19 | - return; | |
| 20 | - } | |
| 21 | - $already_registered = true; | |
| 9 | +if ( ! function_exists( 'gutenberg_register_script_modules' ) ) { | |
| 10 | + /** | |
| 11 | + * Register all script modules. | |
| 12 | + */ | |
| 13 | + function gutenberg_register_script_modules() { | |
| 14 | + // Load build constants | |
| 15 | + $build_constants = require __DIR__ . '/constants.php'; | |
| 16 | + $modules_dir = __DIR__ . '/modules'; | |
| 17 | + $modules_file = $modules_dir . '/registry.php'; | |
| 22 | 18 | |
| 23 | - // Load build constants | |
| 24 | - $constants_file = __DIR__ . '/constants.php'; | |
| 25 | - if ( ! file_exists( $constants_file ) ) { | |
| 26 | - return; | |
| 27 | - } | |
| 28 | - $build_constants = require $constants_file; | |
| 29 | - $modules_dir = __DIR__ . '/modules'; | |
| 30 | - $modules_file = $modules_dir . '/registry.php'; | |
| 19 | + if ( ! file_exists( $modules_file ) ) { | |
| 20 | + return; | |
| 21 | + } | |
| 31 | 22 | |
| 32 | - if ( ! file_exists( $modules_file ) ) { | |
| 33 | - return; | |
| 34 | - } | |
| 23 | + $modules = require $modules_file; | |
| 24 | + $base_url = $build_constants['build_url'] . 'modules/'; | |
| 25 | + $extension = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.js' : '.min.js'; | |
| 35 | 26 | |
| 36 | - $modules = require $modules_file; | |
| 37 | - $base_url = $build_constants['build_url'] . 'modules/'; | |
| 27 | + foreach ( $modules as $module ) { | |
| 28 | + $asset_path = $modules_dir . '/' . $module['asset']; | |
| 29 | + $asset = file_exists( $asset_path ) ? require $asset_path : array(); | |
| 38 | 30 | |
| 39 | - foreach ( $modules as $module ) { | |
| 40 | - // WASM-only modules (e.g., vips worker) only have .min.js; use it even when SCRIPT_DEBUG is `true`. | |
| 41 | - $extension = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && empty( $module['min_only'] ) ) | |
| 42 | - ? '.js' | |
| 43 | - : '.min.js'; | |
| 31 | + // Deregister first to override any previously registered version | |
| 32 | + // (e.g., Core's default modules when running as a plugin). | |
| 33 | + wp_deregister_script_module( $module['id'] ); | |
| 44 | 34 | |
| 45 | - $asset_path = $modules_dir . '/' . $module['asset']; | |
| 46 | - $asset = file_exists( $asset_path ) ? require $asset_path : array(); | |
| 35 | + wp_register_script_module( | |
| 36 | + $module['id'], | |
| 37 | + $base_url . $module['path'] . $extension, | |
| 38 | + $asset['module_dependencies'] ?? array(), | |
| 39 | + $asset['version'] ?? false, | |
| 40 | + array( | |
| 41 | + 'fetchpriority' => 'low', | |
| 42 | + 'in_footer' => true, | |
| 43 | + ) | |
| 44 | + ); | |
| 45 | + } | |
| 46 | + } | |
| 47 | 47 | |
| 48 | - // Deregister first to override any previously registered version | |
| 49 | - // (e.g., Core's default modules when running as a plugin). | |
| 50 | - wp_deregister_script_module( $module['id'] ); | |
| 51 | - | |
| 52 | - wp_register_script_module( | |
| 53 | - $module['id'], | |
| 54 | - $base_url . $module['path'] . $extension, | |
| 55 | - $asset['module_dependencies'] ?? array(), | |
| 56 | - $asset['version'] ?? false, | |
| 57 | - array( | |
| 58 | - 'fetchpriority' => 'low', | |
| 59 | - 'in_footer' => true, | |
| 60 | - ) | |
| 61 | - ); | |
| 62 | - } | |
| 48 | + add_action( 'wp_default_scripts', 'gutenberg_register_script_modules' ); | |
| 63 | 49 | } |
| 64 | - | |
| 65 | -add_action( 'wp_default_scripts', 'gutenberg_register_script_modules' ); | |