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 );