Skip to content

Commit 30e4399

Browse files
committed
Block API: Mark block global as experimental
1 parent cf90039 commit 30e4399

6 files changed

Lines changed: 18 additions & 30 deletions

File tree

docs/designers-developers/developers/block-api/block-context.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,4 @@ registerBlockType( 'my-plugin/record-title', {
6363

6464
### PHP
6565

66-
Note that in PHP, block context is accessed using the `$block` global which is assigned at the time a block is rendered. This is unlike block attributes or block content, which are provided as arguments to the `render_callback` function. At some point in the future, block context may be integrated into the function arguments signature of `render_callback`, or an alternative block settings configuration may enable a render callback to receive the full block array as its argument.
67-
68-
`record-title/index.php`
69-
70-
```php
71-
register_block_type( 'my-plugin/record-title', [
72-
'render_callback' => function() {
73-
global $block;
74-
75-
return 'The current record ID is: ' . $block->context['my-plugin/recordId'];
76-
},
77-
] );
78-
```
66+
_The PHP implementation of block context is currently experimental and subject to breaking changes. It will be documented in the future once the API has stabilized._

lib/class-wp-block.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ function( $inner_block ) use ( $child_context, $registry ) {
156156
* @return string Rendered block output.
157157
*/
158158
public function render() {
159-
global $post, $block;
159+
global $post, $_experimental_block;
160160

161161
$is_dynamic = $this->name && null !== $this->block_type && $this->block_type->is_dynamic();
162162
$block_content = '';
@@ -169,12 +169,12 @@ public function render() {
169169
}
170170

171171
if ( $is_dynamic ) {
172-
$global_post = $post;
173-
$global_block = $block;
174-
$block = $this;
175-
$block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content );
176-
$block = $global_block;
177-
$post = $global_post;
172+
$global_post = $post;
173+
$global_block = $_experimental_block;
174+
$_experimental_block = $this;
175+
$block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content );
176+
$_experimental_block = $global_block;
177+
$post = $global_post;
178178
}
179179

180180
return $block_content;

packages/block-library/src/post-title/index.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
* @return string Returns the filtered post title for the current post wrapped inside "h1" tags.
1212
*/
1313
function render_block_core_post_title() {
14-
global $block;
15-
if ( ! isset( $block->context['postId'] ) ) {
14+
global $_experimental_block;
15+
if ( ! isset( $_experimental_block->context['postId'] ) ) {
1616
return '';
1717
}
1818

19-
return '<h1>' . get_the_title( $block->context['postId'] ) . '</h1>';
19+
return '<h1>' . get_the_title( $_experimental_block->context['postId'] ) . '</h1>';
2020
}
2121

2222
/**

packages/e2e-tests/plugins/block-context.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ function gutenberg_test_register_context_blocks() {
4949
array(
5050
'context' => array( 'gutenberg/recordId' ),
5151
'render_callback' => function() {
52-
global $block;
52+
global $_experimental_block;
5353

54-
$record_id = $block->context['gutenberg/recordId'];
54+
$record_id = $_experimental_block->context['gutenberg/recordId'];
5555

5656
if ( ! is_int( $record_id ) ) {
5757
throw new Exception( 'Expected numeric recordId' );

phpunit/class-block-context-test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ function test_provides_block_context() {
105105
'gutenberg/contextWithoutDefault',
106106
),
107107
'render_callback' => function() use ( &$provided_context ) {
108-
global $block;
108+
global $_experimental_block;
109109

110-
$provided_context[] = $block->context;
110+
$provided_context[] = $_experimental_block->context;
111111

112112
return '';
113113
},

phpunit/class-wp-block-test.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,12 @@ function test_render_assigns_instance_global_for_render_callback() {
260260
),
261261
),
262262
'render_callback' => function() {
263-
global $block;
263+
global $_experimental_block;
264264

265265
return sprintf(
266266
'Hello %s%s',
267-
$block->attributes['toWhom'],
268-
$block->attributes['punctuation']
267+
$_experimental_block->attributes['toWhom'],
268+
$_experimental_block->attributes['punctuation']
269269
);
270270
},
271271
)

0 commit comments

Comments
 (0)