Skip to content

Commit

Permalink
Fix block supports for inner blocks (#26344)
Browse files Browse the repository at this point in the history
  • Loading branch information
nosolosw authored and youknowriad committed Oct 22, 2020
1 parent 3a840ca commit bfe6060
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 */
Expand Down Expand Up @@ -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' );

0 comments on commit bfe6060

Please sign in to comment.