wppaste
WordPress

block_core_gallery_render( array $attributes, string $content ): string

Since
6.0.0
Source
wp-includes/blocks/gallery.php:46
Renders the core/gallery block on the server.

Compatibility

WordPress
since 6.0.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
Attributes of the block being rendered.
$contentstring
Content of the block being rendered.

Return value

string
The content of the block being rendered.

Performance profile

How much work a call to block_core_gallery_render() 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
5

Executed per call on PHP 8.5. The body compiles to 166.

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 · 1 distinct outcome

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

WhenInstructionsCalls it makes
always5none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev166522
8.5166522
8.416652218 fewer instructions than PHP 8.3
8.3184522
8.2184522
8.11845223 fewer instructions than PHP 7.4
7.4187522

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

Source code

function block_core_gallery_render( $attributes, $content ) {	// Adds a style tag for the --wp--style--unstable-gallery-gap var.	// The Gallery block needs to recalculate Image block width based on	// the current gap setting in order to maintain the number of flex columns	// so a css var is added to allow this. 	$gap = $attributes['style']['spacing']['blockGap'] ?? null;	// Skip if gap value contains unsupported characters.	// Regex for CSS value borrowed from `safecss_filter_attr`, and used here	// because we only want to match against the value, not the CSS attribute.	if ( is_array( $gap ) ) {		foreach ( $gap as $key => $value ) {			// Make sure $value is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null.			$value = is_string( $value ) ? $value : '';			$value = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value; 			// Get spacing CSS variable from preset value if provided.			if ( is_string( $value ) && str_contains( $value, 'var:preset|spacing|' ) ) {				$index_to_splice = strrpos( $value, '|' ) + 1;				$slug            = _wp_to_kebab_case( substr( $value, $index_to_splice ) );				$value           = "var(--wp--preset--spacing--$slug)";			} 			$gap[ $key ] = $value;		}	} else {		// Make sure $gap is a string to avoid PHP 8.1 deprecation error in preg_match() when the value is null.		$gap = is_string( $gap ) ? $gap : '';		$gap = $gap && preg_match( '%[\\\(&=}]|/\*%', $gap ) ? null : $gap; 		// Get spacing CSS variable from preset value if provided.		if ( is_string( $gap ) && str_contains( $gap, 'var:preset|spacing|' ) ) {			$index_to_splice = strrpos( $gap, '|' ) + 1;			$slug            = _wp_to_kebab_case( substr( $gap, $index_to_splice ) );			$gap             = "var(--wp--preset--spacing--$slug)";		}	} 	$unique_gallery_classname = wp_unique_id( 'wp-block-gallery-' );	$processed_content        = new WP_HTML_Tag_Processor( $content );	$processed_content->next_tag();	$processed_content->add_class( $unique_gallery_classname ); 	// --gallery-block--gutter-size is deprecated. --wp--style--gallery-gap-default should be used by themes that want to set a default	// gap on the gallery.	$fallback_gap = 'var( --wp--style--gallery-gap-default, var( --gallery-block--gutter-size, var( --wp--style--block-gap, 0.5em ) ) )';	$gap_value    = $gap ? $gap : $fallback_gap;	$gap_column   = $gap_value; 	if ( is_array( $gap_value ) ) {		$gap_row    = isset( $gap_value['top'] ) ? $gap_value['top'] : $fallback_gap;		$gap_column = isset( $gap_value['left'] ) ? $gap_value['left'] : $fallback_gap;		$gap_value  = $gap_row === $gap_column ? $gap_row : $gap_row . ' ' . $gap_column;	} 	// The unstable gallery gap calculation requires a real value (such as `0px`) and not `0`.	if ( '0' === $gap_column ) {		$gap_column = '0px';	} 	// Set the CSS variable to the column value, and the `gap` property to the combined gap value.	$gallery_styles = array(		array(			'selector'     => ".wp-block-gallery.{$unique_gallery_classname}",			'declarations' => array(				'--wp--style--unstable-gallery-gap' => $gap_column,				'gap'                               => $gap_value,			),		),	); 	wp_style_engine_get_stylesheet_from_css_rules(		$gallery_styles,		array(			'context' => 'block-supports',		)	); 	// The WP_HTML_Tag_Processor class calls get_updated_html() internally	// when the instance is treated as a string, but here we explicitly

Changelog

Introduced in 6.0.0. One change between 6.7.10 and 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.

7.0.7
Parameter $block added.verified against source
6.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.11 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.