From 4ac2be1ef3ec9cf769d6eb3dcb77492ebd7e5932 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Mon, 27 Apr 2020 10:08:27 -0400 Subject: [PATCH] Block API: WP_Block: Pass block as third argument of render_callback --- lib/class-wp-block.php | 11 ++++------- packages/block-library/src/post-title/index.php | 7 +++---- packages/e2e-tests/plugins/block-context.php | 6 ++---- phpunit/class-block-context-test.php | 6 ++---- phpunit/class-wp-block-test.php | 8 +++----- 5 files changed, 14 insertions(+), 24 deletions(-) diff --git a/lib/class-wp-block.php b/lib/class-wp-block.php index c4a17406820ada..9c410cef7ca881 100644 --- a/lib/class-wp-block.php +++ b/lib/class-wp-block.php @@ -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 = ''; @@ -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 */ diff --git a/packages/block-library/src/post-title/index.php b/packages/block-library/src/post-title/index.php index 700d8ea968546f..d07a02fa8a4d2c 100644 --- a/packages/block-library/src/post-title/index.php +++ b/packages/block-library/src/post-title/index.php @@ -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 '

' . get_the_title( $_experimental_block->context['postId'] ) . '

'; + return '

' . get_the_title( $block->context['postId'] ) . '

'; } /** diff --git a/packages/e2e-tests/plugins/block-context.php b/packages/e2e-tests/plugins/block-context.php index 9b1b4c27cdc3c2..8f3ca467d50043 100644 --- a/packages/e2e-tests/plugins/block-context.php +++ b/packages/e2e-tests/plugins/block-context.php @@ -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' ); diff --git a/phpunit/class-block-context-test.php b/phpunit/class-block-context-test.php index 52a4048a64bfa5..b78911cf529c4b 100644 --- a/phpunit/class-block-context-test.php +++ b/phpunit/class-block-context-test.php @@ -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 ''; }, diff --git a/phpunit/class-wp-block-test.php b/phpunit/class-wp-block-test.php index ba6385b211aee7..414d52a91bf6c9 100644 --- a/phpunit/class-wp-block-test.php +++ b/phpunit/class-wp-block-test.php @@ -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 ); }, ) );