wppaste
WordPress

block_core_gallery_resolve_dynamic_source( array $source, WP_Block $block ): int[]

Since
7.0.0
Source
wp-includes/blocks/gallery.php:108
Resolves a Gallery block's dynamicContent to an ordered list of image attachment IDs.

Description

The source key is the dispatch discriminator and args holds the source's parameters. This { source, args } shape mirrors the Block Bindings metadata shape so dynamic mode can migrate to an innerBlocks binding with minimal change. core/attached-media is a context-relative anchor (the post the gallery is rendered within); future sources translate their REST-named args (author, categories, after/before, media_type, etc.) into WP_Query arguments here.

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

$sourcearray
The gallery's dynamicContent attribute.
$blockWP_Block
The gallery block instance being rendered.

Return value

int[]
Ordered list of image attachment IDs.

Performance profile

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

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

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()one call below block_core_gallery_resolve_dynamic_source()

Further down the call graph this can also reach hook, 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 · 4 distinct outcomes

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

WhenInstructionsCalls it makes
always5–24none
is_array($source) && $source_name == "core/attached-media"22–27get_the_ID()
is_array($source) && $source_name == "core/attached-media"51–60strtoupper(), post_parent(), array_map()
is_array($source) && $source_name == "core/attached-media"54–63get_the_ID(), strtoupper(), post_parent(), array_map()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev675–62111 fewer instruction than PHP 8.5
8.5685–6311
8.4685–6311
8.3685–6311
8.2685–6311
8.1685–63111 fewer instruction than PHP 7.4
7.4695–6411

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

Used by · 1

Source code

function block_core_gallery_resolve_dynamic_source( $source, $block ) {	if ( ! is_array( $source ) ) {		return array();	} 	$source_name = $source['source'] ?? null;	$args        = isset( $source['args'] ) && is_array( $source['args'] ) ? $source['args'] : array(); 	switch ( $source_name ) {		case 'core/attached-media':			// Prefer the post supplied via block context, falling back to the post			// being rendered. The fallback is what lets a post-bound template (e.g.			// `single`/`page`) resolve against the actual post at render time even			// though the editor has no concrete post to preview — the editor gates			// the dynamic-mode UI on that same context (see `use-dynamic-gallery.js`).			$post_id = $block->context['postId'] ?? get_the_ID();			if ( ! $post_id ) {				return array();			} 			// Map the camelCase `args` (block-attribute convention) to WP_Query			// names, defaulting to the same order as the editor preview (see			// `dynamic-source.js`). Only REST-supported orderby values are			// allowed; `menu_order` is intentionally unsupported (it isn't a			// valid media REST `orderby`).			$orderby = $args['orderBy'] ?? 'date';			if ( ! in_array( $orderby, array( 'date', 'title' ), true ) ) {				$orderby = 'date';			}			$order = strtoupper( $args['order'] ?? 'desc' ) === 'ASC' ? 'ASC' : 'DESC'; 			// Bound the number of resolved images until the gallery supports			// pagination. Kept in sync with the editor query's `per_page` cap; a			// case-insensitive grep for `max_images` finds both this and			// `MAX_IMAGES` in `dynamic-source.js`.			$max_images = 100; 			$query = new WP_Query(				array(					'post_parent'    => $post_id,					'post_type'      => 'attachment',					'post_status'    => 'inherit',					'post_mime_type' => 'image',					'orderby'        => $orderby,					'order'          => $order,					'posts_per_page' => $max_images,					'fields'         => 'ids',					'no_found_rows'  => true,				)			); 			return array_map( 'intval', $query->posts );	} 	// Unknown or not-yet-implemented source type.	return array();}

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.