diff --git a/packages/block-library/src/post-comments-form/edit.js b/packages/block-library/src/post-comments-form/edit.js index fbcd99d93f3f45..24e4599dd46bb7 100644 --- a/packages/block-library/src/post-comments-form/edit.js +++ b/packages/block-library/src/post-comments-form/edit.js @@ -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 */ @@ -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 ( <> @@ -52,29 +95,9 @@ export default function PostCommentsFormEdit( { />
- { ! commentStatus && ! isInSiteEditor && ( - - { __( - 'Post Comments Form block: comments are not enabled for this post type.' - ) } - - ) } - - { 'open' !== commentStatus && ! isInSiteEditor && ( - - { sprintf( - /* translators: 1: Post type (i.e. "post", "page") */ - __( - 'Post Comments Form block: comments to this %s are not allowed.' - ), - postType - ) } - - ) } + { warning && { warning } } - { ( 'open' === commentStatus || isInSiteEditor ) && ( - - ) } + { showPlaceholder ? : null }
); diff --git a/packages/block-library/src/post-comments/edit.js b/packages/block-library/src/post-comments/edit.js index 52e891e5a8cc4e..248922da8b19e6 100644 --- a/packages/block-library/src/post-comments/edit.js +++ b/packages/block-library/src/post-comments/edit.js @@ -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 ) { @@ -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") */ @@ -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; } } @@ -104,7 +104,7 @@ export default function PostCommentsEdit( {
{ warning } - { showPlacholder && ( + { showPlaceholder && (