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

[Block: Post comment date]: Add link setting and block supports #35112

Merged
merged 6 commits into from Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/block-library/src/post-comment-date/block.json
Expand Up @@ -9,10 +9,27 @@
"attributes": {
"format": {
"type": "string"
},
"isLink": {
"type": "boolean",
"default": false
}
},
"usesContext": [ "commentId" ],
"supports": {
"html": false
"html": false,
"color": {
"gradients": true,
"link": true
},
"typography": {
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalFontWeight": true,
"__experimentalFontStyle": true,
"__experimentalTextTransform": true,
"__experimentalLetterSpacing": true
}
}
}
39 changes: 31 additions & 8 deletions packages/block-library/src/post-comment-date/edit.js
Expand Up @@ -4,11 +4,15 @@
import { useEntityProp } from '@wordpress/core-data';
import { __experimentalGetSettings, dateI18n } from '@wordpress/date';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import { PanelBody, CustomSelectControl } from '@wordpress/components';
import {
PanelBody,
CustomSelectControl,
ToggleControl,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';

export default function Edit( { attributes, context, setAttributes } ) {
const { className, format } = attributes;
const { className, format, isLink } = attributes;
const { commentId } = context;

const settings = __experimentalGetSettings();
Expand All @@ -24,6 +28,23 @@ export default function Edit( { attributes, context, setAttributes } ) {
const resolvedFormat = format || siteDateFormat || settings.formats.date;
const blockProps = useBlockProps( { className } );

let commentDate = (
<time dateTime={ dateI18n( 'c', date ) }>
{ dateI18n( resolvedFormat, date ) }
</time>
);

if ( isLink ) {
commentDate = (
<a
href="#comment-date-pseudo-link"
Copy link
Member

Choose a reason for hiding this comment

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

Could we use an actual link here? In PHP it's possible with get_comment_link( $block->context['commentId'] );. Would it work with useEntityProp( 'root', 'comment', 'link', commentId ) or something like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we really want it to be clickable in the editor? Is the issue where duplicate editors were opened fixed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean we have used fake links because the editor would be opened inside the editor.

Copy link
Member

@gziolo gziolo Oct 7, 2021

Choose a reason for hiding this comment

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

We use real links in RichText:
Screen Shot 2021-10-07 at 14 07 15

I wasn't aware of a limitation like that. I see more places where the same pattern is used so it's probably fine to leave it as is for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like this: #30647

onClick={ ( event ) => event.preventDefault() }
>
{ commentDate }
</a>
);
}

return (
<>
<InspectorControls>
Expand All @@ -42,13 +63,15 @@ export default function Edit( { attributes, context, setAttributes } ) {
) }
/>
</PanelBody>
<PanelBody title={ __( 'Link settings' ) }>
<ToggleControl
label={ __( 'Link to comment' ) }
onChange={ () => setAttributes( { isLink: ! isLink } ) }
checked={ isLink }
/>
</PanelBody>
</InspectorControls>

<div { ...blockProps }>
<time dateTime={ dateI18n( 'c', date ) }>
{ dateI18n( resolvedFormat, date ) }
</time>
</div>
<div { ...blockProps }>{ commentDate }</div>
</>
);
}
15 changes: 11 additions & 4 deletions packages/block-library/src/post-comment-date/index.php
Expand Up @@ -19,14 +19,21 @@ function render_block_core_post_comment_date( $attributes, $content, $block ) {
}

$wrapper_attributes = get_block_wrapper_attributes();
$formatted_date = get_comment_date(
isset( $attributes['format'] ) ? $attributes['format'] : '',
$block->context['commentId']
);
$link = get_comment_link( $block->context['commentId'] );

if ( ! empty( $attributes['isLink'] ) ) {
$formatted_date = sprintf( '<a href="%1s">%2s</a>', $link, $formatted_date );
}

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

Expand Down
Expand Up @@ -3,7 +3,9 @@
"clientId": "_clientId_0",
"name": "core/post-comment-date",
"isValid": true,
"attributes": {},
"attributes": {
"isLink": false
},
"innerBlocks": [],
"originalContent": ""
}
Expand Down