| 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 |
* @param array $attributes Block attributes. |
| 12 |
* @param string $content Block default content. |
| 13 |
* @param WP_Block $block Block instance. |
| 14 |
* |
| 15 |
* @return string Returns the pagination numbers for the comments. |
| 16 |
*/ |
| 17 |
function gutenberg_render_block_core_comments_pagination_numbers( $attributes, $content, $block ) { |
| 18 |
// Bail out early if the post ID is not set for some reason. |
| 19 |
if ( empty( $block->context['postId'] ) ) { |
| 20 |
return ''; |
| 21 |
} |
| 22 |
|
| 23 |
$comments_query = new WP_Comment_Query( |
| 24 |
build_comment_query_vars_from_block( $block ) |
| 25 |
); |
| 26 |
$total = $comments_query->max_num_pages; |
| 27 |
|
| 28 |
// Get the current comment page from the URL. |
| 29 |
$current = get_query_var( 'cpage' ); |
| 30 |
if ( ! $current ) { |
| 31 |
// Get the number of the default page. |
| 32 |
$current = 'newest' === get_option( 'default_comments_page' ) ? $total : 1; |
| 33 |
} |
| 34 |
|
| 35 |
// Render links. |
| 36 |
$content = paginate_comments_links( |
| 37 |
array( |
| 38 |
'total' => $total, |
| 39 |
'current' => $current, |
| 40 |
'prev_next' => false, |
| 41 |
'echo' => false, |
| 42 |
) |
| 43 |
); |
| 44 |
|
| 45 |
if ( empty( $content ) ) { |
| 46 |
return ''; |
| 47 |
} |
| 48 |
|
| 49 |
$wrapper_attributes = get_block_wrapper_attributes(); |
| 50 |
|
| 51 |
return sprintf( |
| 52 |
'<div %1$s>%2$s</div>', |
| 53 |
$wrapper_attributes, |
| 54 |
$content |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Registers the `core/comments-pagination-numbers` block on the server. |
| 60 |
*/ |
| 61 |
function gutenberg_register_block_core_comments_pagination_numbers() { |
| 62 |
register_block_type_from_metadata( |
| 63 |
__DIR__ . '/comments-pagination-numbers', |
| 64 |
array( |
| 65 |
'render_callback' => 'gutenberg_render_block_core_comments_pagination_numbers', |
| 66 |
) |
| 67 |
); |
| 68 |
} |
| 69 |
add_action( 'init', 'gutenberg_register_block_core_comments_pagination_numbers', 20 ); |
| 70 |
|