Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Post Comment block #24781

Merged
merged 21 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,25 @@ function gutenberg_reregister_core_block_types() {
$block_names = array_merge(
$block_names,
array(
'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-content.php' => 'core/post-content',
'post-date.php' => 'core/post-date',
'post-excerpt.php' => 'core/post-excerpt',
'post-featured-image.php' => 'core/post-featured-image',
'post-tags.php' => 'core/post-tags',
'post-title.php' => 'core/post-title',
'query.php' => 'core/query',
'query-loop.php' => 'core/query-loop',
'query-pagination.php' => 'core/query-pagination',
'site-logo.php' => 'core/site-logo',
'site-tagline.php' => 'core/site-tagline',
'site-title.php' => 'core/site-title',
'template-part.php' => 'core/template-part',
'post-author.php' => 'core/post-author',
'post-comment.php' => 'core/post-comment',
'post-comment-content.php' => 'core/post-comment-content',
'post-comments.php' => 'core/post-comments',
'post-comments-count.php' => 'core/post-comments-count',
'post-comments-form.php' => 'core/post-comments-form',
'post-content.php' => 'core/post-content',
'post-date.php' => 'core/post-date',
'post-excerpt.php' => 'core/post-excerpt',
'post-featured-image.php' => 'core/post-featured-image',
'post-tags.php' => 'core/post-tags',
'post-title.php' => 'core/post-title',
'query.php' => 'core/query',
'query-loop.php' => 'core/query-loop',
'query-pagination.php' => 'core/query-pagination',
'site-logo.php' => 'core/site-logo',
'site-tagline.php' => 'core/site-tagline',
'site-title.php' => 'core/site-title',
'template-part.php' => 'core/template-part',
)
);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ import * as queryPagination from './query-pagination';
import * as postTitle from './post-title';
import * as postContent from './post-content';
import * as postAuthor from './post-author';
import * as postComment from './post-comment';
import * as postCommentContent from './post-comment-content';
import * as postComments from './post-comments';
import * as postCommentsCount from './post-comments-count';
import * as postCommentsForm from './post-comments-form';
Expand Down Expand Up @@ -208,6 +210,8 @@ export const __experimentalRegisterExperimentalCoreBlocks =
postTitle,
postContent,
postAuthor,
postComment,
postCommentContent,
postComments,
postCommentsCount,
postCommentsForm,
Expand Down
8 changes: 8 additions & 0 deletions packages/block-library/src/post-comment-content/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "core/post-comment-content",
"category": "design",
"usesContext": [ "commentId" ],
"supports": {
"html": false
}
}
19 changes: 19 additions & 0 deletions packages/block-library/src/post-comment-content/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* WordPress dependencies
*/
import { useEntityProp } from '@wordpress/core-data';

// TODO: JSDOC types
export default function Edit( { attributes, context } ) {
const { className } = attributes;
const { commentId } = context;

const [ content ] = useEntityProp(
'root',
'comment',
'content',
commentId
);

return <p className={ className }>{ content }</p>;
}
22 changes: 22 additions & 0 deletions packages/block-library/src/post-comment-content/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { alignJustify as icon } from '@wordpress/icons';

/**
* Internal dependencies
*/
import metadata from './block.json';
import edit from './edit';

const { name } = metadata;
export { metadata, name };

export const settings = {
title: __( 'Post Comment Content' ),
description: __( 'Post Comment Content' ),
icon,
edit,
parent: [ 'core/post-comment' ],
};
35 changes: 35 additions & 0 deletions packages/block-library/src/post-comment-content/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Server-side rendering of the `core/post-comment-content` block.
*
* @package WordPress
*/

/**
* Renders the `core/post-comment-content` block on the server.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
* @return string Return the post comment's content.
*/
function render_block_core_post_comment_content( $attributes, $content, $block ) {
if ( ! isset( $block->context['commentId'] ) ) {
return '';
}

return sprintf( '<div>%1$s</div>', get_comment_text( $block->context['commentId'] ) );
}

