comments-pagination-numbers.php 1.6 KB

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