PluginProbe
Gutenberg / 11.2.1
Gutenberg v11.2.1
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / global-styles.php

global-styles.php in Gutenberg 11.2.1, at lib/global-styles.php

345 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Bootstraps Global Styles.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Takes a tree adhering to the theme.json schema and generates
10 * the corresponding stylesheet.
11 *
12 * @param WP_Theme_JSON_Gutenberg $tree Input tree.
13 * @param string $type Type of stylesheet we want accepts 'all', 'block_styles', and 'css_variables'.
14 *
15 * @return string Stylesheet.
16 */
17 function gutenberg_experimental_global_styles_get_stylesheet( $tree, $type = 'all' ) {
18 // Check if we can use cached.
19 $can_use_cached = (
20 ( 'all' === $type ) &&
21 ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) &&
22 ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&
23 ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) &&
24 ! is_admin()
25 );
26
27 if ( $can_use_cached ) {
28 // Check if we have the styles already cached.
29 $cached = get_transient( 'global_styles' );
30 if ( $cached ) {
31 return $cached;
32 }
33 }
34
35 $stylesheet = $tree->get_stylesheet( $type );
36
37 if ( $can_use_cached ) {
38 // Cache for a minute.
39 // This cache doesn't need to be any longer, we only want to avoid spikes on high-traffic sites.
40 set_transient( 'global_styles', $stylesheet, MINUTE_IN_SECONDS );
41 }
42
43 return $stylesheet;
44 }
45
46 /**
47 * Fetches the preferences for each origin (core, theme, user)
48 * and enqueues the resulting stylesheet.
49 */
50 function gutenberg_experimental_global_styles_enqueue_assets() {
51 if (
52 ! get_theme_support( 'experimental-link-color' ) && // link color support needs the presets CSS variables regardless of the presence of theme.json file.
53 ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
54 return;
55 }
56
57 $settings = gutenberg_get_default_block_editor_settings();
58 $all = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $settings );
59
60 $stylesheet = gutenberg_experimental_global_styles_get_stylesheet( $all );
61 if ( empty( $stylesheet ) ) {
62 return;
63 }
64
65 if ( isset( wp_styles()->registered['global-styles'] ) ) {
66 wp_styles()->registered['global-styles']->extra['after'][0] = $stylesheet;
67 } else {
68 wp_register_style( 'global-styles', false, array(), true, true );
69 wp_add_inline_style( 'global-styles', $stylesheet );
70 wp_enqueue_style( 'global-styles' );
71 }
72 }
73
74 /**
75 * Adds the necessary settings for the Global Styles client UI.
76 *
77 * @param array $settings Existing block editor settings.
78 *
79 * @return array New block editor settings.
80 */
81 function gutenberg_experimental_global_styles_settings( $settings ) {
82 // Set what is the context for this data request.
83 $context = 'all';
84 if (
85 is_callable( 'get_current_screen' ) &&
86 function_exists( 'gutenberg_is_edit_site_page' ) &&
87 gutenberg_is_edit_site_page( get_current_screen()->id ) &&
88 WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() &&
89 gutenberg_supports_block_templates()
90 ) {
91 $context = 'site-editor';
92 }
93
94 if (
95 defined( 'REST_REQUEST' ) &&
96 REST_REQUEST &&
97 isset( $_GET['context'] ) &&
98 'mobile' === $_GET['context'] &&
99 WP_Theme_JSON_Resolver_Gutenberg::theme_has_support()
100 ) {
101 $context = 'mobile';
102 }
103
104 $origin = 'theme';
105 if (
106 WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() &&
107 gutenberg_supports_block_templates()
108 ) {
109 // Only lookup for the user data if we need it.
110 $origin = 'user';
111 }
112 $consolidated = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $settings, $origin );
113
114 if ( 'mobile' === $context ) {
115 $settings['__experimentalStyles'] = $consolidated->get_raw_data()['styles'];
116 }
117
118 if ( 'site-editor' === $context ) {
119 $theme = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $settings, 'theme' );
120 $user_cpt_id = WP_Theme_JSON_Resolver_Gutenberg::get_user_custom_post_type_id();
121
122 $settings['__experimentalGlobalStylesUserEntityId'] = $user_cpt_id;
123 $settings['__experimentalGlobalStylesBaseStyles'] = $theme->get_raw_data();
124 }
125
126 if (
127 'site-editor' !== $context &&
128 'mobile' !== $context &&
129 ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() || get_theme_support( 'experimental-link-color' ) )
130 ) {
131 $block_styles = array( 'css' => gutenberg_experimental_global_styles_get_stylesheet( $consolidated, 'block_styles' ) );
132 $css_variables = array(
133 'css' => gutenberg_experimental_global_styles_get_stylesheet( $consolidated, 'css_variables' ),
134 '__experimentalNoWrapper' => true,
135 );
136
137 // Make sure the styles array exists.
138 // In some contexts, like the navigation editor, it doesn't.
139 if ( ! isset( $settings['styles'] ) ) {
140 $settings['styles'] = array();
141 }
142
143 // Reset existing global styles.
144 foreach ( $settings['styles'] as $key => $style ) {
145 if ( isset( $style['__unstableType'] ) && 'globalStyles' === $style['__unstableType'] ) {
146 unset( $settings['styles'][ $key ] );
147 }
148 }
149
150 // Add the new ones.
151 $settings['styles'][] = $css_variables;
152 $settings['styles'][] = $block_styles;
153 }
154
155 // Copied from get_block_editor_settings() at wordpress-develop/block-editor.php.
156 $settings['__experimentalFeatures'] = $consolidated->get_settings();
157
158 if ( isset( $settings['__experimentalFeatures']['color']['palette'] ) ) {
159 $colors_by_origin = $settings['__experimentalFeatures']['color']['palette'];
160 $settings['colors'] = isset( $colors_by_origin['user'] ) ?
161 $colors_by_origin['user'] : (
162 isset( $colors_by_origin['theme'] ) ?
163 $colors_by_origin['theme'] :
164 $colors_by_origin['core']
165 );
166 }
167
168 if ( isset( $settings['__experimentalFeatures']['color']['gradients'] ) ) {
169 $gradients_by_origin = $settings['__experimentalFeatures']['color']['gradients'];
170 $settings['gradients'] = isset( $gradients_by_origin['user'] ) ?
171 $gradients_by_origin['user'] : (
172 isset( $gradients_by_origin['theme'] ) ?
173 $gradients_by_origin['theme'] :
174 $gradients_by_origin['core']
175 );
176 }
177
178 if ( isset( $settings['__experimentalFeatures']['typography']['fontSizes'] ) ) {
179 $font_sizes_by_origin = $settings['__experimentalFeatures']['typography']['fontSizes'];
180 $settings['fontSizes'] = isset( $font_sizes_by_origin['user'] ) ?
181 $font_sizes_by_origin['user'] : (
182 isset( $font_sizes_by_origin['theme'] ) ?
183 $font_sizes_by_origin['theme'] :
184 $font_sizes_by_origin['core']
185 );
186 }
187
188 if ( isset( $settings['__experimentalFeatures']['color']['custom'] ) ) {
189 $settings['disableCustomColors'] = ! $settings['__experimentalFeatures']['color']['custom'];
190 unset( $settings['__experimentalFeatures']['color']['custom'] );
191 }
192 if ( isset( $settings['__experimentalFeatures']['color']['customGradient'] ) ) {
193 $settings['disableCustomGradients'] = ! $settings['__experimentalFeatures']['color']['customGradient'];
194 unset( $settings['__experimentalFeatures']['color']['customGradient'] );
195 }
196 if ( isset( $settings['__experimentalFeatures']['typography']['customFontSize'] ) ) {
197 $settings['disableCustomFontSizes'] = ! $settings['__experimentalFeatures']['typography']['customFontSize'];
198 unset( $settings['__experimentalFeatures']['typography']['customFontSize'] );
199 }
200 if ( isset( $settings['__experimentalFeatures']['typography']['customLineHeight'] ) ) {
201 $settings['enableCustomLineHeight'] = $settings['__experimentalFeatures']['typography']['customLineHeight'];
202 unset( $settings['__experimentalFeatures']['typography']['customLineHeight'] );
203 }
204 if ( isset( $settings['__experimentalFeatures']['spacing']['units'] ) ) {
205 $settings['enableCustomUnits'] = $settings['__experimentalFeatures']['spacing']['units'];
206 }
207 if ( isset( $settings['__experimentalFeatures']['spacing']['customPadding'] ) ) {
208 $settings['enableCustomSpacing'] = $settings['__experimentalFeatures']['spacing']['customPadding'];
209 unset( $settings['__experimentalFeatures']['spacing']['customPadding'] );
210 }
211
212 return $settings;
213 }
214
215 /**
216 * Register CPT to store/access user data.
217 *
218 * @return array|undefined
219 */
220 function gutenberg_experimental_global_styles_register_user_cpt() {
221 if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
222 return;
223 }
224
225 WP_Theme_JSON_Resolver_Gutenberg::register_user_custom_post_type();
226 }
227
228 /**
229 * Sanitizes global styles user content removing unsafe rules.
230 *
231 * @param string $content Post content to filter.
232 * @return string Filtered post content with unsafe rules removed.
233 */
234 function gutenberg_global_styles_filter_post( $content ) {
235 $decoded_data = json_decode( stripslashes( $content ), true );
236 $json_decoding_error = json_last_error();
237 if (
238 JSON_ERROR_NONE === $json_decoding_error &&
239 is_array( $decoded_data ) &&
240 isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) &&
241 $decoded_data['isGlobalStylesUserThemeJSON']
242 ) {
243 unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
244
245 $data_to_encode = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $decoded_data );
246
247 $data_to_encode['isGlobalStylesUserThemeJSON'] = true;
248 return wp_json_encode( $data_to_encode );
249 }
250 return $content;
251 }
252
253 /**
254 * Adds the filters to filter global styles user theme.json.
255 */
256 function gutenberg_global_styles_kses_init_filters() {
257 add_filter( 'content_save_pre', 'gutenberg_global_styles_filter_post' );
258 }
259
260 /**
261 * Removes the filters to filter global styles user theme.json.
262 */
263 function gutenberg_global_styles_kses_remove_filters() {
264 remove_filter( 'content_save_pre', 'gutenberg_global_styles_filter_post' );
265 }
266
267 /**
268 * Register global styles kses filters if the user does not have unfiltered_html capability.
269 *
270 * @uses render_block_core_navigation()
271 * @throws WP_Error An WP_Error exception parsing the block definition.
272 */
273 function gutenberg_global_styles_kses_init() {
274 gutenberg_global_styles_kses_remove_filters();
275 if ( ! current_user_can( 'unfiltered_html' ) ) {
276 gutenberg_global_styles_kses_init_filters();
277 }
278 }
279
280 /**
281 * This filter is the last being executed on force_filtered_html_on_import.
282 * If the input of the filter is true it means we are in an import situation and should
283 * enable kses, independently of the user capabilities.
284 * So in that case we call gutenberg_global_styles_kses_init_filters;
285 *
286 * @param string $arg Input argument of the filter.
287 * @return string Exactly what was passed as argument.
288 */
289 function gutenberg_global_styles_force_filtered_html_on_import_filter( $arg ) {
290 // force_filtered_html_on_import is true we need to init the global styles kses filters.
291 if ( $arg ) {
292 gutenberg_global_styles_kses_init_filters();
293 }
294 return $arg;
295 }
296
297 /**
298 * This filter is the last being executed on force_filtered_html_on_import.
299 * If the input of the filter is true it means we are in an import situation and should
300 * enable kses, independently of the user capabilities.
301 * So in that case we call gutenberg_global_styles_kses_init_filters;
302 *
303 * @param bool $allow_css Whether the CSS in the test string is considered safe.
304 * @param bool $css_test_string The CSS string to test..
305 * @return bool If $allow_css is true it returns true.
306 * If $allow_css is false and the CSS rule is referencing a WordPress css variable it returns true.
307 * Otherwise the function return false.
308 */
309 function gutenberg_global_styles_include_support_for_wp_variables( $allow_css, $css_test_string ) {
310 if ( $allow_css ) {
311 return $allow_css;
312 }
313 $allowed_preset_attributes = array(
314 'background',
315 'background-color',
316 'border-color',
317 'color',
318 'font-family',
319 'font-size',
320 );
321 $parts = explode( ':', $css_test_string, 2 );
322
323 if ( ! in_array( trim( $parts[0] ), $allowed_preset_attributes, true ) ) {
324 return $allow_css;
325 }
326 return ! ! preg_match( '/^var\(--wp-[a-zA-Z0-9\-]+\)$/', trim( $parts[1] ) );
327 }
328
329 // The else clause can be removed when plugin support requires WordPress 5.8.0+.
330 if ( function_exists( 'get_block_editor_settings' ) ) {
331 add_filter( 'block_editor_settings_all', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX );
332 } else {
333 add_filter( 'block_editor_settings', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX );
334 }
335
336 add_action( 'init', 'gutenberg_experimental_global_styles_register_user_cpt' );
337 add_action( 'wp_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets' );
338
339 // kses actions&filters.
340 add_action( 'init', 'gutenberg_global_styles_kses_init' );
341 add_action( 'set_current_user', 'gutenberg_global_styles_kses_init' );
342 add_filter( 'force_filtered_html_on_import', 'gutenberg_global_styles_force_filtered_html_on_import_filter', 999 );
343 add_filter( 'safecss_filter_attr_allow_css', 'gutenberg_global_styles_include_support_for_wp_variables', 10, 2 );
344 // This filter needs to be executed last.
345