Skip to content

Commit

Permalink
Add option to make PostDate a link (#30499)
Browse files Browse the repository at this point in the history
* Add option to make PostDate a link

* update fixture
  • Loading branch information
ntsekouras authored Apr 5, 2021
1 parent 7c84693 commit 48e470e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
4 changes: 4 additions & 0 deletions packages/block-library/src/post-date/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
},
"format": {
"type": "string"
},
"isLink": {
"type": "boolean",
"default": false
}
},
"usesContext": [
Expand Down
57 changes: 34 additions & 23 deletions packages/block-library/src/post-date/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ import {
import {
ToolbarGroup,
ToolbarButton,
ToggleControl,
Popover,
DateTimePicker,
PanelBody,
CustomSelectControl,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { edit } from '@wordpress/icons';

export default function PostDateEdit( { attributes, context, setAttributes } ) {
const { textAlign, format } = attributes;
const { textAlign, format, isLink } = attributes;
const { postId, postType } = context;

const [ siteFormat ] = useEntityProp( 'root', 'site', 'date_format' );
Expand Down Expand Up @@ -62,6 +63,25 @@ export default function PostDateEdit( { attributes, context, setAttributes } ) {
} ),
} );

let postDate = date ? (
<time dateTime={ dateI18n( 'c', date ) }>
{ dateI18n( resolvedFormat, date ) }
{ isPickerOpen && (
<Popover onClose={ setIsPickerOpen.bind( null, false ) }>
<DateTimePicker
currentDate={ date }
onChange={ setDate }
is12Hour={ is12Hour }
/>
</Popover>
) }
</time>
) : (
__( 'No Date' )
);
if ( isLink && date ) {
postDate = <a href="#post-date-pseudo-link">{ postDate }</a>;
}
return (
<>
<BlockControls>
Expand Down Expand Up @@ -103,28 +123,19 @@ export default function PostDateEdit( { attributes, context, setAttributes } ) {
) }
/>
</PanelBody>
</InspectorControls>

<div { ...blockProps }>
{ date && (
<time dateTime={ dateI18n( 'c', date ) }>
{ dateI18n( resolvedFormat, date ) }

{ isPickerOpen && (
<Popover
onClose={ setIsPickerOpen.bind( null, false ) }
>
<DateTimePicker
currentDate={ date }
onChange={ setDate }
is12Hour={ is12Hour }
/>
</Popover>
<PanelBody title={ __( 'Link settings' ) }>
<ToggleControl
label={ sprintf(
// translators: %s: Name of the post type e.g: "post".
__( 'Link to %s' ),
postType
) }
</time>
) }
{ ! date && __( 'No Date' ) }
</div>
onChange={ () => setAttributes( { isLink: ! isLink } ) }
checked={ isLink }
/>
</PanelBody>
</InspectorControls>
<div { ...blockProps }>{ postDate }</div>
</>
);
}
9 changes: 7 additions & 2 deletions packages/block-library/src/post-date/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ function render_block_core_post_date( $attributes, $content, $block ) {
return '';
}

$post_ID = $block->context['postId'];
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
$formatted_date = get_the_date( isset( $attributes['format'] ) ? $attributes['format'] : '', $post_ID );
if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
$formatted_date = sprintf( '<a href="%1s">%2s</a>', get_the_permalink( $post_ID ), $formatted_date );
}

return sprintf(
'<div %1$s><time datetime="%2$s">%3$s</time></div>',
$wrapper_attributes,
get_the_date( 'c', $block->context['postId'] ),
get_the_date( isset( $attributes['format'] ) ? $attributes['format'] : '', $block->context['postId'] )
get_the_date( 'c', $post_ID ),
$formatted_date
);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/e2e-tests/fixtures/blocks/core__post-date.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"clientId": "_clientId_0",
"name": "core/post-date",
"isValid": true,
"attributes": {},
"attributes": {
"isLink": false
},
"innerBlocks": [],
"originalContent": ""
}
Expand Down

0 comments on commit 48e470e

Please sign in to comment.