wppaste
WordPress

block_core_gallery_render_dynamic_image( int $attachment_id, array $attributes, array $context ): string

Since
7.0.0
Source
wp-includes/blocks/gallery.php:225
Renders a single core/image block for a Gallery block running in dynamic mode, applying the gallery-wide settings that affect how an image renders.

Description

The image markup is generated here (via wp_get_attachment_image()) and rendered through a real core/image block instance so that the image block's own render callback and lightbox behavior run, and so the gallery's existing lightbox/interactivity post-processing can pick it up.

Compatibility

WordPress
since 7.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 1 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$attachment_idint
The image attachment ID.
$attributesarray
The gallery block attributes.
$contextarray
Context to expose to the inner image block.

Return value

string
The rendered image block HTML, or an empty string on failure.

Performance profile

How much work a call to block_core_gallery_render_dynamic_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
Heavy

Reaches the database via get_post().

Scaling
Constant

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

Instructions
21–127

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()one call below block_core_gallery_render_dynamic_image()

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 18 distinct outcomes

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

WhenInstructionsCalls it makes
always21–25wp_get_attachment_image()
$aspect_ratio !== "auto"31–33safecss_filter_attr(), wp_get_attachment_image()
empty($image_attributes) && $caption === ""68–79wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), get_post(), esc_attr(), sprintf(), ->render()
empty($image_attributes) && $caption !== ""75–86wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render()
$aspect_ratio !== "auto" && empty($image_attributes) && $caption === ""78–87safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), get_post(), esc_attr(), sprintf(), ->render()
$aspect_ratio !== "auto" && empty($image_attributes) && $caption !== ""85–94safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render()
!empty($image_attributes) && !isset($image_attributes) && $caption === ""87–98wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), sprintf(), get_post(), esc_attr(), sprintf(), ->render()
!empty($image_attributes) && !isset($image_attributes) && $caption !== ""94–105wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), sprintf(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render()
!empty($image_attributes) && $caption === ""94–105wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), sprintf(), get_post(), esc_attr(), sprintf(), ->render()
$aspect_ratio !== "auto" && !empty($image_attributes) && !isset($image_attributes) && $caption === ""97–106safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), sprintf(), get_post(), esc_attr(), sprintf(), ->render()
!empty($image_attributes) && $caption !== ""101–112wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), sprintf(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render()
!empty($image_attributes) && isset($image_attributes) && $caption === ""101–112wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), esc_attr(), sprintf(), get_post(), esc_attr(), sprintf(), ->render()
6 further outcomes, up to 127 instructions
$aspect_ratio !== "auto" && !empty($image_attributes) && !isset($image_attributes) && $caption !== ""104–113safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), sprintf(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render()
$aspect_ratio !== "auto" && !empty($image_attributes) && $caption === ""104–113safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), sprintf(), get_post(), esc_attr(), sprintf(), ->render()
!empty($image_attributes) && isset($image_attributes) && $caption !== ""108–119wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), esc_attr(), sprintf(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render()
$aspect_ratio !== "auto" && !empty($image_attributes) && $caption !== ""111–120safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), sprintf(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render()
$aspect_ratio !== "auto" && !empty($image_attributes) && isset($image_attributes) && $caption === ""111–120safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), esc_attr(), sprintf(), get_post(), esc_attr(), sprintf(), ->render()
$aspect_ratio !== "auto" && !empty($image_attributes) && isset($image_attributes) && $caption !== ""118–127safecss_filter_attr(), wp_get_attachment_image(), block_core_gallery_dynamic_image_link_attributes(), id(), esc_url(), esc_attr(), esc_attr(), sprintf(), get_post(), wp_kses_post(), esc_attr(), sprintf(), ->render()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev13121–12712
8.513121–12712
8.413121–127122 fewer instructions than PHP 8.3
8.313321–12912
8.213321–12912
8.113321–129123 fewer instructions than PHP 7.4
7.413621–13212

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 · 9

Used by · 1

Source code

function block_core_gallery_render_dynamic_image( $attachment_id, $attributes, $context ) {	$size_slug    = $attributes['sizeSlug'] ?? 'large';	$aspect_ratio = $attributes['aspectRatio'] ?? 'auto'; 	$img_attr = array( 'class' => 'wp-image-' . $attachment_id );	if ( $aspect_ratio && 'auto' !== $aspect_ratio ) {		// Run the aspect ratio through the same sanitization used for every other		// block inline style, so an unsafe value can't break out of the style		// attribute or inject additional markup.		$img_attr['style'] = safecss_filter_attr(			sprintf( 'aspect-ratio:%s;object-fit:cover;', $aspect_ratio )		);	} 	$image_markup = wp_get_attachment_image( $attachment_id, $size_slug, false, $img_attr );	if ( ! $image_markup ) {		return '';	} 	$image_attributes = array_merge(		array(			'id'       => $attachment_id,			'data-id'  => (string) $attachment_id,			'sizeSlug' => $size_slug,		),		block_core_gallery_dynamic_image_link_attributes( $attachment_id, $attributes )	); 	if ( $aspect_ratio && 'auto' !== $aspect_ratio ) {		$image_attributes['aspectRatio'] = $aspect_ratio;		$image_attributes['scale']       = 'cover';	} 	// Wrap in a link when the gallery links images somewhere.	if ( ! empty( $image_attributes['href'] ) ) {		$image_markup = sprintf(			'<a href="%1$s"%2$s%3$s>%4$s</a>',			esc_url( $image_attributes['href'] ),			isset( $image_attributes['linkTarget'] ) ? ' target="' . esc_attr( $image_attributes['linkTarget'] ) . '"' : '',			isset( $image_attributes['rel'] ) ? ' rel="' . esc_attr( $image_attributes['rel'] ) . '"' : '',			$image_markup		);	} 	// Use the raw caption (`post_excerpt`) so the frontend mirrors the editor	// preview, which builds the caption from the REST `caption.raw` value. Gap:	// the REST API exposes no caption run through `wp_get_attachment_caption`, so	// that filter isn't applied here either.	$attachment = get_post( $attachment_id );	$caption    = $attachment ? $attachment->post_excerpt : '';	if ( '' !== $caption ) {		$image_markup .= sprintf(			'<figcaption class="wp-element-caption">%s</figcaption>',			wp_kses_post( $caption )		);	} 	$figure = sprintf(		'<figure class="wp-block-image size-%1$s">%2$s</figure>',		esc_attr( $size_slug ),		$image_markup	); 	$image_block = array(		'blockName'    => 'core/image',		'attrs'        => $image_attributes,		'innerBlocks'  => array(),		'innerHTML'    => $figure,		'innerContent' => array( $figure ),	); 	return ( new WP_Block( $image_block, $context ) )->render();}

Changelog

Introduced in 7.0.0.

Signature, return type and hooks compared across 1 parsed release.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/blocks/gallery.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.