Changeset 58972
- Timestamp:
- 09/03/2024 04:31:44 PM (5 weeks ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-block-bindings-source.php
r58075 r58972 2 2 /** 3 3 * Block Bindings API: WP_Block_Bindings_Source class. 4 *5 4 * 6 5 * @package WordPress … … 61 60 * @since 6.5.0 62 61 * 63 * @param string $name 64 * @param array $source_properties 62 * @param string $name The name of the source. 63 * @param array $source_properties The properties of the source. 65 64 */ 66 65 public function __construct( string $name, array $source_properties ) { … … 72 71 73 72 /** 74 * Retrieves the value from the source. 73 * Calls the callback function specified in the `$get_value_callback` property 74 * with the given arguments and returns the result. It can be modified with 75 * `block_bindings_source_value` filter. 75 76 * 76 77 * @since 6.5.0 78 * @since 6.7.0 `block_bindings_source_value` filter was added. 77 79 * 78 * @param array $source_args Array containing source arguments used to look up the override value, i.e. {"key": "foo"}. 79 * @param WP_Block $block_instance The block instance. 80 * @param string $attribute_name The name of the target attribute. 81 * 80 * @param array $source_args Array containing source arguments used to look up the override value, i.e. {"key": "foo"}. 81 * @param WP_Block $block_instance The block instance. 82 * @param string $attribute_name The name of the target attribute. 82 83 * @return mixed The value of the source. 83 84 */ 84 85 public function get_value( array $source_args, $block_instance, string $attribute_name ) { 85 return call_user_func_array( $this->get_value_callback, array( $source_args, $block_instance, $attribute_name ) ); 86 $value = call_user_func_array( $this->get_value_callback, array( $source_args, $block_instance, $attribute_name ) ); 87 /** 88 * Filters the output of a block bindings source. 89 * 90 * @since 6.7.0 91 * 92 * @param mixed $value The computed value for the source. 93 * @param string $name The name of the source. 94 * @param array $source_args Array containing source arguments used to look up the override value, i.e. { "key": "foo" }. 95 * @param WP_Block $block_instance The block instance. 96 * @param string $attribute_name The name of an attribute. 97 */ 98 return apply_filters( 'block_bindings_source_value', $value, $this->name, $source_args, $block_instance, $attribute_name ); 86 99 } 87 100 -
trunk/tests/phpunit/tests/block-bindings/postMetaSource.php
r57754 r58972 267 267 ); 268 268 } 269 270 /** 271 * Tests that filter `block_bindings_source_value` is applied. 272 * 273 * @ticket 61181 274 */ 275 public function test_filter_block_bindings_source_value() { 276 register_meta( 277 'post', 278 'tests_filter_field', 279 array( 280 'show_in_rest' => true, 281 'single' => true, 282 'type' => 'string', 283 'default' => 'Original value', 284 ) 285 ); 286 287 $filter_value = function ( $value, $source_name, $source_args ) { 288 if ( 'core/post-meta' !== $source_name ) { 289 return $value; 290 } 291 return "Filtered value: {$source_args['key']}"; 292 }; 293 294 add_filter( 'block_bindings_source_value', $filter_value, 10, 3 ); 295 296 $content = $this->get_modified_post_content( '<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"core/post-meta","args":{"key":"tests_filter_field"}}}}} --><p>Fallback value</p><!-- /wp:paragraph -->' ); 297 298 remove_filter( 'block_bindings_source_value', $filter_value ); 299 300 $this->assertSame( 301 '<p>Filtered value: tests_filter_field</p>', 302 $content, 303 'The post content should show the filtered value.' 304 ); 305 } 269 306 } -
trunk/tests/phpunit/tests/block-bindings/render.php
r58398 r58972 299 299 ); 300 300 } 301 302 /** 303 * Tests that filter `block_bindings_source_value` is applied. 304 * 305 * @ticket 61181 306 */ 307 public function test_filter_block_bindings_source_value() { 308 register_block_bindings_source( 309 self::SOURCE_NAME, 310 array( 311 'label' => self::SOURCE_LABEL, 312 'get_value_callback' => function () { 313 return ''; 314 }, 315 ) 316 ); 317 318 $filter_value = function ( $value, $source_name, $source_args, $block_instance, $attribute_name ) { 319 if ( self::SOURCE_NAME !== $source_name ) { 320 return $value; 321 } 322 return "Filtered value: {$source_args['test_key']}. Block instance: {$block_instance->name}. Attribute name: {$attribute_name}."; 323 }; 324 325 add_filter( 'block_bindings_source_value', $filter_value, 10, 5 ); 326 327 $block_content = <<<HTML 328 <!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"test/source", "args":{"test_key":"test_arg"}}}}} --> 329 <p>Default content</p> 330 <!-- /wp:paragraph --> 331 HTML; 332 $parsed_blocks = parse_blocks( $block_content ); 333 $block = new WP_Block( $parsed_blocks[0] ); 334 $result = $block->render(); 335 336 remove_filter( 'block_bindings_source_value', $filter_value ); 337 338 $this->assertSame( 339 '<p>Filtered value: test_arg. Block instance: core/paragraph. Attribute name: content.</p>', 340 trim( $result ), 341 'The block content should show the filtered value.' 342 ); 343 } 301 344 }
Note: See TracChangeset
for help on using the changeset viewer.