Skip to content

Commit

Permalink
Update how block to render is tracked (#26356)
Browse files Browse the repository at this point in the history
  • Loading branch information
nosolosw authored and oandregal committed Oct 26, 2020
1 parent f798da6 commit 25e25a1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 828 deletions.
42 changes: 36 additions & 6 deletions lib/class-wp-block-supports.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class WP_Block_Supports {
*/
private $block_supports = array();

/**
* Tracks the current block to be rendered.
*
* @var array
*/
public static $block_to_render = null;

/**
* Container for the main instance of the class.
*
Expand Down Expand Up @@ -67,13 +74,12 @@ 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.
*
* @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.
Expand Down Expand Up @@ -144,8 +150,7 @@ private function register_attributes() {
* @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 '';
Expand Down Expand Up @@ -191,4 +196,29 @@ 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;
}

add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 );
add_filter( 'register_block_type_args', 'wp_block_supports_track_block_to_render' );
12 changes: 3 additions & 9 deletions lib/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ public function __get( $name ) {
*/
public function render( $options = array() ) {
global $post;
global $current_parsed_block;
$options = array_replace(
array(
'dynamic' => true,
Expand All @@ -217,14 +216,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();
}
}

Expand Down
37 changes: 0 additions & 37 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,40 +508,3 @@ 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' );
Loading

0 comments on commit 25e25a1

Please sign in to comment.