-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
gziolo
merged 57 commits into
WordPress:trunk
from
michalczaplinski:add/post-comment-query-loop-block
Oct 28, 2021
Merged
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 09ff3f7
Add a stub for core/comments-template block
michalczaplinski e9fdf51
Clean up the implementation of core/comments-query
michalczaplinski a5e5ef6
Move query-content & query-setup to separate files
michalczaplinski b6e778c
Remove the post-comments-query block
michalczaplinski 4f81fc5
Refactor getFirstQueryClientIdFromBlocks to utils.js
michalczaplinski 96ef74f
Add the variations of the comments query block
michalczaplinski ecc7a19
Remove unnecesary parameters from block.json
michalczaplinski 37d163f
Add a proper query-placeholder component
michalczaplinski 1283ce0
Add instruction about where to add new integration test fixtures
michalczaplinski 4894f5d
Fix typo in comments-template block.json
michalczaplinski b92b907
Add extra space in error message in full-content.test.js
michalczaplinski 45eef09
Add integration test fixtures for new blocks
michalczaplinski df5bab2
Add postComments icon to core/comments-query block
michalczaplinski f4b9b39
Add offset back to comments-query block
michalczaplinski be56f13
Delete files that are out of scope for this PR
michalczaplinski 9f03cdf
Remove variations from comments-query
michalczaplinski db330ab
Refactor edit component of comments-query
michalczaplinski 539a9b8
Remove unused utils
michalczaplinski 134dd84
Comments Query: Refactor edit and toolbar
michalczaplinski f64fd9c
Comments Query: Update values of perPage, page & offset attributes
michalczaplinski a8c6ae3
Add a CSS class to the QueryToolbar
michalczaplinski 1bf6c0a
CommentsQuery: Update function name
michalczaplinski 2371c81
Remove order and orderBy from block.json
michalczaplinski 667d23a
Comments Template: Stringify the query
michalczaplinski 7a9c6ad
Update the integration test fixture
michalczaplinski abef725
Update icon for comments-template
michalczaplinski 360bb00
Remove unnecessary CSS
michalczaplinski fcd9756
Comments Query: Remove the unnecessary "div"
michalczaplinski 3a85dc8
Comments Query: Remove `displayLayout` from block.json
michalczaplinski 4ff2430
Comments Query: remove _experimentalLayout
michalczaplinski 95e3860
Comments Query: Remove unused queryContext
michalczaplinski e46472e
Comments Query: Remove inherit & pages attribute
michalczaplinski e790f4e
Comments Query: Refactor the attributes
michalczaplinski 553d549
Comments Query: Update the offset label
michalczaplinski 8247bfa
Comments Query: Simplify passing of setAttributes
michalczaplinski 2ebf1bf
Comments Query: update the snapshot
michalczaplinski 4183b29
Update lib/blocks.php
michalczaplinski 59d909a
Comments Query rename:
michalczaplinski a0e782e
Comment Template block: Add index.php
michalczaplinski 2fe9c22
Comments Query: Remove offset attribute
michalczaplinski d28a0f4
Comments Query: Remove outdated fixtures
michalczaplinski 32cb2e9
Comments Query: Add @wordpress/editor to deps
michalczaplinski 8df615b
Comments Query: Add displaying basic comments content
michalczaplinski 947a07d
Comments Query: Remove more old fixtures
michalczaplinski e7fe9fd
Comments Query: Add a very basic server-side render
michalczaplinski 8fb987a
Comments Query: Simplify query for more comments
michalczaplinski 39c600d
Comments Query: Reformat comment-template.php
michalczaplinski c1f24ec
Comments Query: Use the postId from the block context
michalczaplinski d1c9799
Comments Query: Remove redundant Fragment in toolbar
michalczaplinski f52b2d6
Comments Query: Remove unnecessary layout stuff
michalczaplinski cec411f
Comments Query: Remove .php file and list block.json
michalczaplinski ec7e7aa
Comments Query: Get postId from context on server
michalczaplinski 1668cd3
Comments Query: Pass queryPerPage to getEntityRecords
michalczaplinski d289719
Comments Query: Pass queryPerPage to get_approved_comments
michalczaplinski 0264d67
Fix block names array that I messed up while rebasing
michalczaplinski a643247
Update packages/block-library/src/comment-template/edit.js
gziolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [ | ||
[ '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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } /> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?