5
5
* @package gutenberg
6
6
*/
7
7
8
+ /**
9
+ * Returns true if the style is coming from global styles.
10
+ *
11
+ * @param array $style Array containing a '__unstableType' key.
12
+ * @return boolean
13
+ */
14
+ function gutenberg_is_global_styles_in_5_8 ( $ style ) {
15
+ if ( isset ( $ style ['__unstableType ' ] ) && ( 'globalStyles ' === $ style ['__unstableType ' ] ) ) {
16
+ return true ;
17
+ }
18
+
19
+ return false ;
20
+ }
21
+
22
+ /**
23
+ * Returns true if the style is coming from global styles.
24
+ *
25
+ * @param array $style Array containing a '__unstableType' key and a 'css' key with the actual CSS.
26
+ * @return boolean
27
+ */
28
+ function gutenberg_is_global_styles_in_5_9 ( $ style ) {
29
+ /*
30
+ * In WordPress 5.9 we don't have a mechanism to distinguish block styles generated via theme.json
31
+ * from styles that come from the stylesheet of a theme.
32
+ *
33
+ * We do know that the block styles generated via theme.json have some rules for alignment.
34
+ * Hence, by detecting the presence of these rules, we can tell with high certainty
35
+ * whether or not the incoming $style has been generated from theme.json.
36
+ */
37
+ $ root_styles = '.wp-site-blocks > .alignleft { float: left; margin-right: 2em; } ' ;
38
+ $ root_styles .= '.wp-site-blocks > .alignright { float: right; margin-left: 2em; } ' ;
39
+ $ root_styles .= '.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; } ' ;
40
+
41
+ if (
42
+ ( isset ( $ style ['__unstableType ' ] ) && ( 'presets ' === $ style ['__unstableType ' ] ) ) ||
43
+ ( isset ( $ style ['__unstableType ' ] ) && ( 'theme ' === $ style ['__unstableType ' ] ) && str_contains ( $ style ['css ' ], $ root_styles ) )
44
+ ) {
45
+ return true ;
46
+ }
47
+
48
+ return false ;
49
+ }
50
+
8
51
/**
9
52
* Adds styles and __experimentalFeatures to the block editor settings.
10
53
*
15
58
function gutenberg_get_block_editor_settings ( $ settings ) {
16
59
// Set what is the context for this data request.
17
60
$ context = 'other ' ;
18
- if (
19
- is_callable ( 'get_current_screen ' ) &&
20
- function_exists ( 'gutenberg_is_edit_site_page ' ) &&
21
- is_object ( get_current_screen () ) &&
22
- gutenberg_is_edit_site_page ( get_current_screen ()->id )
23
- ) {
24
- $ context = 'site-editor ' ;
25
- }
26
-
27
61
if (
28
62
defined ( 'REST_REQUEST ' ) &&
29
63
REST_REQUEST &&
@@ -34,49 +68,41 @@ function_exists( 'gutenberg_is_edit_site_page' ) &&
34
68
}
35
69
36
70
if ( 'other ' === $ context ) {
71
+ global $ wp_version ;
72
+ $ is_wp_5_8 = version_compare ( $ wp_version , '5.8 ' , '>= ' ) && version_compare ( $ wp_version , '5.9 ' , '< ' );
73
+ $ is_wp_5_9 = version_compare ( $ wp_version , '5.9 ' , '>= ' ) && version_compare ( $ wp_version , '6.0-beta1 ' , '< ' );
74
+ $ is_wp_6_0 = version_compare ( $ wp_version , '6.0-beta1 ' , '>= ' );
75
+
37
76
// Make sure the styles array exists.
38
77
// In some contexts, like the navigation editor, it doesn't.
39
78
if ( ! isset ( $ settings ['styles ' ] ) ) {
40
79
$ settings ['styles ' ] = array ();
41
80
}
42
81
82
+ // Remove existing global styles provided by core.
43
83
$ styles_without_existing_global_styles = array ();
44
84
foreach ( $ settings ['styles ' ] as $ style ) {
45
85
if (
46
- ! isset ( $ style ['__unstableType ' ] ) ||
47
- // '__unstableType' is 'globalStyles' for WordPress 5.8 and 'presets' for WordPress 5.9.
48
- //
49
- // Note that styles classified as'theme', can be from the theme stylesheet
50
- // or from the theme.json (the styles section).
51
- // We are unable to identify which is which, so we can't remove and recreate those.
52
- // Instead, we reload the theme.json styles from the plugin.
53
- // Because they'll use the same selectors and load later,
54
- // they'll have higher priority than core's.
55
- //
56
- // Theoretically, this approach with 'theme' styles could be problematic:
57
- // if we remove style properties in the plugin, if selectors change, etc.
58
- // We need to address this issue directly in core by alowing to identify
59
- // styles coming from theme.json.
60
- //
61
- // A final note about 'theme' styles: this flag is used to identify theme
62
- // styles that may need to be removed if the user toggles
63
- // "Preferences > Use theme styles" in the preferences modal.
64
- //
65
- ! in_array ( $ style ['__unstableType ' ], array ( 'globalStyles ' , 'presets ' ), true )
86
+ ( $ is_wp_5_8 && ! gutenberg_is_global_styles_in_5_8 ( $ style ) ) || // Can be removed when plugin minimum version is 5.9.
87
+ ( $ is_wp_5_9 && ! gutenberg_is_global_styles_in_5_9 ( $ style ) ) || // Can be removed when plugin minimum version is 6.0.
88
+ ( $ is_wp_6_0 && ( ! isset ( $ style ['isGlobalStyles ' ] ) || ! $ style ['isGlobalStyles ' ] ) )
66
89
) {
67
90
$ styles_without_existing_global_styles [] = $ style ;
68
91
}
69
92
}
70
93
94
+ // Recreate global styles.
71
95
$ new_global_styles = array ();
72
96
$ presets = array (
73
97
array (
74
98
'css ' => 'variables ' ,
75
99
'__unstableType ' => 'presets ' ,
100
+ 'isGlobalStyles ' => true ,
76
101
),
77
102
array (
78
103
'css ' => 'presets ' ,
79
104
'__unstableType ' => 'presets ' ,
105
+ 'isGlobalStyles ' => true ,
80
106
),
81
107
);
82
108
foreach ( $ presets as $ preset_style ) {
@@ -91,6 +117,7 @@ function_exists( 'gutenberg_is_edit_site_page' ) &&
91
117
$ block_classes = array (
92
118
'css ' => 'styles ' ,
93
119
'__unstableType ' => 'theme ' ,
120
+ 'isGlobalStyles ' => true ,
94
121
);
95
122
$ actual_css = gutenberg_get_global_stylesheet ( array ( $ block_classes ['css ' ] ) );
96
123
if ( '' !== $ actual_css ) {
0 commit comments