diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 44a422dbf41f8c..687303d8c902da 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -207,57 +207,70 @@ class WP_Theme_JSON_Gutenberg { * - 'value': path to the value in theme.json and block attributes. */ const PROPERTIES_METADATA = array( - 'background' => array( + 'background' => array( 'value' => array( 'color', 'gradient' ), ), - 'background-color' => array( + 'background-color' => array( 'value' => array( 'color', 'background' ), ), - 'border-radius' => array( + 'border-radius' => array( 'value' => array( 'border', 'radius' ), ), - 'border-color' => array( + // This is deliberately separate to the border-radius entry above. + // It allows for backwards compatible shorthand e.g. border-radius: 10px + // while below provides a means to explicitly set the CSS property name + // where the flat array configuration approach builds an incorrect name. + 'corner-border-radius' => array( + 'value' => array( 'border', 'radius' ), + 'properties' => array( + 'border-top-left-radius' => 'topLeft', + 'border-top-right-radius' => 'topRight', + 'border-bottom-left-radius' => 'bottomLeft', + 'border-bottom-right-radius' => 'bottomRight', + ), + ), + 'border-color' => array( 'value' => array( 'border', 'color' ), ), - 'border-width' => array( + 'border-width' => array( 'value' => array( 'border', 'width' ), ), - 'border-style' => array( + 'border-style' => array( 'value' => array( 'border', 'style' ), ), - 'color' => array( + 'color' => array( 'value' => array( 'color', 'text' ), ), - 'font-family' => array( + 'font-family' => array( 'value' => array( 'typography', 'fontFamily' ), ), - 'font-size' => array( + 'font-size' => array( 'value' => array( 'typography', 'fontSize' ), ), - 'font-style' => array( + 'font-style' => array( 'value' => array( 'typography', 'fontStyle' ), ), - 'font-weight' => array( + 'font-weight' => array( 'value' => array( 'typography', 'fontWeight' ), ), - 'letter-spacing' => array( + 'letter-spacing' => array( 'value' => array( 'typography', 'letterSpacing' ), ), - 'line-height' => array( + 'line-height' => array( 'value' => array( 'typography', 'lineHeight' ), ), - 'margin' => array( + 'margin' => array( 'value' => array( 'spacing', 'margin' ), 'properties' => array( 'top', 'right', 'bottom', 'left' ), ), - 'padding' => array( + 'padding' => array( 'value' => array( 'spacing', 'padding' ), 'properties' => array( 'top', 'right', 'bottom', 'left' ), ), - 'text-decoration' => array( + 'text-decoration' => array( 'value' => array( 'typography', 'textDecoration' ), ), - 'text-transform' => array( + 'text-transform' => array( 'value' => array( 'typography', 'textTransform' ), ), ); @@ -365,8 +378,12 @@ private static function to_property( $css_name ) { foreach ( self::PROPERTIES_METADATA as $key => $metadata ) { $to_property[ $key ] = $key; if ( self::has_properties( $metadata ) ) { - foreach ( $metadata['properties'] as $property ) { - $to_property[ $key . '-' . $property ] = $key; + foreach ( $metadata['properties'] as $name => $property ) { + if ( is_numeric( $name ) ) { + $to_property[ $key . '-' . $property ] = $key; + } else { + $to_property[ $name ] = $key; + } } } } @@ -533,7 +550,7 @@ private static function flatten_tree( $tree, $prefix = '', $token = '--' ) { private static function get_property_value( $styles, $path ) { $value = _wp_array_get( $styles, $path, '' ); - if ( '' === $value ) { + if ( '' === $value || is_array( $value ) ) { return $value; } @@ -595,9 +612,10 @@ private static function compute_style_properties( $styles ) { // they contain multiple values instead of a single one. // An example of this is the padding property. if ( self::has_properties( $metadata ) ) { - foreach ( $metadata['properties'] as $property ) { - $properties[] = array( - 'name' => $name . '-' . $property, + foreach ( $metadata['properties'] as $key => $property ) { + $property_name = is_numeric( $key ) ? $name . '-' . $property : $key; + $properties[] = array( + 'name' => $property_name, 'value' => array_merge( $metadata['value'], array( $property ) ), ); } @@ -611,7 +629,7 @@ private static function compute_style_properties( $styles ) { foreach ( $properties as $prop ) { $value = self::get_property_value( $styles, $prop['value'] ); - if ( empty( $value ) ) { + if ( empty( $value ) || is_array( $value ) ) { continue; } diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index fa3a4931a561b2..e5a88a80a25c08 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -111,6 +111,9 @@ function test_get_stylesheet() { ), 'blocks' => array( 'core/group' => array( + 'border' => array( + 'radius' => '10px', + ), 'elements' => array( 'link' => array( 'color' => array( @@ -154,6 +157,14 @@ function test_get_stylesheet() { ), ), ), + 'core/image' => array( + 'border' => array( + 'radius' => array( + 'topLeft' => '10px', + 'bottomRight' => '1em', + ), + ), + ), ), ), 'misc' => 'value', @@ -161,11 +172,11 @@ function test_get_stylesheet() { ); $this->assertEquals( - 'body{--wp--preset--color--grey: grey;--wp--preset--font-family--small: 14px;--wp--preset--font-family--big: 41px;}.wp-block-group{--wp--custom--base-font: 16;--wp--custom--line-height--small: 1.2;--wp--custom--line-height--medium: 1.4;--wp--custom--line-height--large: 1.8;}body{color: var(--wp--preset--color--grey);}a{background-color: #333;color: #111;}.wp-block-group{padding-top: 12px;padding-bottom: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.has-grey-color{color: grey !important;}.has-grey-background-color{background-color: grey !important;}.has-grey-border-color{border-color: grey !important;}', + 'body{--wp--preset--color--grey: grey;--wp--preset--font-family--small: 14px;--wp--preset--font-family--big: 41px;}.wp-block-group{--wp--custom--base-font: 16;--wp--custom--line-height--small: 1.2;--wp--custom--line-height--medium: 1.4;--wp--custom--line-height--large: 1.8;}body{color: var(--wp--preset--color--grey);}a{background-color: #333;color: #111;}.wp-block-group{border-radius: 10px;padding-top: 12px;padding-bottom: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.wp-block-image{border-top-left-radius: 10px;border-bottom-right-radius: 1em;}.has-grey-color{color: grey !important;}.has-grey-background-color{background-color: grey !important;}.has-grey-border-color{border-color: grey !important;}', $theme_json->get_stylesheet() ); $this->assertEquals( - 'body{color: var(--wp--preset--color--grey);}a{background-color: #333;color: #111;}.wp-block-group{padding-top: 12px;padding-bottom: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.has-grey-color{color: grey !important;}.has-grey-background-color{background-color: grey !important;}.has-grey-border-color{border-color: grey !important;}', + 'body{color: var(--wp--preset--color--grey);}a{background-color: #333;color: #111;}.wp-block-group{border-radius: 10px;padding-top: 12px;padding-bottom: 24px;}.wp-block-group a{color: #111;}h1,h2,h3,h4,h5,h6{color: #123456;}h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{background-color: #333;color: #111;font-size: 60px;}.wp-block-post-date{color: #123456;}.wp-block-post-date a{background-color: #777;color: #555;}.wp-block-image{border-top-left-radius: 10px;border-bottom-right-radius: 1em;}.has-grey-color{color: grey !important;}.has-grey-background-color{background-color: grey !important;}.has-grey-border-color{border-color: grey !important;}', $theme_json->get_stylesheet( 'block_styles' ) ); $this->assertEquals( @@ -882,7 +893,7 @@ function test_remove_insecure_properties_removes_unsafe_preset_settings() { array( 'name' => 'Blue', 'slug' => 'blue', - 'color' => 'var(--color, var(--unsafe--falback))', + 'color' => 'var(--color, var(--unsafe--fallback))', ), array( 'name' => 'Pink',