| 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 |
|