From 1d54018ef66c0b4f594168308f95ca3dfbe11f70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Fri, 8 Apr 2022 15:03:49 +0200 Subject: [PATCH 1/4] Prevent styles from being added in the site editor --- src/wp-includes/block-editor.php | 53 ++++++++++++++++---------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index c9945eace991f..fc863dc31cb79 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -379,36 +379,37 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex ); $global_styles = array(); - $presets = array( - array( - 'css' => 'variables', - '__unstableType' => 'presets', - ), - array( - 'css' => 'presets', - '__unstableType' => 'presets', - ), - ); - foreach ( $presets as $preset_style ) { - $actual_css = wp_get_global_stylesheet( array( $preset_style['css'] ) ); - if ( '' !== $actual_css ) { - $preset_style['css'] = $actual_css; - $global_styles[] = $preset_style; + if ( ! empty( $block_editor_context->post ) ) { + $presets = array( + array( + 'css' => 'variables', + '__unstableType' => 'presets', + ), + array( + 'css' => 'presets', + '__unstableType' => 'presets', + ), + ); + foreach ( $presets as $preset_style ) { + $actual_css = wp_get_global_stylesheet( array( $preset_style['css'] ) ); + if ( '' !== $actual_css ) { + $preset_style['css'] = $actual_css; + $global_styles[] = $preset_style; + } } - } - if ( WP_Theme_JSON_Resolver::theme_has_support() ) { - $block_classes = array( - 'css' => 'styles', - '__unstableType' => 'theme', - ); - $actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) ); - if ( '' !== $actual_css ) { - $block_classes['css'] = $actual_css; - $global_styles[] = $block_classes; + if ( WP_Theme_JSON_Resolver::theme_has_support() ) { + $block_classes = array( + 'css' => 'styles', + '__unstableType' => 'theme', + ); + $actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) ); + if ( '' !== $actual_css ) { + $block_classes['css'] = $actual_css; + $global_styles[] = $block_classes; + } } } - $editor_settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() ); $editor_settings['__experimentalFeatures'] = wp_get_global_settings(); From 45c16b0dd1518b9ad9318ea2e1e10653a13159df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Fri, 8 Apr 2022 17:09:18 +0200 Subject: [PATCH 2/4] Revert "Prevent styles from being added in the site editor" This reverts commit f396ed824b7fa0db5d424e6ec977cd822306d75b. --- src/wp-includes/block-editor.php | 53 ++++++++++++++++---------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index fc863dc31cb79..c9945eace991f 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -379,37 +379,36 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex ); $global_styles = array(); - if ( ! empty( $block_editor_context->post ) ) { - $presets = array( - array( - 'css' => 'variables', - '__unstableType' => 'presets', - ), - array( - 'css' => 'presets', - '__unstableType' => 'presets', - ), - ); - foreach ( $presets as $preset_style ) { - $actual_css = wp_get_global_stylesheet( array( $preset_style['css'] ) ); - if ( '' !== $actual_css ) { - $preset_style['css'] = $actual_css; - $global_styles[] = $preset_style; - } + $presets = array( + array( + 'css' => 'variables', + '__unstableType' => 'presets', + ), + array( + 'css' => 'presets', + '__unstableType' => 'presets', + ), + ); + foreach ( $presets as $preset_style ) { + $actual_css = wp_get_global_stylesheet( array( $preset_style['css'] ) ); + if ( '' !== $actual_css ) { + $preset_style['css'] = $actual_css; + $global_styles[] = $preset_style; } + } - if ( WP_Theme_JSON_Resolver::theme_has_support() ) { - $block_classes = array( - 'css' => 'styles', - '__unstableType' => 'theme', - ); - $actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) ); - if ( '' !== $actual_css ) { - $block_classes['css'] = $actual_css; - $global_styles[] = $block_classes; - } + if ( WP_Theme_JSON_Resolver::theme_has_support() ) { + $block_classes = array( + 'css' => 'styles', + '__unstableType' => 'theme', + ); + $actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) ); + if ( '' !== $actual_css ) { + $block_classes['css'] = $actual_css; + $global_styles[] = $block_classes; } } + $editor_settings['styles'] = array_merge( $global_styles, get_block_editor_theme_styles() ); $editor_settings['__experimentalFeatures'] = wp_get_global_settings(); From 3543bf37bd38d39c1a5c467b46d4ac9d7529de19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Fri, 8 Apr 2022 17:11:22 +0200 Subject: [PATCH 3/4] Add __unstableSource and update __unstableType We need to do two orthogonal things: - Be able to tell which are block styles and which are preset styles: the later should still be loaded when the user disables the "theme styles". - Be able to tell which styles come from "theme.json" and which don't: so we can overwrite the former in the site editor, for example. --- src/wp-includes/block-editor.php | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index c9945eace991f..d1a154c7d7a99 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -381,12 +381,14 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex $global_styles = array(); $presets = array( array( - 'css' => 'variables', - '__unstableType' => 'presets', + 'css' => 'variables', + '__unstableType' => 'presets', + '__unstableSource' => 'theme_json', ), array( - 'css' => 'presets', - '__unstableType' => 'presets', + 'css' => 'presets', + '__unstableType' => 'presets', + '__unstableSource' => 'theme_json', ), ); foreach ( $presets as $preset_style ) { @@ -399,8 +401,9 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex if ( WP_Theme_JSON_Resolver::theme_has_support() ) { $block_classes = array( - 'css' => 'styles', - '__unstableType' => 'theme', + 'css' => 'styles', + '__unstableType' => 'styles', + '__unstableSource' => 'theme_json', ); $actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) ); if ( '' !== $actual_css ) { @@ -626,17 +629,19 @@ function get_block_editor_theme_styles() { $response = wp_remote_get( $style ); if ( ! is_wp_error( $response ) ) { $styles[] = array( - 'css' => wp_remote_retrieve_body( $response ), - '__unstableType' => 'theme', + 'css' => wp_remote_retrieve_body( $response ), + '__unstableType' => 'styles', + '__unstableSource' => 'css', ); } } else { $file = get_theme_file_path( $style ); if ( is_file( $file ) ) { $styles[] = array( - 'css' => file_get_contents( $file ), - 'baseURL' => get_theme_file_uri( $style ), - '__unstableType' => 'theme', + 'css' => file_get_contents( $file ), + 'baseURL' => get_theme_file_uri( $style ), + '__unstableType' => 'styles', + '__unstableSource' => 'css', ); } } From 6dc462f3df1ac7ef544244c33d88081a356009c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Wed, 13 Apr 2022 11:35:52 +0200 Subject: [PATCH 4/4] Mark whether styles come from global styles --- src/wp-includes/block-editor.php | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index d1a154c7d7a99..48bf0dad3828d 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -381,14 +381,14 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex $global_styles = array(); $presets = array( array( - 'css' => 'variables', - '__unstableType' => 'presets', - '__unstableSource' => 'theme_json', + 'css' => 'variables', + '__unstableType' => 'presets', + 'isGlobalStyles' => true, ), array( - 'css' => 'presets', - '__unstableType' => 'presets', - '__unstableSource' => 'theme_json', + 'css' => 'presets', + '__unstableType' => 'presets', + 'isGlobalStyles' => true, ), ); foreach ( $presets as $preset_style ) { @@ -401,9 +401,9 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex if ( WP_Theme_JSON_Resolver::theme_has_support() ) { $block_classes = array( - 'css' => 'styles', - '__unstableType' => 'styles', - '__unstableSource' => 'theme_json', + 'css' => 'styles', + '__unstableType' => 'theme', + 'isGlobalStyles' => true, ); $actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) ); if ( '' !== $actual_css ) { @@ -629,19 +629,19 @@ function get_block_editor_theme_styles() { $response = wp_remote_get( $style ); if ( ! is_wp_error( $response ) ) { $styles[] = array( - 'css' => wp_remote_retrieve_body( $response ), - '__unstableType' => 'styles', - '__unstableSource' => 'css', + 'css' => wp_remote_retrieve_body( $response ), + '__unstableType' => 'theme', + 'isGlobalStyles' => false, ); } } else { $file = get_theme_file_path( $style ); if ( is_file( $file ) ) { $styles[] = array( - 'css' => file_get_contents( $file ), - 'baseURL' => get_theme_file_uri( $style ), - '__unstableType' => 'styles', - '__unstableSource' => 'css', + 'css' => file_get_contents( $file ), + 'baseURL' => get_theme_file_uri( $style ), + '__unstableType' => 'theme', + 'isGlobalStyles' => false, ); } }