-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Block Library: Add a Post Comments block. #19581
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
321afe6
Block Library: Add a Post Comments block.
epiqueras 1e21623
Lint
epiqueras cede9bc
Add "no comment" placeholder.
noahtallen 16710f8
Add TODOs for followup tasks
noahtallen 33c6aa0
Check for length in no comments check
noahtallen d16c938
Add e2e test fixtures for post comments block
noahtallen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "core/post-comments", | ||
"category": "layout" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { useEntityId } from '@wordpress/core-data'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
function PostCommentsDisplay( { postId } ) { | ||
return useSelect( | ||
( select ) => { | ||
const comments = select( 'core' ).getEntityRecords( | ||
'root', | ||
'comment', | ||
{ | ||
post: postId, | ||
} | ||
); | ||
// TODO: "No Comments" placeholder should be editable. | ||
return comments | ||
? comments.map( ( comment ) => ( | ||
<p key={ comment.id }>{ comment.content.raw }</p> | ||
) ) | ||
: __( 'No comments.' ); | ||
}, | ||
[ postId ] | ||
); | ||
} | ||
|
||
export default function PostCommentsEdit() { | ||
// TODO: Update to handle multiple post types. | ||
const postId = useEntityId( 'postType', 'post' ); | ||
if ( ! postId ) { | ||
return 'Post Comments Placeholder'; | ||
} | ||
return <PostCommentsDisplay postId={ postId } />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import metadata from './block.json'; | ||
import edit from './edit'; | ||
|
||
const { name } = metadata; | ||
export { metadata, name }; | ||
|
||
export const settings = { | ||
title: __( 'Post Comments' ), | ||
edit, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/post-comments` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `core/post-comments` block on the server. | ||
* | ||
* @return string Returns the filtered post comments for the current post wrapped inside "p" tags. | ||
*/ | ||
function render_block_core_post_comments() { | ||
$post = gutenberg_get_post_from_context(); | ||
epiqueras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( ! $post ) { | ||
return ''; | ||
} | ||
$comments = get_comments( | ||
array( | ||
'post_id' => $post->ID, | ||
) | ||
); | ||
$output = ''; | ||
// TODO: Handle nested comments. | ||
foreach ( $comments as $comment ) { | ||
noahtallen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$output .= '<p>' . $comment->comment_author . '<br />' . $comment->comment_content . '</p>'; | ||
} | ||
return $output; | ||
} | ||
|
||
/** | ||
* Registers the `core/post-comments` block on the server. | ||
*/ | ||
function register_block_core_post_comments() { | ||
register_block_type( | ||
'core/post-comments', | ||
array( | ||
'render_callback' => 'render_block_core_post_comments', | ||
) | ||
); | ||
} | ||
add_action( 'init', 'register_block_core_post_comments' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may have other post types that support comments, for all these post types we are rendering 'Post Comments Placeholder' while I think we should render the same we render for posts (the comments in that post).
For post types that don't support comments but are not templates or template parts, I think we should not show the block on the inserter e.g: remove it from the allowed blocks or if we show in the inserter we should say in the block that comments are not supported. The block should handle the condition and show this message even if hidden from the inserter because a CPT may have supported comments in the past and then the user may disable the comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense, but we'll have to handle it at another level in the code.
It will likely require changes to
core-data
to get post type capabilities and have CPTs act as posts under certain conditions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually a very good remark and one that should lead to some rethinking in how the context EntityProvider and
useEntityProp/useEntityId
works.Ideally, these hooks should just work no matter the CPT (all CPTs can have all properties) and should not have "post" hard-coded.
My first thinking here is that we should be able to do something like:
const entityId = useEntityId();
const entityContent = useEntityProp( null, null, 'content' )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the first block where it's specific to "post" so we need to solve this ASAP on a follow-up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is what I suggested.
But, #19685 (comment) is my preferred alternative that also works on the server.