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

Improve the logic for warnings for Post Comments Form placeholder #40563

Merged
merged 2 commits into from
Apr 25, 2022
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
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

By default we don't display a warning, like we do in the Post Comments block:

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.'
);

As the Post Comments Form is included in the Comments Query Loop, it would not make sense to say that you should "replace it with the Comments Query Loop". 😅

The form rendered in the editor should also be close enough style-wise to the one rendered on the frontend so that we don't need to warn about it.

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