-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Global styles: Allow indirect properties when unfiltered_html is not allowed #46388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andrewserong
merged 5 commits into
trunk
from
fix/saving-block-gap-values-in-global-styles-without-unfiltered-html
Dec 12, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6c941f1
Global styles: Allow inferred properties outside of compute_style_pro…
andrewserong ed1044d
Ensure set_current_user callback is covered
andrewserong fe80e7b
Add a simple test
andrewserong c540e6a
Renamed to indirect, move hooks to separate kses file
andrewserong 5b629c8
Add block-level styles to the test
andrewserong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <?php | ||
| /** | ||
| * Temporary compatibility shims for kses rules present in Gutenberg. | ||
| * | ||
| * The functions in this file should not be backported to core. | ||
| * | ||
| * @package gutenberg | ||
| */ | ||
|
|
||
| /** | ||
| * Sanitizes global styles user content removing unsafe rules. | ||
| * | ||
| * This function is identical to the core version, but called the | ||
| * Gutenberg version of the theme JSON class (`WP_Theme_JSON_Gutenberg`). | ||
| * | ||
| * This function should not be backported to core. | ||
| * | ||
| * @since 5.9.0 | ||
| * | ||
| * @param string $data Post content to filter. | ||
| * @return string Filtered post content with unsafe rules removed. | ||
| */ | ||
| function gutenberg_filter_global_styles_post( $data ) { | ||
| $decoded_data = json_decode( wp_unslash( $data ), true ); | ||
| $json_decoding_error = json_last_error(); | ||
| if ( | ||
| JSON_ERROR_NONE === $json_decoding_error && | ||
| is_array( $decoded_data ) && | ||
| isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) && | ||
| $decoded_data['isGlobalStylesUserThemeJSON'] | ||
| ) { | ||
| unset( $decoded_data['isGlobalStylesUserThemeJSON'] ); | ||
|
|
||
| $data_to_encode = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $decoded_data ); | ||
|
|
||
| $data_to_encode['isGlobalStylesUserThemeJSON'] = true; | ||
| return wp_slash( wp_json_encode( $data_to_encode ) ); | ||
| } | ||
| return $data; | ||
| } | ||
|
|
||
| /** | ||
| * Override core's kses_init_filters hooks for global styles, | ||
| * and use Gutenberg's version instead. This ensures that | ||
| * Gutenberg's `remove_insecure_properties` function can be called. | ||
| * | ||
| * The hooks are only set if they are already added, which ensures | ||
| * that global styles is only filtered for users without the `unfiltered_html` | ||
| * capability. | ||
| * | ||
| * This function should not be backported to core. | ||
| */ | ||
| function gutenberg_override_core_kses_init_filters() { | ||
| if ( has_filter( 'content_save_pre', 'wp_filter_global_styles_post' ) ) { | ||
| remove_filter( 'content_save_pre', 'wp_filter_global_styles_post', 9 ); | ||
| add_filter( 'content_save_pre', 'gutenberg_filter_global_styles_post', 9 ); | ||
| } | ||
|
|
||
| if ( has_filter( 'content_filtered_save_pre', 'wp_filter_global_styles_post' ) ) { | ||
| remove_filter( 'content_filtered_save_pre', 'wp_filter_global_styles_post', 9 ); | ||
| add_filter( 'content_filtered_save_pre', 'gutenberg_filter_global_styles_post', 9 ); | ||
| } | ||
|
|
||
| } | ||
| add_action( 'init', 'gutenberg_override_core_kses_init_filters' ); | ||
| add_action( 'set_current_user', 'gutenberg_override_core_kses_init_filters' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.