Skip to content

Commit

Permalink
Stabilize the pattern overrides block context (#58102)
Browse files Browse the repository at this point in the history
* Add pattern/overrides context to individual block.json files

* Remove addition of block context usage via blocks supports

* Add overrides attribute to pattern block and share it via providesContext

* Revert "Add overrides attribute to pattern block and share it via providesContext"

This reverts commit aca9b906e676a16bfa117ebfaa406e9141b72817.

* Remove the experiment flag after reverting pattern block index.php changes

* Register overrides attribute in block.json
  • Loading branch information
talldan authored Jan 23, 2024
1 parent 38f8eaa commit c6ac9c0
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 81 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Create and save content to reuse across your site. Update the pattern, and the c
- **Name:** core/block
- **Category:** reusable
- **Supports:** ~~customClassName~~, ~~html~~, ~~inserter~~, ~~renaming~~
- **Attributes:** ref
- **Attributes:** overrides, ref

## Button

Expand Down
46 changes: 0 additions & 46 deletions lib/block-supports/pattern.php

This file was deleted.

1 change: 0 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/block-supports/duotone.php';
require __DIR__ . '/block-supports/shadow.php';
require __DIR__ . '/block-supports/background.php';
require __DIR__ . '/block-supports/pattern.php';

// Data views.
require_once __DIR__ . '/experimental/data-views.php';
3 changes: 3 additions & 0 deletions packages/block-library/src/block/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"attributes": {
"ref": {
"type": "number"
},
"overrides": {
"type": "object"
}
},
"supports": {
Expand Down
34 changes: 3 additions & 31 deletions packages/block-library/src/block/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,14 @@ function render_block_core_block( $attributes ) {
$content = $wp_embed->run_shortcode( $reusable_block->post_content );
$content = $wp_embed->autoembed( $content );

$gutenberg_experiments = get_option( 'gutenberg-experiments' );
$has_partial_synced_overrides = $gutenberg_experiments
&& array_key_exists( 'gutenberg-pattern-partial-syncing', $gutenberg_experiments )
&& isset( $attributes['overrides'] );
$has_pattern_overrides = isset( $attributes['overrides'] );

/**
* We set the `pattern/overrides` context through the `render_block_context`
* filter so that it is available when a pattern's inner blocks are
* rendering via do_blocks given it only receives the inner content.
*/
if ( $has_partial_synced_overrides ) {
if ( $has_pattern_overrides ) {
$filter_block_context = static function ( $context ) use ( $attributes ) {
$context['pattern/overrides'] = $attributes['overrides'];
return $context;
Expand All @@ -67,7 +64,7 @@ function render_block_core_block( $attributes ) {
$content = do_blocks( $content );
unset( $seen_refs[ $attributes['ref'] ] );

if ( $has_partial_synced_overrides ) {
if ( $has_pattern_overrides ) {
remove_filter( 'render_block_context', $filter_block_context, 1 );
}

Expand All @@ -86,28 +83,3 @@ function register_block_core_block() {
);
}
add_action( 'init', 'register_block_core_block' );

$gutenberg_experiments = get_option( 'gutenberg-experiments' );
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-pattern-partial-syncing', $gutenberg_experiments ) ) {
/**
* Registers the overrides attribute for core/block.
*
* @param array $args Array of arguments for registering a block type.
* @param string $block_name Block name including namespace.
* @return array $args
*/
function register_block_core_block_args( $args, $block_name ) {
if ( 'core/block' === $block_name ) {
$args['attributes'] = array_merge(
$args['attributes'],
array(
'overrides' => array(
'type' => 'object',
),
)
);
}
return $args;
}
add_filter( 'register_block_type_args', 'register_block_core_block_args', 10, 2 );
}
1 change: 1 addition & 0 deletions packages/block-library/src/button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"description": "Prompt visitors to take action with a button-style link.",
"keywords": [ "link" ],
"textdomain": "default",
"usesContext": [ "pattern/overrides" ],
"attributes": {
"tagName": {
"type": "string",
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/heading/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"description": "Introduce new sections and organize content to help visitors (and search engines) understand the structure of your content.",
"keywords": [ "title", "subtitle" ],
"textdomain": "default",
"usesContext": [ "pattern/overrides" ],
"attributes": {
"textAlign": {
"type": "string"
Expand Down
7 changes: 6 additions & 1 deletion packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
"name": "core/image",
"title": "Image",
"category": "media",
"usesContext": [ "allowResize", "imageCrop", "fixedHeight" ],
"usesContext": [
"allowResize",
"imageCrop",
"fixedHeight",
"pattern/overrides"
],
"description": "Insert an image to make a visual statement.",
"keywords": [ "img", "photo", "picture" ],
"textdomain": "default",
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/paragraph/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"description": "Start with the basic building block of all narrative.",
"keywords": [ "text" ],
"textdomain": "default",
"usesContext": [ "postId" ],
"usesContext": [ "postId", "pattern/overrides" ],
"attributes": {
"align": {
"type": "string"
Expand Down

1 comment on commit c6ac9c0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in c6ac9c0.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7623186970
📝 Reported issues:

Please sign in to comment.