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

83 lines 2.8 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 * Sanitizes global styles user content removing unsafe rules.
10 *
11 * @param string $content Post content to filter.
12 * @return string Filtered post content with unsafe rules removed.
13 */
14 function gutenberg_global_styles_filter_post( $content ) {
15 $decoded_data = json_decode( wp_unslash( $content ), true );
16 $json_decoding_error = json_last_error();
17 if (
18 JSON_ERROR_NONE === $json_decoding_error &&
19 is_array( $decoded_data ) &&
20 isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) &&
21 $decoded_data['isGlobalStylesUserThemeJSON']
22 ) {
23 unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
24
25 $data_to_encode = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $decoded_data );
26
27 $data_to_encode['isGlobalStylesUserThemeJSON'] = true;
28 return wp_slash( wp_json_encode( $data_to_encode ) );
29 }
30 return $content;
31 }
32
33 /**
34 * Adds the filters to filter global styles user theme.json.
35 */
36 function gutenberg_global_styles_kses_init_filters() {
37 // Global Styles filters should be executed before normal post_kses HTML filters which has the default priority of 10.
38 add_filter( 'content_save_pre', 'gutenberg_global_styles_filter_post', 9 );
39 }
40
41 /**
42 * Removes the filters to filter global styles user theme.json.
43 */
44 function gutenberg_global_styles_kses_remove_filters() {
45 remove_filter( 'content_save_pre', 'gutenberg_global_styles_filter_post', 9 );
46 }
47
48 /**
49 * Register global styles kses filters if the user does not have unfiltered_html capability.
50 *
51 * @uses render_block_core_navigation()
52 * @throws WP_Error An WP_Error exception parsing the block definition.
53 */
54 function gutenberg_global_styles_kses_init() {
55 gutenberg_global_styles_kses_remove_filters();
56 if ( ! current_user_can( 'unfiltered_html' ) ) {
57 gutenberg_global_styles_kses_init_filters();
58 }
59 }
60
61 /**
62 * This filter is the last being executed on force_filtered_html_on_import.
63 * If the input of the filter is true it means we are in an import situation and should
64 * enable kses, independently of the user capabilities.
65 * So in that case we call gutenberg_global_styles_kses_init_filters;
66 *
67 * @param string $arg Input argument of the filter.
68 * @return string Exactly what was passed as argument.
69 */
70 function gutenberg_global_styles_force_filtered_html_on_import_filter( $arg ) {
71 // force_filtered_html_on_import is true we need to init the global styles kses filters.
72 if ( $arg ) {
73 gutenberg_global_styles_kses_init_filters();
74 }
75 return $arg;
76 }
77
78 // kses actions&filters.
79 add_action( 'init', 'gutenberg_global_styles_kses_init' );
80 add_action( 'set_current_user', 'gutenberg_global_styles_kses_init' );
81 add_filter( 'force_filtered_html_on_import', 'gutenberg_global_styles_force_filtered_html_on_import_filter', 999 );
82 // This filter needs to be executed last.
83