wpseek.com
A WordPress-centric search engine for devs and theme authors



block_core_gallery_resolve_dynamic_source › WordPress Function

Since7.0.0
Deprecatedn/a
block_core_gallery_resolve_dynamic_source ( $source, $block )
Parameters: (2)
  • (array) $source The gallery's `dynamicContent` attribute.
    Required: Yes
  • (WP_Block) $block The gallery block instance being rendered.
    Required: Yes
Returns:
  • (int[]) Ordered list of image attachment IDs.
Defined at:
Codex:

Resolves a Gallery block's `dynamicContent` to an ordered list of image attachment IDs.

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.


Source

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