comments-pagination-next.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/comments-pagination-next` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/comments-pagination-next` 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 next comments link for the query pagination.
  15. */
  16. function render_block_core_comments_pagination_next( $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. $max_page = ( new WP_Comment_Query( $comment_vars ) )->max_num_pages;
  23. $default_label = __( 'Newer Comments' );
  24. $label = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? $attributes['label'] : $default_label;
  25. $pagination_arrow = get_comments_pagination_arrow( $block, 'next' );
  26. $filter_link_attributes = function() {
  27. return get_block_wrapper_attributes();
  28. };
  29. add_filter( 'next_comments_link_attributes', $filter_link_attributes );
  30. if ( $pagination_arrow ) {
  31. $label .= $pagination_arrow;
  32. }
  33. $next_comments_link = get_next_comments_link( $label, $max_page );
  34. remove_filter( 'next_posts_link_attributes', $filter_link_attributes );
  35. if ( ! isset( $next_comments_link ) ) {
  36. return '';
  37. }
  38. return $next_comments_link;
  39. }
  40. /**
  41. * Registers the `core/comments-pagination-next` block on the server.
  42. */
  43. function register_block_core_comments_pagination_next() {
  44. register_block_type_from_metadata(
  45. __DIR__ . '/comments-pagination-next',
  46. array(
  47. 'render_callback' => 'render_block_core_comments_pagination_next',
  48. )
  49. );
  50. }
  51. add_action( 'init', 'register_block_core_comments_pagination_next' );