From 53f4ef1bbb764a1dc8f1c67b8627a2236d2c1e14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Mon, 14 Dec 2020 17:09:05 +0100 Subject: [PATCH 1/5] Add filter --- lib/class-wp-theme-json.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/class-wp-theme-json.php b/lib/class-wp-theme-json.php index 8f0071f0478ab..a296dca130a90 100644 --- a/lib/class-wp-theme-json.php +++ b/lib/class-wp-theme-json.php @@ -350,6 +350,18 @@ public function __construct( $contexts = array() ) { } } + /** + * Returns the metadata for the properties that we support + * after having called the filter for 3rd party to hook into. + * + * @return array Properties metadata. + */ + private static function get_properties_metadata() { + $result = apply_filters( 'theme_json_properties_metadata', self::PROPERTIES_METADATA ); + error_log( 'result is ' . print_r( $result, true ) ); + return $result; + } + /** * Returns the metadata for each block. * @@ -398,8 +410,9 @@ private static function get_blocks_metadata() { /* * Extract block support keys that are related to the style properties. */ - $block_supports = array(); - foreach ( self::PROPERTIES_METADATA as $key => $metadata ) { + $block_supports = array(); + $properties_metadata = self::get_properties_metadata(); + foreach ( $properties_metadata as $key => $metadata ) { if ( gutenberg_experimental_get( $block_type->supports, $metadata['support'] ) ) { $block_supports[] = $key; } @@ -628,7 +641,8 @@ private static function compute_style_properties( &$declarations, $context, $con return; } - foreach ( self::PROPERTIES_METADATA as $name => $metadata ) { + $properties_metadata = self::get_properties_metadata(); + foreach ( $properties_metadata as $name => $metadata ) { if ( ! in_array( $name, $context_supports, true ) ) { continue; } From dbce6b4f57fce2de1ca05569d28a63bef5d7fd2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Mon, 14 Dec 2020 17:09:23 +0100 Subject: [PATCH 2/5] Use core/group as example --- lib/experimental-default-theme.json | 7 +++++++ packages/block-library/src/group/block.json | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/experimental-default-theme.json b/lib/experimental-default-theme.json index 15904819cfd7f..156f92e185a8f 100644 --- a/lib/experimental-default-theme.json +++ b/lib/experimental-default-theme.json @@ -174,5 +174,12 @@ "customRadius": true } } + }, + "core/group": { + "styles": { + "spacing": { + "boxShadow": "10px 5px 5px red" + } + } } } diff --git a/packages/block-library/src/group/block.json b/packages/block-library/src/group/block.json index e0be130edc00a..fdfd5a9cf2c36 100644 --- a/packages/block-library/src/group/block.json +++ b/packages/block-library/src/group/block.json @@ -24,7 +24,8 @@ }, "spacing": { "padding": true - } + }, + "__experimentalBoxShadow": true }, "editorStyle": "wp-block-group-editor", "style": "wp-block-group" From d3bc9354300a0939c157bdfb612bf6451d15b34a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Mon, 14 Dec 2020 17:21:35 +0100 Subject: [PATCH 3/5] Filter out props that are not declared in the schema --- lib/class-wp-theme-json.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/class-wp-theme-json.php b/lib/class-wp-theme-json.php index a296dca130a90..03e0f08d321a8 100644 --- a/lib/class-wp-theme-json.php +++ b/lib/class-wp-theme-json.php @@ -325,6 +325,7 @@ public function __construct( $contexts = array() ) { $this->process_key( 'styles', $context, self::SCHEMA ); if ( isset( $context['styles'] ) ) { $this->process_key( 'color', $context['styles'], self::SCHEMA['styles'] ); + $this->process_key( 'spacing', $context['styles'], self::SCHEMA['styles'] ); $this->process_key( 'typography', $context['styles'], self::SCHEMA['styles'] ); if ( empty( $context['styles'] ) ) { From a76eb88ae548db086db81bb6e46472273a506f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Mon, 14 Dec 2020 17:22:14 +0100 Subject: [PATCH 4/5] use different key for subtree --- lib/experimental-default-theme.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/experimental-default-theme.json b/lib/experimental-default-theme.json index 156f92e185a8f..009e061911337 100644 --- a/lib/experimental-default-theme.json +++ b/lib/experimental-default-theme.json @@ -177,7 +177,7 @@ }, "core/group": { "styles": { - "spacing": { + "box": { "boxShadow": "10px 5px 5px red" } } From 0cfa9da1ead5d7d4d4b22d3b5b5312a9e69f0b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Mon, 14 Dec 2020 17:31:55 +0100 Subject: [PATCH 5/5] Allow schema to be filtered --- lib/class-wp-theme-json.php | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/class-wp-theme-json.php b/lib/class-wp-theme-json.php index 03e0f08d321a8..59e9b6a8eb9fb 100644 --- a/lib/class-wp-theme-json.php +++ b/lib/class-wp-theme-json.php @@ -310,6 +310,7 @@ public function __construct( $contexts = array() ) { return; } + $schema = self::get_schema(); $metadata = $this->get_blocks_metadata(); foreach ( $contexts as $key => $context ) { if ( ! isset( $metadata[ $key ] ) ) { @@ -319,14 +320,14 @@ public function __construct( $contexts = array() ) { } // Filter out top-level keys that aren't valid according to the schema. - $context = array_intersect_key( $context, self::SCHEMA ); + $context = array_intersect_key( $context, $schema ); // Process styles subtree. - $this->process_key( 'styles', $context, self::SCHEMA ); + $this->process_key( 'styles', $context, $schema ); if ( isset( $context['styles'] ) ) { - $this->process_key( 'color', $context['styles'], self::SCHEMA['styles'] ); - $this->process_key( 'spacing', $context['styles'], self::SCHEMA['styles'] ); - $this->process_key( 'typography', $context['styles'], self::SCHEMA['styles'] ); + $this->process_key( 'color', $context['styles'], $schema['styles'] ); + $this->process_key( 'spacing', $context['styles'], $schema['styles'] ); + $this->process_key( 'typography', $context['styles'], $schema['styles'] ); if ( empty( $context['styles'] ) ) { unset( $context['styles'] ); @@ -336,11 +337,11 @@ public function __construct( $contexts = array() ) { } // Process settings subtree. - $this->process_key( 'settings', $context, self::SCHEMA ); + $this->process_key( 'settings', $context, $schema ); if ( isset( $context['settings'] ) ) { - $this->process_key( 'color', $context['settings'], self::SCHEMA['settings'] ); - $this->process_key( 'spacing', $context['settings'], self::SCHEMA['settings'] ); - $this->process_key( 'typography', $context['settings'], self::SCHEMA['settings'] ); + $this->process_key( 'color', $context['settings'], $schema['settings'] ); + $this->process_key( 'spacing', $context['settings'], $schema['settings'] ); + $this->process_key( 'typography', $context['settings'], $schema['settings'] ); if ( empty( $context['settings'] ) ) { unset( $context['settings'] ); @@ -358,9 +359,17 @@ public function __construct( $contexts = array() ) { * @return array Properties metadata. */ private static function get_properties_metadata() { - $result = apply_filters( 'theme_json_properties_metadata', self::PROPERTIES_METADATA ); - error_log( 'result is ' . print_r( $result, true ) ); - return $result; + return apply_filters( 'theme_json_properties_metadata', self::PROPERTIES_METADATA ); + } + + /** + * Returns the schema to be used to validate the contexts + * after having called the filter for 3rd party to hook into. + * + * @return array Schema. + */ + private static function get_schema() { + return apply_filters( 'theme_json_schema', self::SCHEMA ); } /** @@ -938,6 +947,7 @@ public function get_stylesheet( $type = 'all' ) { */ public function merge( $theme_json ) { $incoming_data = $theme_json->get_raw_data(); + $schema = self::get_schema(); foreach ( array_keys( $incoming_data ) as $context ) { foreach ( array( 'settings', 'styles' ) as $subtree ) { @@ -950,7 +960,7 @@ public function merge( $theme_json ) { continue; } - foreach ( array_keys( self::SCHEMA[ $subtree ] ) as $leaf ) { + foreach ( array_keys( $schema[ $subtree ] ) as $leaf ) { if ( ! isset( $incoming_data[ $context ][ $subtree ][ $leaf ] ) ) { continue; }