Skip to content

Commit

Permalink
Add post comment content block
Browse files Browse the repository at this point in the history
  • Loading branch information
david-szabo97 authored and ockham committed Aug 25, 2020
1 parent 250778b commit 8be0a42
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 19 deletions.
37 changes: 19 additions & 18 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,25 @@ function gutenberg_reregister_core_block_types() {
$block_names = array_merge(
$block_names,
array(
'post-author.php' => 'core/post-author',
'post-comment.php' => 'core/post-comment',
'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
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ 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 @@ -210,6 +211,7 @@ export const __experimentalRegisterExperimentalCoreBlocks =
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>;
}
20 changes: 20 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,20 @@
/**
* 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 Comment Content' ),
description: __( 'Post Comment Content' ),
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' );
2 changes: 1 addition & 1 deletion packages/block-library/src/post-comment/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useState } from '@wordpress/element';
import { blockDefault } from '@wordpress/icons';
import { InnerBlocks } from '@wordpress/block-editor';

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

// TODO: JSDOC types
export default function Edit( { className, attributes, setAttributes } ) {
Expand Down

0 comments on commit 8be0a42

Please sign in to comment.