From 3499aad9cc29095550d4bf40523c71271a669e98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Wed, 21 Oct 2020 13:47:42 +0200 Subject: [PATCH 01/14] Substitute the global by an internal static property --- src/wp-includes/class-wp-block-supports.php | 45 +++++++++++++++++---- src/wp-includes/default-filters.php | 1 + 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/class-wp-block-supports.php b/src/wp-includes/class-wp-block-supports.php index 7bec60b85ddc9..13da79a66f27d 100644 --- a/src/wp-includes/class-wp-block-supports.php +++ b/src/wp-includes/class-wp-block-supports.php @@ -30,6 +30,13 @@ class WP_Block_Supports { */ private static $instance = null; + /** + * Tracks the current block to be rendered. + * + * @var array + */ + private static $block_to_render = null; + /** * Utility method to retrieve the main instance of the class. * @@ -57,6 +64,32 @@ public static function init() { $instance->register_attributes(); } + /** + * Callback hooked to the register_block_type_args filter. + * + * @since 5.6.0 + * + * This hooks into block registration to wrap the render_callback + * of dynamic blocks with a closure that keeps track of the + * current block to be rendered. + * + * @param array $args Block attributes. + * @return array Block attributes. + */ + public static function track_block_to_render( $args ) { + if ( null !== $args['render_callback'] ) { + $block_render_callback = $args['render_callback']; + $args['render_callback'] = function( $attributes, $content, $block ) use ( $block_render_callback ) { + $parent_block = self::$block_to_render; + self::$block_to_render = $block->parsed_block; + $result = $block_render_callback( $attributes, $content, $block ); + self::$block_to_render = $parent_block; + return $result; + }; + } + return $args; + } + /** * Registers a block support. * @@ -79,13 +112,12 @@ public function register( $block_support_name, $block_support_config ) { * * @since 5.6.0 * - * @param array $parsed_block Block as parsed from content. * @return array Array of HTML attributes. */ - public function apply_block_supports( $parsed_block ) { - $block_attributes = $parsed_block['attrs']; + public function apply_block_supports() { + $block_attributes = self::$block_to_render['attrs']; $block_type = WP_Block_Type_Registry::get_instance()->get_registered( - $parsed_block['blockName'] + self::$block_to_render['blockName'] ); // If no render_callback, assume styles have been previously handled. @@ -155,15 +187,12 @@ private function register_attributes() { * * @since 5.6.0 * - * @global array $current_parsed_block Block currently being parsed. - * * @param array $extra_attributes Optional. Extra attributes to render on the block wrapper. * * @return string String of HTML classes. */ function get_block_wrapper_attributes( $extra_attributes = array() ) { - global $current_parsed_block; - $new_attributes = WP_Block_Supports::get_instance()->apply_block_supports( $current_parsed_block ); + $new_attributes = WP_Block_Supports::get_instance()->apply_block_supports(); if ( empty( $new_attributes ) && empty( $extra_attributes ) ) { return ''; diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 96ef7d82eb0f0..d99b5ae642f75 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -306,6 +306,7 @@ add_action( 'init', '_register_core_block_patterns_and_categories' ); add_action( 'init', 'check_theme_switched', 99 ); add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 ); +add_filter( 'register_block_type_args', array( 'WP_Block_Supports', 'track_block_to_render' ) ); add_action( 'after_switch_theme', '_wp_menus_changed' ); add_action( 'after_switch_theme', '_wp_sidebars_changed' ); add_action( 'wp_print_styles', 'print_emoji_styles' ); From a68cd0af157f6dbed1df403f2188de437e91b26e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Wed, 21 Oct 2020 13:49:19 +0200 Subject: [PATCH 02/14] Remove use of current_parsed_block global --- src/wp-includes/blocks.php | 17 +---------------- src/wp-includes/class-wp-block.php | 13 ++++--------- .../includes/testcase-block-supports.php | 5 +---- 3 files changed, 6 insertions(+), 29 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 206e7d3991adc..a1bde9a802951 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -647,24 +647,11 @@ function _excerpt_render_inner_columns_blocks( $columns, $allowed_blocks ) { return $output; } -/** - * Block currently being parsed. - * - * @type array -*/ -global $current_parsed_block; - -$current_parsed_block = array( - 'blockName' => null, - 'attributes' => null, -); - /** * Renders a single block into a HTML string. * * @since 5.0.0 * - * @global array $current_parsed_block Block currently being parsed. * @global WP_Post $post The post to edit. * @global WP_Query $wp_query WordPress Query object. * @global WP_Query $wp_query WordPress Query object. @@ -673,7 +660,7 @@ function _excerpt_render_inner_columns_blocks( $columns, $allowed_blocks ) { * @return string String of rendered HTML. */ function render_block( $parsed_block ) { - global $post, $wp_query, $current_parsed_block; + global $post, $wp_query; /** * Allows render_block() to be short-circuited, by returning a non-null value. @@ -688,8 +675,6 @@ function render_block( $parsed_block ) { return $pre_render; } - $current_parsed_block = $parsed_block; - $source_block = $parsed_block; /** diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 135bad0d3352e..325ca6e65c535 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -192,7 +192,7 @@ public function __get( $name ) { * @return string Rendered block output. */ public function render( $options = array() ) { - global $post, $current_parsed_block; + global $post; $options = wp_parse_args( $options, array( @@ -206,14 +206,9 @@ public function render( $options = array() ) { if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) { $index = 0; foreach ( $this->inner_content as $chunk ) { - if ( is_string( $chunk ) ) { - $block_content .= $chunk; - } else { - $parent_parsed_block = $current_parsed_block; - $current_parsed_block = $this->inner_blocks[ $index ]->parsed_block; - $block_content .= $this->inner_blocks[ $index++ ]->render(); - $current_parsed_block = $parent_parsed_block; - } + $block_content .= is_string( $chunk ) ? + $chunk : + $this->inner_blocks[ $index++ ]->render(); } } diff --git a/tests/phpunit/includes/testcase-block-supports.php b/tests/phpunit/includes/testcase-block-supports.php index b5ed1310bcd92..617a19a070f3c 100644 --- a/tests/phpunit/includes/testcase-block-supports.php +++ b/tests/phpunit/includes/testcase-block-supports.php @@ -95,11 +95,8 @@ private function get_content_from_block( $block ) { /** * Returns the rendered output for the current block. * - * @param array $block Block to render. */ - private function render_example_block( $block ) { - global $current_parsed_block; - $current_parsed_block = $block; + private function render_example_block() { $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'foo-bar-class', From 150563e8c0093312dc8d385ab772d424e8f5847d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Wed, 21 Oct 2020 13:52:06 +0200 Subject: [PATCH 03/14] Fix linting issues --- tests/phpunit/includes/testcase-block-supports.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/phpunit/includes/testcase-block-supports.php b/tests/phpunit/includes/testcase-block-supports.php index 617a19a070f3c..4441ab0438578 100644 --- a/tests/phpunit/includes/testcase-block-supports.php +++ b/tests/phpunit/includes/testcase-block-supports.php @@ -94,10 +94,9 @@ private function get_content_from_block( $block ) { /** * Returns the rendered output for the current block. - * */ private function render_example_block() { - $wrapper_attributes = get_block_wrapper_attributes( + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'foo-bar-class', 'style' => 'test: style;', From dcd7f5ade715061f6c0195ba1f3b80a12c1ee0b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Thu, 22 Oct 2020 12:44:15 +0200 Subject: [PATCH 04/14] Extract wp_block_supports_track_block_to_render method --- src/wp-includes/class-wp-block-supports.php | 63 +++++++++---------- src/wp-includes/default-filters.php | 2 +- .../includes/testcase-block-supports.php | 10 ++- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/src/wp-includes/class-wp-block-supports.php b/src/wp-includes/class-wp-block-supports.php index 13da79a66f27d..7d5e99b7eee1c 100644 --- a/src/wp-includes/class-wp-block-supports.php +++ b/src/wp-includes/class-wp-block-supports.php @@ -24,18 +24,18 @@ class WP_Block_Supports { private $block_supports = array(); /** - * Container for the main instance of the class. + * Tracks the current block to be rendered. * - * @var WP_Block_Supports|null + * @var array */ - private static $instance = null; + public static $block_to_render = null; /** - * Tracks the current block to be rendered. + * Container for the main instance of the class. * - * @var array + * @var WP_Block_Supports|null */ - private static $block_to_render = null; + private static $instance = null; /** * Utility method to retrieve the main instance of the class. @@ -64,32 +64,6 @@ public static function init() { $instance->register_attributes(); } - /** - * Callback hooked to the register_block_type_args filter. - * - * @since 5.6.0 - * - * This hooks into block registration to wrap the render_callback - * of dynamic blocks with a closure that keeps track of the - * current block to be rendered. - * - * @param array $args Block attributes. - * @return array Block attributes. - */ - public static function track_block_to_render( $args ) { - if ( null !== $args['render_callback'] ) { - $block_render_callback = $args['render_callback']; - $args['render_callback'] = function( $attributes, $content, $block ) use ( $block_render_callback ) { - $parent_block = self::$block_to_render; - self::$block_to_render = $block->parsed_block; - $result = $block_render_callback( $attributes, $content, $block ); - self::$block_to_render = $parent_block; - return $result; - }; - } - return $args; - } - /** * Registers a block support. * @@ -105,7 +79,6 @@ public function register( $block_support_name, $block_support_config ) { ); } - /** * Generates an array of HTML attributes, such as classes, by applying to * the given block all of the features that the block supports. @@ -238,3 +211,27 @@ function get_block_wrapper_attributes( $extra_attributes = array() ) { return implode( ' ', $normalized_attributes ); } + +/** + * Callback hooked to the register_block_type_args filter. + * + * This hooks into block registration to wrap the render_callback + * of dynamic blocks with a closure that keeps track of the + * current block to be rendered. + * + * @param array $args Block attributes. + * @return array Block attributes. + */ +function wp_block_supports_track_block_to_render( $args ) { + if ( null !== $args['render_callback'] ) { + $block_render_callback = $args['render_callback']; + $args['render_callback'] = function( $attributes, $content, $block ) use ( $block_render_callback ) { + $parent_block = WP_Block_Supports::$block_to_render; + WP_Block_Supports::$block_to_render = $block->parsed_block; + $result = $block_render_callback( $attributes, $content, $block ); + WP_Block_Supports::$block_to_render = $parent_block; + return $result; + }; + } + return $args; +} diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index d99b5ae642f75..457517c4b03cb 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -306,7 +306,7 @@ add_action( 'init', '_register_core_block_patterns_and_categories' ); add_action( 'init', 'check_theme_switched', 99 ); add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 ); -add_filter( 'register_block_type_args', array( 'WP_Block_Supports', 'track_block_to_render' ) ); +add_filter( 'register_block_type_args', 'wp_block_supports_track_block_to_render' ); add_action( 'after_switch_theme', '_wp_menus_changed' ); add_action( 'after_switch_theme', '_wp_sidebars_changed' ); add_action( 'wp_print_styles', 'print_emoji_styles' ); diff --git a/tests/phpunit/includes/testcase-block-supports.php b/tests/phpunit/includes/testcase-block-supports.php index 4441ab0438578..9f379696e3514 100644 --- a/tests/phpunit/includes/testcase-block-supports.php +++ b/tests/phpunit/includes/testcase-block-supports.php @@ -94,9 +94,15 @@ private function get_content_from_block( $block ) { /** * Returns the rendered output for the current block. + * + * @param array $block Block to render. + * + * @return string Rendered output for the current block. */ - private function render_example_block() { - $wrapper_attributes = get_block_wrapper_attributes( + private function render_example_block( $block ) { + WP_Block_Supports::init(); + WP_Block_Supports::$block_to_render = $block; + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => 'foo-bar-class', 'style' => 'test: style;', From c61fff865781191a4eee63493181e3e3f726c6f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Fri, 23 Oct 2020 12:52:10 +0200 Subject: [PATCH 05/14] Deprecate and patch the WP_Block_Type->render so is compatible with current code Since: https://core.trac.wordpress.org/ticket/49927 Commit 910de8f6890c87f93359c6f2edc6c27b9a3f3292 at wordpress-develop we no longer use the render method of WP_Block_Type, but its equivalent at WP_Block. There's still some tests that use that old path that were failing because the render_callback was called with only 2 arguments instead of the three that WP_Block uses. This commit: - Signals WP_Block_Type->render as deprecated. - Patch it to pass a null value as the third argument (it represents an instance of WP_Block). --- src/wp-includes/class-wp-block-supports.php | 20 +++++++++++++++----- src/wp-includes/class-wp-block-type.php | 4 +++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-block-supports.php b/src/wp-includes/class-wp-block-supports.php index 7d5e99b7eee1c..474ff85663b18 100644 --- a/src/wp-includes/class-wp-block-supports.php +++ b/src/wp-includes/class-wp-block-supports.php @@ -226,11 +226,21 @@ function wp_block_supports_track_block_to_render( $args ) { if ( null !== $args['render_callback'] ) { $block_render_callback = $args['render_callback']; $args['render_callback'] = function( $attributes, $content, $block ) use ( $block_render_callback ) { - $parent_block = WP_Block_Supports::$block_to_render; - WP_Block_Supports::$block_to_render = $block->parsed_block; - $result = $block_render_callback( $attributes, $content, $block ); - WP_Block_Supports::$block_to_render = $parent_block; - return $result; + // Check for null for back compatibility with WP_Block_Type->render + // which is unused since the introduction of WP_Block class. + // + // See: + // - https://core.trac.wordpress.org/ticket/49927 + // - commit 910de8f6890c87f93359c6f2edc6c27b9a3f3292 at wordpress-develop + if ( null !== $block ) { + $parent_block = WP_Block_Supports::$block_to_render; + WP_Block_Supports::$block_to_render = $block->parsed_block; + $result = $block_render_callback( $attributes, $content, $block ); + WP_Block_Supports::$block_to_render = $parent_block; + return $result; + } else { + return $block_render_callback( $attributes, $content ); + } }; } return $args; diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 7687d2b9317d9..bf3fd89ae1f9d 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -180,6 +180,8 @@ public function __construct( $block_type, $args = array() ) { * * @since 5.0.0 * + * @deprecated 5.6 See WP_Block->render introduced in 5.5. + * * @param array $attributes Optional. Block attributes. Default empty array. * @param string $content Optional. Block content. Default empty string. * @return string Rendered block type output. @@ -191,7 +193,7 @@ public function render( $attributes = array(), $content = '' ) { $attributes = $this->prepare_attributes_for_render( $attributes ); - return (string) call_user_func( $this->render_callback, $attributes, $content ); + return (string) call_user_func( $this->render_callback, $attributes, $content, null ); } /** From 121e2a55e01c1c465f9a52561fe6e93160a5c362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Fri, 23 Oct 2020 13:20:00 +0200 Subject: [PATCH 06/14] Fix WP_Test_Block_Type->test_set_props --- tests/phpunit/tests/blocks/block-type.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/block-type.php b/tests/phpunit/tests/blocks/block-type.php index e93aed05a2410..db2e26d270f2f 100644 --- a/tests/phpunit/tests/blocks/block-type.php +++ b/tests/phpunit/tests/blocks/block-type.php @@ -69,18 +69,21 @@ public static function wpSetUpBeforeClass() { /** * @ticket 45097 + * + * @since 5.6 Removed testing whether render_callback + * is the same as the one passed because we wrap + * it in wp_block_supports_track_block_to_render + * so it's not equal. */ public function test_set_props() { $name = 'core/fake'; $args = array( - 'render_callback' => array( $this, 'render_fake_block' ), 'foo' => 'bar', ); $block_type = new WP_Block_Type( $name, $args ); $this->assertSame( $name, $block_type->name ); - $this->assertSame( $args['render_callback'], $block_type->render_callback ); $this->assertSame( $args['foo'], $block_type->foo ); } From d3d7b9e5139bac50b18580520c2fbea74dda3b5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Fri, 23 Oct 2020 13:25:42 +0200 Subject: [PATCH 07/14] Make linter happy --- tests/phpunit/tests/blocks/block-type.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/block-type.php b/tests/phpunit/tests/blocks/block-type.php index db2e26d270f2f..aaf8ca088c2e3 100644 --- a/tests/phpunit/tests/blocks/block-type.php +++ b/tests/phpunit/tests/blocks/block-type.php @@ -69,7 +69,7 @@ public static function wpSetUpBeforeClass() { /** * @ticket 45097 - * + * * @since 5.6 Removed testing whether render_callback * is the same as the one passed because we wrap * it in wp_block_supports_track_block_to_render From 9561a9a178acbe8243316e2ce0a752e5d42f917e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Fri, 23 Oct 2020 13:28:58 +0200 Subject: [PATCH 08/14] Make linter happy --- tests/phpunit/tests/blocks/block-type.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/block-type.php b/tests/phpunit/tests/blocks/block-type.php index aaf8ca088c2e3..a5bd46c22dd8d 100644 --- a/tests/phpunit/tests/blocks/block-type.php +++ b/tests/phpunit/tests/blocks/block-type.php @@ -78,7 +78,7 @@ public static function wpSetUpBeforeClass() { public function test_set_props() { $name = 'core/fake'; $args = array( - 'foo' => 'bar', + 'foo' => 'bar', ); $block_type = new WP_Block_Type( $name, $args ); From ace4d440f495b09f55bdd424a46c0078a45a5b5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Fri, 23 Oct 2020 13:46:35 +0200 Subject: [PATCH 09/14] Fix REST_Block_Type_Controller_Test->test_get_item_invalid Because the render_callback is now wrapped by a function at src/wp-includes/class-wp-block-supports.php it'll be callable. So we pass null as the callback and the wrapper won't be attached. --- tests/phpunit/tests/rest-api/rest-block-type-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index 06db6d96759c4..7daaf4b23105b 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -222,7 +222,7 @@ public function test_get_item_invalid() { 'parent' => 'invalid_parent', 'supports' => 'invalid_supports', 'styles' => 'invalid_styles', - 'render_callback' => 'invalid_callback', + 'render_callback' => null, 'textdomain' => true, ); register_block_type( $block_type, $settings ); From 6e51b2fabf84851a839a51e4ff015fb99939fb7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Fri, 23 Oct 2020 14:04:44 +0200 Subject: [PATCH 10/14] Improve the check we use to decide whether to attach the wrapper By using is_callable we make sure that we don't change any callback behavior: - if the original callback is callable => attach the wrapper - if the original callback is not callable => no wrapper --- src/wp-includes/class-wp-block-supports.php | 2 +- tests/phpunit/tests/rest-api/rest-block-type-controller.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-block-supports.php b/src/wp-includes/class-wp-block-supports.php index 474ff85663b18..1cf6c991ebbda 100644 --- a/src/wp-includes/class-wp-block-supports.php +++ b/src/wp-includes/class-wp-block-supports.php @@ -223,7 +223,7 @@ function get_block_wrapper_attributes( $extra_attributes = array() ) { * @return array Block attributes. */ function wp_block_supports_track_block_to_render( $args ) { - if ( null !== $args['render_callback'] ) { + if ( is_callable( $args['render_callback'] ) ) { $block_render_callback = $args['render_callback']; $args['render_callback'] = function( $attributes, $content, $block ) use ( $block_render_callback ) { // Check for null for back compatibility with WP_Block_Type->render diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index 7daaf4b23105b..06db6d96759c4 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -222,7 +222,7 @@ public function test_get_item_invalid() { 'parent' => 'invalid_parent', 'supports' => 'invalid_supports', 'styles' => 'invalid_styles', - 'render_callback' => null, + 'render_callback' => 'invalid_callback', 'textdomain' => true, ); register_block_type( $block_type, $settings ); From 8b5d3c6059a306aed4ecefda8032ae5d8e83432b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Fri, 23 Oct 2020 16:55:11 +0200 Subject: [PATCH 11/14] Let callback receive 2 or 3 parameters --- src/wp-includes/class-wp-block-supports.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/class-wp-block-supports.php b/src/wp-includes/class-wp-block-supports.php index 1cf6c991ebbda..79302b5c9f950 100644 --- a/src/wp-includes/class-wp-block-supports.php +++ b/src/wp-includes/class-wp-block-supports.php @@ -225,22 +225,23 @@ function get_block_wrapper_attributes( $extra_attributes = array() ) { function wp_block_supports_track_block_to_render( $args ) { if ( is_callable( $args['render_callback'] ) ) { $block_render_callback = $args['render_callback']; - $args['render_callback'] = function( $attributes, $content, $block ) use ( $block_render_callback ) { + $args['render_callback'] = function( $attributes, $content, $block = null ) use ( $block_render_callback ) { // Check for null for back compatibility with WP_Block_Type->render // which is unused since the introduction of WP_Block class. // // See: // - https://core.trac.wordpress.org/ticket/49927 - // - commit 910de8f6890c87f93359c6f2edc6c27b9a3f3292 at wordpress-develop - if ( null !== $block ) { - $parent_block = WP_Block_Supports::$block_to_render; - WP_Block_Supports::$block_to_render = $block->parsed_block; - $result = $block_render_callback( $attributes, $content, $block ); - WP_Block_Supports::$block_to_render = $parent_block; - return $result; - } else { + // - commit 910de8f6890c87f93359c6f2edc6c27b9a3f3292 at wordpress-develop. + + if ( null === $block ) { return $block_render_callback( $attributes, $content ); } + + $parent_block = WP_Block_Supports::$block_to_render; + WP_Block_Supports::$block_to_render = $block->parsed_block; + $result = $block_render_callback( $attributes, $content, $block ); + WP_Block_Supports::$block_to_render = $parent_block; + return $result; }; } return $args; From 31c1565552136300e18f968eb2120f5537c82084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Fri, 23 Oct 2020 17:18:36 +0200 Subject: [PATCH 12/14] Remove unnecessary parameter --- src/wp-includes/class-wp-block-type.php | 4 +--- tests/phpunit/tests/blocks/block-type.php | 5 ++--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index bf3fd89ae1f9d..7687d2b9317d9 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -180,8 +180,6 @@ public function __construct( $block_type, $args = array() ) { * * @since 5.0.0 * - * @deprecated 5.6 See WP_Block->render introduced in 5.5. - * * @param array $attributes Optional. Block attributes. Default empty array. * @param string $content Optional. Block content. Default empty string. * @return string Rendered block type output. @@ -193,7 +191,7 @@ public function render( $attributes = array(), $content = '' ) { $attributes = $this->prepare_attributes_for_render( $attributes ); - return (string) call_user_func( $this->render_callback, $attributes, $content, null ); + return (string) call_user_func( $this->render_callback, $attributes, $content ); } /** diff --git a/tests/phpunit/tests/blocks/block-type.php b/tests/phpunit/tests/blocks/block-type.php index a5bd46c22dd8d..c42d8b2844d85 100644 --- a/tests/phpunit/tests/blocks/block-type.php +++ b/tests/phpunit/tests/blocks/block-type.php @@ -70,9 +70,8 @@ public static function wpSetUpBeforeClass() { /** * @ticket 45097 * - * @since 5.6 Removed testing whether render_callback - * is the same as the one passed because we wrap - * it in wp_block_supports_track_block_to_render + * @since 5.6 Removed assertion for render_callback. + * We wrap that function in wp_block_supports_track_block_to_render * so it's not equal. */ public function test_set_props() { From 99fe74341099d7fca1ca730cb5ff80a105cfdaa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Fri, 23 Oct 2020 17:31:10 +0200 Subject: [PATCH 13/14] Update the block_to_render property in WP_Block->render --- src/wp-includes/class-wp-block-supports.php | 36 --------------------- src/wp-includes/class-wp-block.php | 3 ++ src/wp-includes/default-filters.php | 1 - tests/phpunit/tests/blocks/block-type.php | 8 ++--- 4 files changed, 6 insertions(+), 42 deletions(-) diff --git a/src/wp-includes/class-wp-block-supports.php b/src/wp-includes/class-wp-block-supports.php index 79302b5c9f950..27da185a20921 100644 --- a/src/wp-includes/class-wp-block-supports.php +++ b/src/wp-includes/class-wp-block-supports.php @@ -210,39 +210,3 @@ function get_block_wrapper_attributes( $extra_attributes = array() ) { return implode( ' ', $normalized_attributes ); } - - -/** - * Callback hooked to the register_block_type_args filter. - * - * This hooks into block registration to wrap the render_callback - * of dynamic blocks with a closure that keeps track of the - * current block to be rendered. - * - * @param array $args Block attributes. - * @return array Block attributes. - */ -function wp_block_supports_track_block_to_render( $args ) { - if ( is_callable( $args['render_callback'] ) ) { - $block_render_callback = $args['render_callback']; - $args['render_callback'] = function( $attributes, $content, $block = null ) use ( $block_render_callback ) { - // Check for null for back compatibility with WP_Block_Type->render - // which is unused since the introduction of WP_Block class. - // - // See: - // - https://core.trac.wordpress.org/ticket/49927 - // - commit 910de8f6890c87f93359c6f2edc6c27b9a3f3292 at wordpress-develop. - - if ( null === $block ) { - return $block_render_callback( $attributes, $content ); - } - - $parent_block = WP_Block_Supports::$block_to_render; - WP_Block_Supports::$block_to_render = $block->parsed_block; - $result = $block_render_callback( $attributes, $content, $block ); - WP_Block_Supports::$block_to_render = $parent_block; - return $result; - }; - } - return $args; -} diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 325ca6e65c535..2142fbeeb5616 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -214,7 +214,10 @@ public function render( $options = array() ) { if ( $is_dynamic ) { $global_post = $post; + $parent = WP_Block_Supports::$block_to_render; + WP_Block_Supports::$block_to_render = $this; $block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this ); + WP_Block_Supports::$block_to_render = $parent; $post = $global_post; } diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 457517c4b03cb..96ef7d82eb0f0 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -306,7 +306,6 @@ add_action( 'init', '_register_core_block_patterns_and_categories' ); add_action( 'init', 'check_theme_switched', 99 ); add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 ); -add_filter( 'register_block_type_args', 'wp_block_supports_track_block_to_render' ); add_action( 'after_switch_theme', '_wp_menus_changed' ); add_action( 'after_switch_theme', '_wp_sidebars_changed' ); add_action( 'wp_print_styles', 'print_emoji_styles' ); diff --git a/tests/phpunit/tests/blocks/block-type.php b/tests/phpunit/tests/blocks/block-type.php index c42d8b2844d85..e93aed05a2410 100644 --- a/tests/phpunit/tests/blocks/block-type.php +++ b/tests/phpunit/tests/blocks/block-type.php @@ -69,20 +69,18 @@ public static function wpSetUpBeforeClass() { /** * @ticket 45097 - * - * @since 5.6 Removed assertion for render_callback. - * We wrap that function in wp_block_supports_track_block_to_render - * so it's not equal. */ public function test_set_props() { $name = 'core/fake'; $args = array( - 'foo' => 'bar', + 'render_callback' => array( $this, 'render_fake_block' ), + 'foo' => 'bar', ); $block_type = new WP_Block_Type( $name, $args ); $this->assertSame( $name, $block_type->name ); + $this->assertSame( $args['render_callback'], $block_type->render_callback ); $this->assertSame( $args['foo'], $block_type->foo ); } From 958efc8e6220b8b11dc4cac24fd102f7a2e95478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Fri, 23 Oct 2020 17:44:29 +0200 Subject: [PATCH 14/14] Fix typo --- src/wp-includes/class-wp-block.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block.php b/src/wp-includes/class-wp-block.php index 2142fbeeb5616..e2630a451a1c3 100644 --- a/src/wp-includes/class-wp-block.php +++ b/src/wp-includes/class-wp-block.php @@ -215,7 +215,7 @@ public function render( $options = array() ) { if ( $is_dynamic ) { $global_post = $post; $parent = WP_Block_Supports::$block_to_render; - WP_Block_Supports::$block_to_render = $this; + WP_Block_Supports::$block_to_render = $this->parsed_block; $block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this ); WP_Block_Supports::$block_to_render = $parent; $post = $global_post;