Skip to content

Commit

Permalink
Block Library: Add a Post Comments Count block. (#19953)
Browse files Browse the repository at this point in the history
* Block Library: Add Post Comments Count block.

* Block Library: Add Post Comments Count block server-side rendering.

* E2E Tests: Add Post Comments Count block fixtures.

* Block Library: Add Post Comments Count block class name functionality.
  • Loading branch information
epiqueras committed Feb 12, 2020
1 parent c9bbef7 commit bf15039
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 21 deletions.
43 changes: 22 additions & 21 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,28 @@ function gutenberg_reregister_core_block_types() {
}

$block_names = array(
'archives.php' => 'core/archives',
'block.php' => 'core/block',
'calendar.php' => 'core/calendar',
'categories.php' => 'core/categories',
'latest-comments.php' => 'core/latest-comments',
'latest-posts.php' => 'core/latest-posts',
'legacy-widget.php' => 'core/legacy-widget',
'navigation.php' => 'core/navigation',
'rss.php' => 'core/rss',
'shortcode.php' => 'core/shortcode',
'search.php' => 'core/search',
'social-link.php' => 'core/social-link',
'tag-cloud.php' => 'core/tag-cloud',
'site-title.php' => 'core/site-title',
'template-part.php' => 'core/template-part',
'post-title.php' => 'core/post-title',
'post-content.php' => 'core/post-content',
'post-author.php' => 'core/post-author',
'post-comments-form.php' => 'core/post-comments-form',
'post-date.php' => 'core/post-date',
'post-excerpt.php' => 'core/post-excerpt',
'archives.php' => 'core/archives',
'block.php' => 'core/block',
'calendar.php' => 'core/calendar',
'categories.php' => 'core/categories',
'latest-comments.php' => 'core/latest-comments',
'latest-posts.php' => 'core/latest-posts',
'legacy-widget.php' => 'core/legacy-widget',
'navigation.php' => 'core/navigation',
'rss.php' => 'core/rss',
'shortcode.php' => 'core/shortcode',
'search.php' => 'core/search',
'social-link.php' => 'core/social-link',
'tag-cloud.php' => 'core/tag-cloud',
'site-title.php' => 'core/site-title',
'template-part.php' => 'core/template-part',
'post-title.php' => 'core/post-title',
'post-content.php' => 'core/post-content',
'post-author.php' => 'core/post-author',
'post-comments-count.php' => 'core/post-comments-count',
'post-comments-form.php' => 'core/post-comments-form',
'post-date.php' => 'core/post-date',
'post-excerpt.php' => 'core/post-excerpt',
);

$registry = WP_Block_Type_Registry::get_instance();
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 postCommentsCount from './post-comments-count';
import * as postCommentsForm from './post-comments-form';
import * as postDate from './post-date';
import * as postExcerpt from './post-excerpt';
Expand Down Expand Up @@ -196,6 +197,7 @@ export const __experimentalRegisterExperimentalCoreBlocks =
postTitle,
postContent,
postAuthor,
postCommentsCount,
postCommentsForm,
postDate,
postExcerpt,
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/post-comments-count/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "core/post-comments-count",
"category": "layout"
}
38 changes: 38 additions & 0 deletions packages/block-library/src/post-comments-count/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* WordPress dependencies
*/
import { useEntityId } from '@wordpress/core-data';
import { useState, useEffect } from '@wordpress/element';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';

function PostCommentsCountDisplay( { className } ) {
const postId = useEntityId( 'postType', 'post' );
const [ commentsCount, setCommentsCount ] = useState();
useEffect( () => {
const currentPostId = postId;
apiFetch( {
path: addQueryArgs( '/wp/v2/comments', {
post: postId,
} ),
parse: false,
} ).then( ( res ) => {
// Stale requests will have the `currentPostId` of an older closure.
if ( currentPostId === postId ) {
setCommentsCount( res.headers.get( 'X-WP-Total' ) );
}
} );
}, [ postId ] );
return (
<span className={ className }>
{ commentsCount !== undefined && commentsCount }
</span>
);
}

export default function PostCommentsCountEdit( { className } ) {
if ( ! useEntityId( 'postType', 'post' ) ) {
return 'Post Comments Count Placeholder';
}
return <PostCommentsCountDisplay className={ className } />;
}
21 changes: 21 additions & 0 deletions packages/block-library/src/post-comments-count/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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 Count' ),
supports: {
html: false,
},
edit,
};
47 changes: 47 additions & 0 deletions packages/block-library/src/post-comments-count/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Server-side rendering of the `core/post-comments-count` block.
*
* @package WordPress
*/

/**
* Renders the `core/post-comments-count` block on the server.
*
* @param array $attributes The block attributes.
*
* @return string Returns the filtered post comments count for the current post.
*/
function render_block_core_post_comments_count( $attributes ) {
$post = gutenberg_get_post_from_context();
if ( ! $post ) {
return '';
}
$class = 'wp-block-post-comments-count';
if ( isset( $attributes['className'] ) ) {
$class .= ' ' . $attributes['className'];
}
return sprintf(
'<span class="%1$s">%2$s</span>',
esc_attr( $class ),
get_comments_number( $post )
);
}

/**
* Registers the `core/post-comments-count` block on the server.
*/
function register_block_core_post_comments_count() {
register_block_type(
'core/post-comments-count',
array(
'attributes' => array(
'className' => array(
'type' => 'string',
),
),
'render_callback' => 'render_block_core_post_comments_count',
)
);
}
add_action( 'init', 'register_block_core_post_comments_count' );
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-comments-count /-->
10 changes: 10 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-comments-count.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"clientId": "_clientId_0",
"name": "core/post-comments-count",
"isValid": true,
"attributes": {},
"innerBlocks": [],
"originalContent": ""
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"blockName": "core/post-comments-count",
"attrs": {},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n",
"innerContent": [
"\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-comments-count /-->

0 comments on commit bf15039

Please sign in to comment.