From 84cb8ed7e385a9a83b316820031ff3513f0fce4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Wed, 21 Oct 2020 10:31:13 +0200 Subject: [PATCH 1/4] Hook into the render callback of blocks --- lib/compat.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/compat.php b/lib/compat.php index 2c4dfb9033c103..2ab325c8b3e3a4 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -518,3 +518,14 @@ function gutenberg_override_reusable_block_post_type_labels() { ); } add_filter( 'post_type_labels_wp_block', 'gutenberg_override_reusable_block_post_type_labels', 10, 0 ); + +function gutenberg_check_render_callback( $args, $block_name ){ + if ( NULL !== $args['render_callback'] ) { + $block_render_callback = $args['render_callback']; + $args['render_callback'] = function( $attributes, $content, $block ) use ( $block_render_callback ) { + return $block_render_callback( $attributes, $content, $block ); + }; + } + return $args; +} +add_filter( 'register_block_type_args', 'gutenberg_check_render_callback', 10, 2 ); From 1e2c7f7fe1ac523327ca094eb2eb52edfb5a4b4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Wed, 21 Oct 2020 10:34:43 +0200 Subject: [PATCH 2/4] Keep track of the current parsed block --- lib/compat.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/compat.php b/lib/compat.php index 2ab325c8b3e3a4..61738218e42cc6 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -523,7 +523,12 @@ function gutenberg_check_render_callback( $args, $block_name ){ if ( NULL !== $args['render_callback'] ) { $block_render_callback = $args['render_callback']; $args['render_callback'] = function( $attributes, $content, $block ) use ( $block_render_callback ) { - return $block_render_callback( $attributes, $content, $block ); + global $current_parsed_block; + $parent_parsed_block = $current_parsed_block; + $current_parsed_block = $block->parsed_block; + $result = $block_render_callback( $attributes, $content, $block ); + $current_parsed_block = $parent_parsed_block; + return $result; }; } return $args; From 3f6a46c933ac7aee17c22549c183f4f66adaa936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Wed, 21 Oct 2020 10:53:16 +0200 Subject: [PATCH 3/4] Rename, clarify, and refactor --- lib/compat.php | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/compat.php b/lib/compat.php index 61738218e42cc6..c23171d00be36a 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -366,18 +366,11 @@ function gutenberg_replace_default_block_categories( $default_categories ) { } add_filter( 'block_categories', 'gutenberg_replace_default_block_categories' ); -global $current_parsed_block; -$current_parsed_block = array( - 'blockName' => null, - 'attributes' => null, -); - /** * Shim that hooks into `pre_render_block` so as to override `render_block` with * a function that assigns block context. * * The context handling can be removed when plugin support requires WordPress 5.5.0+. - * The global current_parsed_block assignment can be removed when plugin support requires WordPress 5.6.0+. * * @see https://core.trac.wordpress.org/ticket/49927 * @see https://core.trac.wordpress.org/changeset/48243 @@ -389,7 +382,6 @@ function gutenberg_replace_default_block_categories( $default_categories ) { */ function gutenberg_render_block_with_assigned_block_context( $pre_render, $parsed_block ) { global $post, $wp_query; - global $current_parsed_block; /* * If a non-null value is provided, a filter has run at an earlier priority @@ -399,8 +391,6 @@ function gutenberg_render_block_with_assigned_block_context( $pre_render, $parse return $pre_render; } - $current_parsed_block = $parsed_block; - $source_block = $parsed_block; /** This filter is documented in src/wp-includes/blocks.php */ @@ -519,7 +509,28 @@ function gutenberg_override_reusable_block_post_type_labels() { } add_filter( 'post_type_labels_wp_block', 'gutenberg_override_reusable_block_post_type_labels', 10, 0 ); -function gutenberg_check_render_callback( $args, $block_name ){ +global $current_parsed_block; +$current_parsed_block = array( + 'blockName' => null, + 'attributes' => null, +); + +/** + * Wraps the render_callback of dynamic blocks to keep track + * of the current block being rendered via a global variable + * called $current_parsed_block. + * + * This is for get_block_wrapper_attributes to get access + * to the runtime data of the block being rendered. + * + * This shim can be removed when the plugin requires WordPress 5.6. + * + * @since 9.2.1 + * + * @param array $args Block attributes. + * @return array Block attributes. + */ +function gutenberg_current_parsed_block_tracking( $args ){ if ( NULL !== $args['render_callback'] ) { $block_render_callback = $args['render_callback']; $args['render_callback'] = function( $attributes, $content, $block ) use ( $block_render_callback ) { @@ -533,4 +544,4 @@ function gutenberg_check_render_callback( $args, $block_name ){ } return $args; } -add_filter( 'register_block_type_args', 'gutenberg_check_render_callback', 10, 2 ); +add_filter( 'register_block_type_args', 'gutenberg_current_parsed_block_tracking' ); From 304e9ba9291d6a3c6815b7dbf819f1d9c3f8b830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Wed, 21 Oct 2020 11:06:54 +0200 Subject: [PATCH 4/4] Make linter happy --- lib/compat.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/compat.php b/lib/compat.php index c23171d00be36a..5779460ec61da5 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -530,9 +530,9 @@ function gutenberg_override_reusable_block_post_type_labels() { * @param array $args Block attributes. * @return array Block attributes. */ -function gutenberg_current_parsed_block_tracking( $args ){ - if ( NULL !== $args['render_callback'] ) { - $block_render_callback = $args['render_callback']; +function gutenberg_current_parsed_block_tracking( $args ) { + if ( null !== $args['render_callback'] ) { + $block_render_callback = $args['render_callback']; $args['render_callback'] = function( $attributes, $content, $block ) use ( $block_render_callback ) { global $current_parsed_block; $parent_parsed_block = $current_parsed_block;