query-pagination-numbers.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/query-pagination-numbers` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/query-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 Query.
  15. */
  16. function render_block_core_query_pagination_numbers( $attributes, $content, $block ) {
  17. $page_key = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
  18. $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
  19. $max_page = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0;
  20. $wrapper_attributes = get_block_wrapper_attributes();
  21. $content = '';
  22. global $wp_query;
  23. if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
  24. // Take into account if we have set a bigger `max page`
  25. // than what the query has.
  26. $total = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page;
  27. $paginate_args = array(
  28. 'prev_next' => false,
  29. 'total' => $total,
  30. );
  31. $content = paginate_links( $paginate_args );
  32. } else {
  33. $block_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
  34. // `paginate_links` works with the global $wp_query, so we have to
  35. // temporarily switch it with our custom query.
  36. $prev_wp_query = $wp_query;
  37. $wp_query = $block_query;
  38. $total = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page;
  39. $paginate_args = array(
  40. 'base' => '%_%',
  41. 'format' => "?$page_key=%#%",
  42. 'current' => max( 1, $page ),
  43. 'total' => $total,
  44. 'prev_next' => false,
  45. );
  46. if ( 1 !== $page ) {
  47. /**
  48. * `paginate_links` doesn't use the provided `format` when the page is `1`.
  49. * This is great for the main query as it removes the extra query params
  50. * making the URL shorter, but in the case of multiple custom queries is
  51. * problematic. It results in returning an empty link which ends up with
  52. * a link to the current page.
  53. *
  54. * A way to address this is to add a `fake` query arg with no value that
  55. * is the same for all custom queries. This way the link is not empty and
  56. * preserves all the other existent query args.
  57. *
  58. * @see https://developer.wordpress.org/reference/functions/paginate_links/
  59. *
  60. * The proper fix of this should be in core. Track Ticket:
  61. * @see https://core.trac.wordpress.org/ticket/53868
  62. *
  63. * TODO: After two WP versions (starting from the WP version the core patch landed),
  64. * we should remove this and call `paginate_links` with the proper new arg.
  65. */
  66. $paginate_args['add_args'] = array( 'cst' => '' );
  67. }
  68. // We still need to preserve `paged` query param if exists, as is used
  69. // for Queries that inherit from global context.
  70. $paged = empty( $_GET['paged'] ) ? null : (int) $_GET['paged'];
  71. if ( $paged ) {
  72. $paginate_args['add_args'] = array( 'paged' => $paged );
  73. }
  74. $content = paginate_links( $paginate_args );
  75. wp_reset_postdata(); // Restore original Post Data.
  76. $wp_query = $prev_wp_query;
  77. }
  78. if ( empty( $content ) ) {
  79. return '';
  80. }
  81. return sprintf(
  82. '<div %1$s>%2$s</div>',
  83. $wrapper_attributes,
  84. $content
  85. );
  86. }
  87. /**
  88. * Registers the `core/query-pagination-numbers` block on the server.
  89. */
  90. function register_block_core_query_pagination_numbers() {
  91. register_block_type_from_metadata(
  92. __DIR__ . '/query-pagination-numbers',
  93. array(
  94. 'render_callback' => 'render_block_core_query_pagination_numbers',
  95. )
  96. );
  97. }
  98. add_action( 'init', 'register_block_core_query_pagination_numbers' );