From a422ce902f34d893ef6ea1f2ea1d67568b41eebf Mon Sep 17 00:00:00 2001 From: David Arenas Date: Thu, 25 Nov 2021 17:24:57 +0100 Subject: [PATCH 01/11] Block Library: Init Comments Pagination Numbers Copy/paste the Query Pagination Numbers block and add a few changes, like the block name. --- .../comments-pagination-numbers/block.json | 16 +++ .../src/comments-pagination-numbers/edit.js | 22 ++++ .../comments-pagination-numbers/editor.scss | 12 +++ .../src/comments-pagination-numbers/index.js | 18 ++++ .../src/comments-pagination-numbers/index.php | 101 ++++++++++++++++++ 5 files changed, 169 insertions(+) create mode 100644 packages/block-library/src/comments-pagination-numbers/block.json create mode 100644 packages/block-library/src/comments-pagination-numbers/edit.js create mode 100644 packages/block-library/src/comments-pagination-numbers/editor.scss create mode 100644 packages/block-library/src/comments-pagination-numbers/index.js create mode 100644 packages/block-library/src/comments-pagination-numbers/index.php diff --git a/packages/block-library/src/comments-pagination-numbers/block.json b/packages/block-library/src/comments-pagination-numbers/block.json new file mode 100644 index 0000000000000..602f39ba2c3d3 --- /dev/null +++ b/packages/block-library/src/comments-pagination-numbers/block.json @@ -0,0 +1,16 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 2, + "name": "core/comments-pagination-numbers", + "title": "Comments Pagination Numbers", + "category": "design", + "parent": [ "core/comments-pagination" ], + "description": "Displays a list of page numbers for comment pagination", + "textdomain": "default", + "usesContext": [ "queryId", "query" ], + "supports": { + "reusable": false, + "html": false + }, + "editorStyle": "comments-pagination-numbers-editor" +} diff --git a/packages/block-library/src/comments-pagination-numbers/edit.js b/packages/block-library/src/comments-pagination-numbers/edit.js new file mode 100644 index 0000000000000..74c099d245c86 --- /dev/null +++ b/packages/block-library/src/comments-pagination-numbers/edit.js @@ -0,0 +1,22 @@ +/** + * WordPress dependencies + */ +import { useBlockProps } from '@wordpress/block-editor'; + +const PaginationItem = ( { content, tag: Tag = 'a', extraClass = '' } ) => ( + { content } +); + +export default function QueryPaginationNumbersEdit() { + return ( +
+ + + + + + + +
+ ); +} diff --git a/packages/block-library/src/comments-pagination-numbers/editor.scss b/packages/block-library/src/comments-pagination-numbers/editor.scss new file mode 100644 index 0000000000000..7f47f1aaf0e60 --- /dev/null +++ b/packages/block-library/src/comments-pagination-numbers/editor.scss @@ -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; + } + } +} diff --git a/packages/block-library/src/comments-pagination-numbers/index.js b/packages/block-library/src/comments-pagination-numbers/index.js new file mode 100644 index 0000000000000..9f1818b1f14a0 --- /dev/null +++ b/packages/block-library/src/comments-pagination-numbers/index.js @@ -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, +}; diff --git a/packages/block-library/src/comments-pagination-numbers/index.php b/packages/block-library/src/comments-pagination-numbers/index.php new file mode 100644 index 0000000000000..f4a46aed04318 --- /dev/null +++ b/packages/block-library/src/comments-pagination-numbers/index.php @@ -0,0 +1,101 @@ +context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; + $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; + $max_page = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0; + + $wrapper_attributes = get_block_wrapper_attributes(); + $content = ''; + global $wp_query; + if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) { + // Take into account if we have set a bigger `max page` + // than what the query has. + $total = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page; + $paginate_args = array( + 'prev_next' => false, + 'total' => $total, + ); + $content = paginate_links( $paginate_args ); + } else { + $block_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) ); + // `paginate_links` works with the global $wp_query, so we have to + // temporarily switch it with our custom query. + $prev_wp_query = $wp_query; + $wp_query = $block_query; + $total = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page; + $paginate_args = array( + 'base' => '%_%', + 'format' => "?$page_key=%#%", + 'current' => max( 1, $page ), + 'total' => $total, + 'prev_next' => false, + ); + if ( 1 !== $page ) { + /** + * `paginate_links` doesn't use the provided `format` when the page is `1`. + * This is great for the main query as it removes the extra query params + * making the URL shorter, but in the case of multiple custom queries is + * problematic. It results in returning an empty link which ends up with + * a link to the current page. + * + * A way to address this is to add a `fake` query arg with no value that + * is the same for all custom queries. This way the link is not empty and + * preserves all the other existent query args. + * + * @see https://developer.wordpress.org/reference/functions/paginate_links/ + * + * The proper fix of this should be in core. Track Ticket: + * @see https://core.trac.wordpress.org/ticket/53868 + * + * TODO: After two WP versions (starting from the WP version the core patch landed), + * we should remove this and call `paginate_links` with the proper new arg. + */ + $paginate_args['add_args'] = array( 'cst' => '' ); + } + // We still need to preserve `paged` query param if exists, as is used + // for Queries that inherit from global context. + $paged = empty( $_GET['paged'] ) ? null : (int) $_GET['paged']; + if ( $paged ) { + $paginate_args['add_args'] = array( 'paged' => $paged ); + } + $content = paginate_links( $paginate_args ); + wp_reset_postdata(); // Restore original Post Data. + $wp_query = $prev_wp_query; + } + if ( empty( $content ) ) { + return ''; + } + return sprintf( + '
%2$s
', + $wrapper_attributes, + $content + ); +} + +/** + * Registers the `core/comments-pagination-numbers` block on the server. + */ +function register_block_core_query_pagination_numbers() { + register_block_type_from_metadata( + __DIR__ . '/comments-pagination-numbers', + array( + 'render_callback' => 'render_block_core_query_pagination_numbers', + ) + ); +} +add_action( 'init', 'register_block_core_query_pagination_numbers' ); From 1a8c4be01ae54b9de9ed1e90b84366671bf18e5e Mon Sep 17 00:00:00 2001 From: David Arenas Date: Fri, 26 Nov 2021 20:15:34 +0100 Subject: [PATCH 02/11] Block Library: Implement SSR for Comment Pagination Numbers --- lib/blocks.php | 111 +++++++++--------- .../comments-pagination-numbers/block.json | 4 +- .../src/comments-pagination-numbers/index.php | 103 ++++++---------- packages/block-library/src/index.js | 2 + 4 files changed, 99 insertions(+), 121 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 4fa8b5027b0a2..090da06a5f9d0 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -53,61 +53,62 @@ function gutenberg_reregister_core_block_types() { 'embed', ), 'block_names' => array( - 'archives.php' => 'core/archives', - 'block.php' => 'core/block', - 'calendar.php' => 'core/calendar', - 'categories.php' => 'core/categories', - 'comment-author-avatar.php' => 'core/comment-author-avatar', - 'comment-author-name.php' => 'core/comment-author-name', - 'comment-content.php' => 'core/comment-content', - 'comment-date.php' => 'core/comment-date', - 'comment-edit-link.php' => 'core/comment-edit-link', - 'comment-reply-link.php' => 'core/comment-reply-link', - 'comment-template.php' => 'core/comment-template', - 'file.php' => 'core/file', - 'home-link.php' => 'core/home-link', - 'image.php' => 'core/image', - 'gallery.php' => 'core/gallery', - 'latest-comments.php' => 'core/latest-comments', - 'latest-posts.php' => 'core/latest-posts', - 'loginout.php' => 'core/loginout', - 'navigation.php' => 'core/navigation', - 'navigation-area.php' => 'core/navigation-area', - 'navigation-link.php' => 'core/navigation-link', - 'navigation-submenu.php' => 'core/navigation-submenu', - 'page-list.php' => 'core/page-list', - 'pattern.php' => 'core/pattern', - '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-comments-link.php' => 'core/post-comments-link', - '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-navigation-link.php' => 'core/post-navigation-link', - 'post-terms.php' => 'core/post-terms', - 'post-title.php' => 'core/post-title', - 'query.php' => 'core/query', - 'post-template.php' => 'core/post-template', - 'query-pagination.php' => 'core/query-pagination', - 'query-pagination-next.php' => 'core/query-pagination-next', - 'query-pagination-numbers.php' => 'core/query-pagination-numbers', - 'query-pagination-previous.php' => 'core/query-pagination-previous', - 'query-title.php' => 'core/query-title', - 'rss.php' => 'core/rss', - 'search.php' => 'core/search', - 'shortcode.php' => 'core/shortcode', - 'social-link.php' => 'core/social-link', - 'site-logo.php' => 'core/site-logo', - 'site-tagline.php' => 'core/site-tagline', - 'site-title.php' => 'core/site-title', - // 'table-of-contents.php' => 'core/table-of-contents', - 'tag-cloud.php' => 'core/tag-cloud', - 'template-part.php' => 'core/template-part', - 'term-description.php' => 'core/term-description', + 'archives.php' => 'core/archives', + 'block.php' => 'core/block', + 'calendar.php' => 'core/calendar', + 'categories.php' => 'core/categories', + 'comment-author-avatar.php' => 'core/comment-author-avatar', + 'comment-author-name.php' => 'core/comment-author-name', + 'comment-content.php' => 'core/comment-content', + 'comment-date.php' => 'core/comment-date', + 'comment-edit-link.php' => 'core/comment-edit-link', + 'comment-reply-link.php' => 'core/comment-reply-link', + 'comment-template.php' => 'core/comment-template', + 'comments-pagination-numbers.php' => 'core/comments-pagination-numbers', + 'file.php' => 'core/file', + 'home-link.php' => 'core/home-link', + 'image.php' => 'core/image', + 'gallery.php' => 'core/gallery', + 'latest-comments.php' => 'core/latest-comments', + 'latest-posts.php' => 'core/latest-posts', + 'loginout.php' => 'core/loginout', + 'navigation.php' => 'core/navigation', + 'navigation-area.php' => 'core/navigation-area', + 'navigation-link.php' => 'core/navigation-link', + 'navigation-submenu.php' => 'core/navigation-submenu', + 'page-list.php' => 'core/page-list', + 'pattern.php' => 'core/pattern', + '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-comments-link.php' => 'core/post-comments-link', + '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-navigation-link.php' => 'core/post-navigation-link', + 'post-terms.php' => 'core/post-terms', + 'post-title.php' => 'core/post-title', + 'query.php' => 'core/query', + 'post-template.php' => 'core/post-template', + 'query-pagination.php' => 'core/query-pagination', + 'query-pagination-next.php' => 'core/query-pagination-next', + 'query-pagination-numbers.php' => 'core/query-pagination-numbers', + 'query-pagination-previous.php' => 'core/query-pagination-previous', + 'query-title.php' => 'core/query-title', + 'rss.php' => 'core/rss', + 'search.php' => 'core/search', + 'shortcode.php' => 'core/shortcode', + 'social-link.php' => 'core/social-link', + 'site-logo.php' => 'core/site-logo', + 'site-tagline.php' => 'core/site-tagline', + 'site-title.php' => 'core/site-title', + // 'table-of-contents.php' => 'core/table-of-contents', + 'tag-cloud.php' => 'core/tag-cloud', + 'template-part.php' => 'core/template-part', + 'term-description.php' => 'core/term-description', ), ), __DIR__ . '/../build/edit-widgets/blocks/' => array( diff --git a/packages/block-library/src/comments-pagination-numbers/block.json b/packages/block-library/src/comments-pagination-numbers/block.json index 602f39ba2c3d3..dd2ed903357b9 100644 --- a/packages/block-library/src/comments-pagination-numbers/block.json +++ b/packages/block-library/src/comments-pagination-numbers/block.json @@ -4,10 +4,10 @@ "name": "core/comments-pagination-numbers", "title": "Comments Pagination Numbers", "category": "design", - "parent": [ "core/comments-pagination" ], + "parent": [ "core/comments-query-loop" ], "description": "Displays a list of page numbers for comment pagination", "textdomain": "default", - "usesContext": [ "queryId", "query" ], + "usesContext": [ "queryId", "queryPerPage", "postId" ], "supports": { "reusable": false, "html": false diff --git a/packages/block-library/src/comments-pagination-numbers/index.php b/packages/block-library/src/comments-pagination-numbers/index.php index f4a46aed04318..5687bae0b196f 100644 --- a/packages/block-library/src/comments-pagination-numbers/index.php +++ b/packages/block-library/src/comments-pagination-numbers/index.php @@ -12,74 +12,49 @@ * @param string $content Block default content. * @param WP_Block $block Block instance. * - * @return string Returns the pagination numbers for the Query. + * @return string Returns the pagination numbers for the comments. */ -function render_block_core_query_pagination_numbers( $attributes, $content, $block ) { - $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page'; - $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; - $max_page = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0; +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(); - $wrapper_attributes = get_block_wrapper_attributes(); - $content = ''; - global $wp_query; - if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) { - // Take into account if we have set a bigger `max page` - // than what the query has. - $total = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page; - $paginate_args = array( - 'prev_next' => false, - 'total' => $total, - ); - $content = paginate_links( $paginate_args ); - } else { - $block_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) ); - // `paginate_links` works with the global $wp_query, so we have to - // temporarily switch it with our custom query. - $prev_wp_query = $wp_query; - $wp_query = $block_query; - $total = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page; - $paginate_args = array( - 'base' => '%_%', - 'format' => "?$page_key=%#%", - 'current' => max( 1, $page ), + 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' ) ? 1 : $total; + + // Get the current comment page from the URL. + $current = empty( $_GET['cpage'] ) ? $default_page : (int) $_GET['cpage']; + + // Render links. + $content = paginate_comments_links( + array( 'total' => $total, + 'current' => $current, 'prev_next' => false, - ); - if ( 1 !== $page ) { - /** - * `paginate_links` doesn't use the provided `format` when the page is `1`. - * This is great for the main query as it removes the extra query params - * making the URL shorter, but in the case of multiple custom queries is - * problematic. It results in returning an empty link which ends up with - * a link to the current page. - * - * A way to address this is to add a `fake` query arg with no value that - * is the same for all custom queries. This way the link is not empty and - * preserves all the other existent query args. - * - * @see https://developer.wordpress.org/reference/functions/paginate_links/ - * - * The proper fix of this should be in core. Track Ticket: - * @see https://core.trac.wordpress.org/ticket/53868 - * - * TODO: After two WP versions (starting from the WP version the core patch landed), - * we should remove this and call `paginate_links` with the proper new arg. - */ - $paginate_args['add_args'] = array( 'cst' => '' ); - } - // We still need to preserve `paged` query param if exists, as is used - // for Queries that inherit from global context. - $paged = empty( $_GET['paged'] ) ? null : (int) $_GET['paged']; - if ( $paged ) { - $paginate_args['add_args'] = array( 'paged' => $paged ); - } - $content = paginate_links( $paginate_args ); - wp_reset_postdata(); // Restore original Post Data. - $wp_query = $prev_wp_query; - } + 'echo' => false, + ) + ); + if ( empty( $content ) ) { return ''; } + + $wrapper_attributes = get_block_wrapper_attributes(); + return sprintf( '
%2$s
', $wrapper_attributes, @@ -90,12 +65,12 @@ function render_block_core_query_pagination_numbers( $attributes, $content, $blo /** * Registers the `core/comments-pagination-numbers` block on the server. */ -function register_block_core_query_pagination_numbers() { +function register_block_core_comments_pagination_numbers() { register_block_type_from_metadata( __DIR__ . '/comments-pagination-numbers', array( - 'render_callback' => 'render_block_core_query_pagination_numbers', + 'render_callback' => 'render_block_core_comments_pagination_numbers', ) ); } -add_action( 'init', 'register_block_core_query_pagination_numbers' ); +add_action( 'init', 'register_block_core_comments_pagination_numbers' ); diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 76ac03d8ef5f3..8fa9fb5eb310e 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -31,6 +31,7 @@ import * as commentReplyLink from './comment-reply-link'; import * as commentTemplate from './comment-template'; import * as commentsQueryLoop from './comments-query-loop'; import * as commentsPagination from './comments-pagination'; +import * as commentsPaginationNumbers from './comments-pagination-numbers'; import * as cover from './cover'; import * as embed from './embed'; import * as file from './file'; @@ -256,6 +257,7 @@ export const __experimentalRegisterExperimentalCoreBlocks = commentTemplate, commentsQueryLoop, commentsPagination, + commentsPaginationNumbers, navigationArea, postComment, postCommentsCount, From e8378cf6d5426682e8cdd03d097f2469f935e675 Mon Sep 17 00:00:00 2001 From: David Arenas Date: Fri, 26 Nov 2021 20:23:59 +0100 Subject: [PATCH 03/11] Block Library: Add fixtures for Comment Pagination Numbers --- .../blocks/core__comments-pagination-numbers.html | 1 + .../blocks/core__comments-pagination-numbers.json | 10 ++++++++++ .../core__comments-pagination-numbers.parsed.json | 9 +++++++++ .../core__comments-pagination-numbers.serialized.html | 1 + 4 files changed, 21 insertions(+) create mode 100644 test/integration/fixtures/blocks/core__comments-pagination-numbers.html create mode 100644 test/integration/fixtures/blocks/core__comments-pagination-numbers.json create mode 100644 test/integration/fixtures/blocks/core__comments-pagination-numbers.parsed.json create mode 100644 test/integration/fixtures/blocks/core__comments-pagination-numbers.serialized.html diff --git a/test/integration/fixtures/blocks/core__comments-pagination-numbers.html b/test/integration/fixtures/blocks/core__comments-pagination-numbers.html new file mode 100644 index 0000000000000..d901be9ff8a23 --- /dev/null +++ b/test/integration/fixtures/blocks/core__comments-pagination-numbers.html @@ -0,0 +1 @@ + diff --git a/test/integration/fixtures/blocks/core__comments-pagination-numbers.json b/test/integration/fixtures/blocks/core__comments-pagination-numbers.json new file mode 100644 index 0000000000000..350179e9a4e37 --- /dev/null +++ b/test/integration/fixtures/blocks/core__comments-pagination-numbers.json @@ -0,0 +1,10 @@ +[ + { + "clientId": "_clientId_0", + "name": "core/comments-pagination-numbers", + "isValid": true, + "attributes": {}, + "innerBlocks": [], + "originalContent": "" + } +] diff --git a/test/integration/fixtures/blocks/core__comments-pagination-numbers.parsed.json b/test/integration/fixtures/blocks/core__comments-pagination-numbers.parsed.json new file mode 100644 index 0000000000000..d6d847d8b3e50 --- /dev/null +++ b/test/integration/fixtures/blocks/core__comments-pagination-numbers.parsed.json @@ -0,0 +1,9 @@ +[ + { + "blockName": "core/comments-pagination-numbers", + "attrs": {}, + "innerBlocks": [], + "innerHTML": "", + "innerContent": [] + } +] diff --git a/test/integration/fixtures/blocks/core__comments-pagination-numbers.serialized.html b/test/integration/fixtures/blocks/core__comments-pagination-numbers.serialized.html new file mode 100644 index 0000000000000..d901be9ff8a23 --- /dev/null +++ b/test/integration/fixtures/blocks/core__comments-pagination-numbers.serialized.html @@ -0,0 +1 @@ + From 817648337a73a72904057fbca656696e88cbef34 Mon Sep 17 00:00:00 2001 From: David Arenas Date: Tue, 30 Nov 2021 19:56:08 +0100 Subject: [PATCH 04/11] Update 'Comments Pagination Numbers' block description MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Greg Ziółkowski --- .../block-library/src/comments-pagination-numbers/block.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/comments-pagination-numbers/block.json b/packages/block-library/src/comments-pagination-numbers/block.json index dd2ed903357b9..9e89fd6715d17 100644 --- a/packages/block-library/src/comments-pagination-numbers/block.json +++ b/packages/block-library/src/comments-pagination-numbers/block.json @@ -5,7 +5,7 @@ "title": "Comments Pagination Numbers", "category": "design", "parent": [ "core/comments-query-loop" ], - "description": "Displays a list of page numbers for comment pagination", + "description": "Displays a list of page numbers for comments pagination.", "textdomain": "default", "usesContext": [ "queryId", "queryPerPage", "postId" ], "supports": { From 2180ab5b36889cacba31c69d4ea76450a3ae8a9c Mon Sep 17 00:00:00 2001 From: David Arenas Date: Tue, 30 Nov 2021 20:51:29 +0100 Subject: [PATCH 05/11] Fix 'Comments Pagination Numbers' edit name --- packages/block-library/src/comments-pagination-numbers/edit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/comments-pagination-numbers/edit.js b/packages/block-library/src/comments-pagination-numbers/edit.js index 74c099d245c86..39c047f062125 100644 --- a/packages/block-library/src/comments-pagination-numbers/edit.js +++ b/packages/block-library/src/comments-pagination-numbers/edit.js @@ -7,7 +7,7 @@ const PaginationItem = ( { content, tag: Tag = 'a', extraClass = '' } ) => ( { content } ); -export default function QueryPaginationNumbersEdit() { +export default function CommentsPaginationNumbersEdit() { return (
From bea7423cdb06cb844363500d40a919ab8539c92a Mon Sep 17 00:00:00 2001 From: David Arenas Date: Tue, 30 Nov 2021 21:07:08 +0100 Subject: [PATCH 06/11] Only allow Numbers inside Comments Pagination --- .../src/comments-pagination-numbers/block.json | 2 +- packages/block-library/src/comments-pagination/edit.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/comments-pagination-numbers/block.json b/packages/block-library/src/comments-pagination-numbers/block.json index 9e89fd6715d17..62e2f1c5945bf 100644 --- a/packages/block-library/src/comments-pagination-numbers/block.json +++ b/packages/block-library/src/comments-pagination-numbers/block.json @@ -4,7 +4,7 @@ "name": "core/comments-pagination-numbers", "title": "Comments Pagination Numbers", "category": "design", - "parent": [ "core/comments-query-loop" ], + "parent": [ "core/comments-pagination" ], "description": "Displays a list of page numbers for comments pagination.", "textdomain": "default", "usesContext": [ "queryId", "queryPerPage", "postId" ], diff --git a/packages/block-library/src/comments-pagination/edit.js b/packages/block-library/src/comments-pagination/edit.js index b75cd887b6958..bd9051e16f05d 100644 --- a/packages/block-library/src/comments-pagination/edit.js +++ b/packages/block-library/src/comments-pagination/edit.js @@ -17,6 +17,9 @@ import { PanelBody } from '@wordpress/components'; */ import { CommentsPaginationArrowControls } from './comments-pagination-arrow-controls'; +// TODO: add pagination-previous/next blocks once they are implemented. +const TEMPLATE = [ [ 'core/comments-pagination-numbers' ] ]; + const getDefaultBlockLayout = ( blockTypeOrName ) => { const layoutBlockSupportConfig = getBlockSupport( blockTypeOrName, @@ -48,6 +51,11 @@ export default function QueryPaginationEdit( { }, [] ); const blockProps = useBlockProps(); const innerBlocksProps = useInnerBlocksProps( blockProps, { + template: TEMPLATE, + allowedBlocks: [ + // TODO: add pagination-previous/next blocks once they are implemented. + 'core/comments-pagination-numbers', + ], __experimentalLayout: usedLayout, } ); return ( From 2b89d5aed47dec6d660404d67ea53cf01c24202a Mon Sep 17 00:00:00 2001 From: David Arenas Date: Thu, 2 Dec 2021 11:47:52 +0100 Subject: [PATCH 07/11] Show Comments Pagination block in site editor --- packages/block-library/src/comments-pagination/block.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/block-library/src/comments-pagination/block.json b/packages/block-library/src/comments-pagination/block.json index f81e64adf028e..7ddbce715c44b 100644 --- a/packages/block-library/src/comments-pagination/block.json +++ b/packages/block-library/src/comments-pagination/block.json @@ -20,7 +20,6 @@ "align": true, "reusable": false, "html": false, - "inserter": false, "color": { "gradients": true, "link": true From 6a998c76237e1b831613fe7b28a0c3ff2a531b7a Mon Sep 17 00:00:00 2001 From: David Arenas Date: Thu, 2 Dec 2021 14:03:42 +0100 Subject: [PATCH 08/11] Get `cpage` value from the WP query --- .../block-library/src/comments-pagination-numbers/index.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/comments-pagination-numbers/index.php b/packages/block-library/src/comments-pagination-numbers/index.php index 5687bae0b196f..36f33a446e68b 100644 --- a/packages/block-library/src/comments-pagination-numbers/index.php +++ b/packages/block-library/src/comments-pagination-numbers/index.php @@ -37,7 +37,8 @@ function render_block_core_comments_pagination_numbers( $attributes, $content, $ $default_page = 'newest' === get_option( 'default_comments_page' ) ? 1 : $total; // Get the current comment page from the URL. - $current = empty( $_GET['cpage'] ) ? $default_page : (int) $_GET['cpage']; + $cpage = get_query_var( 'cpage' ); + $current = ! $cpage ? $default_page : $cpage; // Render links. $content = paginate_comments_links( From 73fc4d65cc5f93cfa789f7f2dbc54d6364b780fc Mon Sep 17 00:00:00 2001 From: David Arenas Date: Thu, 2 Dec 2021 14:29:07 +0100 Subject: [PATCH 09/11] Fix default comments page number --- .../src/comments-pagination-numbers/index.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/comments-pagination-numbers/index.php b/packages/block-library/src/comments-pagination-numbers/index.php index 36f33a446e68b..58d11824c027b 100644 --- a/packages/block-library/src/comments-pagination-numbers/index.php +++ b/packages/block-library/src/comments-pagination-numbers/index.php @@ -34,11 +34,13 @@ function render_block_core_comments_pagination_numbers( $attributes, $content, $ $total = get_comment_pages_count( $comments, $per_page ); // Get the number of the default page. - $default_page = 'newest' === get_option( 'default_comments_page' ) ? 1 : $total; + $default_page = 'newest' === get_option( 'default_comments_page' ) ? $total : 1; // Get the current comment page from the URL. - $cpage = get_query_var( 'cpage' ); - $current = ! $cpage ? $default_page : $cpage; + $current = get_query_var( 'cpage' ); + if ( ! $current ) { + $current = $default_page; + } // Render links. $content = paginate_comments_links( From 30b4c9f86e3786d1280ad26290a7d880bd5e1453 Mon Sep 17 00:00:00 2001 From: David Arenas Date: Thu, 2 Dec 2021 14:36:43 +0100 Subject: [PATCH 10/11] Add Comment Pagination missing styles --- packages/block-library/src/editor.scss | 2 ++ packages/block-library/src/style.scss | 1 + 2 files changed, 3 insertions(+) diff --git a/packages/block-library/src/editor.scss b/packages/block-library/src/editor.scss index 7aebaf17a09cb..29e905b79844b 100644 --- a/packages/block-library/src/editor.scss +++ b/packages/block-library/src/editor.scss @@ -5,6 +5,8 @@ @import "./buttons/editor.scss"; @import "./categories/editor.scss"; @import "./columns/editor.scss"; +@import "./comments-pagination/editor.scss"; +@import "./comments-pagination-numbers/editor.scss"; @import "./cover/editor.scss"; @import "./embed/editor.scss"; @import "./file/editor.scss"; diff --git a/packages/block-library/src/style.scss b/packages/block-library/src/style.scss index 2b28eab0c71b9..46856ba0071bf 100644 --- a/packages/block-library/src/style.scss +++ b/packages/block-library/src/style.scss @@ -6,6 +6,7 @@ @import "./categories/style.scss"; @import "./code/style.scss"; @import "./columns/style.scss"; +@import "./comments-pagination/style.scss"; @import "./cover/style.scss"; @import "./embed/style.scss"; @import "./file/style.scss"; From 4f35aba37f17fefc8bc4b65a2647c70d3719626b Mon Sep 17 00:00:00 2001 From: David Arenas Date: Thu, 2 Dec 2021 14:43:44 +0100 Subject: [PATCH 11/11] Change block category to `theme` Co-authored-by: Carlos Bravo <37012961+c4rl0sbr4v0@users.noreply.github.com> --- .../block-library/src/comments-pagination-numbers/block.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/comments-pagination-numbers/block.json b/packages/block-library/src/comments-pagination-numbers/block.json index 62e2f1c5945bf..c54fead949adb 100644 --- a/packages/block-library/src/comments-pagination-numbers/block.json +++ b/packages/block-library/src/comments-pagination-numbers/block.json @@ -3,7 +3,7 @@ "apiVersion": 2, "name": "core/comments-pagination-numbers", "title": "Comments Pagination Numbers", - "category": "design", + "category": "theme", "parent": [ "core/comments-pagination" ], "description": "Displays a list of page numbers for comments pagination.", "textdomain": "default",