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 Comments: Add Warning via editor.BlockEdit filter #40898

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@wordpress/date": "file:../date",
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/dom": "file:../dom",
"@wordpress/editor": "file:../editor",
Copy link
Contributor Author

@ockham ockham May 9, 2022

Choose a reason for hiding this comment

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

This is probably the major downside of this PR 😬 In the past, we've been keen to avoid having the blocks depend on the editor.

As for the reason for this change, see this inline comment: https://github.com/WordPress/gutenberg/pull/40898/files#diff-e1efd67de68dbd5ce8acea06af13216e3c2455c7b87c16d31cea7df4f282a9fbR209-R211

"@wordpress/element": "file:../element",
"@wordpress/hooks": "file:../hooks",
"@wordpress/html-entities": "file:../html-entities",
Expand Down
149 changes: 95 additions & 54 deletions packages/block-library/src/post-comments/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import {
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { store as editorStore } from '@wordpress/editor';
import { __, sprintf } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { useEntityProp, store as coreStore } from '@wordpress/core-data';
import { useDisabled } from '@wordpress/compose';
import { createHigherOrderComponent, useDisabled } from '@wordpress/compose';
import { createInterpolateElement } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';

/**
* Internal dependencies
Expand All @@ -28,67 +30,23 @@ export default function PostCommentsEdit( {
attributes: { textAlign },
setAttributes,
context: { postType, postId },
showPlaceholder,
} ) {
let [ postTitle ] = useEntityProp( 'postType', postType, 'title', postId );
postTitle = postTitle || __( 'Post Title' );

const [ commentStatus ] = useEntityProp(
'postType',
postType,
'comment_status',
postId
);

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

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

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

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" block.'
);
let showPlaceholder = true;

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

const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );

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

const disabledRef = useDisabled();

return (
Expand All @@ -103,8 +61,6 @@ export default function PostCommentsEdit( {
</BlockControls>

<div { ...blockProps }>
<Warning>{ warning }</Warning>

{ showPlaceholder && (
<div
className="wp-block-post-comments__placeholder"
Expand Down Expand Up @@ -245,3 +201,88 @@ export default function PostCommentsEdit( {
</>
);
}

export const withPostCommentsBlockDeprecationWarning = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const { name } = props;

// Unfortunately, the `editor.BlockEdit` filter doesn't give us access to
// block context from which we could infer postId and postType, so we
// have to gather that information ourselves.
const { postId, postType } = useSelect( ( select ) => ( {
postId: select( editorStore ).getCurrentPostId(),
postType: select( editorStore ).getCurrentPostType(),
} ) );

const [ commentStatus ] = useEntityProp(
'postType',
postType,
'comment_status',
postId
);

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

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

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

if ( name !== 'core/post-comments' ) {
return <BlockEdit { ...props } />;
}

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" block.'
);
let showPlaceholder = true;

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

return (
<>
<Warning>{ warning }</Warning>
<BlockEdit { ...props } showPlaceholder={ showPlaceholder } />
</>
);
}
);

addFilter(
'editor.BlockEdit',
'core/post-comments/with-post-comments-block-deprecation-warning',
withPostCommentsBlockDeprecationWarning
);