Skip to content

Commit

Permalink
Block Library: Add a Post Comments block.
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras authored and ockham committed Feb 24, 2020
1 parent 1e26a8f commit 86589dd
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function gutenberg_reregister_core_block_types() {
'post-title.php' => 'core/post-title',
'post-content.php' => 'core/post-content',
'post-author.php' => 'core/post-author',
'post-comments.php' => 'core/post-comments',
'post-comments-count.php' => 'core/post-comments-count',
'post-comments-form.php' => 'core/post-comments-form',
'post-date.php' => 'core/post-date',
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import * as templatePart from './template-part';
import * as postTitle from './post-title';
import * as postContent from './post-content';
import * as postAuthor from './post-author';
import * as postComments from './post-comments';
import * as postCommentsCount from './post-comments-count';
import * as postCommentsForm from './post-comments-form';
import * as postDate from './post-date';
Expand Down Expand Up @@ -198,6 +199,7 @@ export const __experimentalRegisterExperimentalCoreBlocks =
postTitle,
postContent,
postAuthor,
postComments,
postCommentsCount,
postCommentsForm,
postDate,
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/post-comments/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "core/post-comments",
"category": "layout"
}
28 changes: 28 additions & 0 deletions packages/block-library/src/post-comments/edit.js
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 } />;
}
18 changes: 18 additions & 0 deletions packages/block-library/src/post-comments/index.js
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,
};
41 changes: 41 additions & 0 deletions packages/block-library/src/post-comments/index.php
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' );
6 changes: 6 additions & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export const defaultEntities = [
transientEdits: { blocks: true },
},
{ name: 'user', kind: 'root', baseURL: '/wp/v2/users', plural: 'users' },
{
name: 'comment',
kind: 'root',
baseURL: '/wp/v2/comments',
plural: 'comments',
},
];

export const kinds = [
Expand Down

0 comments on commit 86589dd

Please sign in to comment.