- Timestamp:
- 02/16/2024 12:53:16 PM (13 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-block-bindings-registry.php
r57575 r57641 34 34 35 35 /** 36 * Supported source properties that can be passed to the registered source. 37 * 38 * @since 6.5.0 39 * @var array 40 */ 41 private $allowed_source_properties = array( 42 'label', 43 'get_value_callback', 44 'uses_context', 45 ); 46 47 /** 48 * Supported blocks that can use the block bindings API. 49 * 50 * @since 6.5.0 51 * @var array 52 */ 53 private $supported_blocks = array( 54 'core/paragraph', 55 'core/heading', 56 'core/image', 57 'core/button', 58 ); 59 60 /** 36 61 * Registers a new block bindings source. 37 62 * … … 54 79 * The array of arguments that are used to register a source. 55 80 * 56 * @type string $label The label of the source. 57 * @type callback $get_value_callback A callback executed when the source is processed during block rendering. 58 * The callback should have the following signature: 59 * 60 * `function ($source_args, $block_instance,$attribute_name): mixed` 61 * - @param array $source_args Array containing source arguments 62 * used to look up the override value, 63 * i.e. {"key": "foo"}. 64 * - @param WP_Block $block_instance The block instance. 65 * - @param string $attribute_name The name of the target attribute. 66 * The callback has a mixed return type; it may return a string to override 67 * the block's original value, null, false to remove an attribute, etc. 81 * @type string $label The label of the source. 82 * @type callback $get_value_callback A callback executed when the source is processed during block rendering. 83 * The callback should have the following signature: 84 * 85 * `function ($source_args, $block_instance,$attribute_name): mixed` 86 * - @param array $source_args Array containing source arguments 87 * used to look up the override value, 88 * i.e. {"key": "foo"}. 89 * - @param WP_Block $block_instance The block instance. 90 * - @param string $attribute_name The name of the target attribute. 91 * The callback has a mixed return type; it may return a string to override 92 * the block's original value, null, false to remove an attribute, etc. 93 * @type array $uses_context (optional) Array of values to add to block `uses_context` needed by the source. 68 94 * } 69 95 * @return WP_Block_Bindings_Source|false Source when the registration was successful, or `false` on failure. … … 108 134 } 109 135 110 / * Validate that the source properties contain the label */136 // Validates that the source properties contain the label. 111 137 if ( ! isset( $source_properties['label'] ) ) { 112 138 _doing_it_wrong( … … 118 144 } 119 145 120 / * Validate that the source properties contain the get_value_callback */146 // Validates that the source properties contain the get_value_callback. 121 147 if ( ! isset( $source_properties['get_value_callback'] ) ) { 122 148 _doing_it_wrong( … … 128 154 } 129 155 130 / * Validate that the get_value_callback is a valid callback */156 // Validates that the get_value_callback is a valid callback. 131 157 if ( ! is_callable( $source_properties['get_value_callback'] ) ) { 132 158 _doing_it_wrong( 133 159 __METHOD__, 134 160 __( 'The "get_value_callback" parameter must be a valid callback.' ), 161 '6.5.0' 162 ); 163 return false; 164 } 165 166 // Validates that the uses_context parameter is an array. 167 if ( isset( $source_properties['uses_context'] ) && ! is_array( $source_properties['uses_context'] ) ) { 168 _doing_it_wrong( 169 __METHOD__, 170 __( 'The "uses_context" parameter must be an array.' ), 171 '6.5.0' 172 ); 173 return false; 174 } 175 176 if ( ! empty( array_diff( array_keys( $source_properties ), $this->allowed_source_properties ) ) ) { 177 _doing_it_wrong( 178 __METHOD__, 179 __( 'The $source_properties array contains invalid properties.' ), 135 180 '6.5.0' 136 181 ); … … 145 190 $this->sources[ $source_name ] = $source; 146 191 192 // Adds `uses_context` defined by block bindings sources. 193 add_filter( 194 'get_block_type_uses_context', 195 function ( $uses_context, $block_type ) use ( $source ) { 196 if ( ! in_array( $block_type->name, $this->supported_blocks, true ) || empty( $source->uses_context ) ) { 197 return $uses_context; 198 } 199 // Use array_values to reset the array keys. 200 return array_values( array_unique( array_merge( $uses_context, $source->uses_context ) ) ); 201 }, 202 10, 203 2 204 ); 205 147 206 return $source; 148 207 }
Note: See TracChangeset
for help on using the changeset viewer.