wpseek.com
A WordPress-centric search engine for devs and theme authors
render_block_core_image › WordPress Function
Since5.9.0
Deprecatedn/a
› render_block_core_image ( $attributes, $content, $block )
| Parameters: (3) |
|
| Returns: |
|
| Defined at: |
|
| Codex: |
Renders the `core/image` block on the server, adding a data-id attribute to the element if core/gallery has added on pre-render.
Source
function render_block_core_image( $attributes, $content, $block ) {
if ( false === stripos( $content, '<img' ) ) {
return '';
}
$processor = new class( $content ) extends WP_HTML_Tag_Processor {
/**
* Return input span for an empty FIGCAPTION element.
*
* Returns span of input for an empty FIGCAPTION, if currently matched on a
* FIGCAPTION opening tag and if the element is properly closed and empty.
*
* @since 6.9.0
*
* @return WP_HTML_Span|false Span of input if the element is empty; otherwise false.
*/
public function block_core_image_extract_empty_figcaption_element() {
$this->set_bookmark( 'here' );
$opener = $this->bookmarks['here'];
// Allow comments within the definition of “empty.”
while ( $this->next_token() && '#comment' === $this->get_token_name() ) {
continue;
}
if ( 'FIGCAPTION' !== $this->get_tag() || ! $this->is_tag_closer() ) {
return false;
}
$this->set_bookmark( 'here' );
$closer = $this->bookmarks['here'];
return new WP_HTML_Span( $opener->start, $closer->start + $closer->length - $opener->start );
}
};
if ( ! $processor->next_tag( 'img' ) || ! $processor->get_attribute( 'src' ) ) {
return '';
}
$has_id_binding = isset( $attributes['metadata']['bindings']['id'] ) && isset( $attributes['id'] );
// Ensure the `wp-image-id` classname on the image block supports block bindings.
if ( $has_id_binding ) {
// If there's a mismatch with the 'wp-image-' class and the actual id, the id was
// probably overridden by block bindings. Update it to the correct value.
// See https://github.com/WordPress/gutenberg/issues/62886 for why this is needed.
$id = $attributes['id'];
$image_classnames = $processor->get_attribute( 'class' );
$class_with_binding_value = "wp-image-$id";
if ( is_string( $image_classnames ) && ! str_contains( $image_classnames, $class_with_binding_value ) ) {
$image_classnames = preg_replace( '/wp-image-(\d+)/', $class_with_binding_value, $image_classnames );
$processor->set_attribute( 'class', $image_classnames );
}
}
// For backwards compatibility, the data-id html attribute is only set for
// image blocks nested in a gallery. Detect if the image is in a gallery by
// checking the data-id attribute.
// See the `block_core_gallery_data_id_backcompatibility` function.
if ( isset( $attributes['data-id'] ) ) {
// If there's a binding for the `id`, the `id` attribute is used for the
// value, since `data-id` does not support block bindings.
// Else the `data-id` is used for backwards compatibility, since
// third parties may be filtering its value.
$data_id = $has_id_binding ? $attributes['id'] : $attributes['data-id'];
$processor->set_attribute( 'data-id', $data_id );
}
/*
* If the `caption` attribute is empty and we encounter a `<figcaption>` element,
* we take note of its span so we can remove it later.
*/
if ( $processor->next_tag( 'FIGCAPTION' ) && empty( $attributes['caption'] ) ) {
$figcaption_span = $processor->block_core_image_extract_empty_figcaption_element();
}
$link_destination = isset( $attributes['linkDestination'] ) ? $attributes['linkDestination'] : 'none';
$lightbox_settings = block_core_image_get_lightbox_settings( $block->parsed_block );
/*
* If the lightbox is enabled and the image is not linked, adds the filter and
* the JavaScript view file.
*/
if (
isset( $lightbox_settings ) &&
'none' === $link_destination &&
isset( $lightbox_settings['enabled'] ) &&
true === $lightbox_settings['enabled']
) {
wp_enqueue_script_module( '@wordpress/block-library/image/view' );
/*
* This render needs to happen in a filter with priority 15 to ensure that
* it runs after the duotone filter and that duotone styles are applied to
* the image in the lightbox. Lightbox has to work with any plugins that
* might use filters as well. Removing this can be considered in the future
* if the way the blocks are rendered changes, or if a new kind of filter is
* introduced.
*/
add_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15, 2 );
} else {
/*
* Remove the filter if previously added by other Image blocks.
*/
remove_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15 );
}
$output = $processor->get_updated_html();
if ( ! empty( $figcaption_span ) ) {
return substr( $output, 0, $figcaption_span->start ) . substr( $output, $figcaption_span->start + $figcaption_span->length );
}
return $output;
}