From 487741ed17d5b49f31c8b4080d463a82efbc8e0f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 3 Sep 2025 11:05:21 +0200 Subject: [PATCH 1/8] Image block: Save `
` if `caption` attr is bound --- packages/block-library/src/image/save.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/image/save.js b/packages/block-library/src/image/save.js index 69c26cde52a9cd..64d1d46195c4c3 100644 --- a/packages/block-library/src/image/save.js +++ b/packages/block-library/src/image/save.js @@ -31,6 +31,7 @@ export default function save( { attributes } ) { linkTarget, sizeSlug, title, + metadata: { bindings = {} } = {}, } = attributes; const newRel = ! rel ? undefined : rel; @@ -70,6 +71,8 @@ export default function save( { attributes } ) { /> ); + const displayCaption = ! RichText.isEmpty( caption ) || bindings.caption; + const figure = ( <> { href ? ( @@ -84,7 +87,7 @@ export default function save( { attributes } ) { ) : ( image ) } - { ! RichText.isEmpty( caption ) && ( + { displayCaption && ( Date: Wed, 3 Sep 2025 11:09:44 +0200 Subject: [PATCH 2/8] Block Bindings: Support Image block's caption attr --- packages/block-editor/src/utils/block-bindings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-editor/src/utils/block-bindings.js b/packages/block-editor/src/utils/block-bindings.js index e67ee74648da8c..f7f44e2f599430 100644 --- a/packages/block-editor/src/utils/block-bindings.js +++ b/packages/block-editor/src/utils/block-bindings.js @@ -14,7 +14,7 @@ const PATTERN_OVERRIDES_SOURCE = 'core/pattern-overrides'; const BLOCK_BINDINGS_ALLOWED_BLOCKS = { 'core/paragraph': [ 'content' ], 'core/heading': [ 'content' ], - 'core/image': [ 'id', 'url', 'title', 'alt' ], + 'core/image': [ 'id', 'url', 'title', 'alt', 'caption' ], 'core/button': [ 'url', 'text', 'linkTarget', 'rel' ], 'core/post-date': [ 'datetime' ], }; From 87afa9cad2a1d826e507436086512d428fddcd11 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 3 Sep 2025 12:16:29 +0200 Subject: [PATCH 3/8] Image Block: Save figcaption if block has pattern overrides --- packages/block-library/src/image/save.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/image/save.js b/packages/block-library/src/image/save.js index 64d1d46195c4c3..5bc29dff1f0453 100644 --- a/packages/block-library/src/image/save.js +++ b/packages/block-library/src/image/save.js @@ -71,7 +71,10 @@ export default function save( { attributes } ) { /> ); - const displayCaption = ! RichText.isEmpty( caption ) || bindings.caption; + const displayCaption = + ! RichText.isEmpty( caption ) || + bindings.caption || + bindings?.__default?.source === 'core/pattern-overrides'; const figure = ( <> From b21bbc77072a9c065e7eaca1c3d8c48bdf531d3f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 3 Sep 2025 12:17:00 +0200 Subject: [PATCH 4/8] Image block: Enable caption control if attribute is bound --- packages/block-library/src/image/image.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/image/image.js b/packages/block-library/src/image/image.js index 83e107b459b26d..fc51d0c1eaa6a4 100644 --- a/packages/block-library/src/image/image.js +++ b/packages/block-library/src/image/image.js @@ -625,7 +625,7 @@ export default function Image( { lockAltControlsMessage, lockTitleControls = false, lockTitleControlsMessage, - lockCaption = false, + hideCaptionControls = false, } = useSelect( ( select ) => { if ( ! isSingleSelected ) { @@ -635,6 +635,7 @@ export default function Image( { url: urlBinding, alt: altBinding, title: titleBinding, + caption: captionBinding, } = metadata?.bindings || {}; const hasParentPattern = !! context[ 'pattern/overrides' ]; const urlBindingSource = getBlockBindingsSource( @@ -658,10 +659,7 @@ export default function Image( { // Disable editing the link of the URL if the image is inside a pattern instance. // This is a temporary solution until we support overriding the link on the frontend. hasParentPattern || arePatternOverridesEnabled, - lockCaption: - // Disable editing the caption if the image is inside a pattern instance. - // This is a temporary solution until we support overriding the caption on the frontend. - hasParentPattern, + hideCaptionControls: !! captionBinding, lockAltControls: !! altBinding && ! altBindingSource?.canUserEditValue?.( { @@ -1147,10 +1145,9 @@ export default function Image( { label={ __( 'Image caption text' ) } showToolbarButton={ isSingleSelected && - hasNonContentControls && - ! arePatternOverridesEnabled + ( hasNonContentControls || isContentOnlyMode ) && + ! hideCaptionControls } - readOnly={ lockCaption } /> ); From f9103e5c999a35e26cdf190a0dcfa6286b035c6d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 15 Sep 2025 14:00:50 +0200 Subject: [PATCH 5/8] Block Bindings: Support Image block caption attribute --- lib/compat/wordpress-6.9/block-bindings.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.9/block-bindings.php b/lib/compat/wordpress-6.9/block-bindings.php index 9880f98b24da1f..41bef0470f5018 100644 --- a/lib/compat/wordpress-6.9/block-bindings.php +++ b/lib/compat/wordpress-6.9/block-bindings.php @@ -8,7 +8,19 @@ */ -// The following filter can be removed once the minimum required WordPress version is 6.9 or newer. +// The following filters can be removed once the minimum required WordPress version is 6.9 or newer. +add_filter( + 'block_bindings_supported_attributes_core/image', + function ( $attributes ) { + if ( ! in_array( 'caption', $attributes, true ) ) { + $attributes[] = 'caption'; + } + return $attributes; + }, + 10, + 3 +); + add_filter( 'block_bindings_supported_attributes_core/post-date', function ( $attributes ) { From bfd6b8d56ac2d7973478c4490b7ffd6e88143208 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 15 Sep 2025 14:17:27 +0200 Subject: [PATCH 6/8] Add backport changelog --- backport-changelog/6.9/9702.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 backport-changelog/6.9/9702.md diff --git a/backport-changelog/6.9/9702.md b/backport-changelog/6.9/9702.md new file mode 100644 index 00000000000000..310cbe927f68a1 --- /dev/null +++ b/backport-changelog/6.9/9702.md @@ -0,0 +1,3 @@ +https://github.com/WordPress/wordpress-develop/pull/9702 + +* https://github.com/WordPress/gutenberg/pull/71483 \ No newline at end of file From 91ca74b4c28622875c6eac43f43ee2dc257272ea Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 16 Sep 2025 11:59:27 +0200 Subject: [PATCH 7/8] Add test fixture --- .../core__image__caption-block-bindings.html | 3 +++ .../core__image__caption-block-bindings.json | 22 +++++++++++++++++++ ..._image__caption-block-bindings.parsed.json | 22 +++++++++++++++++++ ...ge__caption-block-bindings.serialized.html | 3 +++ 4 files changed, 50 insertions(+) create mode 100644 test/integration/fixtures/blocks/core__image__caption-block-bindings.html create mode 100644 test/integration/fixtures/blocks/core__image__caption-block-bindings.json create mode 100644 test/integration/fixtures/blocks/core__image__caption-block-bindings.parsed.json create mode 100644 test/integration/fixtures/blocks/core__image__caption-block-bindings.serialized.html diff --git a/test/integration/fixtures/blocks/core__image__caption-block-bindings.html b/test/integration/fixtures/blocks/core__image__caption-block-bindings.html new file mode 100644 index 00000000000000..a1656e48b91570 --- /dev/null +++ b/test/integration/fixtures/blocks/core__image__caption-block-bindings.html @@ -0,0 +1,3 @@ + +
+ diff --git a/test/integration/fixtures/blocks/core__image__caption-block-bindings.json b/test/integration/fixtures/blocks/core__image__caption-block-bindings.json new file mode 100644 index 00000000000000..c862b5024d87cd --- /dev/null +++ b/test/integration/fixtures/blocks/core__image__caption-block-bindings.json @@ -0,0 +1,22 @@ +[ + { + "name": "core/image", + "isValid": true, + "attributes": { + "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==", + "alt": "", + "caption": "", + "metadata": { + "bindings": { + "caption": { + "source": "core/post-data", + "args": { + "key": "date" + } + } + } + } + }, + "innerBlocks": [] + } +] diff --git a/test/integration/fixtures/blocks/core__image__caption-block-bindings.parsed.json b/test/integration/fixtures/blocks/core__image__caption-block-bindings.parsed.json new file mode 100644 index 00000000000000..adc42578314962 --- /dev/null +++ b/test/integration/fixtures/blocks/core__image__caption-block-bindings.parsed.json @@ -0,0 +1,22 @@ +[ + { + "blockName": "core/image", + "attrs": { + "metadata": { + "bindings": { + "caption": { + "source": "core/post-data", + "args": { + "key": "date" + } + } + } + } + }, + "innerBlocks": [], + "innerHTML": "\n
\"\"
\n", + "innerContent": [ + "\n
\"\"
\n" + ] + } +] diff --git a/test/integration/fixtures/blocks/core__image__caption-block-bindings.serialized.html b/test/integration/fixtures/blocks/core__image__caption-block-bindings.serialized.html new file mode 100644 index 00000000000000..71f01b95ecf5ce --- /dev/null +++ b/test/integration/fixtures/blocks/core__image__caption-block-bindings.serialized.html @@ -0,0 +1,3 @@ + +
+ From 3b103b262ac9c5da30ba420e8020ad518305a04b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 16 Sep 2025 12:05:45 +0200 Subject: [PATCH 8/8] Use new block-agnostic filter --- lib/compat/wordpress-6.9/block-bindings.php | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/lib/compat/wordpress-6.9/block-bindings.php b/lib/compat/wordpress-6.9/block-bindings.php index 41bef0470f5018..f46a2dce398ce6 100644 --- a/lib/compat/wordpress-6.9/block-bindings.php +++ b/lib/compat/wordpress-6.9/block-bindings.php @@ -8,29 +8,20 @@ */ -// The following filters can be removed once the minimum required WordPress version is 6.9 or newer. +// The following filter can be removed once the minimum required WordPress version is 6.9 or newer. add_filter( - 'block_bindings_supported_attributes_core/image', - function ( $attributes ) { - if ( ! in_array( 'caption', $attributes, true ) ) { + 'block_bindings_supported_attributes', + function ( $attributes, $block_type ) { + if ( 'core/image' === $block_type && ! in_array( 'caption', $attributes, true ) ) { $attributes[] = 'caption'; } - return $attributes; - }, - 10, - 3 -); - -add_filter( - 'block_bindings_supported_attributes_core/post-date', - function ( $attributes ) { - if ( ! in_array( 'datetime', $attributes, true ) ) { + if ( 'core/post-date' === $block_type && ! in_array( 'datetime', $attributes, true ) ) { $attributes[] = 'datetime'; } return $attributes; }, 10, - 3 + 2 ); /**