Make WordPress Core

Changeset 58972


Ignore:
Timestamp:
09/03/2024 04:31:44 PM (2 years ago)
Author:
cbravobernal
Message:

Block bindings: Adds a filter to customize the output of a block bindings source.

Introduces a filter to the block_bindings_source_value to allow developers to
modify the value returned by any block binding source.

Props snehapatil02, cbravobernal, gziolo, santosguillamot, bacoords, codersantosh.
Fixes #61181.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block-bindings-source.php

    r58075 r58972  
    22/**
    33 * Block Bindings API: WP_Block_Bindings_Source class.
    4  *
    54 *
    65 * @package WordPress
     
    6160         * @since 6.5.0
    6261         *
    63          * @param string $name               The name of the source.
    64          * @param array  $source_properties  The properties of the source.
     62         * @param string $name              The name of the source.
     63         * @param array  $source_properties The properties of the source.
    6564         */
    6665        public function __construct( string $name, array $source_properties ) {
     
    7271
    7372        /**
    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.
    7576         *
    7677         * @since 6.5.0
     78         * @since 6.7.0 `block_bindings_source_value` filter was added.
    7779         *
    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.
    8283         * @return mixed The value of the source.
    8384         */
    8485        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 );
    8699        }
    87100
  • trunk/tests/phpunit/tests/block-bindings/postMetaSource.php

    r57754 r58972  
    267267                );
    268268        }
     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        }
    269306}
  • trunk/tests/phpunit/tests/block-bindings/render.php

    r58398 r58972  
    299299                );
    300300        }
     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 -->
     331HTML;
     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        }
    301344}
Note: See TracChangeset for help on using the changeset viewer.