From b44a473b55d2e7c56610d59f7a3cf34afa13bba3 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Mon, 30 Jan 2023 19:39:15 +1000 Subject: [PATCH 1/8] Block Type: Add selectors to block.json schema --- src/wp-admin/includes/post.php | 1 + src/wp-includes/blocks.php | 1 + src/wp-includes/class-wp-block-type.php | 10 ++++++++++ .../endpoints/class-wp-rest-block-types-controller.php | 9 +++++++++ tests/phpunit/data/blocks/notice/block.json | 3 +++ tests/phpunit/tests/admin/includesPost.php | 2 ++ tests/phpunit/tests/blocks/register.php | 4 ++++ .../tests/rest-api/rest-block-type-controller.php | 7 ++++++- 8 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 824b969450759..5cab4431cc755 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -2192,6 +2192,7 @@ function get_block_editor_server_block_settings() { 'attributes' => 'attributes', 'provides_context' => 'providesContext', 'uses_context' => 'usesContext', + 'selectors' => 'selectors', 'supports' => 'supports', 'category' => 'category', 'styles' => 'styles', diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 9dcd1932147fd..8f1ac5fff70b5 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -382,6 +382,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { 'attributes' => 'attributes', 'providesContext' => 'provides_context', 'usesContext' => 'uses_context', + 'selectors' => 'selectors', 'supports' => 'supports', 'styles' => 'styles', 'variations' => 'variations', diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 3b3787740a1d3..abb039153e257 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -117,6 +117,14 @@ class WP_Block_Type { */ public $variations = array(); + /** + * Custom CSS selectors for theme.json style generation. + * + * @since 6.2.0 + * @var array|null + */ + public $selectors = null; + /** * Supported features. * @@ -245,6 +253,7 @@ class WP_Block_Type { * @since 6.1.0 Added the `editor_script_handles`, `script_handles`, `view_script_handles, * `editor_style_handles`, and `style_handles` properties. * Deprecated the `editor_script`, `script`, `view_script`, `editor_style`, and `style` properties. + * @since 6.2.0 Added the `selectors` property. * * @see register_block_type() * @@ -268,6 +277,7 @@ class WP_Block_Type { * @type string|null $textdomain The translation textdomain. * @type array[] $styles Alternative block styles. * @type array[] $variations Block variations. + * @type array|null $selectors Custom CSS selectors for theme.json style generation. * @type array|null $supports Supported features. * @type array|null $example Structured data for the block preview. * @type callable|null $render_callback Block type render callback. diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index e1f34baef6df0..f530be6244171 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -278,6 +278,7 @@ public function prepare_item_for_response( $item, $request ) { 'ancestor', 'provides_context', 'uses_context', + 'selectors', 'supports', 'styles', 'textdomain', @@ -518,6 +519,14 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), + 'selectors' => array( + 'description' => __( 'Custom CSS selectors.' ), + 'type' => 'object', + 'default' => array(), + 'properties' => array(), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), 'supports' => array( 'description' => __( 'Block supports.' ), 'type' => 'object', diff --git a/tests/phpunit/data/blocks/notice/block.json b/tests/phpunit/data/blocks/notice/block.json index 9d2fadfb5dc86..c3a3b4dde9513 100644 --- a/tests/phpunit/data/blocks/notice/block.json +++ b/tests/phpunit/data/blocks/notice/block.json @@ -27,6 +27,9 @@ "type": "string" } }, + "selectors": { + "root": ".wp-block-notice" + }, "supports": { "align": true, "lightBlockWrapper": true diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index 799d927dfa909..07660e6f30539 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -826,6 +826,7 @@ public function test_get_block_editor_server_block_settings() { 'category' => 'common', 'render_callback' => 'foo', 'ancestor' => array( 'core/test-ancestor' ), + 'selectors' => array( 'root' => '.wp-block-test' ), ); register_block_type( $name, $settings ); @@ -845,6 +846,7 @@ public function test_get_block_editor_server_block_settings() { 'lock' => array( 'type' => 'object' ), ), 'usesContext' => array(), + 'selectors' => array( 'root' => '.wp-block-test' ), 'category' => 'common', 'styles' => array(), 'ancestor' => array( 'core/test-ancestor' ), diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index e1cd22db45d44..f8ff44f83830d 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -515,6 +515,10 @@ public function test_block_registers_with_metadata_fixture() { $result->provides_context ); $this->assertSameSets( array( 'groupId' ), $result->uses_context ); + $this->assertSame( + array( 'root' => '.wp-block-notice' ), + $result->selectors + ); $this->assertSame( array( 'align' => true, diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index 4dc177722e494..cca806687dba6 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -216,6 +216,7 @@ public function test_get_item_invalid() { 'example' => 'invalid_example', 'parent' => 'invalid_parent', 'ancestor' => 'invalid_ancestor', + 'selectors' => 'invalid_selectors', 'supports' => 'invalid_supports', 'styles' => array(), 'render_callback' => 'invalid_callback', @@ -247,6 +248,7 @@ public function test_get_item_invalid() { $this->assertSameSets( array( 'invalid_keywords' ), $data['keywords'] ); $this->assertSameSets( array( 'invalid_parent' ), $data['parent'] ); $this->assertSameSets( array( 'invalid_ancestor' ), $data['ancestor'] ); + $this->assertSameSets( array(), $data['selectors'] ); $this->assertSameSets( array(), $data['supports'] ); $this->assertSameSets( array(), $data['styles'] ); $this->assertNull( $data['example'] ); @@ -284,6 +286,7 @@ public function test_get_item_defaults() { 'keywords' => false, 'parent' => false, 'ancestor' => false, + 'selectors' => false, 'supports' => false, 'styles' => false, 'render_callback' => false, @@ -316,6 +319,7 @@ public function test_get_item_defaults() { $this->assertSameSets( array(), $data['keywords'] ); $this->assertSameSets( array(), $data['parent'] ); $this->assertSameSets( array(), $data['ancestor'] ); + $this->assertSameSets( array(), $data['selectors'] ); $this->assertSameSets( array(), $data['supports'] ); $this->assertSameSets( array(), $data['styles'] ); $this->assertNull( $data['example'] ); @@ -541,7 +545,7 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertCount( 28, $properties ); + $this->assertCount( 29, $properties ); $this->assertArrayHasKey( 'api_version', $properties ); $this->assertArrayHasKey( 'title', $properties ); $this->assertArrayHasKey( 'icon', $properties ); @@ -551,6 +555,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'textdomain', $properties ); $this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'attributes', $properties ); + $this->assertArrayHasKey( 'selectors', $properties ); $this->assertArrayHasKey( 'supports', $properties ); $this->assertArrayHasKey( 'category', $properties ); $this->assertArrayHasKey( 'is_dynamic', $properties ); From 787630538be8837c2e90d4eb3830ba49624bd690 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Tue, 14 Feb 2023 16:38:00 +1000 Subject: [PATCH 2/8] Update selectors default and since --- src/wp-includes/class-wp-block-type.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index abb039153e257..1fb8894164a7b 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -120,10 +120,10 @@ class WP_Block_Type { /** * Custom CSS selectors for theme.json style generation. * - * @since 6.2.0 - * @var array|null + * @since 6.3.0 + * @var array */ - public $selectors = null; + public $selectors = array(); /** * Supported features. @@ -253,7 +253,7 @@ class WP_Block_Type { * @since 6.1.0 Added the `editor_script_handles`, `script_handles`, `view_script_handles, * `editor_style_handles`, and `style_handles` properties. * Deprecated the `editor_script`, `script`, `view_script`, `editor_style`, and `style` properties. - * @since 6.2.0 Added the `selectors` property. + * @since 6.3.0 Added the `selectors` property. * * @see register_block_type() * @@ -277,7 +277,7 @@ class WP_Block_Type { * @type string|null $textdomain The translation textdomain. * @type array[] $styles Alternative block styles. * @type array[] $variations Block variations. - * @type array|null $selectors Custom CSS selectors for theme.json style generation. + * @type array $selectors Custom CSS selectors for theme.json style generation. * @type array|null $supports Supported features. * @type array|null $example Structured data for the block preview. * @type callable|null $render_callback Block type render callback. From 59033a62d0e81d1990dc8588f6277a85b2d5f916 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Wed, 15 Mar 2023 18:40:23 +1000 Subject: [PATCH 3/8] Add editor only selectors to block type --- src/wp-admin/includes/post.php | 1 + src/wp-includes/blocks.php | 1 + src/wp-includes/class-wp-block-type.php | 11 +++++- .../class-wp-rest-block-types-controller.php | 9 +++++ tests/phpunit/data/blocks/notice/block.json | 3 ++ tests/phpunit/tests/admin/includesPost.php | 36 ++++++++++--------- tests/phpunit/tests/blocks/register.php | 4 +++ .../rest-api/rest-block-type-controller.php | 7 +++- 8 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 5cab4431cc755..0543ed5b94a8d 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -2193,6 +2193,7 @@ function get_block_editor_server_block_settings() { 'provides_context' => 'providesContext', 'uses_context' => 'usesContext', 'selectors' => 'selectors', + 'editor_selectors' => 'editorSelectors', 'supports' => 'supports', 'category' => 'category', 'styles' => 'styles', diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 8f1ac5fff70b5..e8d6df23b4046 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -383,6 +383,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { 'providesContext' => 'provides_context', 'usesContext' => 'uses_context', 'selectors' => 'selectors', + 'editorSelectors' => 'editor_selectors', 'supports' => 'supports', 'styles' => 'styles', 'variations' => 'variations', diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 1fb8894164a7b..64eca1f804101 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -117,6 +117,14 @@ class WP_Block_Type { */ public $variations = array(); + /** + * Editor-only custom CSS selectors for theme.json style generation. + * + * @since 6.3.0 + * @var array + */ + public $editor_selectors = array(); + /** * Custom CSS selectors for theme.json style generation. * @@ -253,7 +261,7 @@ class WP_Block_Type { * @since 6.1.0 Added the `editor_script_handles`, `script_handles`, `view_script_handles, * `editor_style_handles`, and `style_handles` properties. * Deprecated the `editor_script`, `script`, `view_script`, `editor_style`, and `style` properties. - * @since 6.3.0 Added the `selectors` property. + * @since 6.3.0 Added the `selectors` and `editor_selectors` property. * * @see register_block_type() * @@ -278,6 +286,7 @@ class WP_Block_Type { * @type array[] $styles Alternative block styles. * @type array[] $variations Block variations. * @type array $selectors Custom CSS selectors for theme.json style generation. + * @type array $editor_selectors Editor-only custom CSS selectors for theme.json style generation. * @type array|null $supports Supported features. * @type array|null $example Structured data for the block preview. * @type callable|null $render_callback Block type render callback. diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index f530be6244171..0130bd9501af3 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -279,6 +279,7 @@ public function prepare_item_for_response( $item, $request ) { 'provides_context', 'uses_context', 'selectors', + 'editor_selectors', 'supports', 'styles', 'textdomain', @@ -527,6 +528,14 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), + 'editor_selectors' => array( + 'description' => __( 'Editor-only custom CSS selectors.' ), + 'type' => 'object', + 'default' => array(), + 'properties' => array(), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), 'supports' => array( 'description' => __( 'Block supports.' ), 'type' => 'object', diff --git a/tests/phpunit/data/blocks/notice/block.json b/tests/phpunit/data/blocks/notice/block.json index c3a3b4dde9513..6fdcb87bfad16 100644 --- a/tests/phpunit/data/blocks/notice/block.json +++ b/tests/phpunit/data/blocks/notice/block.json @@ -27,6 +27,9 @@ "type": "string" } }, + "editorSelectors": { + "root": ".editor-only.wp-block-notice" + }, "selectors": { "root": ".wp-block-notice" }, diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index 07660e6f30539..b1262990cde57 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -822,11 +822,12 @@ public function test_post_exists_should_match_nonempty_title_content_and_date() public function test_get_block_editor_server_block_settings() { $name = 'core/test'; $settings = array( - 'icon' => 'text', - 'category' => 'common', - 'render_callback' => 'foo', - 'ancestor' => array( 'core/test-ancestor' ), - 'selectors' => array( 'root' => '.wp-block-test' ), + 'icon' => 'text', + 'category' => 'common', + 'render_callback' => 'foo', + 'ancestor' => array( 'core/test-ancestor' ), + 'editor_selectors' => array( 'root' => '.editor-only.wp-block-test' ), + 'selectors' => array( 'root' => '.wp-block-test' ), ); register_block_type( $name, $settings ); @@ -838,20 +839,21 @@ public function test_get_block_editor_server_block_settings() { $this->assertArrayHasKey( $name, $blocks ); $this->assertSame( array( - 'apiVersion' => 1, - 'title' => '', - 'description' => '', - 'icon' => 'text', - 'attributes' => array( + 'apiVersion' => 1, + 'title' => '', + 'description' => '', + 'icon' => 'text', + 'attributes' => array( 'lock' => array( 'type' => 'object' ), ), - 'usesContext' => array(), - 'selectors' => array( 'root' => '.wp-block-test' ), - 'category' => 'common', - 'styles' => array(), - 'ancestor' => array( 'core/test-ancestor' ), - 'keywords' => array(), - 'variations' => array(), + 'usesContext' => array(), + 'editorSelectors' => array( 'root' => '.editor-only.wp-block-test' ), + 'selectors' => array( 'root' => '.wp-block-test' ), + 'category' => 'common', + 'styles' => array(), + 'ancestor' => array( 'core/test-ancestor' ), + 'keywords' => array(), + 'variations' => array(), ), $blocks[ $name ] ); diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index f8ff44f83830d..cd6e54dab3052 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -515,6 +515,10 @@ public function test_block_registers_with_metadata_fixture() { $result->provides_context ); $this->assertSameSets( array( 'groupId' ), $result->uses_context ); + $this->assertSame( + array( 'root' => '.editor-only.wp-block-notice' ), + $result->editor_selectors + ); $this->assertSame( array( 'root' => '.wp-block-notice' ), $result->selectors diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index cca806687dba6..1ea5eb03e7eca 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -216,6 +216,7 @@ public function test_get_item_invalid() { 'example' => 'invalid_example', 'parent' => 'invalid_parent', 'ancestor' => 'invalid_ancestor', + 'editor_selectors' => 'invalid_editor_selectors', 'selectors' => 'invalid_selectors', 'supports' => 'invalid_supports', 'styles' => array(), @@ -248,6 +249,7 @@ public function test_get_item_invalid() { $this->assertSameSets( array( 'invalid_keywords' ), $data['keywords'] ); $this->assertSameSets( array( 'invalid_parent' ), $data['parent'] ); $this->assertSameSets( array( 'invalid_ancestor' ), $data['ancestor'] ); + $this->assertSameSets( array(), $data['editor_selectors'] ); $this->assertSameSets( array(), $data['selectors'] ); $this->assertSameSets( array(), $data['supports'] ); $this->assertSameSets( array(), $data['styles'] ); @@ -286,6 +288,7 @@ public function test_get_item_defaults() { 'keywords' => false, 'parent' => false, 'ancestor' => false, + 'editor_selectors' => false, 'selectors' => false, 'supports' => false, 'styles' => false, @@ -319,6 +322,7 @@ public function test_get_item_defaults() { $this->assertSameSets( array(), $data['keywords'] ); $this->assertSameSets( array(), $data['parent'] ); $this->assertSameSets( array(), $data['ancestor'] ); + $this->assertSameSets( array(), $data['editor_selectors'] ); $this->assertSameSets( array(), $data['selectors'] ); $this->assertSameSets( array(), $data['supports'] ); $this->assertSameSets( array(), $data['styles'] ); @@ -545,7 +549,7 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertCount( 29, $properties ); + $this->assertCount( 30, $properties ); $this->assertArrayHasKey( 'api_version', $properties ); $this->assertArrayHasKey( 'title', $properties ); $this->assertArrayHasKey( 'icon', $properties ); @@ -555,6 +559,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'textdomain', $properties ); $this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'attributes', $properties ); + $this->assertArrayHasKey( 'editor_selectors', $properties ); $this->assertArrayHasKey( 'selectors', $properties ); $this->assertArrayHasKey( 'supports', $properties ); $this->assertArrayHasKey( 'category', $properties ); From 635ffee4c33e931ed7243208b88ef1e7f62040ea Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Thu, 16 Mar 2023 16:55:21 +1000 Subject: [PATCH 4/8] Fix editor selectors position in expected block settings --- tests/phpunit/tests/admin/includesPost.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index b1262990cde57..920c1c43429ce 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -847,8 +847,8 @@ public function test_get_block_editor_server_block_settings() { 'lock' => array( 'type' => 'object' ), ), 'usesContext' => array(), - 'editorSelectors' => array( 'root' => '.editor-only.wp-block-test' ), 'selectors' => array( 'root' => '.wp-block-test' ), + 'editorSelectors' => array( 'root' => '.editor-only.wp-block-test' ), 'category' => 'common', 'styles' => array(), 'ancestor' => array( 'core/test-ancestor' ), From 82d1762b4bc5a9e8eaf114a58022f74302f7a973 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Fri, 17 Mar 2023 12:52:19 +1000 Subject: [PATCH 5/8] Update DocBlocks, add ticket refs and assertion messages --- src/wp-admin/includes/post.php | 1 + src/wp-includes/blocks.php | 1 + .../class-wp-rest-block-types-controller.php | 2 ++ tests/phpunit/tests/blocks/register.php | 9 +++++++-- .../tests/rest-api/rest-block-type-controller.php | 15 +++++++++------ 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 0543ed5b94a8d..aafe2a469f1e6 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -2178,6 +2178,7 @@ function taxonomy_meta_box_sanitize_cb_input( $taxonomy, $terms ) { * of a block relevant for client registration. * * @since 5.0.0 + * @since 6.3.0 Added `selectors` and `editor_selectors` fields. * * @return array An associative array of registered block data. */ diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index e8d6df23b4046..2dfead2f883ab 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -300,6 +300,7 @@ function get_block_metadata_i18n_schema() { * @since 5.7.0 Added support for `textdomain` field and i18n handling for all translatable fields. * @since 5.9.0 Added support for `variations` and `viewScript` fields. * @since 6.1.0 Added support for `render` field. + * @since 6.3.0 Added `selectors` and `editor_selectors` fields. * * @param string $file_or_folder Path to the JSON file with metadata definition for * the block or path to the folder where the `block.json` file is located. diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index 0130bd9501af3..a4c586cd3681b 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -237,6 +237,7 @@ public function get_item( $request ) { * * @since 5.5.0 * @since 5.9.0 Renamed `$block_type` to `$item` to match parent class for PHP 8 named parameter support. + * @since 6.3.0 Added `selectors` and `editor_selectors` fields. * * @param WP_Block_Type $item Block type data. * @param WP_REST_Request $request Full details about the request. @@ -381,6 +382,7 @@ protected function prepare_links( $block_type ) { * Retrieves the block type' schema, conforming to JSON Schema. * * @since 5.5.0 + * @since 6.3.0 Added `selectors` and `editor_selectors` fields. * * @return array Item schema data. */ diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index cd6e54dab3052..1620260b90b92 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -483,6 +483,7 @@ public function test_metadata_not_found_in_the_current_directory() { * * @ticket 50263 * @ticket 50328 + * @ticket 57585 */ public function test_block_registers_with_metadata_fixture() { $result = register_block_type_from_metadata( @@ -515,13 +516,17 @@ public function test_block_registers_with_metadata_fixture() { $result->provides_context ); $this->assertSameSets( array( 'groupId' ), $result->uses_context ); + // @ticket 57585 $this->assertSame( array( 'root' => '.editor-only.wp-block-notice' ), - $result->editor_selectors + $result->editor_selectors, + 'Block type should contain editor selectors from metadata.' ); + // @ticket 57585 $this->assertSame( array( 'root' => '.wp-block-notice' ), - $result->selectors + $result->selectors, + 'Block type should contain selectors from metadata.' ); $this->assertSame( array( diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index 1ea5eb03e7eca..a426ed141bdac 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -196,6 +196,7 @@ public function test_get_block_invalid_name() { /** * @ticket 47620 + * @ticket 57585 */ public function test_get_item_invalid() { $block_type = 'fake/invalid'; @@ -249,8 +250,8 @@ public function test_get_item_invalid() { $this->assertSameSets( array( 'invalid_keywords' ), $data['keywords'] ); $this->assertSameSets( array( 'invalid_parent' ), $data['parent'] ); $this->assertSameSets( array( 'invalid_ancestor' ), $data['ancestor'] ); - $this->assertSameSets( array(), $data['editor_selectors'] ); - $this->assertSameSets( array(), $data['selectors'] ); + $this->assertSameSets( array(), $data['editor_selectors'], 'invalid editor_selectors defaults to empty array' ); + $this->assertSameSets( array(), $data['selectors'], 'invalid selectors defaults to empty array' ); $this->assertSameSets( array(), $data['supports'] ); $this->assertSameSets( array(), $data['styles'] ); $this->assertNull( $data['example'] ); @@ -269,6 +270,7 @@ public function test_get_item_invalid() { /** * @ticket 47620 + * @ticket 57585 */ public function test_get_item_defaults() { $block_type = 'fake/false'; @@ -322,8 +324,8 @@ public function test_get_item_defaults() { $this->assertSameSets( array(), $data['keywords'] ); $this->assertSameSets( array(), $data['parent'] ); $this->assertSameSets( array(), $data['ancestor'] ); - $this->assertSameSets( array(), $data['editor_selectors'] ); - $this->assertSameSets( array(), $data['selectors'] ); + $this->assertSameSets( array(), $data['editor_selectors'], 'editor_selectors defaults to empty array' ); + $this->assertSameSets( array(), $data['selectors'], 'selectors defaults to empty array' ); $this->assertSameSets( array(), $data['supports'] ); $this->assertSameSets( array(), $data['styles'] ); $this->assertNull( $data['example'] ); @@ -542,6 +544,7 @@ public function test_get_variation() { /** * @ticket 47620 + * @ticket 57585 */ public function test_get_item_schema() { wp_set_current_user( self::$admin_id ); @@ -559,8 +562,8 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'textdomain', $properties ); $this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'attributes', $properties ); - $this->assertArrayHasKey( 'editor_selectors', $properties ); - $this->assertArrayHasKey( 'selectors', $properties ); + $this->assertArrayHasKey( 'editor_selectors', $properties, 'schema must contain editor_selectors' ); + $this->assertArrayHasKey( 'selectors', $properties, 'schema must contain selectors' ); $this->assertArrayHasKey( 'supports', $properties ); $this->assertArrayHasKey( 'category', $properties ); $this->assertArrayHasKey( 'is_dynamic', $properties ); From df7fbdb0a553ce05b4510b423f2078fbe4c34a3a Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Tue, 28 Mar 2023 17:30:58 +1000 Subject: [PATCH 6/8] Revert "Fix editor selectors position in expected block settings" This reverts commit 635ffee4c33e931ed7243208b88ef1e7f62040ea. --- tests/phpunit/tests/admin/includesPost.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index 920c1c43429ce..b1262990cde57 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -847,8 +847,8 @@ public function test_get_block_editor_server_block_settings() { 'lock' => array( 'type' => 'object' ), ), 'usesContext' => array(), - 'selectors' => array( 'root' => '.wp-block-test' ), 'editorSelectors' => array( 'root' => '.editor-only.wp-block-test' ), + 'selectors' => array( 'root' => '.wp-block-test' ), 'category' => 'common', 'styles' => array(), 'ancestor' => array( 'core/test-ancestor' ), From 8ea6c4579cf0c78340c7331fd61d722722837777 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Tue, 28 Mar 2023 17:34:20 +1000 Subject: [PATCH 7/8] Revert "Add editor only selectors to block type" --- src/wp-admin/includes/post.php | 1 - src/wp-includes/blocks.php | 1 - src/wp-includes/class-wp-block-type.php | 11 +----- .../class-wp-rest-block-types-controller.php | 9 ----- tests/phpunit/data/blocks/notice/block.json | 3 -- tests/phpunit/tests/admin/includesPost.php | 36 +++++++++---------- tests/phpunit/tests/blocks/register.php | 6 ---- .../rest-api/rest-block-type-controller.php | 7 +--- 8 files changed, 19 insertions(+), 55 deletions(-) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index aafe2a469f1e6..bbef05dc67d68 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -2194,7 +2194,6 @@ function get_block_editor_server_block_settings() { 'provides_context' => 'providesContext', 'uses_context' => 'usesContext', 'selectors' => 'selectors', - 'editor_selectors' => 'editorSelectors', 'supports' => 'supports', 'category' => 'category', 'styles' => 'styles', diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 2dfead2f883ab..003a931005598 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -384,7 +384,6 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { 'providesContext' => 'provides_context', 'usesContext' => 'uses_context', 'selectors' => 'selectors', - 'editorSelectors' => 'editor_selectors', 'supports' => 'supports', 'styles' => 'styles', 'variations' => 'variations', diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 64eca1f804101..1fb8894164a7b 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -117,14 +117,6 @@ class WP_Block_Type { */ public $variations = array(); - /** - * Editor-only custom CSS selectors for theme.json style generation. - * - * @since 6.3.0 - * @var array - */ - public $editor_selectors = array(); - /** * Custom CSS selectors for theme.json style generation. * @@ -261,7 +253,7 @@ class WP_Block_Type { * @since 6.1.0 Added the `editor_script_handles`, `script_handles`, `view_script_handles, * `editor_style_handles`, and `style_handles` properties. * Deprecated the `editor_script`, `script`, `view_script`, `editor_style`, and `style` properties. - * @since 6.3.0 Added the `selectors` and `editor_selectors` property. + * @since 6.3.0 Added the `selectors` property. * * @see register_block_type() * @@ -286,7 +278,6 @@ class WP_Block_Type { * @type array[] $styles Alternative block styles. * @type array[] $variations Block variations. * @type array $selectors Custom CSS selectors for theme.json style generation. - * @type array $editor_selectors Editor-only custom CSS selectors for theme.json style generation. * @type array|null $supports Supported features. * @type array|null $example Structured data for the block preview. * @type callable|null $render_callback Block type render callback. diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index a4c586cd3681b..0ba07ac85357b 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -280,7 +280,6 @@ public function prepare_item_for_response( $item, $request ) { 'provides_context', 'uses_context', 'selectors', - 'editor_selectors', 'supports', 'styles', 'textdomain', @@ -530,14 +529,6 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'editor_selectors' => array( - 'description' => __( 'Editor-only custom CSS selectors.' ), - 'type' => 'object', - 'default' => array(), - 'properties' => array(), - 'context' => array( 'embed', 'view', 'edit' ), - 'readonly' => true, - ), 'supports' => array( 'description' => __( 'Block supports.' ), 'type' => 'object', diff --git a/tests/phpunit/data/blocks/notice/block.json b/tests/phpunit/data/blocks/notice/block.json index 6fdcb87bfad16..c3a3b4dde9513 100644 --- a/tests/phpunit/data/blocks/notice/block.json +++ b/tests/phpunit/data/blocks/notice/block.json @@ -27,9 +27,6 @@ "type": "string" } }, - "editorSelectors": { - "root": ".editor-only.wp-block-notice" - }, "selectors": { "root": ".wp-block-notice" }, diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index b1262990cde57..07660e6f30539 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -822,12 +822,11 @@ public function test_post_exists_should_match_nonempty_title_content_and_date() public function test_get_block_editor_server_block_settings() { $name = 'core/test'; $settings = array( - 'icon' => 'text', - 'category' => 'common', - 'render_callback' => 'foo', - 'ancestor' => array( 'core/test-ancestor' ), - 'editor_selectors' => array( 'root' => '.editor-only.wp-block-test' ), - 'selectors' => array( 'root' => '.wp-block-test' ), + 'icon' => 'text', + 'category' => 'common', + 'render_callback' => 'foo', + 'ancestor' => array( 'core/test-ancestor' ), + 'selectors' => array( 'root' => '.wp-block-test' ), ); register_block_type( $name, $settings ); @@ -839,21 +838,20 @@ public function test_get_block_editor_server_block_settings() { $this->assertArrayHasKey( $name, $blocks ); $this->assertSame( array( - 'apiVersion' => 1, - 'title' => '', - 'description' => '', - 'icon' => 'text', - 'attributes' => array( + 'apiVersion' => 1, + 'title' => '', + 'description' => '', + 'icon' => 'text', + 'attributes' => array( 'lock' => array( 'type' => 'object' ), ), - 'usesContext' => array(), - 'editorSelectors' => array( 'root' => '.editor-only.wp-block-test' ), - 'selectors' => array( 'root' => '.wp-block-test' ), - 'category' => 'common', - 'styles' => array(), - 'ancestor' => array( 'core/test-ancestor' ), - 'keywords' => array(), - 'variations' => array(), + 'usesContext' => array(), + 'selectors' => array( 'root' => '.wp-block-test' ), + 'category' => 'common', + 'styles' => array(), + 'ancestor' => array( 'core/test-ancestor' ), + 'keywords' => array(), + 'variations' => array(), ), $blocks[ $name ] ); diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 1620260b90b92..ac9ba7ada3362 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -517,12 +517,6 @@ public function test_block_registers_with_metadata_fixture() { ); $this->assertSameSets( array( 'groupId' ), $result->uses_context ); // @ticket 57585 - $this->assertSame( - array( 'root' => '.editor-only.wp-block-notice' ), - $result->editor_selectors, - 'Block type should contain editor selectors from metadata.' - ); - // @ticket 57585 $this->assertSame( array( 'root' => '.wp-block-notice' ), $result->selectors, diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index a426ed141bdac..b19321e224bb9 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -217,7 +217,6 @@ public function test_get_item_invalid() { 'example' => 'invalid_example', 'parent' => 'invalid_parent', 'ancestor' => 'invalid_ancestor', - 'editor_selectors' => 'invalid_editor_selectors', 'selectors' => 'invalid_selectors', 'supports' => 'invalid_supports', 'styles' => array(), @@ -250,7 +249,6 @@ public function test_get_item_invalid() { $this->assertSameSets( array( 'invalid_keywords' ), $data['keywords'] ); $this->assertSameSets( array( 'invalid_parent' ), $data['parent'] ); $this->assertSameSets( array( 'invalid_ancestor' ), $data['ancestor'] ); - $this->assertSameSets( array(), $data['editor_selectors'], 'invalid editor_selectors defaults to empty array' ); $this->assertSameSets( array(), $data['selectors'], 'invalid selectors defaults to empty array' ); $this->assertSameSets( array(), $data['supports'] ); $this->assertSameSets( array(), $data['styles'] ); @@ -290,7 +288,6 @@ public function test_get_item_defaults() { 'keywords' => false, 'parent' => false, 'ancestor' => false, - 'editor_selectors' => false, 'selectors' => false, 'supports' => false, 'styles' => false, @@ -324,7 +321,6 @@ public function test_get_item_defaults() { $this->assertSameSets( array(), $data['keywords'] ); $this->assertSameSets( array(), $data['parent'] ); $this->assertSameSets( array(), $data['ancestor'] ); - $this->assertSameSets( array(), $data['editor_selectors'], 'editor_selectors defaults to empty array' ); $this->assertSameSets( array(), $data['selectors'], 'selectors defaults to empty array' ); $this->assertSameSets( array(), $data['supports'] ); $this->assertSameSets( array(), $data['styles'] ); @@ -552,7 +548,7 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertCount( 30, $properties ); + $this->assertCount( 29, $properties ); $this->assertArrayHasKey( 'api_version', $properties ); $this->assertArrayHasKey( 'title', $properties ); $this->assertArrayHasKey( 'icon', $properties ); @@ -562,7 +558,6 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'textdomain', $properties ); $this->assertArrayHasKey( 'name', $properties ); $this->assertArrayHasKey( 'attributes', $properties ); - $this->assertArrayHasKey( 'editor_selectors', $properties, 'schema must contain editor_selectors' ); $this->assertArrayHasKey( 'selectors', $properties, 'schema must contain selectors' ); $this->assertArrayHasKey( 'supports', $properties ); $this->assertArrayHasKey( 'category', $properties ); From 935cebb210abf70917507576cc040e737052ebf0 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Tue, 28 Mar 2023 18:21:09 +1000 Subject: [PATCH 8/8] Remove references to editor_selectors in DocBlocks --- src/wp-admin/includes/post.php | 2 +- src/wp-includes/blocks.php | 2 +- .../endpoints/class-wp-rest-block-types-controller.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index bbef05dc67d68..e86cdce43e035 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -2178,7 +2178,7 @@ function taxonomy_meta_box_sanitize_cb_input( $taxonomy, $terms ) { * of a block relevant for client registration. * * @since 5.0.0 - * @since 6.3.0 Added `selectors` and `editor_selectors` fields. + * @since 6.3.0 Added `selectors` field. * * @return array An associative array of registered block data. */ diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 003a931005598..21b524655ddbe 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -300,7 +300,7 @@ function get_block_metadata_i18n_schema() { * @since 5.7.0 Added support for `textdomain` field and i18n handling for all translatable fields. * @since 5.9.0 Added support for `variations` and `viewScript` fields. * @since 6.1.0 Added support for `render` field. - * @since 6.3.0 Added `selectors` and `editor_selectors` fields. + * @since 6.3.0 Added `selectors` field. * * @param string $file_or_folder Path to the JSON file with metadata definition for * the block or path to the folder where the `block.json` file is located. diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index 0ba07ac85357b..26bd0e05449e6 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -237,7 +237,7 @@ public function get_item( $request ) { * * @since 5.5.0 * @since 5.9.0 Renamed `$block_type` to `$item` to match parent class for PHP 8 named parameter support. - * @since 6.3.0 Added `selectors` and `editor_selectors` fields. + * @since 6.3.0 Added `selectors` field. * * @param WP_Block_Type $item Block type data. * @param WP_REST_Request $request Full details about the request. @@ -381,7 +381,7 @@ protected function prepare_links( $block_type ) { * Retrieves the block type' schema, conforming to JSON Schema. * * @since 5.5.0 - * @since 6.3.0 Added `selectors` and `editor_selectors` fields. + * @since 6.3.0 Added `selectors` field. * * @return array Item schema data. */