PluginProbe
Gutenberg / 12.6.0
Gutenberg v12.6.0
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 12.6.0, at lib/global-styles.php

221 lines 7.6 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 * Adds the necessary settings for the Global Styles client UI.
10 *
11 * @param array $settings Existing block editor settings.
12 *
13 * @return array New block editor settings.
14 */
15 function gutenberg_experimental_global_styles_settings( $settings ) {
16 // Set what is the context for this data request.
17 $context = 'other';
18 if (
19 is_callable( 'get_current_screen' ) &&
20 function_exists( 'gutenberg_is_edit_site_page' ) &&
21 gutenberg_is_edit_site_page( get_current_screen()->id )
22 ) {
23 $context = 'site-editor';
24 }
25
26 if (
27 defined( 'REST_REQUEST' ) &&
28 REST_REQUEST &&
29 isset( $_GET['context'] ) &&
30 'mobile' === $_GET['context']
31 ) {
32 $context = 'mobile';
33 }
34
35 if ( 'mobile' === $context && WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
36 $settings['__experimentalStyles'] = wp_get_global_styles();
37 }
38
39 if ( 'other' === $context ) {
40 // Make sure the styles array exists.
41 // In some contexts, like the navigation editor, it doesn't.
42 if ( ! isset( $settings['styles'] ) ) {
43 $settings['styles'] = array();
44 }
45
46 // Reset existing global styles.
47 $styles_without_existing_global_styles = array();
48 foreach ( $settings['styles'] as $style ) {
49 if ( ! isset( $style['__unstableType'] ) || 'globalStyles' !== $style['__unstableType'] ) {
50 $styles_without_existing_global_styles[] = $style;
51 }
52 }
53
54 $new_global_styles = array();
55
56 $css_variables = wp_get_global_stylesheet( array( 'variables' ) );
57 if ( '' !== $css_variables ) {
58 $new_global_styles[] = array(
59 'css' => $css_variables,
60 '__unstableType' => 'presets',
61 );
62 }
63
64 $css_presets = wp_get_global_stylesheet( array( 'presets' ) );
65 if ( '' !== $css_presets ) {
66 $new_global_styles[] = array(
67 'css' => $css_presets,
68 '__unstableType' => 'presets',
69 );
70 }
71
72 if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
73 $css_blocks = wp_get_global_stylesheet( array( 'styles' ) );
74 if ( '' !== $css_blocks ) {
75 $new_global_styles[] = array(
76 'css' => $css_blocks,
77 '__unstableType' => 'theme',
78 );
79 }
80 }
81
82 $settings['styles'] = array_merge( $new_global_styles, $styles_without_existing_global_styles );
83 }
84
85 // Copied from get_block_editor_settings() at wordpress-develop/block-editor.php.
86 $settings['__experimentalFeatures'] = wp_get_global_settings();
87
88 if ( isset( $settings['__experimentalFeatures']['color']['palette'] ) ) {
89 $colors_by_origin = $settings['__experimentalFeatures']['color']['palette'];
90 $settings['colors'] = isset( $colors_by_origin['custom'] ) ?
91 $colors_by_origin['custom'] : (
92 isset( $colors_by_origin['theme'] ) ?
93 $colors_by_origin['theme'] :
94 $colors_by_origin['default']
95 );
96 }
97
98 if ( isset( $settings['__experimentalFeatures']['color']['gradients'] ) ) {
99 $gradients_by_origin = $settings['__experimentalFeatures']['color']['gradients'];
100 $settings['gradients'] = isset( $gradients_by_origin['custom'] ) ?
101 $gradients_by_origin['custom'] : (
102 isset( $gradients_by_origin['theme'] ) ?
103 $gradients_by_origin['theme'] :
104 $gradients_by_origin['default']
105 );
106 }
107
108 if ( isset( $settings['__experimentalFeatures']['typography']['fontSizes'] ) ) {
109 $font_sizes_by_origin = $settings['__experimentalFeatures']['typography']['fontSizes'];
110 $settings['fontSizes'] = isset( $font_sizes_by_origin['custom'] ) ?
111 $font_sizes_by_origin['user'] : (
112 isset( $font_sizes_by_origin['theme'] ) ?
113 $font_sizes_by_origin['theme'] :
114 $font_sizes_by_origin['default']
115 );
116 }
117
118 if ( isset( $settings['__experimentalFeatures']['color']['custom'] ) ) {
119 $settings['disableCustomColors'] = ! $settings['__experimentalFeatures']['color']['custom'];
120 unset( $settings['__experimentalFeatures']['color']['custom'] );
121 }
122 if ( isset( $settings['__experimentalFeatures']['color']['customGradient'] ) ) {
123 $settings['disableCustomGradients'] = ! $settings['__experimentalFeatures']['color']['customGradient'];
124 unset( $settings['__experimentalFeatures']['color']['customGradient'] );
125 }
126 if ( isset( $settings['__experimentalFeatures']['typography']['customFontSize'] ) ) {
127 $settings['disableCustomFontSizes'] = ! $settings['__experimentalFeatures']['typography']['customFontSize'];
128 unset( $settings['__experimentalFeatures']['typography']['customFontSize'] );
129 }
130 if ( isset( $settings['__experimentalFeatures']['typography']['lineHeight'] ) ) {
131 $settings['enableCustomLineHeight'] = $settings['__experimentalFeatures']['typography']['lineHeight'];
132 unset( $settings['__experimentalFeatures']['typography']['lineHeight'] );
133 }
134 if ( isset( $settings['__experimentalFeatures']['spacing']['units'] ) ) {
135 $settings['enableCustomUnits'] = $settings['__experimentalFeatures']['spacing']['units'];
136 }
137 if ( isset( $settings['__experimentalFeatures']['spacing']['padding'] ) ) {
138 $settings['enableCustomSpacing'] = $settings['__experimentalFeatures']['spacing']['padding'];
139 unset( $settings['__experimentalFeatures']['spacing']['padding'] );
140 }
141
142 return $settings;
143 }
144
145 /**
146 * Sanitizes global styles user content removing unsafe rules.
147 *
148 * @param string $content Post content to filter.
149 * @return string Filtered post content with unsafe rules removed.
150 */
151 function gutenberg_global_styles_filter_post( $content ) {
152 $decoded_data = json_decode( wp_unslash( $content ), true );
153 $json_decoding_error = json_last_error();
154 if (
155 JSON_ERROR_NONE === $json_decoding_error &&
156 is_array( $decoded_data ) &&
157 isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) &&
158 $decoded_data['isGlobalStylesUserThemeJSON']
159 ) {
160 unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
161
162 $data_to_encode = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $decoded_data );
163
164 $data_to_encode['isGlobalStylesUserThemeJSON'] = true;
165 return wp_slash( wp_json_encode( $data_to_encode ) );
166 }
167 return $content;
168 }
169
170 /**
171 * Adds the filters to filter global styles user theme.json.
172 */
173 function gutenberg_global_styles_kses_init_filters() {
174 add_filter( 'content_save_pre', 'gutenberg_global_styles_filter_post' );
175 }
176
177 /**
178 * Removes the filters to filter global styles user theme.json.
179 */
180 function gutenberg_global_styles_kses_remove_filters() {
181 remove_filter( 'content_save_pre', 'gutenberg_global_styles_filter_post' );
182 }
183
184 /**
185 * Register global styles kses filters if the user does not have unfiltered_html capability.
186 *
187 * @uses render_block_core_navigation()
188 * @throws WP_Error An WP_Error exception parsing the block definition.
189 */
190 function gutenberg_global_styles_kses_init() {
191 gutenberg_global_styles_kses_remove_filters();
192 if ( ! current_user_can( 'unfiltered_html' ) ) {
193 gutenberg_global_styles_kses_init_filters();
194 }
195 }
196
197 /**
198 * This filter is the last being executed on force_filtered_html_on_import.
199 * If the input of the filter is true it means we are in an import situation and should
200 * enable kses, independently of the user capabilities.
201 * So in that case we call gutenberg_global_styles_kses_init_filters;
202 *
203 * @param string $arg Input argument of the filter.
204 * @return string Exactly what was passed as argument.
205 */
206 function gutenberg_global_styles_force_filtered_html_on_import_filter( $arg ) {
207 // force_filtered_html_on_import is true we need to init the global styles kses filters.
208 if ( $arg ) {
209 gutenberg_global_styles_kses_init_filters();
210 }
211 return $arg;
212 }
213
214 add_filter( 'block_editor_settings_all', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX );
215
216 // kses actions&filters.
217 add_action( 'init', 'gutenberg_global_styles_kses_init' );
218 add_action( 'set_current_user', 'gutenberg_global_styles_kses_init' );
219 add_filter( 'force_filtered_html_on_import', 'gutenberg_global_styles_force_filtered_html_on_import_filter', 999 );
220 // This filter needs to be executed last.
221