Make WordPress Core

Changeset 60798


Ignore:
Timestamp:
09/25/2025 09:00:11 AM (5 months ago)
Author:
Bernhard Reiter
Message:

Block Bindings: Support Image block's caption attribute.

This is now possible thank to the logic added in [60684].

Props bernhard-reiter, mukesh27.
Fixes #64031.

Location:
trunk
Files:
2 edited

Legend:

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

    r60790 r60798  
    110110        'core/paragraph' => array( 'content' ),
    111111        'core/heading'   => array( 'content' ),
    112         'core/image'     => array( 'id', 'url', 'title', 'alt' ),
     112        'core/image'     => array( 'id', 'url', 'title', 'alt', 'caption' ),
    113113        'core/button'    => array( 'url', 'text', 'linkTarget', 'rel' ),
    114114        'core/post-date' => array( 'datetime' ),
  • trunk/tests/phpunit/tests/block-bindings/render.php

    r60720 r60798  
    103103                '<div class="wp-block-button"><a class="wp-block-button__link wp-element-button">test source value</a></div>',
    104104            ),
     105            'image block'     => array(
     106                'caption',
     107                <<<HTML
     108<!-- wp:image {"id":66,"sizeSlug":"large","linkDestination":"none"} -->
     109<figure class="wp-block-image size-large"><img src="breakfast.jpg" alt="" class="wp-image-1"/><figcaption class="wp-element-caption">Breakfast at a <em>café</em> in Wrocław.</figcaption></figure>
     110<!-- /wp:image -->
     111HTML
     112            ,
     113                '<figure class="wp-block-image size-large"><img src="breakfast.jpg" alt="" class="wp-image-1"/><figcaption class="wp-element-caption">test source value</figcaption></figure>',
     114            ),
    105115            'test block'      => array(
    106116                'myAttribute',
     
    283293     * for the Image block in the placeholder state.
    284294     *
     295     * Furthermore tests if the caption attribute is correctly processed.
     296     *
    285297     * @ticket 60282
     298     * @ticket 64031
    286299     *
    287300     * @covers ::register_block_bindings_source
    288301     */
    289302    public function test_update_block_with_value_from_source_image_placeholder() {
    290         $get_value_callback = function () {
    291             return 'https://example.com/image.jpg';
     303        $get_value_callback = function ( $source_args, $block_instance, $attribute_name ) {
     304            if ( 'url' === $attribute_name ) {
     305                return 'https://example.com/image.jpg';
     306            }
     307            if ( 'caption' === $attribute_name ) {
     308                return 'Example Image';
     309            }
    292310        };
    293311
     
    301319
    302320        $block_content = <<<HTML
    303 <!-- wp:image {"metadata":{"bindings":{"url":{"source":"test/source"}}}} -->
    304 <figure class="wp-block-image"><img alt=""/></figure>
     321<!-- wp:image {"metadata":{"bindings":{"url":{"source":"test/source"},"caption":{"source":"test/source"}}}} -->
     322<figure class="wp-block-image"><img alt=""/><figcaption class="wp-element-caption"></figcaption></figure>
    305323<!-- /wp:image -->
    306324HTML;
     
    315333        );
    316334        $this->assertSame(
    317             '<figure class="wp-block-image"><img src="https://example.com/image.jpg" alt=""/></figure>',
     335            'Example Image',
     336            $block->attributes['caption'],
     337            "The 'caption' attribute should be updated with the value returned by the source."
     338        );
     339        $this->assertSame(
     340            '<figure class="wp-block-image"><img src="https://example.com/image.jpg" alt=""/><figcaption class="wp-element-caption">Example Image</figcaption></figure>',
    318341            trim( $result ),
    319342            'The block content should be updated with the value returned by the source.'
Note: See TracChangeset for help on using the changeset viewer.