Skip to content

Commit

Permalink
Improve the logic for warnings for Post Comments Form placeholder (#4…
Browse files Browse the repository at this point in the history
…0563)

* Improve the warning for Comments Form placeholder

* Fix typo on showPlaceholder variable name

Co-authored-by: Luis Herranz <luisherranz@gmail.com>
  • Loading branch information
michalczaplinski and luisherranz authored Apr 25, 2022
1 parent e34aac6 commit c5b9ca1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
73 changes: 48 additions & 25 deletions packages/block-library/src/post-comments-form/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import {
BlockControls,
Warning,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useEntityProp } from '@wordpress/core-data';
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { __, sprintf } from '@wordpress/i18n';

import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
Expand All @@ -39,7 +40,49 @@ export default function PostCommentsFormEdit( {
} ),
} );

const isInSiteEditor = postType === undefined || postId === undefined;
const isSiteEditor = postType === undefined || postId === undefined;

const { defaultCommentStatus } = useSelect(
( select ) =>
select( blockEditorStore ).getSettings()
.__experimentalDiscussionSettings
);

const postTypeSupportsComments = useSelect( ( select ) =>
postType
? !! select( coreStore ).getPostType( postType )?.supports.comments
: false
);

let warning = false;
let showPlaceholder = true;

if ( ! isSiteEditor && 'open' !== commentStatus ) {
if ( 'closed' === commentStatus ) {
warning = sprintf(
/* translators: 1: Post type (i.e. "post", "page") */
__(
'Post Comments Form block: Comments on this %s are not allowed.'
),
postType
);
showPlaceholder = false;
} else if ( ! postTypeSupportsComments ) {
warning = sprintf(
/* translators: 1: Post type (i.e. "post", "page") */
__(
'Post Comments Form block: Comments for this post type (%s) are not enabled.'
),
postType
);
showPlaceholder = false;
} else if ( 'open' !== defaultCommentStatus ) {
warning = __(
'Post Comments Form block: Comments are not enabled.'
);
showPlaceholder = false;
}
}

return (
<>
Expand All @@ -52,29 +95,9 @@ export default function PostCommentsFormEdit( {
/>
</BlockControls>
<div { ...blockProps }>
{ ! commentStatus && ! isInSiteEditor && (
<Warning>
{ __(
'Post Comments Form block: comments are not enabled for this post type.'
) }
</Warning>
) }

{ 'open' !== commentStatus && ! isInSiteEditor && (
<Warning>
{ sprintf(
/* translators: 1: Post type (i.e. "post", "page") */
__(
'Post Comments Form block: comments to this %s are not allowed.'
),
postType
) }
</Warning>
) }
{ warning && <Warning>{ warning }</Warning> }

{ ( 'open' === commentStatus || isInSiteEditor ) && (
<CommentsForm />
) }
{ showPlaceholder ? <CommentsForm /> : null }
</div>
</>
);
Expand Down
10 changes: 5 additions & 5 deletions packages/block-library/src/post-comments/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function PostCommentsEdit( {
let warning = __(
'Post Comments block: This is just a placeholder, not a real comment. The final styling may differ because it also depends on the current theme. For better compatibility with the Block Editor, please consider replacing this block with the "Comments Query Loop" block.'
);
let showPlacholder = true;
let showPlaceholder = true;

if ( ! isSiteEditor && 'open' !== commentStatus ) {
if ( 'closed' === commentStatus ) {
Expand All @@ -66,7 +66,7 @@ export default function PostCommentsEdit( {
),
postType
);
showPlacholder = false;
showPlaceholder = false;
} else if ( ! postTypeSupportsComments ) {
warning = sprintf(
/* translators: 1: Post type (i.e. "post", "page") */
Expand All @@ -75,10 +75,10 @@ export default function PostCommentsEdit( {
),
postType
);
showPlacholder = false;
showPlaceholder = false;
} else if ( 'open' !== defaultCommentStatus ) {
warning = __( 'Post Comments block: Comments are not enabled.' );
showPlacholder = false;
showPlaceholder = false;
}
}

Expand All @@ -104,7 +104,7 @@ export default function PostCommentsEdit( {
<div { ...blockProps }>
<Warning>{ warning }</Warning>

{ showPlacholder && (
{ showPlaceholder && (
<div
className="wp-block-post-comments__placeholder"
ref={ disabledRef }
Expand Down

0 comments on commit c5b9ca1

Please sign in to comment.