Skip to content

Commit

Permalink
Block API: WP_Block: Pass block as third argument of render_callback
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Apr 27, 2020
1 parent 3ab05e8 commit 4ac2be1
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 24 deletions.
11 changes: 4 additions & 7 deletions lib/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function( $inner_block ) use ( $child_context, $registry ) {
* @return string Rendered block output.
*/
public function render() {
global $post, $_experimental_block;
global $post;

$is_dynamic = $this->name && null !== $this->block_type && $this->block_type->is_dynamic();
$block_content = '';
Expand All @@ -178,12 +178,9 @@ public function render() {
$attributes = $this->block_type->prepare_attributes_for_render( $attributes );
}

$global_post = $post;
$global_block = $_experimental_block;
$_experimental_block = $this;
$block_content = (string) call_user_func( $this->block_type->render_callback, $attributes, $block_content );
$_experimental_block = $global_block;
$post = $global_post;
$global_post = $post;
$block_content = (string) call_user_func( $this->block_type->render_callback, $attributes, $block_content, $this );
$post = $global_post;
}

/** This filter is documented in src/wp-includes/blocks.php */
Expand Down
7 changes: 3 additions & 4 deletions packages/block-library/src/post-title/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
*
* @return string Returns the filtered post title for the current post wrapped inside "h1" tags.
*/
function render_block_core_post_title() {
global $_experimental_block;
if ( ! isset( $_experimental_block->context['postId'] ) ) {
function render_block_core_post_title( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
}

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

/**
Expand Down
6 changes: 2 additions & 4 deletions packages/e2e-tests/plugins/block-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ function gutenberg_test_register_context_blocks() {
'gutenberg/test-context-consumer',
array(
'context' => array( 'gutenberg/recordId' ),
'render_callback' => function() {
global $_experimental_block;

$record_id = $_experimental_block->context['gutenberg/recordId'];
'render_callback' => function( $attributes, $content, $block ) {
$record_id = $block->context['gutenberg/recordId'];

if ( ! is_int( $record_id ) ) {
throw new Exception( 'Expected numeric recordId' );
Expand Down
6 changes: 2 additions & 4 deletions phpunit/class-block-context-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,8 @@ function test_provides_block_context() {
'gutenberg/contextWithAssigned',
'gutenberg/contextWithoutDefault',
),
'render_callback' => function() use ( &$provided_context ) {
global $_experimental_block;

$provided_context[] = $_experimental_block->context;
'render_callback' => function( $attributes, $content, $block ) use ( &$provided_context ) {
$provided_context[] = $block->context;

return '';
},
Expand Down
8 changes: 3 additions & 5 deletions phpunit/class-wp-block-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,12 @@ function test_render_static_block_type_returns_own_content() {
$this->assertSame( 'abc', $block->render() );
}

function test_render_assigns_instance_global_for_render_callback() {
function test_render_passes_block_for_render_callback() {
$this->registry->register(
'core/greeting',
array(
'render_callback' => function() {
global $_experimental_block;

return sprintf( 'Hello from %s', $_experimental_block->name );
'render_callback' => function( $attributes, $content, $block ) {
return sprintf( 'Hello from %s', $block->name );
},
)
);
Expand Down

0 comments on commit 4ac2be1

Please sign in to comment.