/**
* Registers the `core/post-comment-content` block on the server.
*/
function register_block_core_post_comment_content() {
register_block_type_from_metadata(
__DIR__ . '/post-comment-content',
array(
'render_callback' => 'render_block_core_post_comment_content',
)
);
}
add_action( 'init', 'register_block_core_post_comment_content' );
15 changes: 15 additions & 0 deletions packages/block-library/src/post-comment/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "core/post-comment",
"category": "design",
"attributes": {
"commentId": {
"type": "number"
}
},
"providesContext": {
"commentId": "commentId"
},
"supports": {
"html": false
}
}
46 changes: 46 additions & 0 deletions packages/block-library/src/post-comment/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Placeholder, TextControl, Button } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { blockDefault } from '@wordpress/icons';
import { InnerBlocks } from '@wordpress/block-editor';

const ALLOWED_BLOCKS = [ [ 'core/post-comment-content' ] ];

// TODO: JSDOC types
export default function Edit( { className, attributes, setAttributes } ) {
const { commentId } = attributes;
const [ commentIdInput, setCommentIdInput ] = useState( commentId );

if ( ! commentId ) {
return (
<Placeholder
icon={ blockDefault }
label={ __( 'Post Comment' ) }
instructions={ __( 'Input post comment ID' ) }
>
<TextControl
value={ commentId }
onChange={ ( val ) => setCommentIdInput( parseInt( val ) ) }
/>

<Button
isPrimary
onClick={ () => {
setAttributes( { commentId: commentIdInput } );
} }
>
{ __( 'Save' ) }
</Button>
</Placeholder>
);
}

return (
<div className={ className }>
<InnerBlocks allowedBlocks={ ALLOWED_BLOCKS } />
</div>
);
}
23 changes: 23 additions & 0 deletions packages/block-library/src/post-comment/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { comment as icon } from '@wordpress/icons';

/**
* Internal dependencies
*/
import metadata from './block.json';
import edit from './edit';
import save from './save';

const { name } = metadata;
export { metadata, name };

export const settings = {
title: __( 'Post Comment' ),
description: __( 'Post Comment' ),
icon,
edit,
save,
};
17 changes: 17 additions & 0 deletions packages/block-library/src/post-comment/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Server-side rendering of the `core/post-comment` block.
*
* @package WordPress
*/

/**
* Registers the `core/post-comment` block on the server.
* We need to do this to make context available for inner blocks.
*/
function register_block_core_post_comment() {
register_block_type_from_metadata(
__DIR__ . '/post-comment'
);
}
add_action( 'init', 'register_block_core_post_comment' );
14 changes: 14 additions & 0 deletions packages/block-library/src/post-comment/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';

export default function save( { attributes } ) {
const { className } = attributes;

return (
<div className={ className }>
<InnerBlocks.Content />
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-comment-content /-->
10 changes: 10 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-comment-content.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"clientId": "_clientId_0",
"name": "core/post-comment-content",
"isValid": true,
"attributes": {},
"innerBlocks": [],
"originalContent": ""
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"blockName": "core/post-comment-content",
"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-comment-content /-->
3 changes: 3 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-comment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:core/post-comment -->
<div class="wp-block-post-comment"></div>
<!-- /wp:core/post-comment -->
10 changes: 10 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"clientId": "_clientId_0",
"name": "core/post-comment",
"isValid": true,
"attributes": {},
"innerBlocks": [],
"originalContent": "<div class=\"wp-block-post-comment\"></div>"
}
]
20 changes: 20 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-comment.parsed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"blockName": "core/post-comment",
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n<div class=\"wp-block-post-comment\"></div>\n",
"innerContent": [
"\n<div class=\"wp-block-post-comment\"></div>\n"
]
},
{
"blockName": null,
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n",
"innerContent": [
"\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:post-comment -->
<div class="wp-block-post-comment"></div>
<!-- /wp:post-comment -->