Generates the render output for the block.
Parameters
$options
arrayoptional- Optional options object.
dynamic
boolDefaults to'true'
. Optionally set to false to avoid using the block’s render_callback.
Default:
array()
Source
public function render( $options = array() ) {
global $post;
/*
* There can be only one root interactive block at a time because the rendered HTML of that block contains
* the rendered HTML of all its inner blocks, including any interactive block.
*/
static $root_interactive_block = null;
/**
* Filters whether Interactivity API should process directives.
*
* @since 6.6.0
*
* @param bool $enabled Whether the directives processing is enabled.
*/
$interactivity_process_directives_enabled = apply_filters( 'interactivity_process_directives', true );
if (
$interactivity_process_directives_enabled && null === $root_interactive_block && (
( isset( $this->block_type->supports['interactivity'] ) && true === $this->block_type->supports['interactivity'] ) ||
! empty( $this->block_type->supports['interactivity']['interactive'] )
)
) {
$root_interactive_block = $this;
}
$options = wp_parse_args(
$options,
array(
'dynamic' => true,
)
);
// Process the block bindings and get attributes updated with the values from the sources.
$computed_attributes = $this->process_block_bindings();
if ( ! empty( $computed_attributes ) ) {
// Merge the computed attributes with the original attributes.
$this->attributes = array_merge( $this->attributes, $computed_attributes );
}
$is_dynamic = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic();
$block_content = '';
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 {
$inner_block = $this->inner_blocks[ $index ];
$parent_block = $this;
/** This filter is documented in wp-includes/blocks.php */
$pre_render = apply_filters( 'pre_render_block', null, $inner_block->parsed_block, $parent_block );
if ( ! is_null( $pre_render ) ) {
$block_content .= $pre_render;
} else {
$source_block = $inner_block->parsed_block;
/** This filter is documented in wp-includes/blocks.php */
$inner_block->parsed_block = apply_filters( 'render_block_data', $inner_block->parsed_block, $source_block, $parent_block );
/** This filter is documented in wp-includes/blocks.php */
$inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block );
$block_content .= $inner_block->render();
}
++$index;
}
}
}
if ( ! empty( $computed_attributes ) && ! empty( $block_content ) ) {
foreach ( $computed_attributes as $attribute_name => $source_value ) {
$block_content = $this->replace_html( $block_content, $attribute_name, $source_value );
}
}
if ( $is_dynamic ) {
$global_post = $post;
$parent = WP_Block_Supports::$block_to_render;
WP_Block_Supports::$block_to_render = $this->parsed_block;
$block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this );
WP_Block_Supports::$block_to_render = $parent;
$post = $global_post;
}
if ( ( ! empty( $this->block_type->script_handles ) ) ) {
foreach ( $this->block_type->script_handles as $script_handle ) {
wp_enqueue_script( $script_handle );
}
}
if ( ! empty( $this->block_type->view_script_handles ) ) {
foreach ( $this->block_type->view_script_handles as $view_script_handle ) {
wp_enqueue_script( $view_script_handle );
}
}
if ( ! empty( $this->block_type->view_script_module_ids ) ) {
foreach ( $this->block_type->view_script_module_ids as $view_script_module_id ) {
wp_enqueue_script_module( $view_script_module_id );
}
}
if ( ( ! empty( $this->block_type->style_handles ) ) ) {
foreach ( $this->block_type->style_handles as $style_handle ) {
wp_enqueue_style( $style_handle );
}
}
if ( ( ! empty( $this->block_type->view_style_handles ) ) ) {
foreach ( $this->block_type->view_style_handles as $view_style_handle ) {
wp_enqueue_style( $view_style_handle );
}
}
/**
* Filters the content of a single block.
*
* @since 5.0.0
* @since 5.9.0 The `$instance` parameter was added.
*
* @param string $block_content The block content.
* @param array $block The full block, including name and attributes.
* @param WP_Block $instance The block instance.
*/
$block_content = apply_filters( 'render_block', $block_content, $this->parsed_block, $this );
/**
* Filters the content of a single block.
*
* The dynamic portion of the hook name, `$name`, refers to
* the block name, e.g. "core/paragraph".
*
* @since 5.7.0
* @since 5.9.0 The `$instance` parameter was added.
*
* @param string $block_content The block content.
* @param array $block The full block, including name and attributes.
* @param WP_Block $instance The block instance.
*/
$block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block, $this );
if ( $root_interactive_block === $this ) {
// The root interactive block has finished rendering. Time to process directives.
$block_content = wp_interactivity_process_directives( $block_content );
$root_interactive_block = null;
}
return $block_content;
}
Hooks
- apply_filters( ‘interactivity_process_directives’,
bool $enabled ) Filters whether Interactivity API should process directives.
- apply_filters( ‘pre_render_block’,
string|null $pre_render ,array $parsed_block ,WP_Block|null $parent_block ) Allows render_block() to be short-circuited, by returning a non-null value.
- apply_filters( ‘render_block’,
string $block_content ,array $block ,WP_Block $instance ) Filters the content of a single block.
- apply_filters( ‘render_block_context’,
array $context ,array $parsed_block ,WP_Block|null $parent_block ) Filters the default context provided to a rendered block.
- apply_filters( ‘render_block_data’,
array $parsed_block ,array $source_block ,WP_Block|null $parent_block ) Filters the block being rendered in render_block() , before it’s processed.
- apply_filters( “render_block_{$this->name}”,
string $block_content ,array $block ,WP_Block $instance ) Filters the content of a single block.
User Contributed Notes
You must log in before being able to contribute a note or feedback.