Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Type: Add selectors to block.json schema #3942

2 changes: 2 additions & 0 deletions src/wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2192,6 +2192,8 @@ function get_block_editor_server_block_settings() {
'attributes' => 'attributes',
'provides_context' => 'providesContext',
'uses_context' => 'usesContext',
'selectors' => 'selectors',
'editor_selectors' => 'editorSelectors',
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
'supports' => 'supports',
'category' => 'category',
'styles' => 'styles',
Expand Down
2 changes: 2 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
'attributes' => 'attributes',
'providesContext' => 'provides_context',
'usesContext' => 'uses_context',
'selectors' => 'selectors',
'editorSelectors' => 'editor_selectors',
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
'supports' => 'supports',
'styles' => 'styles',
'variations' => 'variations',
Expand Down
19 changes: 19 additions & 0 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ 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.
*
* @since 6.3.0
* @var array
*/
public $selectors = array();

/**
* Supported features.
*
Expand Down Expand Up @@ -245,6 +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` and `editor_selectors` property.
*
* @see register_block_type()
*
Expand All @@ -268,6 +285,8 @@ class WP_Block_Type {
* @type string|null $textdomain The translation textdomain.
* @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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ public function prepare_item_for_response( $item, $request ) {
'ancestor',
'provides_context',
'uses_context',
'selectors',
'editor_selectors',
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
'supports',
'styles',
'textdomain',
Expand Down Expand Up @@ -518,6 +520,22 @@ 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,
),
'editor_selectors' => array(
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
'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',
Expand Down
6 changes: 6 additions & 0 deletions tests/phpunit/data/blocks/notice/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
"type": "string"
}
},
"editorSelectors": {
"root": ".editor-only.wp-block-notice"
},
"selectors": {
"root": ".wp-block-notice"
},
"supports": {
"align": true,
"lightBlockWrapper": true
Expand Down
34 changes: 19 additions & 15 deletions tests/phpunit/tests/admin/includesPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,10 +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' ),
'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 );
Expand All @@ -837,19 +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(),
'category' => 'common',
'styles' => array(),
'ancestor' => array( 'core/test-ancestor' ),
'keywords' => array(),
'variations' => array(),
'usesContext' => array(),
'selectors' => array( 'root' => '.wp-block-test' ),
'editorSelectors' => array( 'root' => '.editor-only.wp-block-test' ),
'category' => 'common',
'styles' => array(),
'ancestor' => array( 'core/test-ancestor' ),
'keywords' => array(),
'variations' => array(),
),
$blocks[ $name ]
);
Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,14 @@ 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
);
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
$this->assertSame(
array(
'align' => true,
Expand Down
12 changes: 11 additions & 1 deletion tests/phpunit/tests/rest-api/rest-block-type-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ public function test_get_item_invalid() {
'example' => 'invalid_example',
'parent' => 'invalid_parent',
'ancestor' => 'invalid_ancestor',
'editor_selectors' => 'invalid_editor_selectors',
'selectors' => 'invalid_selectors',
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
'supports' => 'invalid_supports',
'styles' => array(),
'render_callback' => 'invalid_callback',
Expand Down Expand Up @@ -247,6 +249,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['supports'] );
$this->assertSameSets( array(), $data['styles'] );
$this->assertNull( $data['example'] );
Expand Down Expand Up @@ -284,6 +288,8 @@ public function test_get_item_defaults() {
'keywords' => false,
'parent' => false,
'ancestor' => false,
'editor_selectors' => false,
'selectors' => false,
'supports' => false,
'styles' => false,
'render_callback' => false,
Expand Down Expand Up @@ -316,6 +322,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['supports'] );
$this->assertSameSets( array(), $data['styles'] );
$this->assertNull( $data['example'] );
Expand Down Expand Up @@ -541,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( 28, $properties );
$this->assertCount( 30, $properties );
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
$this->assertArrayHasKey( 'api_version', $properties );
$this->assertArrayHasKey( 'title', $properties );
$this->assertArrayHasKey( 'icon', $properties );
Expand All @@ -551,6 +559,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 );
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
$this->assertArrayHasKey( 'supports', $properties );
$this->assertArrayHasKey( 'category', $properties );
$this->assertArrayHasKey( 'is_dynamic', $properties );
Expand Down