diff --git a/docs/designers-developers/developers/backward-compatibility/deprecations.md b/docs/designers-developers/developers/backward-compatibility/deprecations.md index 2016845ea53ef2..da93e0733d409d 100644 --- a/docs/designers-developers/developers/backward-compatibility/deprecations.md +++ b/docs/designers-developers/developers/backward-compatibility/deprecations.md @@ -2,10 +2,14 @@ For features included in the Gutenberg plugin, the deprecation policy is intended to support backward compatibility for two minor plugin releases, when possible. Features and code included in a stable release of WordPress are not included in this deprecation timeline, and are instead subject to the [versioning policies of the WordPress project](https://make.wordpress.org/core/handbook/about/release-cycle/version-numbering/). The current deprecations are listed below and are grouped by _the version at which they will be removed completely_. If your plugin depends on these behaviors, you must update to the recommended alternative before the noted version. +## 8.6.0 + +- Block API integration with [Block Context](https://github.com/WordPress/gutenberg/blob/master/docs/designers-developers/developers/block-api/block-context.md) was updated. When registering a block use `usesContext` and `providesContext` pair in JavaScript files and `uses_context` and `provides_context` pair in PHP files instead of previous pair `context` and `providesContext`. + ## 8.3.0 - The PHP function `gutenberg_get_post_from_context` has been removed. Use [Block Context](https://github.com/WordPress/gutenberg/blob/master/docs/designers-developers/developers/block-api/block-context.md) instead. -- The old Block Pattern APIs `register_pattern`/`unregister_pattern` have been removed. Use the [new functions](https://github.com/WordPress/gutenberg/blob/master/docs/designers-developers/developers/block-api/block-patterns.md#register_block_pattern) instead. +- The old Block Pattern APIs `register_pattern`/`unregister_pattern` have been removed. Use the [new functions](https://github.com/WordPress/gutenberg/blob/master/docs/designers-developers/developers/block-api/block-patterns.md#register_block_pattern) instead. ## 5.5.0 diff --git a/docs/designers-developers/developers/block-api/block-context.md b/docs/designers-developers/developers/block-api/block-context.md index 4ee517d148e1af..70fece6a400ef3 100644 --- a/docs/designers-developers/developers/block-api/block-context.md +++ b/docs/designers-developers/developers/block-api/block-context.md @@ -34,14 +34,14 @@ As seen in the above example, it is recommended that you include a namespace as ### Consuming Block Context -A block can inherit a context value from an ancestor provider by assigning a `context` property in its registered settings. This should be assigned as an array of the context names the block seeks to inherit. +A block can inherit a context value from an ancestor provider by assigning a `usesContext` property in its registered settings. This should be assigned as an array of the context names the block seeks to inherit. `record-title/block.json` ```json { "name": "my-plugin/record-title", - "context": [ "my-plugin/recordId" ] + "usesContext": [ "my-plugin/recordId" ] } ``` diff --git a/docs/rfc/block-registration.md b/docs/rfc/block-registration.md index aed1e31c5409c9..9d35678b4804ba 100644 --- a/docs/rfc/block-registration.md +++ b/docs/rfc/block-registration.md @@ -77,6 +77,12 @@ To register a new block type, start by creating a `block.json` file. This file: "selector": ".message" } }, + "providesContext": { + "my-plugin/message": "message" + }, + "usesContext": [ + "groupId" + ], "supports": { "align": true, "lightBlockWrapper": true @@ -252,6 +258,46 @@ Attributes provide the structured data needs of a block. They can exist in diffe See the [the attributes documentation](/docs/designers-developers/developers/block-api/block-attributes.md) for more details. +### Provides Context + +* Type: `object` +* Optional +* Localized: No +* Property: `providesContext` +* Default: `{}` + +Context provided for available access by descendants of blocks of this type, in the form of an object which maps a context name to one of the block's own attribute. + +See [the block context documentation](/docs/designers-developers/developers/block-api/block-context.md) for more details. + +```json +{ + "providesContext": { + "my-plugin/recordId": "recordId" + } +} +``` + +### Context + +* Type: `string[]` +* Optional +* Localized: No +* Property: `usesContext` +* Default: `[]` + +Array of the names of context values to inherit from an ancestor provider. + +See [the block context documentation](/docs/designers-developers/developers/block-api/block-context.md) for more details. + +```json +{ + "usesContext": [ + "message" + ] +} +``` + ### Supports * Type: `object` diff --git a/lib/class-wp-block.php b/lib/class-wp-block.php index ccc3f5f9423077..a8f1c763a0f170 100644 --- a/lib/class-wp-block.php +++ b/lib/class-wp-block.php @@ -105,10 +105,29 @@ public function __construct( $block, $available_context = array(), $registry = n $this->block_type = $registry->get_registered( $this->name ); + if ( ! empty( $this->block_type->context ) ) { + $message = sprintf( + /* translators: 1: Block name. */ + __( 'The "context" parameter provided in block type "%s" is deprecated. Please use "uses_context" instead.', 'gutenberg' ), + $this->name + ); + _doing_it_wrong( __CLASS__, $message, '8.6.0' ); + $this->block_type->uses_context = $this->block_type->context; + } + if ( ! empty( $this->block_type->providesContext ) ) { + $message = sprintf( + /* translators: 1: Block name. */ + __( 'The "providesContext" parameter provided in block type "%s" is deprecated. Please use "provides_context".', 'gutenberg' ), + $this->name + ); + _doing_it_wrong( __CLASS__, $message, '8.6.0' ); + $this->block_type->provides_context = $this->block_type->providesContext; + } + $this->available_context = $available_context; - if ( ! empty( $this->block_type->context ) ) { - foreach ( $this->block_type->context as $context_name ) { + if ( ! empty( $this->block_type->uses_context ) ) { + foreach ( $this->block_type->uses_context as $context_name ) { if ( array_key_exists( $context_name, $this->available_context ) ) { $this->context[ $context_name ] = $this->available_context[ $context_name ]; } @@ -118,15 +137,13 @@ public function __construct( $block, $available_context = array(), $registry = n if ( ! empty( $block['innerBlocks'] ) ) { $child_context = $this->available_context; - /* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */ - if ( ! empty( $this->block_type->providesContext ) ) { - foreach ( $this->block_type->providesContext as $context_name => $attribute_name ) { + if ( ! empty( $this->block_type->provides_context ) ) { + foreach ( $this->block_type->provides_context as $context_name => $attribute_name ) { if ( array_key_exists( $attribute_name, $this->attributes ) ) { $child_context[ $context_name ] = $this->attributes[ $attribute_name ]; } } } - /* phpcs:enable */ $this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry ); } diff --git a/lib/class-wp-rest-block-types-controller.php b/lib/class-wp-rest-block-types-controller.php index b8592a9419319f..0bc5f92fea5534 100644 --- a/lib/class-wp-rest-block-types-controller.php +++ b/lib/class-wp-rest-block-types-controller.php @@ -239,21 +239,23 @@ public function prepare_item_for_response( $block_type, $request ) { $schema = $this->get_item_schema(); $extra_fields = array( - 'name' => 'name', - 'title' => 'title', - 'description' => 'description', - 'icon' => 'icon', - 'category' => 'category', - 'keywords' => 'keywords', - 'parent' => 'parent', - 'supports' => 'supports', - 'styles' => 'styles', - 'textdomain' => 'textdomain', - 'example' => 'example', - 'editor_script' => 'editor_script', - 'script' => 'script', - 'editor_style' => 'editor_style', - 'style' => 'style', + 'name' => 'name', + 'title' => 'title', + 'description' => 'description', + 'icon' => 'icon', + 'category' => 'category', + 'keywords' => 'keywords', + 'parent' => 'parent', + 'provides_context' => 'provides_context', + 'uses_context' => 'uses_context', + 'supports' => 'supports', + 'styles' => 'styles', + 'textdomain' => 'textdomain', + 'example' => 'example', + 'editor_script' => 'editor_script', + 'script' => 'script', + 'editor_style' => 'editor_style', + 'style' => 'style', ); foreach ( $extra_fields as $key => $extra_field ) { if ( rest_is_field_included( $key, $fields ) ) { @@ -337,35 +339,35 @@ public function get_item_schema() { 'title' => 'block-type', 'type' => 'object', 'properties' => array( - 'title' => array( + 'title' => array( 'description' => __( 'Title of block type.', 'gutenberg' ), 'type' => 'string', 'default' => '', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'name' => array( + 'name' => array( 'description' => __( 'Unique name identifying the block type.', 'gutenberg' ), 'type' => 'string', 'default' => '', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'description' => array( + 'description' => array( 'description' => __( 'Description of block type.', 'gutenberg' ), 'type' => 'string', 'default' => '', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'icon' => array( + 'icon' => array( 'description' => __( 'Icon of block type.', 'gutenberg' ), 'type' => array( 'string', 'null' ), 'default' => null, 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'attributes' => array( + 'attributes' => array( 'description' => __( 'Block attributes.', 'gutenberg' ), 'type' => array( 'object', 'null' ), 'properties' => array(), @@ -376,7 +378,28 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'supports' => array( + 'provides_context' => array( + 'description' => __( 'Context provided by blocks of this type.', 'gutenberg' ), + 'type' => 'object', + 'properties' => array(), + 'additionalProperties' => array( + 'type' => 'string', + ), + 'default' => array(), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'uses_context' => array( + 'description' => __( 'Context values inherited by blocks of this type.', 'gutenberg' ), + 'type' => 'array', + 'default' => array(), + 'items' => array( + 'type' => 'string', + ), + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'supports' => array( 'description' => __( 'Block supports.', 'gutenberg' ), 'type' => 'object', 'default' => array(), @@ -384,49 +407,49 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'category' => array( + 'category' => array( 'description' => __( 'Block category.', 'gutenberg' ), 'type' => array( 'string', null ), 'default' => null, 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'is_dynamic' => array( + 'is_dynamic' => array( 'description' => __( 'Is the block dynamically rendered.', 'gutenberg' ), 'type' => 'boolean', 'default' => false, 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'editor_script' => array( + 'editor_script' => array( 'description' => __( 'Editor script handle.', 'gutenberg' ), 'type' => array( 'string', null ), 'default' => null, 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'script' => array( + 'script' => array( 'description' => __( 'Public facing script handle.', 'gutenberg' ), 'type' => array( 'string', null ), 'default' => null, 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'editor_style' => array( + 'editor_style' => array( 'description' => __( 'Editor style handle.', 'gutenberg' ), 'type' => array( 'string', null ), 'default' => null, 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'style' => array( + 'style' => array( 'description' => __( 'Public facing style handle.', 'gutenberg' ), 'type' => array( 'string', null ), 'default' => null, 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'styles' => array( + 'styles' => array( 'description' => __( 'Block style variations.', 'gutenberg' ), 'type' => 'array', 'properties' => array(), @@ -437,14 +460,14 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'textdomain' => array( + 'textdomain' => array( 'description' => __( 'Public text domain.', 'gutenberg' ), 'type' => array( 'string', 'null' ), 'default' => null, 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'parent' => array( + 'parent' => array( 'description' => __( 'Parent blocks.', 'gutenberg' ), 'type' => array( 'array', 'null' ), 'items' => array( @@ -454,7 +477,7 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'keywords' => array( + 'keywords' => array( 'description' => __( 'Block keywords.', 'gutenberg' ), 'type' => 'array', 'items' => array( @@ -464,7 +487,7 @@ public function get_item_schema() { 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), - 'example' => array( + 'example' => array( 'description' => __( 'Block example.', 'gutenberg' ), 'type' => array( 'object', 'null' ), 'default' => null, diff --git a/lib/compat.php b/lib/compat.php index 202c1345c2a8aa..29ee0a43e076aa 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -167,17 +167,20 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $settings = array(); $property_mappings = array( - 'title' => 'title', - 'category' => 'category', - 'context' => 'context', - 'parent' => 'parent', - 'icon' => 'icon', - 'description' => 'description', - 'keywords' => 'keywords', - 'attributes' => 'attributes', - 'supports' => 'supports', - 'styles' => 'styles', - 'example' => 'example', + 'title' => 'title', + 'category' => 'category', + 'parent' => 'parent', + 'icon' => 'icon', + 'description' => 'description', + 'keywords' => 'keywords', + 'attributes' => 'attributes', + 'providesContext' => 'provides_context', + 'usesContext' => 'uses_context', + // Deprecated: remove with Gutenberg 8.6 release. + 'context' => 'context', + 'supports' => 'supports', + 'styles' => 'styles', + 'example' => 'example', ); foreach ( $property_mappings as $key => $mapped_key ) { diff --git a/packages/block-editor/src/components/block-edit/edit.js b/packages/block-editor/src/components/block-edit/edit.js index 936fb24c4fc2a1..8596c37a30c513 100644 --- a/packages/block-editor/src/components/block-edit/edit.js +++ b/packages/block-editor/src/components/block-edit/edit.js @@ -13,6 +13,7 @@ import { hasBlockSupport, getBlockType, } from '@wordpress/blocks'; +import deprecated from '@wordpress/deprecated'; import { useContext, useMemo } from '@wordpress/element'; /** @@ -36,13 +37,22 @@ export const Edit = ( props ) => { const blockContext = useContext( BlockContext ); // Assign context values using the block type's declared context needs. - const context = useMemo( - () => - blockType && blockType.context - ? pick( blockContext, blockType.context ) - : DEFAULT_BLOCK_CONTEXT, - [ blockType, blockContext ] - ); + const context = useMemo( () => { + if ( blockType && blockType.context ) { + deprecated( 'Block type "context" option', { + alternative: '"usesContext"', + version: '8.6.0', + hint: `Block "${ name }".`, + link: + 'https://developer.wordpress.org/block-editor/developers/block-api/block-context/', + } ); + return pick( blockContext, blockType.context ); + } + + return blockType && blockType.usesContext + ? pick( blockContext, blockType.usesContext ) + : DEFAULT_BLOCK_CONTEXT; + }, [ blockType, blockContext ] ); if ( ! blockType ) { return null; diff --git a/packages/block-editor/src/components/block-edit/test/edit.js b/packages/block-editor/src/components/block-edit/test/edit.js index 8c8c312bbe7c3a..3e89608bb096e5 100644 --- a/packages/block-editor/src/components/block-edit/test/edit.js +++ b/packages/block-editor/src/components/block-edit/test/edit.js @@ -86,7 +86,7 @@ describe( 'Edit', () => { registerBlockType( 'core/test-block', { category: 'text', title: 'block title', - context: [ 'value' ], + usesContext: [ 'value' ], edit, save: noop, } ); @@ -106,7 +106,7 @@ describe( 'Edit', () => { registerBlockType( 'core/test-block', { category: 'text', title: 'block title', - context: [ 'value' ], + usesContext: [ 'value' ], supports: { lightBlockWrapper: true, }, diff --git a/packages/block-library/src/post-author/block.json b/packages/block-library/src/post-author/block.json index dd7a983f9761cc..3ea69f0d1b9368 100644 --- a/packages/block-library/src/post-author/block.json +++ b/packages/block-library/src/post-author/block.json @@ -32,7 +32,7 @@ "type": "string" } }, - "context": [ + "usesContext": [ "postType", "postId" ], diff --git a/packages/block-library/src/post-comments-count/block.json b/packages/block-library/src/post-comments-count/block.json index f41d613f2e6b44..6f4412ee86e4e2 100644 --- a/packages/block-library/src/post-comments-count/block.json +++ b/packages/block-library/src/post-comments-count/block.json @@ -1,7 +1,7 @@ { "name": "core/post-comments-count", "category": "design", - "context": [ + "usesContext": [ "postId" ], "supports": { diff --git a/packages/block-library/src/post-comments-form/block.json b/packages/block-library/src/post-comments-form/block.json index cbf6c6b7199b56..af682a41175837 100644 --- a/packages/block-library/src/post-comments-form/block.json +++ b/packages/block-library/src/post-comments-form/block.json @@ -1,7 +1,7 @@ { "name": "core/post-comments-form", "category": "design", - "context": [ + "usesContext": [ "postId" ], "supports": { diff --git a/packages/block-library/src/post-comments/block.json b/packages/block-library/src/post-comments/block.json index ae0e5317336c5c..cf67b7037f31e1 100644 --- a/packages/block-library/src/post-comments/block.json +++ b/packages/block-library/src/post-comments/block.json @@ -1,5 +1,7 @@ { "name": "core/post-comments", "category": "design", - "context": [ "postId" ] + "usesContext": [ + "postId" + ] } diff --git a/packages/block-library/src/post-content/block.json b/packages/block-library/src/post-content/block.json index c67f01f5054048..215989a2a8feb9 100644 --- a/packages/block-library/src/post-content/block.json +++ b/packages/block-library/src/post-content/block.json @@ -1,7 +1,7 @@ { "name": "core/post-content", "category": "design", - "context": [ + "usesContext": [ "postId", "postType" ], diff --git a/packages/block-library/src/post-date/block.json b/packages/block-library/src/post-date/block.json index 95bb439c0b33f8..b52aa056853f3d 100644 --- a/packages/block-library/src/post-date/block.json +++ b/packages/block-library/src/post-date/block.json @@ -6,7 +6,7 @@ "type": "string" } }, - "context": [ + "usesContext": [ "postId" ], "supports": { diff --git a/packages/block-library/src/post-excerpt/block.json b/packages/block-library/src/post-excerpt/block.json index 764d519bb5b037..c94a5f8d2175fa 100644 --- a/packages/block-library/src/post-excerpt/block.json +++ b/packages/block-library/src/post-excerpt/block.json @@ -14,7 +14,7 @@ "default": true } }, - "context": [ + "usesContext": [ "postId" ], "supports": { diff --git a/packages/block-library/src/post-featured-image/block.json b/packages/block-library/src/post-featured-image/block.json index 29fb3d2141cec7..106cdc3c76f510 100644 --- a/packages/block-library/src/post-featured-image/block.json +++ b/packages/block-library/src/post-featured-image/block.json @@ -1,7 +1,7 @@ { "name": "core/post-featured-image", "category": "design", - "context": [ + "usesContext": [ "postId" ], "supports": { diff --git a/packages/block-library/src/post-tags/block.json b/packages/block-library/src/post-tags/block.json index a3c624b012d74f..b3b0e6772c4e99 100644 --- a/packages/block-library/src/post-tags/block.json +++ b/packages/block-library/src/post-tags/block.json @@ -1,7 +1,7 @@ { "name": "core/post-tags", "category": "design", - "context": [ + "usesContext": [ "postId" ], "supports": { diff --git a/packages/block-library/src/post-title/block.json b/packages/block-library/src/post-title/block.json index da9e1dc8c3978c..7d91d12073d18c 100644 --- a/packages/block-library/src/post-title/block.json +++ b/packages/block-library/src/post-title/block.json @@ -1,7 +1,7 @@ { "name": "core/post-title", "category": "design", - "context": [ + "usesContext": [ "postId", "postType" ], diff --git a/packages/block-library/src/query-loop/block.json b/packages/block-library/src/query-loop/block.json index a3f8beb410c292..1bcce0e8bd23fa 100644 --- a/packages/block-library/src/query-loop/block.json +++ b/packages/block-library/src/query-loop/block.json @@ -1,7 +1,7 @@ { "name": "core/query-loop", "category": "design", - "context": [ + "usesContext": [ "queryId", "query", "queryContext" diff --git a/packages/block-library/src/query-pagination/block.json b/packages/block-library/src/query-pagination/block.json index f18886c44e2781..3d8f4bdc38fe27 100644 --- a/packages/block-library/src/query-pagination/block.json +++ b/packages/block-library/src/query-pagination/block.json @@ -1,7 +1,7 @@ { "name": "core/query-pagination", "category": "design", - "context": [ + "usesContext": [ "queryId", "query", "queryContext" diff --git a/packages/e2e-tests/plugins/block-context.php b/packages/e2e-tests/plugins/block-context.php index 06bb1102c3daca..d1cc347640718b 100644 --- a/packages/e2e-tests/plugins/block-context.php +++ b/packages/e2e-tests/plugins/block-context.php @@ -32,13 +32,13 @@ function gutenberg_test_register_context_blocks() { register_block_type( 'gutenberg/test-context-provider', array( - 'attributes' => array( + 'attributes' => array( 'recordId' => array( 'type' => 'number', 'default' => 0, ), ), - 'providesContext' => array( + 'provides_context' => array( 'gutenberg/recordId' => 'recordId', ), ) @@ -47,7 +47,7 @@ function gutenberg_test_register_context_blocks() { register_block_type( 'gutenberg/test-context-consumer', array( - 'context' => array( + 'uses_context' => array( 'gutenberg/recordId', 'postId', 'postType', diff --git a/packages/e2e-tests/plugins/block-context/index.js b/packages/e2e-tests/plugins/block-context/index.js index 3ab0a67b9e6811..0bca6a508074e7 100644 --- a/packages/e2e-tests/plugins/block-context/index.js +++ b/packages/e2e-tests/plugins/block-context/index.js @@ -49,7 +49,7 @@ // TODO: While redundant with server-side registration, it's required // to assign this value since it is not picked in the implementation of // `get_block_editor_server_block_settings`. - context: [ 'gutenberg/recordId' ], + usesContext: [ 'gutenberg/recordId' ], category: 'text', diff --git a/phpunit/class-block-context-test.php b/phpunit/class-block-context-test.php index f3a7fc1011a757..2d075bf4a5db49 100644 --- a/phpunit/class-block-context-test.php +++ b/phpunit/class-block-context-test.php @@ -72,7 +72,7 @@ function test_provides_block_context() { $this->register_block_type( 'gutenberg/test-context-provider', array( - 'attributes' => array( + 'attributes' => array( 'contextWithAssigned' => array( 'type' => 'number', ), @@ -87,7 +87,7 @@ function test_provides_block_context() { 'type' => 'number', ), ), - 'providesContext' => array( + 'provides_context' => array( 'gutenberg/contextWithAssigned' => 'contextWithAssigned', 'gutenberg/contextWithDefault' => 'contextWithDefault', 'gutenberg/contextWithoutDefault' => 'contextWithoutDefault', @@ -99,7 +99,7 @@ function test_provides_block_context() { $this->register_block_type( 'gutenberg/test-context-consumer', array( - 'context' => array( + 'uses_context' => array( 'gutenberg/contextWithDefault', 'gutenberg/contextWithAssigned', 'gutenberg/contextWithoutDefault', @@ -141,7 +141,7 @@ function test_provides_default_context() { $this->register_block_type( 'gutenberg/test-context-consumer', array( - 'context' => array( 'postId', 'postType' ), + 'uses_context' => array( 'postId', 'postType' ), 'render_callback' => function( $attributes, $content, $block ) use ( &$provided_context ) { $provided_context[] = $block->context; @@ -172,7 +172,7 @@ function test_default_context_is_filterable() { $this->register_block_type( 'gutenberg/test-context-consumer', array( - 'context' => array( 'example' ), + 'uses_context' => array( 'example' ), 'render_callback' => function( $attributes, $content, $block ) use ( &$provided_context ) { $provided_context[] = $block->context; diff --git a/phpunit/class-register-block-type-from-metadata-test.php b/phpunit/class-register-block-type-from-metadata-test.php index 81e240f5ad3598..6a79194c62f1ca 100644 --- a/phpunit/class-register-block-type-from-metadata-test.php +++ b/phpunit/class-register-block-type-from-metadata-test.php @@ -153,10 +153,10 @@ function test_block_registers_with_metadata_fixture() { $this->assertSame( 'my-plugin/notice', $result->name ); $this->assertSame( 'Notice', $result->title ); $this->assertSame( 'common', $result->category ); - $this->assertEquals( array( 'core/group' ), $result->parent ); + $this->assertEqualSets( array( 'core/group' ), $result->parent ); $this->assertSame( 'star', $result->icon ); $this->assertSame( 'Shows warning, error or success notices…', $result->description ); - $this->assertEquals( array( 'alert', 'message' ), $result->keywords ); + $this->assertEqualSets( array( 'alert', 'message' ), $result->keywords ); $this->assertEquals( array( 'message' => array( @@ -167,6 +167,13 @@ function test_block_registers_with_metadata_fixture() { ), $result->attributes ); + $this->assertEquals( + array( + 'my-plugin/message' => 'message', + ), + $result->provides_context + ); + $this->assertEqualSets( array( 'groupId' ), $result->uses_context ); $this->assertEquals( array( 'align' => true, diff --git a/phpunit/class-wp-block-test.php b/phpunit/class-wp-block-test.php index 236ed46c0cb0ec..a71d4e90d3f123 100644 --- a/phpunit/class-wp-block-test.php +++ b/phpunit/class-wp-block-test.php @@ -133,7 +133,7 @@ function test_constructor_assigns_context_from_block_type() { $this->registry->register( 'core/example', array( - 'context' => array( 'requested' ), + 'uses_context' => array( 'requested' ), ) ); @@ -164,12 +164,12 @@ function test_constructor_prepares_context_for_inner_blocks() { $this->registry->register( 'core/outer', array( - 'attributes' => array( + 'attributes' => array( 'recordId' => array( 'type' => 'number', ), ), - 'providesContext' => array( + 'provides_context' => array( 'core/recordId' => 'recordId', ), ) @@ -177,7 +177,7 @@ function test_constructor_prepares_context_for_inner_blocks() { $this->registry->register( 'core/inner', array( - 'context' => array( 'core/recordId' ), + 'uses_context' => array( 'core/recordId' ), ) ); @@ -197,15 +197,15 @@ function test_constructor_assigns_merged_context() { $this->registry->register( 'core/example', array( - 'attributes' => array( + 'attributes' => array( 'value' => array( 'type' => array( 'string', 'null' ), ), ), - 'providesContext' => array( + 'provides_context' => array( 'core/value' => 'value', ), - 'context' => array( 'core/value' ), + 'uses_context' => array( 'core/value' ), ) ); diff --git a/phpunit/class-wp-rest-block-types-controller-test.php b/phpunit/class-wp-rest-block-types-controller-test.php index 91c921ea33d645..fe9f89f847d22d 100644 --- a/phpunit/class-wp-rest-block-types-controller-test.php +++ b/phpunit/class-wp-rest-block-types-controller-test.php @@ -218,7 +218,7 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertCount( 17, $properties ); + $this->assertCount( 19, $properties ); $this->assertArrayHasKey( 'title', $properties ); $this->assertArrayHasKey( 'icon', $properties ); $this->assertArrayHasKey( 'description', $properties ); @@ -236,6 +236,8 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'style', $properties ); $this->assertArrayHasKey( 'parent', $properties ); $this->assertArrayHasKey( 'example', $properties ); + $this->assertArrayHasKey( 'uses_context', $properties ); + $this->assertArrayHasKey( 'provides_context', $properties ); } /** @@ -333,21 +335,23 @@ public function check_block_type_object( $block_type, $data, $links ) { $this->assertEquals( $data['is_dynamic'], $block_type->is_dynamic() ); $extra_fields = array( - 'name' => 'name', - 'category' => 'category', - 'editor_script' => 'editor_script', - 'script' => 'script', - 'editor_style' => 'editor_style', - 'style' => 'style', - 'supports' => 'supports', - 'title' => 'title', - 'icon' => 'icon', - 'description' => 'description', - 'keywords' => 'keywords', - 'parent' => 'parent', - 'styles' => 'styles', - 'textdomain' => 'textdomain', - 'example' => 'example', + 'name' => 'name', + 'category' => 'category', + 'editor_script' => 'editor_script', + 'script' => 'script', + 'editor_style' => 'editor_style', + 'style' => 'style', + 'title' => 'title', + 'icon' => 'icon', + 'description' => 'description', + 'keywords' => 'keywords', + 'parent' => 'parent', + 'provides_context' => 'provides_context', + 'uses_context' => 'uses_context', + 'supports' => 'supports', + 'styles' => 'styles', + 'textdomain' => 'textdomain', + 'example' => 'example', ); foreach ( $extra_fields as $key => $extra_field ) { diff --git a/phpunit/fixtures/block.json b/phpunit/fixtures/block.json index 2d71b8f26a4dce..be4205ce767af2 100644 --- a/phpunit/fixtures/block.json +++ b/phpunit/fixtures/block.json @@ -5,6 +5,12 @@ "parent": [ "core/group" ], + "providesContext": { + "my-plugin/message": "message" + }, + "usesContext": [ + "groupId" + ], "icon": "star", "description": "Shows warning, error or success notices…", "keywords": [