Changeset 57641
- Timestamp:
- 02/16/2024 12:53:16 PM (5 months ago)
- Location:
- trunk
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-bindings.php
r57562 r57641 76 76 * The array of arguments that are used to register a source. 77 77 * 78 * @type string $label The label of the source.79 * @type callback $get_value_callback A callback executed when the source is processed during block rendering.80 * The callback should have the following signature:78 * @type string $label The label of the source. 79 * @type callback $get_value_callback A callback executed when the source is processed during block rendering. 80 * The callback should have the following signature: 81 81 * 82 * `function ($source_args, $block_instance,$attribute_name): mixed` 83 * - @param array $source_args Array containing source arguments 84 * used to look up the override value, 85 * i.e. {"key": "foo"}. 86 * - @param WP_Block $block_instance The block instance. 87 * - @param string $attribute_name The name of an attribute . 88 * The callback has a mixed return type; it may return a string to override 89 * the block's original value, null, false to remove an attribute, etc. 82 * `function ($source_args, $block_instance,$attribute_name): mixed` 83 * - @param array $source_args Array containing source arguments 84 * used to look up the override value, 85 * i.e. {"key": "foo"}. 86 * - @param WP_Block $block_instance The block instance. 87 * - @param string $attribute_name The name of an attribute . 88 * The callback has a mixed return type; it may return a string to override 89 * the block's original value, null, false to remove an attribute, etc. 90 * @type array $uses_context (optional) Array of values to add to block `uses_context` needed by the source. 90 91 * } 91 92 * @return WP_Block_Bindings_Source|false Source when the registration was successful, or `false` on failure. -
trunk/src/wp-includes/block-bindings/pattern-overrides.php
r57585 r57641 40 40 'label' => _x( 'Pattern Overrides', 'block bindings source' ), 41 41 'get_value_callback' => '_block_bindings_pattern_overrides_get_value', 42 42 43 ) 43 44 ); -
trunk/src/wp-includes/block-bindings/post-meta.php
r57526 r57641 14 14 * @access private 15 15 * 16 * @param array $source_args Array containing source arguments used to look up the override value. 17 * Example: array( "key" => "foo" ). 16 * @param array $source_args Array containing source arguments used to look up the override value. 17 * Example: array( "key" => "foo" ). 18 * @param WP_Block $block_instance The block instance. 18 19 * @return mixed The value computed for the source. 19 20 */ 20 function _block_bindings_post_meta_get_value( array $source_args ) {21 if ( ! isset( $source_args['key'] ) ) {21 function _block_bindings_post_meta_get_value( array $source_args ) { 22 if ( ( $source_args['key'] ) ) { 22 23 return null; 23 24 } 24 25 25 // Use the postId attribute if available. 26 if ( isset( $source_args['postId'] ) ) { 27 $post_id = $source_args['postId']; 28 } else { 29 // $block_instance->context['postId'] is not available in the Image block. 30 $post_id = get_the_ID(); 26 if ( empty( $block_instance->context['postId'] ) ) { 27 return null; 31 28 } 29 32 30 33 31 // If a post isn't public, we need to prevent unauthorized users from accessing the post meta. … … 52 50 'label' => _x( 'Post Meta', 'block bindings source' ), 53 51 'get_value_callback' => '_block_bindings_post_meta_get_value', 52 54 53 ) 55 54 ); -
trunk/src/wp-includes/class-wp-block-bindings-registry.php
r57575 r57641 34 34 35 35 /** 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 / 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 / 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 / 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 135 180 '6.5.0' 136 181 ); … … 145 190 $this->sources[ $source_name ] = $source; 146 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 147 206 return $source; 148 207 } -
trunk/src/wp-includes/class-wp-block-bindings-source.php
r57562 r57641 47 47 48 48 /** 49 50 51 52 53 54 55 56 49 57 * Constructor. 50 58 * … … 58 66 */ 59 67 public function __construct( string $name, array $source_properties ) { 60 $this->name = $name; 61 $this->label = $source_properties['label']; 62 $this->get_value_callback = $source_properties['get_value_callback']; 68 $this->name = $name; 69 foreach ( $source_properties as $property_name => $property_value ) { 70 $this->$property_name = $property_value; 71 } 63 72 } 64 73 -
trunk/src/wp-includes/class-wp-block-type.php
r57565 r57641 181 181 * @var string[] 182 182 */ 183 p ublic$uses_context = array();183 p $uses_context = array(); 184 184 185 185 /** … … 367 367 } 368 368 369 370 371 372 369 373 if ( ! in_array( $name, $this->deprecated_properties, true ) ) { 370 374 return; … … 395 399 */ 396 400 public function __isset( $name ) { 397 if ( 'variations' === $name) {401 if ( ) { 398 402 return true; 399 403 } … … 418 422 */ 419 423 public function __set( $name, $value ) { 420 if ( 'variations' === $name ) {421 $this->variations = $value;422 return;423 }424 425 424 if ( ! in_array( $name, $this->deprecated_properties, true ) ) { 426 425 $this->{$name} = $value; … … 617 616 return apply_filters( 'get_block_type_variations', $this->variations, $this ); 618 617 } 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 619 637 } -
trunk/src/wp-includes/class-wp-block.php
r57576 r57641 232 232 */ 233 233 private function process_block_bindings() { 234 $parsed_block = $this->parsed_block; 235 236 $computed_attributes = array(); 237 238 // Allowed blocks that support block bindings. 239 // TODO: Look for a mechanism to opt-in for this. Maybe adding a property to block attributes? 240 $allowed_blocks = array( 234 $parsed_block = $this->parsed_block; 235 $computed_attributes = array(); 236 $supported_block_attributes = array( 241 237 'core/paragraph' => array( 'content' ), 242 238 'core/heading' => array( 'content' ), … … 245 241 ); 246 242 247 // If the block doesn't have the bindings property, isn't one of the allowed243 // If the block doesn't have the bindings property, isn't one of the ed 248 244 // block types, or the bindings property is not an array, return the block content. 249 245 if ( 250 ! isset( $ allowed_blocks[ $this->name ] ) ||246 ! isset( $s[ $this->name ] ) || 251 247 empty( $parsed_block['attrs']['metadata']['bindings'] ) || 252 248 ! is_array( $parsed_block['attrs']['metadata']['bindings'] ) … … 256 252 257 253 foreach ( $parsed_block['attrs']['metadata']['bindings'] as $attribute_name => $block_binding ) { 258 // If the attribute is not in the allowed list, process next attribute.259 if ( ! in_array( $attribute_name, $ allowed_blocks[ $this->name ], true ) ) {254 // If the attribute is not in the ed list, process next attribute. 255 if ( ! in_array( $attribute_name, $s[ $this->name ], true ) ) { 260 256 continue; 261 257 } -
trunk/tests/phpunit/tests/block-bindings/render.php
r57574 r57641 116 116 117 117 /** 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 118 161 * Tests if the block content is updated with the value returned by the source 119 162 * for the Image block in the placeholder state. -
trunk/tests/phpunit/tests/block-bindings/wpBlockBindingsRegistry.php
r57562 r57641 40 40 return 'test-value'; 41 41 }, 42 42 43 ); 43 44 } … … 158 159 159 160 self::$test_source_properties['get_value_callback'] = 'not-a-callback'; 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 160 178 161 179 $result = $this->registry->register( self::$test_source_name, self::$test_source_properties ); … … 180 198 $result 181 199 ); 200 201 202 203 204 205 206 182 207 } 183 208 … … 322 347 $this->assertTrue( $result ); 323 348 } 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 324 394 }
Note: See TracChangeset
for help on using the changeset viewer.