Skip to content

Commit

Permalink
Global Styles: Allow content/wide widths when unfiltered_html is not …
Browse files Browse the repository at this point in the history
…allowed (#46712)

## What?
Follow-up of #46388.

Ensures that the content and wide width settings set via Global Styles are considered safe and thus output as custom properties on the body element.

## Why?
Because secure `layout.contentSize` and `layout.wideSize` settings are being stripped out for users without the `unfiltered_html` capability as part of `remove_insecure_settings`.

## How?
- Updates the `INDIRECT_PROPERTIES_METADATA` array introduced in #46388 to allow the `layout.contentSize` and `layout.wideSize` settings.
- Modifies the `remove_insecure_settings` to check the `INDIRECT_PROPERTIES_METADATA` array for settings that are not included in `PRESETS_METADATA`.
  • Loading branch information
mmtr authored Dec 23, 2022
1 parent 7c2f34d commit f303663
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
47 changes: 36 additions & 11 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,19 @@ class WP_Theme_JSON_Gutenberg {
* @var array
*/
const INDIRECT_PROPERTIES_METADATA = array(
'gap' => array( 'spacing', 'blockGap' ),
'column-gap' => array( 'spacing', 'blockGap', 'left' ),
'row-gap' => array( 'spacing', 'blockGap', 'top' ),
'gap' => array(
array( 'spacing', 'blockGap' ),
),
'column-gap' => array(
array( 'spacing', 'blockGap', 'left' ),
),
'row-gap' => array(
array( 'spacing', 'blockGap', 'top' ),
),
'max-width' => array(
array( 'layout', 'contentSize' ),
array( 'layout', 'wideSize' ),
),
);

/**
Expand Down Expand Up @@ -2821,6 +2831,19 @@ protected static function remove_insecure_settings( $input ) {
}
}
}

foreach ( static::INDIRECT_PROPERTIES_METADATA as $property => $paths ) {
foreach ( $paths as $path ) {
$value = _wp_array_get( $input, $path, array() );
if (
isset( $value ) &&
! is_array( $value ) &&
static::is_safe_css_declaration( $property, $value )
) {
_wp_array_set( $output, $path, $value );
}
}
}
return $output;
}

Expand Down Expand Up @@ -2852,14 +2875,16 @@ protected static function remove_insecure_styles( $input ) {
}

// Ensure indirect properties not handled by `compute_style_properties` are allowed.
foreach ( static::INDIRECT_PROPERTIES_METADATA as $property => $path ) {
$value = _wp_array_get( $input, $path, array() );
if (
isset( $value ) &&
! is_array( $value ) &&
static::is_safe_css_declaration( $property, $value )
) {
_wp_array_set( $output, $path, $value );
foreach ( static::INDIRECT_PROPERTIES_METADATA as $property => $paths ) {
foreach ( $paths as $path ) {
$value = _wp_array_get( $input, $path, array() );
if (
isset( $value ) &&
! is_array( $value ) &&
static::is_safe_css_declaration( $property, $value )
) {
_wp_array_set( $output, $path, $value );
}
}
}

Expand Down
20 changes: 16 additions & 4 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,8 @@ public function test_get_stylesheet_with_block_support_feature_level_selectors()
public function test_allow_indirect_properties() {
$actual = WP_Theme_JSON_Gutenberg::remove_insecure_properties(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/social-links' => array(
'spacing' => array(
Expand All @@ -770,12 +770,18 @@ public function test_allow_indirect_properties() {
'blockGap' => '3em',
),
),
'settings' => array(
'layout' => array(
'contentSize' => '800px',
'wideSize' => '1000px',
),
),
)
);

$expected = array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'blocks' => array(
'core/social-links' => array(
'spacing' => array(
Expand All @@ -790,6 +796,12 @@ public function test_allow_indirect_properties() {
'blockGap' => '3em',
),
),
'settings' => array(
'layout' => array(
'contentSize' => '800px',
'wideSize' => '1000px',
),
),
);

$this->assertEqualSetsWithIndex( $expected, $actual );
Expand Down

0 comments on commit f303663

Please sign in to comment.