| @@ -5,81 +5,85 @@ | ||
| 5 | 5 | * |
| 6 | 6 | * @package gutenberg |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | -/** | |
| 10 | - * Registers a script according to `wp_register_script`. Honors this request by | |
| 11 | - * reassigning internal dependency properties of any script handle already | |
| 12 | - * registered by that name. It does not deregister the original script, to | |
| 13 | - * avoid losing inline scripts which may have been attached. | |
| 14 | - * | |
| 15 | - * @param WP_Scripts $scripts WP_Scripts instance. | |
| 16 | - * @param string $handle Name of the script. Should be unique. | |
| 17 | - * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. | |
| 18 | - * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array. | |
| 19 | - * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL | |
| 20 | - * as a query string for cache busting purposes. If version is set to false, a version | |
| 21 | - * number is automatically added equal to current installed WordPress version. | |
| 22 | - * If set to null, no version is added. | |
| 23 | - * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>. | |
| 24 | - * Default 'false'. | |
| 25 | - */ | |
| 26 | -function gutenberg_override_script( $scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) { | |
| 27 | - $script = $scripts->query( $handle, 'registered' ); | |
| 28 | - if ( $script ) { | |
| 29 | - /* | |
| 30 | - * In many ways, this is a reimplementation of `wp_register_script` but | |
| 31 | - * bypassing consideration of whether a script by the given handle had | |
| 32 | - * already been registered. | |
| 33 | - */ | |
| 9 | +if ( ! function_exists( 'gutenberg_override_script' ) ) { | |
| 10 | + /** | |
| 11 | + * Registers a script according to `wp_register_script`. Honors this request by | |
| 12 | + * reassigning internal dependency properties of any script handle already | |
| 13 | + * registered by that name. It does not deregister the original script, to | |
| 14 | + * avoid losing inline scripts which may have been attached. | |
| 15 | + * | |
| 16 | + * @param WP_Scripts $scripts WP_Scripts instance. | |
| 17 | + * @param string $handle Name of the script. Should be unique. | |
| 18 | + * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. | |
| 19 | + * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array. | |
| 20 | + * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL | |
| 21 | + * as a query string for cache busting purposes. If version is set to false, a version | |
| 22 | + * number is automatically added equal to current installed WordPress version. | |
| 23 | + * If set to null, no version is added. | |
| 24 | + * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>. | |
| 25 | + * Default 'false'. | |
| 26 | + */ | |
| 27 | + function gutenberg_override_script( $scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) { | |
| 28 | + $script = $scripts->query( $handle, 'registered' ); | |
| 29 | + if ( $script ) { | |
| 30 | + /* | |
| 31 | + * In many ways, this is a reimplementation of `wp_register_script` but | |
| 32 | + * bypassing consideration of whether a script by the given handle had | |
| 33 | + * already been registered. | |
| 34 | + */ | |
| 34 | 35 | |
| 35 | - // See: `_WP_Dependency::__construct` . | |
| 36 | - $script->src = $src; | |
| 37 | - $script->deps = $deps; | |
| 38 | - $script->ver = $ver; | |
| 39 | - $script->args = $in_footer ? 1 : null; | |
| 40 | - } else { | |
| 41 | - $scripts->add( $handle, $src, $deps, $ver, ( $in_footer ? 1 : null ) ); | |
| 42 | - } | |
| 36 | + // See: `_WP_Dependency::__construct` . | |
| 37 | + $script->src = $src; | |
| 38 | + $script->deps = $deps; | |
| 39 | + $script->ver = $ver; | |
| 40 | + $script->args = $in_footer ? 1 : null; | |
| 41 | + } else { | |
| 42 | + $scripts->add( $handle, $src, $deps, $ver, ( $in_footer ? 1 : null ) ); | |
| 43 | + } | |
| 43 | 44 | |
| 44 | - if ( in_array( 'wp-i18n', $deps, true ) ) { | |
| 45 | - $scripts->set_translations( $handle ); | |
| 45 | + if ( in_array( 'wp-i18n', $deps, true ) ) { | |
| 46 | + $scripts->set_translations( $handle ); | |
| 47 | + } | |
| 46 | 48 | } |
| 47 | 49 | } |
| 48 | 50 | |
| 49 | -/** | |
| 50 | - * Register all package scripts. | |
| 51 | - */ | |
| 52 | -function gutenberg_register_package_scripts( $scripts ) { | |
| 53 | - // Load build constants | |
| 54 | - $build_constants = require __DIR__ . '/constants.php'; | |
| 55 | - $default_version = ! SCRIPT_DEBUG ? $build_constants['version'] : time(); | |
| 56 | - $extension = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.js' : '.min.js'; | |
| 51 | +if ( ! function_exists( 'gutenberg_register_package_scripts' ) ) { | |
| 52 | + /** | |
| 53 | + * Register all package scripts. | |
| 54 | + */ | |
| 55 | + function gutenberg_register_package_scripts( $scripts ) { | |
| 56 | + // Load build constants | |
| 57 | + $build_constants = require __DIR__ . '/constants.php'; | |
| 58 | + $default_version = ! SCRIPT_DEBUG ? $build_constants['version'] : time(); | |
| 59 | + $extension = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.js' : '.min.js'; | |
| 57 | 60 | |
| 58 | - $scripts_dir = __DIR__ . '/scripts'; | |
| 59 | - $scripts_file = $scripts_dir . '/registry.php'; | |
| 61 | + $scripts_dir = __DIR__ . '/scripts'; | |
| 62 | + $scripts_file = $scripts_dir . '/registry.php'; | |
| 60 | 63 | |
| 61 | - if ( ! file_exists( $scripts_file ) ) { | |
| 62 | - return; | |
| 63 | - } | |
| 64 | + if ( ! file_exists( $scripts_file ) ) { | |
| 65 | + return; | |
| 66 | + } | |
| 64 | 67 | |
| 65 | - $scripts_data = require $scripts_file; | |
| 66 | - $plugin_dir = dirname( __FILE__ ); | |
| 68 | + $scripts_data = require $scripts_file; | |
| 69 | + $plugin_dir = dirname( __FILE__ ); | |
| 67 | 70 | |
| 68 | - foreach ( $scripts_data as $script_data ) { | |
| 69 | - $asset_file = $scripts_dir . '/' . $script_data['asset']; | |
| 70 | - $asset = file_exists( $asset_file ) ? require $asset_file : array(); | |
| 71 | - $dependencies = $asset['dependencies'] ?? array(); | |
| 72 | - $version = $asset['version'] ?? $default_version; | |
| 71 | + foreach ( $scripts_data as $script_data ) { | |
| 72 | + $asset_file = $scripts_dir . '/' . $script_data['asset']; | |
| 73 | + $asset = file_exists( $asset_file ) ? require $asset_file : array(); | |
| 74 | + $dependencies = $asset['dependencies'] ?? array(); | |
| 75 | + $version = $asset['version'] ?? $default_version; | |
| 73 | 76 | |
| 74 | - gutenberg_override_script( | |
| 75 | - $scripts, | |
| 76 | - $script_data['handle'], | |
| 77 | - $build_constants['build_url'] . 'scripts/' . $script_data['path'] . $extension, | |
| 78 | - $dependencies, | |
| 79 | - $version, | |
| 80 | - true | |
| 81 | - ); | |
| 77 | + gutenberg_override_script( | |
| 78 | + $scripts, | |
| 79 | + $script_data['handle'], | |
| 80 | + $build_constants['build_url'] . 'scripts/' . $script_data['path'] . $extension, | |
| 81 | + $dependencies, | |
| 82 | + $version, | |
| 83 | + true | |
| 84 | + ); | |
| 85 | + } | |
| 82 | 86 | } |
| 87 | + | |
| 88 | + add_action( 'wp_default_scripts', 'gutenberg_register_package_scripts' ); | |
| 83 | 89 | } |
| 84 | - | |
| 85 | -add_action( 'wp_default_scripts', 'gutenberg_register_package_scripts' ); | |