| 1 |
<?php |
| 2 |
/** |
| 3 |
* Updates client assets for the editor. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Registers vendor JavaScript files to be used as dependencies of the editor |
| 10 |
* and plugins. |
| 11 |
* |
| 12 |
* This function is called from a script during the plugin build process, so it |
| 13 |
* should not call any WordPress PHP functions. |
| 14 |
* |
| 15 |
* @since 13.0 |
| 16 |
* |
| 17 |
* @param WP_Scripts $scripts WP_Scripts instance. |
| 18 |
*/ |
| 19 |
function gutenberg_register_vendor_scripts( $scripts ) { |
| 20 |
$extension = SCRIPT_DEBUG ? '.js' : '.min.js'; |
| 21 |
|
| 22 |
gutenberg_override_script( |
| 23 |
$scripts, |
| 24 |
'react', |
| 25 |
gutenberg_url( 'build/vendors/react' . $extension ), |
| 26 |
// See https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md#externalising-react. |
| 27 |
SCRIPT_DEBUG ? array( 'wp-react-refresh-entry', 'wp-polyfill' ) : array( 'wp-polyfill' ) |
| 28 |
); |
| 29 |
gutenberg_override_script( |
| 30 |
$scripts, |
| 31 |
'react-dom', |
| 32 |
gutenberg_url( 'build/vendors/react-dom' . $extension ), |
| 33 |
array( 'react' ) |
| 34 |
); |
| 35 |
} |
| 36 |
add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' ); |
| 37 |
|
| 38 |
/** |
| 39 |
* Sets the editor styles to be consumed by JS. |
| 40 |
*/ |
| 41 |
function gutenberg_resolve_assets() { |
| 42 |
global $pagenow; |
| 43 |
|
| 44 |
$script_handles = array(); |
| 45 |
$style_handles = array( |
| 46 |
'wp-block-editor', |
| 47 |
'wp-block-library', |
| 48 |
'wp-edit-blocks', |
| 49 |
); |
| 50 |
|
| 51 |
if ( current_theme_supports( 'wp-block-styles' ) ) { |
| 52 |
$style_handles[] = 'wp-block-library-theme'; |
| 53 |
} |
| 54 |
|
| 55 |
if ( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) { |
| 56 |
$style_handles[] = 'wp-widgets'; |
| 57 |
$style_handles[] = 'wp-edit-widgets'; |
| 58 |
} |
| 59 |
|
| 60 |
$block_registry = WP_Block_Type_Registry::get_instance(); |
| 61 |
|
| 62 |
foreach ( $block_registry->get_all_registered() as $block_type ) { |
| 63 |
if ( ! empty( $block_type->style ) ) { |
| 64 |
$style_handles[] = $block_type->style; |
| 65 |
} |
| 66 |
|
| 67 |
if ( ! empty( $block_type->editor_style ) ) { |
| 68 |
$style_handles[] = $block_type->editor_style; |
| 69 |
} |
| 70 |
|
| 71 |
if ( ! empty( $block_type->script ) ) { |
| 72 |
$script_handles[] = $block_type->script; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
$style_handles = array_unique( $style_handles ); |
| 77 |
$done = wp_styles()->done; |
| 78 |
|
| 79 |
ob_start(); |
| 80 |
|
| 81 |
// We do not need reset styles for the iframed editor. |
| 82 |
wp_styles()->done = array( 'wp-reset-editor-styles' ); |
| 83 |
wp_styles()->do_items( $style_handles ); |
| 84 |
wp_styles()->done = $done; |
| 85 |
|
| 86 |
$styles = ob_get_clean(); |
| 87 |
|
| 88 |
$script_handles = array_unique( $script_handles ); |
| 89 |
$done = wp_scripts()->done; |
| 90 |
|
| 91 |
ob_start(); |
| 92 |
|
| 93 |
wp_scripts()->done = array(); |
| 94 |
wp_scripts()->do_items( $script_handles ); |
| 95 |
wp_scripts()->done = $done; |
| 96 |
|
| 97 |
$scripts = ob_get_clean(); |
| 98 |
|
| 99 |
return array( |
| 100 |
'styles' => $styles, |
| 101 |
'scripts' => $scripts, |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
add_filter( |
| 106 |
'block_editor_settings_all', |
| 107 |
function( $settings ) { |
| 108 |
// The `__unstableResolvedAssets` are generated by `_wp_get_iframed_editor_assets` for WP >= 6.0. |
| 109 |
if ( function_exists( '_wp_get_iframed_editor_assets' ) ) { |
| 110 |
return $settings; |
| 111 |
} |
| 112 |
|
| 113 |
// In the future we can allow WP Dependency handles to be passed. |
| 114 |
$settings['__unstableResolvedAssets'] = gutenberg_resolve_assets(); |
| 115 |
return $settings; |
| 116 |
} |
| 117 |
); |
| 118 |
|