wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



block_core_gallery_render_dynamic_image › WordPress Function

Seit7.0.0
Veraltetn/v
block_core_gallery_render_dynamic_image ( $attachment_id, $attributes, $context )
Parameter: (3)
  • (int) $attachment_id The image attachment ID.
    Erforderlich: Ja
  • (array) $attributes The gallery block attributes.
    Erforderlich: Ja
  • (array) $context Context to expose to the inner image block.
    Erforderlich: Ja
Gibt zurück:
  • (string) The rendered image block HTML, or an empty string on failure.
Definiert in:
Codex:

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.

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.


Quellcode

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();
}