Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit site: prevent enqueuing entire stylesheet in iframe #54254

Merged
merged 3 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions lib/compat/wordpress-6.4/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,92 @@ function gutenberg_update_wp_date_settings( $scripts ) {

add_action( 'wp_default_scripts', 'gutenberg_update_wp_date_settings' );

/**
* Collect the block editor assets that need to be loaded into the editor's iframe.
*
* @since 6.0.0
* @access private
*
* @return array {
* The block editor assets.
*
* @type string|false $styles String containing the HTML for styles.
* @type string|false $scripts String containing the HTML for scripts.
* }
*/
function _gutenberg_get_iframed_editor_assets_6_4() {
global $wp_styles, $wp_scripts;

// Keep track of the styles and scripts instance to restore later.
$current_wp_styles = $wp_styles;
$current_wp_scripts = $wp_scripts;

// Create new instances to collect the assets.
$wp_styles = new WP_Styles();
$wp_scripts = new WP_Scripts();

// Register all currently registered styles and scripts. The actions that
// follow enqueue assets, but don't necessarily register them.
$wp_styles->registered = $current_wp_styles->registered;
$wp_scripts->registered = $current_wp_scripts->registered;

// We generally do not need reset styles for the iframed editor.
// However, if it's a classic theme, margins will be added to every block,
// which is reset specifically for list items, so classic themes rely on
// these reset styles.
$wp_styles->done =
wp_theme_has_theme_json() ? array( 'wp-reset-editor-styles' ) : array();

wp_enqueue_script( 'wp-polyfill' );
// Enqueue the `editorStyle` handles for all core block, and dependencies.
wp_enqueue_style( 'wp-edit-blocks' );

if ( current_theme_supports( 'wp-block-styles' ) ) {
wp_enqueue_style( 'wp-block-library-theme' );
}

// We don't want to load EDITOR scripts in the iframe, only enqueue
// front-end assets for the content.
add_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' );
do_action( 'enqueue_block_assets' );
remove_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' );

$block_registry = WP_Block_Type_Registry::get_instance();

// Additionally, do enqueue `editorStyle` assets for all blocks, which
// contains editor-only styling for blocks (editor content).
foreach ( $block_registry->get_all_registered() as $block_type ) {
if ( isset( $block_type->editor_style_handles ) && is_array( $block_type->editor_style_handles ) ) {
foreach ( $block_type->editor_style_handles as $style_handle ) {
wp_enqueue_style( $style_handle );
}
}
}

ob_start();
wp_print_styles();
$styles = ob_get_clean();

ob_start();
wp_print_head_scripts();
wp_print_footer_scripts();
$scripts = ob_get_clean();

// Restore the original instances.
$wp_styles = $current_wp_styles;
$wp_scripts = $current_wp_scripts;

return array(
'styles' => $styles,
'scripts' => $scripts,
);
}

add_filter(
'block_editor_settings_all',
static function( $settings ) {
// We must override what core is passing now.
$settings['__unstableResolvedAssets'] = _gutenberg_get_iframed_editor_assets_6_4();
return $settings;
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@
}
}

.edit-site-sidebar-navigation-screen-navigation-menus__content .popover-slot .wp-block-navigation-submenu {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rule seems now useless and in my tests could be safely removed.

display: none;
}

.edit-site-sidebar-navigation-screen-navigation-menus__loading.components-spinner {
margin-left: auto;
margin-right: auto;
Expand Down