Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefix / suffix to Post Date block #61920

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ Display the publish date for an entry such as a post or page. ([Source](https://
- **Name:** core/post-date
- **Category:** theme
- **Supports:** color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** displayType, format, isLink, textAlign
- **Attributes:** displayType, format, isLink, prefix, suffix, textAlign

## Excerpt

Expand Down
8 changes: 8 additions & 0 deletions packages/block-library/src/post-date/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@
"textAlign": {
"type": "string"
},
"prefix": {
"type": "string",
"default": ""
},
"format": {
"type": "string"
},
"suffix": {
"type": "string",
"default": ""
},
"isLink": {
"type": "boolean",
"default": false
Expand Down
26 changes: 23 additions & 3 deletions packages/block-library/src/post-date/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
AlignmentControl,
BlockControls,
InspectorControls,
RichText,
useBlockProps,
__experimentalDateFormatPicker as DateFormatPicker,
__experimentalPublishDateTimePicker as PublishDateTimePicker,
Expand All @@ -30,7 +31,7 @@ import { DOWN } from '@wordpress/keycodes';
import { useSelect } from '@wordpress/data';

export default function PostDateEdit( {
attributes: { textAlign, format, isLink, displayType },
attributes: { textAlign, format, prefix, suffix, isLink, displayType },
context: { postId, postType: postTypeSlug, queryId },
setAttributes,
} ) {
Expand Down Expand Up @@ -148,7 +149,6 @@ export default function PostDateEdit( {
</ToolbarGroup>
) }
</BlockControls>

<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<DateFormatPicker
Expand Down Expand Up @@ -188,7 +188,27 @@ export default function PostDateEdit( {
</PanelBody>
</InspectorControls>

<div { ...blockProps }>{ postDate }</div>
<div { ...blockProps }>
<RichText
className="wp-block-post-date__prefix"
multiline={ false }
aria-label={ __( 'Prefix' ) }
placeholder={ __( 'Prefix' ) + ' ' }
value={ prefix }
onChange={ ( value ) => setAttributes( { prefix: value } ) }
tagName="span"
/>
{ postDate }
<RichText
className="wp-block-post-terms__suffix"
multiline={ false }
aria-label={ __( 'Suffix' ) }
placeholder={ ' ' + __( 'Suffix' ) }
value={ suffix }
onChange={ ( value ) => setAttributes( { suffix: value } ) }
tagName="span"
/>
</div>
</>
);
}
Expand Down
16 changes: 14 additions & 2 deletions packages/block-library/src/post-date/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,27 @@ function render_block_core_post_date( $attributes, $content, $block ) {

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );

$prefix = '';
if ( isset( $attributes['prefix'] ) && $attributes['prefix'] ) {
$prefix = wp_kses_post( '<span class="wp-block-post-date__prefix">' . $attributes['prefix'] . '</span>' );
}

if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
$formatted_date = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $formatted_date );
}

$suffix = '';
if ( isset( $attributes['suffix'] ) && $attributes['suffix'] ) {
$suffix = wp_kses_post( '<span class="wp-block-post-date__suffix">' . $attributes['suffix'] . '</span>' );
}

return sprintf(
'<div %1$s><time datetime="%2$s">%3$s</time></div>',
'<div %1$s>%2$s<time datetime="%3$s">%4$s</time>%5$s</div>',
Copy link
Contributor

Choose a reason for hiding this comment

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

I think prefix and suffix would need esc_html. A contributor user could add scripts there.

Copy link
Member

Choose a reason for hiding this comment

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

It would need to be wo_kses_post because the rich text string may contain formats such as bold / italics etc.

Copy link
Author

Choose a reason for hiding this comment

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

Applied wp_kses_post to the Prefix and Suffix for the Post Date block output 4d1ca75

$wrapper_attributes,
$prefix,
$unformatted_date,
$formatted_date
$formatted_date,
$suffix
);
}

Expand Down
4 changes: 3 additions & 1 deletion test/integration/fixtures/blocks/core__post-date.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"isValid": true,
"attributes": {
"isLink": false,
"displayType": "date"
"displayType": "date",
"prefix": "",
"suffix": ""
},
"innerBlocks": []
}
Expand Down
Loading