Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port changes from core PR #26417

Merged
merged 4 commits into from
Oct 26, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/class-wp-block-supports.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,25 @@ 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 ) {
mcsf marked this conversation as resolved.
Show resolved Hide resolved
$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 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor stylistic point: I'd prefer the early-return method (which we tend to prefer in JS too) over the if-else pair. And that way we also avoid double negatives like !== null:

if ( null === $block ) {
  return $block_render_callback( $attributes, $content );
}

$parent_block = …

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra advantage for early-return – now the main logic branch is at the top execution level and the error handling is an obvious detail at a deeper level.

P.S. Sorry for the drive-by comment… :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nb: I still remember Else considered harmful. ;)

}
};
}
return $args;
Expand Down