Skip to content

Commit ecdab39

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@60798 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 98af3c5 commit ecdab39

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/wp-includes/class-wp-block.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class WP_Block {
109109
private const BLOCK_BINDINGS_SUPPORTED_ATTRIBUTES = array(
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' ),
115115
);

tests/phpunit/tests/block-bindings/render.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ public function data_update_block_with_value_from_source() {
102102
,
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 -->
111+
HTML
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',
107117
<<<HTML
@@ -282,13 +292,21 @@ public function test_passing_uses_context_to_source() {
282292
* Tests if the block content is updated with the value returned by the source
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

294312
register_block_bindings_source(
@@ -300,8 +318,8 @@ public function test_update_block_with_value_from_source_image_placeholder() {
300318
);
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 -->
306324
HTML;
307325
$parsed_blocks = parse_blocks( $block_content );
@@ -314,7 +332,12 @@ public function test_update_block_with_value_from_source_image_placeholder() {
314332
"The 'url' attribute should be updated with the value returned by the source."
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.'
320343
);

0 commit comments

Comments
 (0)