-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Library: Add a Post Comments block. (#19581)
Co-authored-by: Noah Allen <noahtallen@gmail.com>
- Loading branch information
1 parent
341e83c
commit 6ddcdc3
Showing
11 changed files
with
140 additions
and
0 deletions.
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.length | ||
? 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(); | ||
if ( ! $post ) { | ||
return ''; | ||
} | ||
$comments = get_comments( | ||
array( | ||
'post_id' => $post->ID, | ||
) | ||
); | ||
$output = ''; | ||
// TODO: Handle nested comments. | ||
foreach ( $comments as $comment ) { | ||
$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
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 @@ | ||
<!-- wp:post-comments /--> |
10 changes: 10 additions & 0 deletions
10
packages/e2e-tests/fixtures/blocks/core__post-comments.json
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,10 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/post-comments", | ||
"isValid": true, | ||
"attributes": {}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
} | ||
] |
18 changes: 18 additions & 0 deletions
18
packages/e2e-tests/fixtures/blocks/core__post-comments.parsed.json
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 @@ | ||
[ | ||
{ | ||
"blockName": "core/post-comments", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "", | ||
"innerContent": [] | ||
}, | ||
{ | ||
"blockName": null, | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n", | ||
"innerContent": [ | ||
"\n" | ||
] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
packages/e2e-tests/fixtures/blocks/core__post-comments.serialized.html
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 @@ | ||
<!-- wp:post-comments /--> |