1<?php
2/**
3 * Server-side rendering of the `core/comments-pagination-numbers` block.
4 *
5 * @package WordPress
6 */
7
8/**
9 * Renders the `core/comments-pagination-numbers` block on the server.
10 *
11 * @since 6.0.0
12 *
13 * @param array $attributes Block attributes.
14 * @param string $content Block default content.
15 * @param WP_Block $block Block instance.
16 *
17 * @return string Returns the pagination numbers for the comments.
18 */
19function render_block_core_comments_pagination_numbers( $attributes, $content, $block ) {
20 // Bail out early if the post ID is not set for some reason.
21 if ( empty( $block->context['postId'] ) ) {
22 return '';
23 }
24
25 $comment_vars = build_comment_query_vars_from_block( $block );
26
27 $total = ( new WP_Comment_Query( $comment_vars ) )->max_num_pages;
28 $current = ! empty( $comment_vars['paged'] ) ? $comment_vars['paged'] : null;
29
30 // Render links.
31 $content = paginate_comments_links(
32 array(
33 'total' => $total,
34 'current' => $current,
35 'prev_next' => false,
36 'echo' => false,
37 )
38 );
39
40 if ( empty( $content ) ) {
41 return '';
42 }
43
44 $wrapper_attributes = get_block_wrapper_attributes();
45
46 return sprintf(
47 '<div %1$s>%2$s</div>',
48 $wrapper_attributes,
49 $content
50 );
51}
52
53/**
54 * Registers the `core/comments-pagination-numbers` block on the server.
55 *
56 * @since 6.0.0
57 */
58function register_block_core_comments_pagination_numbers() {
59 register_block_type_from_metadata(
60 __DIR__ . '/comments-pagination-numbers',
61 array(
62 'render_callback' => 'render_block_core_comments_pagination_numbers',
63 )
64 );
65}
66add_action( 'init', 'register_block_core_comments_pagination_numbers' );
67