wppaste
WordPress

WP_Block::process_block_bindings(): array

Since
6.5.0, 6.6.0, 6.7.0
Source
wp-includes/class-wp-block.php:279
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.

Compatibility

WordPress
since 6.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

array
The computed block attributes for the provided block bindings.

Performance profile

How much work a call to WP_Block::process_block_bindings() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
10–46

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

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 one call costs · 2 distinct outcomes

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

WhenInstructionsCalls it makes
always10–32get_block_bindings_supported_attributes()
!empty($supported_block_attributes) && !empty($value) && isset($value)44–46get_block_bindings_supported_attributes(), bindings()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10610–4621
8.510610–4621
8.410610–46213 fewer instructions than PHP 8.3
8.310910–4621
8.210910–4621
8.110910–46211 fewer instruction than PHP 7.4
7.411010–4621

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.

Used by · 1

Source code

	private function process_block_bindings() {		$block_type                 = $this->name;		$parsed_block               = $this->parsed_block;		$computed_attributes        = array();		$supported_block_attributes = get_block_bindings_supported_attributes( $block_type ); 		// 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 (			empty( $supported_block_attributes ) ||			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 as $attribute_name ) {				// Retain any non-pattern override bindings that might be present.				$updated_bindings[ $attribute_name ] = $bindings[ $attribute_name ] ?? array( 'source' => 'core/pattern-overrides' );			}			$bindings = $updated_bindings;			/*			 * Update the bindings metadata of the computed attributes.			 * This ensures the block receives the expanded __default binding metadata when it renders.			 */			$computed_attributes['metadata'] = array_merge(				$parsed_block['attrs']['metadata'],				array( 'bindings' => $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, 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;			} 			// Adds the necessary context defined by the source.			if ( ! empty( $block_binding_source->uses_context ) ) {				foreach ( $block_binding_source->uses_context as $context_name ) {					if ( array_key_exists( $context_name, $this->available_context ) ) {						$this->context[ $context_name ] = $this->available_context[ $context_name ];					}				}			} 			$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;			}		}

Changelog

Introduced in 6.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.7.0
Return any updated bindings metadata in the computed attributes.from the docblock
6.6.0
Handle the __default attribute for pattern overrides.from the docblock
6.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-block.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.