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

Stabilize the pattern overrides block context #58102

Merged
merged 6 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

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

Should we add the overrides attribute in block.json or should we do in #58105?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, I did make that change but reverted it accidentally, I'll bring it back again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed in c8ca2e9

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
Loading