diff --git a/lib/compat.php b/lib/compat.php index 2c4dfb9033c10..5779460ec61da 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 */ @@ -518,3 +508,40 @@ function gutenberg_override_reusable_block_post_type_labels() { ); } add_filter( 'post_type_labels_wp_block', 'gutenberg_override_reusable_block_post_type_labels', 10, 0 ); + +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 ) { + 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; +} +add_filter( 'register_block_type_args', 'gutenberg_current_parsed_block_tracking' );