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

Add block supports render hook #984

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
43 changes: 43 additions & 0 deletions src/wp-includes/class-wp-block-supports.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ public static function init() {
$instance->register_attributes();
}

/**
* Applies the render_block filter.
*
* @param string $block_content Rendered block content.
* @param array $block Block object.
* @return string Filtered block content.
*/
public static function render( $block_content, $block ) {
$instance = self::get_instance();
return $instance->render_block_supports( $block_content, $block );
}

/**
* Registers a block support.
*
Expand Down Expand Up @@ -155,6 +167,37 @@ private function register_attributes() {
}
}
}

/**
* Modify the rendered block content with additional filters.
*
* @param string $block_content Rendered block content.
* @param array $block Block object.
* @return string Filtered block content.
*/
public function render_block_supports( $block_content, $block ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
$block_attributes = $block['attrs'];

if ( ! $block_type ) {
return $block_content;
}

foreach ( $this->block_supports as $name => $block_support_config ) {
if ( ! isset( $block_support_config['render_block'] ) ) {
continue;
}

$block_content = call_user_func(
$block_support_config['render_block'],
Copy link
Contributor

Choose a reason for hiding this comment

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

So far the block supports were only used to actually add classnames or inline styles to the wrapper, this PR is changing that by allowing them to register a generic hook for render_block.

The use-case for these is to potentially support static blocks (adding style and classes to static blocks that don't explicitly call the get_block_wrapper_attributes but also to allow adding style tags in addition to the block markup. These are needed by two Gutenberg PRs. WordPress/gutenberg#26752 and WordPress/gutenberg#29335

It's not entirely clear why this hook is better than just using render_block directly though.

Curious though about your thoughts here @mcsf @mtias

Copy link

Choose a reason for hiding this comment

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

A bit late... but I see that the parent PR (WordPress/gutenberg#26752) is progressing nicely without this. Yes, I'm more in favour of not adding a new indirect API, just recommend render_block. If later we find that we have more compelling use cases for custom server rendering in block-supports, we can reassess.

$block_type,
$block_attributes,
$block_content
);
}

return $block_content;
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@
add_action( 'init', '_register_core_block_patterns_and_categories' );
add_action( 'init', 'check_theme_switched', 99 );
add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 );
add_filter( 'render_block', array( 'WP_Block_Supports', 'render' ), 10, 2 );
add_action( 'after_switch_theme', '_wp_menus_changed' );
add_action( 'after_switch_theme', '_wp_sidebars_changed' );
add_action( 'wp_print_styles', 'print_emoji_styles' );
Expand Down