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

Post Comment: Handle the case where a comment does not exist #35810

Merged
merged 1 commit into from
Oct 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 49 additions & 29 deletions packages/block-library/src/post-comment-author/edit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, _x } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';
Expand All @@ -10,18 +10,23 @@ import { PanelBody, ToggleControl } from '@wordpress/components';
/**
* Renders the `core/post-comment-author` block on the editor.
*
* @param {Object} props React props.
* @param {Object} props.setAttributes Callback for updating block attributes.
* @param {Object} props.attributes Block attributes.
* @param {Object} props.context Inherited context.
* @param {Object} props React props.
* @param {Object} props.setAttributes Callback for updating block attributes.
* @param {Object} props.attributes Block attributes.
* @param {string} props.attributes.className Block class name.
* @param {string} props.attributes.isLink Whether the author name should be linked.
* @param {string} props.attributes.linkTarget Target of the link.
* @param {Object} props.context Inherited context.
* @param {string} props.context.commentId The comment ID.
*
* @return {JSX.Element} React element.
*/
export default function Edit( { attributes, context, setAttributes } ) {
const { className, isLink, linkTarget } = attributes;
const { commentId } = context;
export default function Edit( {
attributes: { className, isLink, linkTarget },
context: { commentId },
setAttributes,
} ) {
const blockProps = useBlockProps( { className } );

const displayName = useSelect(
( select ) => {
const { getEntityRecord } = select( coreStore );
Expand All @@ -38,6 +43,40 @@ export default function Edit( { attributes, context, setAttributes } ) {
[ commentId ]
);

const inspectorControls = (
<InspectorControls>
<PanelBody title={ __( 'Link settings' ) }>
<ToggleControl
label={ __( 'Link to authors URL' ) }
onChange={ () => setAttributes( { isLink: ! isLink } ) }
checked={ isLink }
/>
{ isLink && (
<ToggleControl
label={ __( 'Open in new tab' ) }
onChange={ ( value ) =>
setAttributes( {
linkTarget: value ? '_blank' : '_self',
} )
}
checked={ linkTarget === '_blank' }
/>
) }
</PanelBody>
</InspectorControls>
);

if ( ! commentId || ! displayName ) {
return (
<>
{ inspectorControls }
<div { ...blockProps }>
<p>{ _x( 'Post Comment Author', 'block title' ) }</p>
</div>
</>
);
}

const displayAuthor = isLink ? (
<a
href="#comment-author-pseudo-link"
Expand All @@ -51,26 +90,7 @@ export default function Edit( { attributes, context, setAttributes } ) {

return (
<>
<InspectorControls>
<PanelBody title={ __( 'Link settings' ) }>
<ToggleControl
label={ __( 'Link to authors URL' ) }
onChange={ () => setAttributes( { isLink: ! isLink } ) }
checked={ isLink }
/>
{ isLink && (
<ToggleControl
label={ __( 'Open in new tab' ) }
onChange={ ( value ) =>
setAttributes( {
linkTarget: value ? '_blank' : '_self',
} )
}
checked={ linkTarget === '_blank' }
/>
) }
</PanelBody>
</InspectorControls>
{ inspectorControls }
<div { ...blockProps }>{ displayAuthor }</div>
</>
);
Expand Down
9 changes: 7 additions & 2 deletions packages/block-library/src/post-comment-author/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ function render_block_core_post_comment_author( $attributes, $content, $block )
return '';
}

$comment = get_comment( $block->context['commentId'] );
if ( empty( $comment ) ) {
return '';
}

$wrapper_attributes = get_block_wrapper_attributes();
$comment_author = get_comment_author( $block->context['commentId'] );
$link = get_comment_author_url( $block->context['commentId'] );
$comment_author = get_comment_author( $comment );
$link = get_comment_author_url( $comment );

if ( ! empty( $attributes['isLink'] ) && ! empty( $attributes['linkTarget'] ) ) {
$comment_author = sprintf( '<a rel="external nofollow ugc" href="%1s" target="%2s" >%3s</a>', $link, $attributes['linkTarget'], $comment_author );
Expand Down
44 changes: 27 additions & 17 deletions packages/block-library/src/post-comment-content/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { _x } from '@wordpress/i18n';
import { RawHTML } from '@wordpress/element';
import { Disabled } from '@wordpress/components';
import { useEntityProp } from '@wordpress/core-data';
Expand Down Expand Up @@ -38,32 +38,42 @@ export default function Edit( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );

const [ content ] = useEntityProp(
'root',
'comment',
'content',
commentId
);

const blockControls = (
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( newAlign ) =>
setAttributes( { textAlign: newAlign } )
}
/>
</BlockControls>
);

if ( ! commentId || ! content ) {
return (
<>
{ blockControls }
<div { ...blockProps }>
<p>{ _x( 'Post Comment Content', 'block title' ) }</p>
</div>
</>
);
}

return (
<>
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( newAlign ) =>
setAttributes( { textAlign: newAlign } )
}
/>
</BlockControls>
{ blockControls }
<div { ...blockProps }>
{ ! commentId || ! content ? (
<p>{ __( 'Post Comment Content' ) }</p>
) : (
<Disabled>
<RawHTML key="html">{ content.rendered }</RawHTML>
</Disabled>
) }
<Disabled>
<RawHTML key="html">{ content.rendered }</RawHTML>
</Disabled>
</div>
</>
);
Expand Down
7 changes: 6 additions & 1 deletion packages/block-library/src/post-comment-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ function render_block_core_post_comment_content( $attributes, $content, $block )
return '';
}

$comment_text = get_comment_text( $block->context['commentId'] );
$comment = get_comment( $block->context['commentId'] );
if ( empty( $comment ) ) {
return '';
}

$comment_text = get_comment_text( $comment );
if ( ! $comment_text ) {
return '';
}
Expand Down
96 changes: 63 additions & 33 deletions packages/block-library/src/post-comment-date/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,77 @@ import {
CustomSelectControl,
ToggleControl,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { __, _x } from '@wordpress/i18n';

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

const settings = __experimentalGetSettings();
const [ siteDateFormat ] = useEntityProp( 'root', 'site', 'date_format' );
/**
* Renders the `core/post-comment-date` block on the editor.
*
* @param {Object} props React props.
* @param {Object} props.setAttributes Callback for updating block attributes.
* @param {Object} props.attributes Block attributes.
* @param {string} props.attributes.className Block class name.
* @param {string} props.attributes.format Format of the date.
* @param {string} props.attributes.isLink Whether the author name should be linked.
* @param {Object} props.context Inherited context.
* @param {string} props.context.commentId The comment ID.
*
* @return {JSX.Element} React element.
*/
export default function Edit( {
attributes: { className, format, isLink },
context: { commentId },
setAttributes,
} ) {
const blockProps = useBlockProps( { className } );
const [ date ] = useEntityProp( 'root', 'comment', 'date', commentId );
const [ siteDateFormat ] = useEntityProp( 'root', 'site', 'date_format' );

const settings = __experimentalGetSettings();
const formatOptions = Object.values( settings.formats ).map(
( formatOption ) => ( {
key: formatOption,
name: dateI18n( formatOption, date ),
name: dateI18n( formatOption, date || new Date() ),
Copy link
Member Author

Choose a reason for hiding this comment

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

This one was tricky. When there is no valid comment, then we have to provide a fallback value.

} )
);
const resolvedFormat = format || siteDateFormat || settings.formats.date;
const blockProps = useBlockProps( { className } );

const inspectorControls = (
<InspectorControls>
<PanelBody title={ __( 'Format settings' ) }>
<CustomSelectControl
hideLabelFromVision
label={ __( 'Date Format' ) }
options={ formatOptions }
onChange={ ( { selectedItem } ) =>
setAttributes( {
format: selectedItem.key,
} )
}
value={ formatOptions.find(
( option ) => option.key === resolvedFormat
) }
/>
</PanelBody>
<PanelBody title={ __( 'Link settings' ) }>
<ToggleControl
label={ __( 'Link to comment' ) }
onChange={ () => setAttributes( { isLink: ! isLink } ) }
checked={ isLink }
/>
</PanelBody>
</InspectorControls>
);

if ( ! commentId || ! date ) {
return (
<>
{ inspectorControls }
<div { ...blockProps }>
<p>{ _x( 'Post Comment Date', 'block title' ) }</p>
</div>
</>
);
}

let commentDate = (
<time dateTime={ dateI18n( 'c', date ) }>
Expand All @@ -47,30 +100,7 @@ export default function Edit( { attributes, context, setAttributes } ) {

return (
<>
<InspectorControls>
<PanelBody title={ __( 'Format settings' ) }>
<CustomSelectControl
hideLabelFromVision
label={ __( 'Date Format' ) }
options={ formatOptions }
onChange={ ( { selectedItem } ) =>
setAttributes( {
format: selectedItem.key,
} )
}
value={ formatOptions.find(
( option ) => option.key === resolvedFormat
) }
/>
</PanelBody>
<PanelBody title={ __( 'Link settings' ) }>
<ToggleControl
label={ __( 'Link to comment' ) }
onChange={ () => setAttributes( { isLink: ! isLink } ) }
checked={ isLink }
/>
</PanelBody>
</InspectorControls>
{ inspectorControls }
<div { ...blockProps }>{ commentDate }</div>
</>
);
Expand Down
11 changes: 8 additions & 3 deletions packages/block-library/src/post-comment-date/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ function render_block_core_post_comment_date( $attributes, $content, $block ) {
return '';
}

$comment = get_comment( $block->context['commentId'] );
if ( empty( $comment ) ) {
return '';
}

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

if ( ! empty( $attributes['isLink'] ) ) {
$formatted_date = sprintf( '<a href="%1s">%2s</a>', $link, $formatted_date );
Expand All @@ -32,7 +37,7 @@ function render_block_core_post_comment_date( $attributes, $content, $block ) {
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( 'c', $comment ),
$formatted_date
);
}
Expand Down
7 changes: 3 additions & 4 deletions packages/block-library/src/post-comment/edit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, _x } from '@wordpress/i18n';
import { Placeholder, TextControl, Button } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { blockDefault } from '@wordpress/icons';
Expand All @@ -20,8 +20,7 @@ const TEMPLATE = [
[ 'core/post-comment-author' ],
];

export default function Edit( { attributes, setAttributes } ) {
const { commentId } = attributes;
export default function Edit( { attributes: { commentId }, setAttributes } ) {
const [ commentIdInput, setCommentIdInput ] = useState( commentId );
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps, {
Expand All @@ -34,7 +33,7 @@ export default function Edit( { attributes, setAttributes } ) {
<div { ...blockProps }>
<Placeholder
icon={ blockDefault }
label={ __( 'Post Comment' ) }
label={ _x( 'Post Comment', 'block title' ) }
instructions={ __(
'To show a comment, input the comment ID.'
) }
Expand Down