Make WordPress Core


Ignore:
Timestamp:
09/03/2024 04:31:44 PM (6 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.