Skip to content

Commit

Permalink
Add caption to post featured image
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinan committed Jan 4, 2023
1 parent 2addaea commit 557bb56
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ Display a post's featured image. ([Source](https://github.com/WordPress/gutenber
- **Name:** core/post-featured-image
- **Category:** theme
- **Supports:** align (center, full, left, right, wide), color (~~background~~, ~~text~~), spacing (margin, padding), ~~html~~
- **Attributes:** customGradient, customOverlayColor, dimRatio, gradient, height, isLink, linkTarget, overlayColor, rel, scale, sizeSlug, width
- **Attributes:** caption, customGradient, customOverlayColor, dimRatio, gradient, height, isLink, linkTarget, overlayColor, rel, scale, sizeSlug, width

## Post Navigation Link

Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/post-featured-image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
},
"customGradient": {
"type": "string"
},
"caption": {
"type": "string"
}
},
"usesContext": [ "postId", "postType", "queryId" ],
Expand Down
78 changes: 75 additions & 3 deletions packages/block-library/src/post-featured-image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Placeholder,
Button,
TextControl,
ToolbarButton,
} from '@wordpress/components';
import {
InspectorControls,
Expand All @@ -23,12 +24,16 @@ import {
MediaReplaceFlow,
useBlockProps,
store as blockEditorStore,
__experimentalGetElementClassName,
__experimentalUseBorderProps as useBorderProps,
RichText,
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
import { upload } from '@wordpress/icons';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';
import { upload, caption as captionIcon } from '@wordpress/icons';
import { store as noticesStore } from '@wordpress/notices';

import { useEffect, useState, useCallback } from '@wordpress/element';
import { usePrevious } from '@wordpress/compose';
/**
* Internal dependencies
*/
Expand All @@ -47,10 +52,12 @@ function PostFeaturedImageDisplay( {
clientId,
attributes,
setAttributes,
isSelected,
insertBlocksAfter,
context: { postId, postType: postTypeSlug, queryId },
} ) {
const isDescendentOfQueryLoop = Number.isFinite( queryId );
const { isLink, height, width, scale, sizeSlug, rel, linkTarget } =
const { isLink, height, width, scale, sizeSlug, rel, linkTarget, caption } =
attributes;
const [ featuredImage, setFeaturedImage ] = useEntityProp(
'postType',
Expand All @@ -59,6 +66,32 @@ function PostFeaturedImageDisplay( {
postId
);

const prevCaption = usePrevious( caption );
const [ showCaption, setShowCaption ] = useState( !! caption );
// We need to show the caption when changes come from
// history navigation(undo/redo).
useEffect( () => {
if ( caption && ! prevCaption ) {
setShowCaption( true );
}
}, [ caption, prevCaption ] );

// Focus the caption when we click to add one.
const captionRef = useCallback(
( node ) => {
if ( node && ! caption ) {
node.focus();
}
},
[ caption ]
);

useEffect( () => {
if ( ! isSelected && ! caption ) {
setShowCaption( false );
}
}, [ isSelected, caption ] );

const { media, postType } = useSelect(
( select ) => {
const { getMedia, getPostType } = select( coreStore );
Expand Down Expand Up @@ -121,6 +154,23 @@ function PostFeaturedImageDisplay( {

const controls = (
<>
<BlockControls group="block">
<ToolbarButton
onClick={ () => {
setShowCaption( ! showCaption );
if ( showCaption && caption ) {
setAttributes( { caption: undefined } );
}
} }
icon={ captionIcon }
isPressed={ showCaption }
label={
showCaption
? __( 'Remove caption' )
: __( 'Add caption' )
}
/>
</BlockControls>
<DimensionControls
clientId={ clientId }
attributes={ attributes }
Expand Down Expand Up @@ -262,6 +312,28 @@ function PostFeaturedImageDisplay( {
setAttributes={ setAttributes }
clientId={ clientId }
/>
{ showCaption &&
( ! RichText.isEmpty( caption ) || isSelected ) && (
<RichText
tagName="figcaption"
className={ __experimentalGetElementClassName(
'caption'
) }
aria-label={ __( 'Image caption text' ) }
ref={ captionRef }
placeholder={ __( 'Add caption' ) }
value={ caption }
onChange={ ( value ) =>
setAttributes( { caption: value } )
}
inlineToolbar
__unstableOnSplitAtEnd={ () =>
insertBlocksAfter(
createBlock( getDefaultBlockName() )
)
}
/>
) }
</figure>
</>
);
Expand Down
8 changes: 7 additions & 1 deletion packages/block-library/src/post-featured-image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
$size_slug = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'post-thumbnail';
$attr = get_block_core_post_featured_image_border_attributes( $attributes );
$overlay_markup = get_block_core_post_featured_image_overlay_element_markup( $attributes );
$caption = isset( $attributes['caption'] ) ? $attributes['caption'] : '';

if ( $is_link ) {
$attr['alt'] = trim( strip_tags( get_the_title( $post_ID ) ) );
Expand Down Expand Up @@ -61,11 +62,16 @@ function render_block_core_post_featured_image( $attributes, $content, $block )
$featured_image = $featured_image . $overlay_markup;
}

$image_caption = '';
if ( $caption ) {
$image_caption = '<figcaption class="wp-element-caption">' . $caption . '</figcaption>';
}

$wrapper_attributes = empty( $attributes['width'] )
? get_block_wrapper_attributes()
: get_block_wrapper_attributes( array( 'style' => "width:{$attributes['width']};" ) );

return "<figure {$wrapper_attributes}>{$featured_image}</figure>";
return "<figure {$wrapper_attributes}>{$featured_image}{$image_caption}</figure>";
}

/**
Expand Down

1 comment on commit 557bb56

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3838351176
📝 Reported issues:

Please sign in to comment.