-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Library: Add Comments Pagination Numbers block (#36890)
* Block Library: Init Comments Pagination Numbers Copy/paste the Query Pagination Numbers block and add a few changes, like the block name. * Block Library: Implement SSR for Comment Pagination Numbers * Block Library: Add fixtures for Comment Pagination Numbers * Update 'Comments Pagination Numbers' block description Co-authored-by: Greg Ziółkowski <[email protected]> * Fix 'Comments Pagination Numbers' edit name * Only allow Numbers inside Comments Pagination * Show Comments Pagination block in site editor * Get `cpage` value from the WP query * Fix default comments page number * Add Comment Pagination missing styles * Change block category to `theme` Co-authored-by: Carlos Bravo <[email protected]> Co-authored-by: Greg Ziółkowski <[email protected]> Co-authored-by: Carlos Bravo <[email protected]>
- Loading branch information
1 parent
abec4ed
commit 16f3e25
Showing
14 changed files
with
236 additions
and
56 deletions.
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
16 changes: 16 additions & 0 deletions
16
packages/block-library/src/comments-pagination-numbers/block.json
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,16 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"name": "core/comments-pagination-numbers", | ||
"title": "Comments Pagination Numbers", | ||
"category": "theme", | ||
"parent": [ "core/comments-pagination" ], | ||
"description": "Displays a list of page numbers for comments pagination.", | ||
"textdomain": "default", | ||
"usesContext": [ "queryId", "queryPerPage", "postId" ], | ||
"supports": { | ||
"reusable": false, | ||
"html": false | ||
}, | ||
"editorStyle": "comments-pagination-numbers-editor" | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/block-library/src/comments-pagination-numbers/edit.js
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,22 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
|
||
const PaginationItem = ( { content, tag: Tag = 'a', extraClass = '' } ) => ( | ||
<Tag className={ `page-numbers ${ extraClass }` }>{ content }</Tag> | ||
); | ||
|
||
export default function CommentsPaginationNumbersEdit() { | ||
return ( | ||
<div { ...useBlockProps() }> | ||
<PaginationItem content="1" /> | ||
<PaginationItem content="2" /> | ||
<PaginationItem content="3" tag="span" extraClass="current" /> | ||
<PaginationItem content="4" /> | ||
<PaginationItem content="5" /> | ||
<PaginationItem content="..." tag="span" extraClass="dots" /> | ||
<PaginationItem content="8" /> | ||
</div> | ||
); | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/block-library/src/comments-pagination-numbers/editor.scss
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,12 @@ | ||
.wp-block-comments-pagination-numbers { | ||
a { | ||
text-decoration: underline; | ||
} | ||
.page-numbers { | ||
margin-right: 2px; | ||
&:last-child { | ||
/*rtl:ignore*/ | ||
margin-right: 0; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/block-library/src/comments-pagination-numbers/index.js
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,18 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { queryPaginationNumbers 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 = { | ||
icon, | ||
edit, | ||
}; |
79 changes: 79 additions & 0 deletions
79
packages/block-library/src/comments-pagination-numbers/index.php
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,79 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/comments-pagination-numbers` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `core/comments-pagination-numbers` 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 pagination numbers for the comments. | ||
*/ | ||
function render_block_core_comments_pagination_numbers( $attributes, $content, $block ) { | ||
// Get the post ID from which comments should be retrieved. | ||
$post_id = isset( $block->context['postId'] ) | ||
? $block->context['postId'] | ||
: get_the_id(); | ||
|
||
if ( ! $post_id ) { | ||
return ''; | ||
} | ||
|
||
// Get the 'comments per page' setting. | ||
$per_page = isset( $block->context['queryPerPage'] ) | ||
? $block->context['queryPerPage'] | ||
: get_option( 'comments_per_page' ); | ||
|
||
// Get the total number of pages. | ||
$comments = get_approved_comments( $post_id ); | ||
$total = get_comment_pages_count( $comments, $per_page ); | ||
|
||
// Get the number of the default page. | ||
$default_page = 'newest' === get_option( 'default_comments_page' ) ? $total : 1; | ||
|
||
// Get the current comment page from the URL. | ||
$current = get_query_var( 'cpage' ); | ||
if ( ! $current ) { | ||
$current = $default_page; | ||
} | ||
|
||
// Render links. | ||
$content = paginate_comments_links( | ||
array( | ||
'total' => $total, | ||
'current' => $current, | ||
'prev_next' => false, | ||
'echo' => false, | ||
) | ||
); | ||
|
||
if ( empty( $content ) ) { | ||
return ''; | ||
} | ||
|
||
$wrapper_attributes = get_block_wrapper_attributes(); | ||
|
||
return sprintf( | ||
'<div %1$s>%2$s</div>', | ||
$wrapper_attributes, | ||
$content | ||
); | ||
} | ||
|
||
/** | ||
* Registers the `core/comments-pagination-numbers` block on the server. | ||
*/ | ||
function register_block_core_comments_pagination_numbers() { | ||
register_block_type_from_metadata( | ||
__DIR__ . '/comments-pagination-numbers', | ||
array( | ||
'render_callback' => 'render_block_core_comments_pagination_numbers', | ||
) | ||
); | ||
} | ||
add_action( 'init', 'register_block_core_comments_pagination_numbers' ); |
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
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
1 change: 1 addition & 0 deletions
1
test/integration/fixtures/blocks/core__comments-pagination-numbers.html
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 @@ | ||
<!-- wp:comments-pagination-numbers /--> |
10 changes: 10 additions & 0 deletions
10
test/integration/fixtures/blocks/core__comments-pagination-numbers.json
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,10 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/comments-pagination-numbers", | ||
"isValid": true, | ||
"attributes": {}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
} | ||
] |
9 changes: 9 additions & 0 deletions
9
test/integration/fixtures/blocks/core__comments-pagination-numbers.parsed.json
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,9 @@ | ||
[ | ||
{ | ||
"blockName": "core/comments-pagination-numbers", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "", | ||
"innerContent": [] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
test/integration/fixtures/blocks/core__comments-pagination-numbers.serialized.html
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 @@ | ||
<!-- wp:comments-pagination-numbers /--> |