WP_Block::process_block_bindings(): array

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Processes the block bindings and updates the block attributes with the values from the sources.

Description

A block might contain bindings in its attributes. Bindings are mappings between an attribute of the block and a source. A "source" is a function registered with register_block_bindings_source() that defines how to retrieve a value from outside the block, e.g. from post meta.

This function will process those bindings and update the block’s attributes with the values coming from the bindings.

Example

The "bindings" property for an Image block might look like this:

{
  "metadata": {
    "bindings": {
      "title": {
        "source": "core/post-meta",
        "args": { "key": "text_custom_field" }
      },
      "url": {
        "source": "core/post-meta",
        "args": { "key": "url_custom_field" }
      }
    }
  }
}

The above example will replace the title and url attributes of the Image block with the values of the text_custom_field and url_custom_field post meta.

Return

array The computed block attributes for the provided block bindings.

Source

private function process_block_bindings() {
	$parsed_block               = $this->parsed_block;
	$computed_attributes        = array();
	$supported_block_attributes = array(
		'core/paragraph' => array( 'content' ),
		'core/heading'   => array( 'content' ),
		'core/image'     => array( 'id', 'url', 'title', 'alt' ),
		'core/button'    => array( 'url', 'text', 'linkTarget', 'rel' ),
	);

	// If the block doesn't have the bindings property, isn't one of the supported
	// block types, or the bindings property is not an array, return the block content.
	if (
		! isset( $supported_block_attributes[ $this->name ] ) ||
		empty( $parsed_block['attrs']['metadata']['bindings'] ) ||
		! is_array( $parsed_block['attrs']['metadata']['bindings'] )
	) {
		return $computed_attributes;
	}

	$bindings = $parsed_block['attrs']['metadata']['bindings'];

	/*
	 * If the default binding is set for pattern overrides, replace it
	 * with a pattern override binding for all supported attributes.
	 */
	if (
		isset( $bindings['__default']['source'] ) &&
		'core/pattern-overrides' === $bindings['__default']['source']
	) {
		$updated_bindings = array();

		/*
		 * Build a binding array of all supported attributes.
		 * Note that this also omits the `__default` attribute from the
		 * resulting array.
		 */
		foreach ( $supported_block_attributes[ $parsed_block['blockName'] ] as $attribute_name ) {
			// Retain any non-pattern override bindings that might be present.
			$updated_bindings[ $attribute_name ] = isset( $bindings[ $attribute_name ] )
				? $bindings[ $attribute_name ]
				: array( 'source' => 'core/pattern-overrides' );
		}
		$bindings = $updated_bindings;
	}

	foreach ( $bindings as $attribute_name => $block_binding ) {
		// If the attribute is not in the supported list, process next attribute.
		if ( ! in_array( $attribute_name, $supported_block_attributes[ $this->name ], true ) ) {
			continue;
		}
		// If no source is provided, or that source is not registered, process next attribute.
		if ( ! isset( $block_binding['source'] ) || ! is_string( $block_binding['source'] ) ) {
			continue;
		}

		$block_binding_source = get_block_bindings_source( $block_binding['source'] );
		if ( null === $block_binding_source ) {
			continue;
		}

		$source_args  = ! empty( $block_binding['args'] ) && is_array( $block_binding['args'] ) ? $block_binding['args'] : array();
		$source_value = $block_binding_source->get_value( $source_args, $this, $attribute_name );

		// If the value is not null, process the HTML based on the block and the attribute.
		if ( ! is_null( $source_value ) ) {
			$computed_attributes[ $attribute_name ] = $source_value;
		}
	}

	return $computed_attributes;
}

Changelog

VersionDescription
6.6.0Handle the __default attribute for pattern overrides.
6.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.