wppaste
WordPress

render_block_core_image( array $attributes, string $content, WP_Block $block ): string

Since
5.9.0
Source
wp-includes/blocks/image.php:20
Renders the core/image block on the server, adding a data-id attribute to the element if core/gallery has added on pre-render.

Compatibility

WordPress
since 5.9.0
PHP
7.4–8.6-dev
  • 6.7.10
  • 6.8.11
  • 6.9.10
  • 7.0.7
  • 7.1.3

Present in every tracked release (6.7.10 to 7.1.3), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$attributesarray
The block attributes.
$contentstring
The block content.
$blockWP_Block
The block object.

Return value

string
The block content with the data-id attribute added.

Performance profile

How much work a call to render_block_core_image() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
10–88

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 97.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 13 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost render_block_core_image() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always10stripos()
!->next_tag()18stripos(), ->next_tag()
->next_tag() && !->get_attribute()22stripos(), ->next_tag(), ->get_attribute()
->next_tag() && ->get_attribute() && !isset($attributes)49–58stripos(), ->next_tag(), ->get_attribute(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html()
->next_tag() && ->get_attribute() && isset($attributes)56–66stripos(), ->next_tag(), ->get_attribute(), ->set_attribute(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html()
->next_tag() && ->get_attribute() && !isset($attributes)57–68stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html()
->next_tag() && ->get_attribute() && !isset($attributes) && isset($lightbox_settings) && $link_destination === "none"61–63stripos(), ->next_tag(), ->get_attribute(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html()
->next_tag() && ->get_attribute()64–76stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->set_attribute(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html()
->next_tag() && ->get_attribute() && isset($attributes) && isset($lightbox_settings) && $link_destination === "none"68–71stripos(), ->next_tag(), ->get_attribute(), ->set_attribute(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html()
->next_tag() && ->get_attribute() && !isset($attributes) && isset($lightbox_settings) && $link_destination === "none"69–73stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html()
->next_tag() && ->get_attribute() && is_string($image_classnames) && !$image_classnames && isset($attributes)73–83stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->set_attribute(), ->set_attribute(), block_core_image_get_lightbox_settings(), remove_filter(), ->get_updated_html()
->next_tag() && ->get_attribute() && isset($lightbox_settings) && $link_destination === "none"76–81stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->set_attribute(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html()
1 further outcome, up to 88 instructions
->next_tag() && ->get_attribute() && is_string($image_classnames) && !$image_classnames && isset($attributes) && isset($lightbox_settings) && $link_destination === "none"85–88stripos(), ->next_tag(), ->get_attribute(), ->get_attribute(), ->set_attribute(), ->set_attribute(), block_core_image_get_lightbox_settings(), wp_enqueue_script_module(), add_filter(), ->get_updated_html()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9710–8814
8.59710–8814
8.49710–88146 fewer instructions than PHP 8.3
8.310310–9414
8.210310–9414
8.110310–94143 fewer instructions than PHP 7.4
7.410610–9614

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 7

Source code

function render_block_core_image( $attributes, $content, $block ) {	if ( false === stripos( $content, '<img' ) ) {		return '';	} 	$p = new WP_HTML_Tag_Processor( $content ); 	if ( ! $p->next_tag( 'img' ) || ! $p->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         = $p->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 );			$p->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'];		$p->set_attribute( 'data-id', $data_id );	} 	$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 );	} 	return $p->get_updated_html();}

Changelog

Introduced in 5.9.0. Unchanged from 6.7.10 through 7.1.3.

  1. 6.7.10
  2. 6.8.11
  3. 6.9.10
  4. 7.0.7
  5. 7.1.3

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.10 tag, from src/wp-includes/blocks/image.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.