Geen omschrijving

query-pagination-numbers.php 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. // We still need to preserve `paged` query param if exists, as is used
  47. // for Queries that inherit from global context.
  48. $paged = empty( $_GET['paged'] ) ? null : (int) $_GET['paged'];
  49. if ( $paged ) {
  50. $paginate_args['add_args'] = array( 'paged' => $paged );
  51. }
  52. $content = paginate_links( $paginate_args );
  53. wp_reset_postdata(); // Restore original Post Data.
  54. $wp_query = $prev_wp_query;
  55. }
  56. if ( empty( $content ) ) {
  57. return '';
  58. }
  59. return sprintf(
  60. '<div %1$s>%2$s</div>',
  61. $wrapper_attributes,
  62. $content
  63. );
  64. }
  65. /**
  66. * Registers the `core/query-pagination-numbers` block on the server.
  67. */
  68. function register_block_core_query_pagination_numbers() {
  69. register_block_type_from_metadata(
  70. __DIR__ . '/query-pagination-numbers',
  71. array(
  72. 'render_callback' => 'render_block_core_query_pagination_numbers',
  73. )
  74. );
  75. }
  76. add_action( 'init', 'register_block_core_query_pagination_numbers' );