PluginProbe
Gutenberg / 10.8.1
Gutenberg v10.8.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 10.8.1, at lib/global-styles.php

317 lines 11.5 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 $settings['styles'][] = $css_variables;
138 $settings['styles'][] = $block_styles;
139 }
140
141 // Copied from get_block_editor_settings() at wordpress-develop/block-editor.php.
142 $settings['__experimentalFeatures'] = $consolidated->get_settings();
143 if ( isset( $settings['__experimentalFeatures']['color']['palette'] ) ) {
144 $settings['colors'] = $settings['__experimentalFeatures']['color']['palette'];
145 unset( $settings['__experimentalFeatures']['color']['palette'] );
146 }
147 if ( isset( $settings['__experimentalFeatures']['color']['gradients'] ) ) {
148 $settings['gradients'] = $settings['__experimentalFeatures']['color']['gradients'];
149 unset( $settings['__experimentalFeatures']['color']['gradients'] );
150 }
151 if ( isset( $settings['__experimentalFeatures']['color']['custom'] ) ) {
152 $settings['disableCustomColors'] = ! $settings['__experimentalFeatures']['color']['custom'];
153 unset( $settings['__experimentalFeatures']['color']['custom'] );
154 }
155 if ( isset( $settings['__experimentalFeatures']['color']['customGradient'] ) ) {
156 $settings['disableCustomGradients'] = ! $settings['__experimentalFeatures']['color']['customGradient'];
157 unset( $settings['__experimentalFeatures']['color']['customGradient'] );
158 }
159 if ( isset( $settings['__experimentalFeatures']['typography']['fontSizes'] ) ) {
160 $settings['fontSizes'] = $settings['__experimentalFeatures']['typography']['fontSizes'];
161 unset( $settings['__experimentalFeatures']['typography']['fontSizes'] );
162 }
163 if ( isset( $settings['__experimentalFeatures']['typography']['customFontSize'] ) ) {
164 $settings['disableCustomFontSizes'] = ! $settings['__experimentalFeatures']['typography']['customFontSize'];
165 unset( $settings['__experimentalFeatures']['typography']['customFontSize'] );
166 }
167 if ( isset( $settings['__experimentalFeatures']['typography']['customLineHeight'] ) ) {
168 $settings['enableCustomLineHeight'] = $settings['__experimentalFeatures']['typography']['customLineHeight'];
169 unset( $settings['__experimentalFeatures']['typography']['customLineHeight'] );
170 }
171 if ( isset( $settings['__experimentalFeatures']['spacing']['units'] ) ) {
172 if ( ! is_array( $settings['__experimentalFeatures']['spacing']['units'] ) ) {
173 $settings['enableCustomUnits'] = false;
174 } else {
175 $settings['enableCustomUnits'] = count( $settings['__experimentalFeatures']['spacing']['units'] ) > 0;
176 }
177 unset( $settings['__experimentalFeatures']['spacing']['units'] );
178 }
179 if ( isset( $settings['__experimentalFeatures']['spacing']['customPadding'] ) ) {
180 $settings['enableCustomSpacing'] = $settings['__experimentalFeatures']['spacing']['customPadding'];
181 unset( $settings['__experimentalFeatures']['spacing']['customPadding'] );
182 }
183
184 return $settings;
185 }
186
187 /**
188 * Register CPT to store/access user data.
189 *
190 * @return array|undefined
191 */
192 function gutenberg_experimental_global_styles_register_user_cpt() {
193 if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
194 return;
195 }
196
197 WP_Theme_JSON_Resolver_Gutenberg::register_user_custom_post_type();
198 }
199
200 /**
201 * Sanitizes global styles user content removing unsafe rules.
202 *
203 * @param string $content Post content to filter.
204 * @return string Filtered post content with unsafe rules removed.
205 */
206 function gutenberg_global_styles_filter_post( $content ) {
207 $decoded_data = json_decode( stripslashes( $content ), true );
208 $json_decoding_error = json_last_error();
209 if (
210 JSON_ERROR_NONE === $json_decoding_error &&
211 is_array( $decoded_data ) &&
212 isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) &&
213 $decoded_data['isGlobalStylesUserThemeJSON']
214 ) {
215 unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
216 $theme_json = new WP_Theme_JSON_Gutenberg( $decoded_data );
217 $theme_json->remove_insecure_properties();
218 $data_to_encode = $theme_json->get_raw_data();
219 $data_to_encode['isGlobalStylesUserThemeJSON'] = true;
220 return wp_json_encode( $data_to_encode );
221 }
222 return $content;
223 }
224
225 /**
226 * Adds the filters to filter global styles user theme.json.
227 */
228 function gutenberg_global_styles_kses_init_filters() {
229 add_filter( 'content_save_pre', 'gutenberg_global_styles_filter_post' );
230 }
231
232 /**
233 * Removes the filters to filter global styles user theme.json.
234 */
235 function gutenberg_global_styles_kses_remove_filters() {
236 remove_filter( 'content_save_pre', 'gutenberg_global_styles_filter_post' );
237 }
238
239 /**
240 * Register global styles kses filters if the user does not have unfiltered_html capability.
241 *
242 * @uses render_block_core_navigation()
243 * @throws WP_Error An WP_Error exception parsing the block definition.
244 */
245 function gutenberg_global_styles_kses_init() {
246 gutenberg_global_styles_kses_remove_filters();
247 if ( ! current_user_can( 'unfiltered_html' ) ) {
248 gutenberg_global_styles_kses_init_filters();
249 }
250 }
251
252 /**
253 * This filter is the last being executed on force_filtered_html_on_import.
254 * If the input of the filter is true it means we are in an import situation and should
255 * enable kses, independently of the user capabilities.
256 * So in that case we call gutenberg_global_styles_kses_init_filters;
257 *
258 * @param string $arg Input argument of the filter.
259 * @return string Exactly what was passed as argument.
260 */
261 function gutenberg_global_styles_force_filtered_html_on_import_filter( $arg ) {
262 // force_filtered_html_on_import is true we need to init the global styles kses filters.
263 if ( $arg ) {
264 gutenberg_global_styles_kses_init_filters();
265 }
266 return $arg;
267 }
268
269 /**
270 * This filter is the last being executed on force_filtered_html_on_import.
271 * If the input of the filter is true it means we are in an import situation and should
272 * enable kses, independently of the user capabilities.
273 * So in that case we call gutenberg_global_styles_kses_init_filters;
274 *
275 * @param bool $allow_css Whether the CSS in the test string is considered safe.
276 * @param bool $css_test_string The CSS string to test..
277 * @return bool If $allow_css is true it returns true.
278 * If $allow_css is false and the CSS rule is referencing a WordPress css variable it returns true.
279 * Otherwise the function return false.
280 */
281 function gutenberg_global_styles_include_support_for_wp_variables( $allow_css, $css_test_string ) {
282 if ( $allow_css ) {
283 return $allow_css;
284 }
285 $allowed_preset_attributes = array(
286 'background',
287 'background-color',
288 'border-color',
289 'color',
290 'font-family',
291 'font-size',
292 );
293 $parts = explode( ':', $css_test_string, 2 );
294
295 if ( ! in_array( trim( $parts[0] ), $allowed_preset_attributes, true ) ) {
296 return $allow_css;
297 }
298 return ! ! preg_match( '/^var\(--wp-[a-zA-Z0-9\-]+\)$/', trim( $parts[1] ) );
299 }
300
301 // The else clause can be removed when plugin support requires WordPress 5.8.0+.
302 if ( function_exists( 'get_block_editor_settings' ) ) {
303 add_filter( 'block_editor_settings_all', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX );
304 } else {
305 add_filter( 'block_editor_settings', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX );
306 }
307
308 add_action( 'init', 'gutenberg_experimental_global_styles_register_user_cpt' );
309 add_action( 'wp_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets' );
310
311 // kses actions&filters.
312 add_action( 'init', 'gutenberg_global_styles_kses_init' );
313 add_action( 'set_current_user', 'gutenberg_global_styles_kses_init' );
314 add_filter( 'force_filtered_html_on_import', 'gutenberg_global_styles_force_filtered_html_on_import_filter', 999 );
315 add_filter( 'safecss_filter_attr_allow_css', 'gutenberg_global_styles_include_support_for_wp_variables', 10, 2 );
316 // This filter needs to be executed last.
317