Skip to content

Commit

Permalink
Update protected property check to look for a null value instead of f…
Browse files Browse the repository at this point in the history
…alsy, add tests
  • Loading branch information
andrewserong committed Sep 30, 2021
1 parent 9e70deb commit afb35ef
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class WP_Theme_JSON_Gutenberg {
* Protected style properties.
*
* These properties are only rendered if a corresponding setting in the
* `PROPERTIES_METADATA` path enables it.
* `PROPERTIES_METADATA` path enables it via a value other than `null`.
*/
const PROTECTED_PROPERTIES = array( '--wp--style--block-gap' );

Expand Down Expand Up @@ -594,8 +594,8 @@ private static function compute_style_properties( $styles, $settings = array(),
foreach ( $properties as $css_property => $value_path ) {
$value = self::get_property_value( $styles, $value_path );

if ( in_array( $css_property, self::PROTECTED_PROPERTIES ) ) {
if ( ! _wp_array_get( $settings, $value_path, false ) ) {
if ( in_array( $css_property, self::PROTECTED_PROPERTIES, true ) ) {
if ( _wp_array_get( $settings, $value_path, null ) === null ) {
continue;
}
}
Expand Down
57 changes: 57 additions & 0 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,63 @@ function test_get_stylesheet_support_for_shorthand_and_longhand_values() {
);
}

function test_get_stylesheet_skips_disabled_protected_properties() {
$theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'spacing' => array(
'blockGap' => null,
),
),
'styles' => array(
'spacing' => array(
'blockGap' => '1em',
),
'blocks' => array(
'core/columns' => array(
'spacing' => array(
'blockGap' => '24px',
),
),
),
),
)
);

$expected = '.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }';
$this->assertEquals( $expected, $theme_json->get_stylesheet() );
$this->assertEquals( $expected, $theme_json->get_stylesheet( 'block_styles' ) );
}

function test_get_stylesheet_renders_enabled_protected_properties() {
$theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'settings' => array(
'spacing' => array(
'blockGap' => true,
),
),
'styles' => array(
'spacing' => array(
'blockGap' => '1em',
),
'blocks' => array(
'core/columns' => array(
'spacing' => array(
'blockGap' => '24px',
),
),
),
),
)
);
$expected = 'body{--wp--style--block-gap: 1em;}.wp-site-blocks > .alignleft { float: left; margin-right: 2em; }.wp-site-blocks > .alignright { float: right; margin-left: 2em; }.wp-site-blocks > .aligncenter { justify-content: center; margin-left: auto; margin-right: auto; }.wp-site-blocks > * + * { margin-top: var( --wp--style--block-gap ); margin-bottom: 0; }.wp-block-columns{--wp--style--block-gap: 24px;}';
$this->assertEquals( $expected, $theme_json->get_stylesheet() );
$this->assertEquals( $expected, $theme_json->get_stylesheet( 'block_styles' ) );
}

function test_get_stylesheet() {
$theme_json = new WP_Theme_JSON_Gutenberg(
array(
Expand Down

0 comments on commit afb35ef

Please sign in to comment.