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 committed Jan 11, 2020
1 parent 54aaba6 commit b1de10f
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function gutenberg_reregister_core_block_types() {
'template-part.php' => 'core/template-part',
'post-title.php' => 'core/post-title',
'post-content.php' => 'core/post-content',
'post-comments.php' => 'core/post-comments',
);

$registry = WP_Block_Type_Registry::get_instance();
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import * as siteTitle from './site-title';
import * as templatePart from './template-part';
import * as postTitle from './post-title';
import * as postContent from './post-content';
import * as postComments from './post-comments';

/**
* Function to register an individual block.
Expand Down Expand Up @@ -187,7 +188,7 @@ export const __experimentalRegisterExperimentalCoreBlocks =

// Register Full Site Editing Blocks.
...( __experimentalEnableFullSiteEditing ?
[ siteTitle, templatePart, postTitle, postContent ] :
[ siteTitle, templatePart, postTitle, postContent, postComments ] :
[] ),
].forEach( registerBlock );
} :
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' );
1 change: 1 addition & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const defaultEntities = [
{ name: 'media', kind: 'root', baseURL: '/wp/v2/media', plural: 'mediaItems' },
{ name: 'taxonomy', kind: 'root', key: 'slug', baseURL: '/wp/v2/taxonomies', plural: 'taxonomies' },
{ name: 'widgetArea', kind: 'root', baseURL: '/__experimental/widget-areas', plural: 'widgetAreas', transientEdits: { blocks: true } },
{ name: 'comment', kind: 'root', baseURL: '/wp/v2/comments' },
];

export const kinds = [
Expand Down

0 comments on commit b1de10f

Please sign in to comment.