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( { />