| 1 |
<?php |
| 2 |
/** |
| 3 |
* Updates the script-loader.php file |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Removes the actions that output all duotone SVG filter presets. |
| 10 |
* WP_Duotone_Gutenberg now handles which SVG filters should be output |
| 11 |
* depending on the block content. |
| 12 |
*/ |
| 13 |
remove_action( 'wp_body_open', 'wp_global_styles_render_svg_filters' ); |
| 14 |
remove_action( 'in_admin_header', 'wp_global_styles_render_svg_filters' ); |
| 15 |
|
| 16 |
/** |
| 17 |
* Collect the block editor assets that need to be loaded into the editor's iframe. |
| 18 |
* |
| 19 |
* @since 6.0.0 |
| 20 |
* @access private |
| 21 |
* |
| 22 |
* @return array { |
| 23 |
* The block editor assets. |
| 24 |
* |
| 25 |
* @type string|false $styles String containing the HTML for styles. |
| 26 |
* @type string|false $scripts String containing the HTML for scripts. |
| 27 |
* } |
| 28 |
*/ |
| 29 |
function _gutenberg_get_iframed_editor_assets() { |
| 30 |
global $wp_styles, $wp_scripts, $pagenow; |
| 31 |
|
| 32 |
// Keep track of the styles and scripts instance to restore later. |
| 33 |
$current_wp_styles = $wp_styles; |
| 34 |
$current_wp_scripts = $wp_scripts; |
| 35 |
|
| 36 |
// Create new instances to collect the assets. |
| 37 |
$wp_styles = new WP_Styles(); |
| 38 |
$wp_scripts = new WP_Scripts(); |
| 39 |
|
| 40 |
// Register all currently registered styles and scripts. The actions that |
| 41 |
// follow enqueue assets, but don't necessarily register them. |
| 42 |
$wp_styles->registered = $current_wp_styles->registered; |
| 43 |
$wp_scripts->registered = $current_wp_scripts->registered; |
| 44 |
|
| 45 |
// We generally do not need reset styles for the iframed editor. |
| 46 |
// However, if it's a classic theme, margins will be added to every block, |
| 47 |
// which is reset specifically for list items, so classic themes rely on |
| 48 |
// these reset styles. |
| 49 |
$wp_styles->done = |
| 50 |
wp_theme_has_theme_json() ? array( 'wp-reset-editor-styles' ) : array(); |
| 51 |
|
| 52 |
wp_enqueue_script( 'wp-polyfill' ); |
| 53 |
// Enqueue the `editorStyle` handles for all core block, and dependencies. |
| 54 |
wp_enqueue_style( 'wp-edit-blocks' ); |
| 55 |
|
| 56 |
if ( 'site-editor.php' === $pagenow ) { |
| 57 |
wp_enqueue_style( 'wp-edit-site' ); |
| 58 |
} |
| 59 |
|
| 60 |
if ( current_theme_supports( 'wp-block-styles' ) ) { |
| 61 |
wp_enqueue_style( 'wp-block-library-theme' ); |
| 62 |
} |
| 63 |
|
| 64 |
// We don't want to load EDITOR scripts in the iframe, only enqueue |
| 65 |
// front-end assets for the content. |
| 66 |
add_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' ); |
| 67 |
do_action( 'enqueue_block_assets' ); |
| 68 |
remove_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' ); |
| 69 |
|
| 70 |
$block_registry = WP_Block_Type_Registry::get_instance(); |
| 71 |
|
| 72 |
// Additionally, do enqueue `editorStyle` assets for all blocks, which |
| 73 |
// contains editor-only styling for blocks (editor content). |
| 74 |
foreach ( $block_registry->get_all_registered() as $block_type ) { |
| 75 |
if ( isset( $block_type->editor_style_handles ) && is_array( $block_type->editor_style_handles ) ) { |
| 76 |
foreach ( $block_type->editor_style_handles as $style_handle ) { |
| 77 |
wp_enqueue_style( $style_handle ); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
ob_start(); |
| 83 |
wp_print_styles(); |
| 84 |
wp_print_fonts( true ); |
| 85 |
$styles = ob_get_clean(); |
| 86 |
|
| 87 |
ob_start(); |
| 88 |
wp_print_head_scripts(); |
| 89 |
wp_print_footer_scripts(); |
| 90 |
$scripts = ob_get_clean(); |
| 91 |
|
| 92 |
// Restore the original instances. |
| 93 |
$wp_styles = $current_wp_styles; |
| 94 |
$wp_scripts = $current_wp_scripts; |
| 95 |
|
| 96 |
return array( |
| 97 |
'styles' => $styles, |
| 98 |
'scripts' => $scripts, |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
add_filter( |
| 103 |
'block_editor_settings_all', |
| 104 |
static function( $settings ) { |
| 105 |
// We must override what core is passing now. |
| 106 |
$settings['__unstableResolvedAssets'] = _gutenberg_get_iframed_editor_assets(); |
| 107 |
return $settings; |
| 108 |
} |
| 109 |
); |
| 110 |
|