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

Site Editor: Add new Comments Query Loop block #35231

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
df5b93c
Add most basic skeleton for Comments Query Loop block
michalczaplinski Sep 27, 2021
09ff3f7
Add a stub for core/comments-template block
michalczaplinski Sep 29, 2021
e9fdf51
Clean up the implementation of core/comments-query
michalczaplinski Sep 29, 2021
a5e5ef6
Move query-content & query-setup to separate files
michalczaplinski Sep 30, 2021
b6e778c
Remove the post-comments-query block
michalczaplinski Sep 30, 2021
4f81fc5
Refactor getFirstQueryClientIdFromBlocks to utils.js
michalczaplinski Oct 1, 2021
96ef74f
Add the variations of the comments query block
michalczaplinski Oct 5, 2021
ecc7a19
Remove unnecesary parameters from block.json
michalczaplinski Oct 5, 2021
37d163f
Add a proper query-placeholder component
michalczaplinski Oct 5, 2021
1283ce0
Add instruction about where to add new integration test fixtures
michalczaplinski Oct 5, 2021
4894f5d
Fix typo in comments-template block.json
michalczaplinski Oct 6, 2021
b92b907
Add extra space in error message in full-content.test.js
michalczaplinski Oct 6, 2021
45eef09
Add integration test fixtures for new blocks
michalczaplinski Oct 6, 2021
df5bab2
Add postComments icon to core/comments-query block
michalczaplinski Oct 6, 2021
f4b9b39
Add offset back to comments-query block
michalczaplinski Oct 7, 2021
be56f13
Delete files that are out of scope for this PR
michalczaplinski Oct 7, 2021
9f03cdf
Remove variations from comments-query
michalczaplinski Oct 7, 2021
db330ab
Refactor edit component of comments-query
michalczaplinski Oct 7, 2021
539a9b8
Remove unused utils
michalczaplinski Oct 7, 2021
134dd84
Comments Query: Refactor edit and toolbar
michalczaplinski Oct 7, 2021
f64fd9c
Comments Query: Update values of perPage, page & offset attributes
michalczaplinski Oct 7, 2021
a8c6ae3
Add a CSS class to the QueryToolbar
michalczaplinski Oct 8, 2021
1bf6c0a
CommentsQuery: Update function name
michalczaplinski Oct 8, 2021
2371c81
Remove order and orderBy from block.json
michalczaplinski Oct 8, 2021
667d23a
Comments Template: Stringify the query
michalczaplinski Oct 8, 2021
7a9c6ad
Update the integration test fixture
michalczaplinski Oct 8, 2021
abef725
Update icon for comments-template
michalczaplinski Oct 8, 2021
360bb00
Remove unnecessary CSS
michalczaplinski Oct 8, 2021
fcd9756
Comments Query: Remove the unnecessary "div"
michalczaplinski Oct 15, 2021
3a85dc8
Comments Query: Remove `displayLayout` from block.json
michalczaplinski Oct 15, 2021
4ff2430
Comments Query: remove _experimentalLayout
michalczaplinski Oct 16, 2021
95e3860
Comments Query: Remove unused queryContext
michalczaplinski Oct 16, 2021
e46472e
Comments Query: Remove inherit & pages attribute
michalczaplinski Oct 16, 2021
e790f4e
Comments Query: Refactor the attributes
michalczaplinski Oct 19, 2021
553d549
Comments Query: Update the offset label
michalczaplinski Oct 19, 2021
8247bfa
Comments Query: Simplify passing of setAttributes
michalczaplinski Oct 19, 2021
2ebf1bf
Comments Query: update the snapshot
michalczaplinski Oct 21, 2021
4183b29
Update lib/blocks.php
michalczaplinski Oct 21, 2021
59d909a
Comments Query rename:
michalczaplinski Oct 21, 2021
a0e782e
Comment Template block: Add index.php
michalczaplinski Oct 22, 2021
2fe9c22
Comments Query: Remove offset attribute
michalczaplinski Oct 22, 2021
d28a0f4
Comments Query: Remove outdated fixtures
michalczaplinski Oct 23, 2021
32cb2e9
Comments Query: Add @wordpress/editor to deps
michalczaplinski Oct 24, 2021
8df615b
Comments Query: Add displaying basic comments content
michalczaplinski Oct 24, 2021
947a07d
Comments Query: Remove more old fixtures
michalczaplinski Oct 24, 2021
e7fe9fd
Comments Query: Add a very basic server-side render
michalczaplinski Oct 24, 2021
8fb987a
Comments Query: Simplify query for more comments
michalczaplinski Oct 24, 2021
39c600d
Comments Query: Reformat comment-template.php
michalczaplinski Oct 25, 2021
c1f24ec
Comments Query: Use the postId from the block context
michalczaplinski Oct 26, 2021
d1c9799
Comments Query: Remove redundant Fragment in toolbar
michalczaplinski Oct 26, 2021
f52b2d6
Comments Query: Remove unnecessary layout stuff
michalczaplinski Oct 27, 2021
cec411f
Comments Query: Remove .php file and list block.json
michalczaplinski Oct 27, 2021
ec7e7aa
Comments Query: Get postId from context on server
michalczaplinski Oct 27, 2021
1668cd3
Comments Query: Pass queryPerPage to getEntityRecords
michalczaplinski Oct 27, 2021
d289719
Comments Query: Pass queryPerPage to get_approved_comments
michalczaplinski Oct 27, 2021
0264d67
Fix block names array that I messed up while rebasing
michalczaplinski Oct 27, 2021
a643247
Update packages/block-library/src/comment-template/edit.js
gziolo Oct 28, 2021
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
2 changes: 2 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function gutenberg_reregister_core_block_types() {
'code',
'column',
'columns',
'comments-query-loop',
'cover',
'gallery',
'group',
Expand Down Expand Up @@ -80,6 +81,7 @@ function gutenberg_reregister_core_block_types() {
'post-comments-count.php' => 'core/post-comments-count',
'post-comments-form.php' => 'core/post-comments-form',
'post-comments-link.php' => 'core/post-comments-link',
'comment-template.php' => 'core/comment-template',
'post-content.php' => 'core/post-content',
'post-date.php' => 'core/post-date',
'post-excerpt.php' => 'core/post-excerpt',
Expand Down
15 changes: 15 additions & 0 deletions packages/block-library/src/comment-template/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"apiVersion": 2,
"name": "core/comment-template",
"title": "Comment Template",
"category": "design",
"parent": [ "core/comments-query-loop" ],
"description": "Contains the block elements used to render a comment, like the title, date, author, avatar and more.",
"textdomain": "default",
"usesContext": [ "queryId", "queryPerPage", "postId" ],
"supports": {
"reusable": false,
"html": false,
"align": true
}
}
93 changes: 93 additions & 0 deletions packages/block-library/src/comment-template/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* WordPress dependencies
*/
import { useState, useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import {
BlockContextProvider,
BlockPreview,
useBlockProps,
__experimentalUseInnerBlocksProps as useInnerBlocksProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { Spinner } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';

const TEMPLATE = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kjellr , @jameskoster do you have any suggestions for the default template?

[ 'core/post-comment-author' ],
[ 'core/post-comment-date' ],
[ 'core/post-comment-content' ],
];

export default function CommentTemplateEdit( {
clientId,
context: { postId, queryPerPage },
} ) {
const innerBlocksProps = useInnerBlocksProps( {}, { template: TEMPLATE } );
const blockProps = useBlockProps();

const [ activeBlockContext, setActiveBlockContext ] = useState();

const { comments, blocks } = useSelect(
( select ) => {
const { getEntityRecords } = select( coreStore );
const { getBlocks } = select( blockEditorStore );

return {
comments: getEntityRecords( 'root', 'comment', {
gziolo marked this conversation as resolved.
Show resolved Hide resolved
post: postId,
status: 'approve',
per_page: queryPerPage,
gziolo marked this conversation as resolved.
Show resolved Hide resolved
order: 'asc',
} ),
blocks: getBlocks( clientId ),
};
},
[ queryPerPage, postId, clientId ]
);

const blockContexts = useMemo(
() => comments?.map( ( comment ) => ( { commentId: comment.id } ) ),
[ comments ]
);

if ( ! comments ) {
return (
<p { ...blockProps }>
<Spinner />
</p>
);
}

if ( ! comments.length ) {
return <p { ...blockProps }> { __( 'No results found.' ) }</p>;
}

return (
<ul { ...blockProps }>
{ blockContexts &&
blockContexts.map( ( blockContext ) => (
<BlockContextProvider
key={ blockContext.commentId }
value={ blockContext }
>
{ blockContext ===
( activeBlockContext || blockContexts[ 0 ] ) ? (
<li { ...innerBlocksProps } />
) : (
<li>
<BlockPreview
blocks={ blocks }
__experimentalLive
__experimentalOnClick={ () =>
setActiveBlockContext( blockContext )
}
/>
</li>
) }
</BlockContextProvider>
) ) }
</ul>
);
}
20 changes: 20 additions & 0 deletions packages/block-library/src/comment-template/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import { layout 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 = {
icon,
edit,
save,
};
65 changes: 65 additions & 0 deletions packages/block-library/src/comment-template/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Server-side rendering of the `core/comment-template` block.
*
* @package WordPress
*/

/**
* Renders the `core/comment-template` block on the server.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
*
* @return string Returns the HTML representing the comments using the layout
* defined by the block's inner blocks.
*/
function render_block_core_comment_template( $attributes, $content, $block ) {

$post_id = $block->context['postId'];

// Bail out early if the post ID is not set for some reason.
if ( ! isset( $post_id ) ) {
return '';
}

$number = $block->context['queryPerPage'];

// Get an array of comments for the current post.
$comments = get_approved_comments( $post_id, array( 'number' => $number ) );

if ( count( $comments ) === 0 ) {
return '';
}

$content = '';
foreach ( $comments as $comment ) {
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
$block_content = ( new WP_Block(
$block->parsed_block,
array(
'commentId' => $comment->comment_ID,
)
) )->render( array( 'dynamic' => false ) );
$content .= '<li>' . $block_content . '</li>';
}

return sprintf(
'<ul>%1$s</ul>',
$content
);
}

/**
* Registers the `core/comment-template` block on the server.
*/
function register_block_core_comment_template() {
register_block_type_from_metadata(
__DIR__ . '/comment-template',
array(
'render_callback' => 'render_block_core_comment_template',
'skip_inner_blocks' => true,
)
);
}
add_action( 'init', 'register_block_core_comment_template' );
8 changes: 8 additions & 0 deletions packages/block-library/src/comment-template/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';

export default function CommentTemplateSave() {
return <InnerBlocks.Content />;
}
33 changes: 33 additions & 0 deletions packages/block-library/src/comments-query-loop/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"apiVersion": 2,
"name": "core/comments-query-loop",
"title": "Comments Query Loop",
"category": "theme",
"description": "An advanced block that allows displaying post comments based on different query parameters and visual configurations.",
"textdomain": "default",
"attributes": {
"queryId": {
"type": "number"
},
"queryPerPage": {
"type": "number",
"default": 4
},
"tagName": {
"type": "string",
"default": "div"
}
},
"providesContext": {
"queryId": "queryId",
"queryPerPage": "queryPerPage"
},
"supports": {
"align": [ "wide", "full" ],
"html": false,
"color": {
"gradients": true,
"link": true
}
}
}
53 changes: 53 additions & 0 deletions packages/block-library/src/comments-query-loop/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* WordPress dependencies
*/
import {
BlockControls,
InspectorControls,
useBlockProps,
__experimentalUseInnerBlocksProps as useInnerBlocksProps,
} from '@wordpress/block-editor';
import { SelectControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import QueryToolbar from './toolbar';

const TEMPLATE = [ [ 'core/comment-template' ] ];

export default function CommentsQueryLoopEdit( { attributes, setAttributes } ) {
const { queryPerPage, tagName: TagName } = attributes;

const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( blockProps, {
template: TEMPLATE,
} );

return (
<>
<BlockControls>
<QueryToolbar
queryPerPage={ queryPerPage }
setQuery={ setAttributes }
/>
</BlockControls>
<InspectorControls __experimentalGroup="advanced">
<SelectControl
label={ __( 'HTML element' ) }
options={ [
{ label: __( 'Default (<div>)' ), value: 'div' },
{ label: '<section>', value: 'section' },
{ label: '<aside>', value: 'aside' },
] }
value={ TagName }
onChange={ ( value ) =>
setAttributes( { tagName: value } )
}
/>
</InspectorControls>
<TagName { ...innerBlocksProps } />
</>
);
}
20 changes: 20 additions & 0 deletions packages/block-library/src/comments-query-loop/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import { postComments 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 = {
icon,
edit,
save,
};
14 changes: 14 additions & 0 deletions packages/block-library/src/comments-query-loop/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* WordPress dependencies
*/
import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';

export default function CommentsQueryLoopSave( {
attributes: { tagName: Tag },
} ) {
return (
<Tag { ...useBlockProps.save() }>
<InnerBlocks.Content />
</Tag>
);
}
Loading