-
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.
- Loading branch information
Showing
7 changed files
with
100 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,28 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { useEntityId } from '@wordpress/core-data'; | ||
|
||
function PostCommentsDisplay( { postId } ) { | ||
return useSelect( | ||
( select ) => { | ||
const comments = select( 'core' ).getEntityRecords( 'root', 'comment', { | ||
post: postId, | ||
} ); | ||
return ( | ||
comments && | ||
comments.map( ( comment ) => <p key={ comment.id }>{ comment.content.raw }</p> ) | ||
); | ||
}, | ||
[ postId ] | ||
); | ||
} | ||
|
||
export default function PostCommentsEdit() { | ||
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,41 @@ | ||
<?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 = ''; | ||
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