query-pagination-next.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/query-pagination-next` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the `core/query-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 posts link for the query pagination.
  15. */
  16. function render_block_core_query_pagination_next( $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. $default_label = __( 'Next Page' );
  22. $label = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? esc_html( $attributes['label'] ) : $default_label;
  23. $pagination_arrow = get_query_pagination_arrow( $block, true );
  24. if ( $pagination_arrow ) {
  25. $label .= $pagination_arrow;
  26. }
  27. $content = '';
  28. // Check if the pagination is for Query that inherits the global context.
  29. if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
  30. $filter_link_attributes = function() use ( $wrapper_attributes ) {
  31. return $wrapper_attributes;
  32. };
  33. add_filter( 'next_posts_link_attributes', $filter_link_attributes );
  34. // Take into account if we have set a bigger `max page`
  35. // than what the query has.
  36. global $wp_query;
  37. if ( $max_page > $wp_query->max_num_pages ) {
  38. $max_page = $wp_query->max_num_pages;
  39. }
  40. $content = get_next_posts_link( $label, $max_page );
  41. remove_filter( 'next_posts_link_attributes', $filter_link_attributes );
  42. } elseif ( ! $max_page || $max_page > $page ) {
  43. $custom_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
  44. $custom_query_max_pages = (int) $custom_query->max_num_pages;
  45. if ( $custom_query_max_pages && $custom_query_max_pages !== $page ) {
  46. $content = sprintf(
  47. '<a href="%1$s" %2$s>%3$s</a>',
  48. esc_url( add_query_arg( $page_key, $page + 1 ) ),
  49. $wrapper_attributes,
  50. $label
  51. );
  52. }
  53. wp_reset_postdata(); // Restore original Post Data.
  54. }
  55. return $content;
  56. }
  57. /**
  58. * Registers the `core/query-pagination-next` block on the server.
  59. */
  60. function register_block_core_query_pagination_next() {
  61. register_block_type_from_metadata(
  62. __DIR__ . '/query-pagination-next',
  63. array(
  64. 'render_callback' => 'render_block_core_query_pagination_next',
  65. )
  66. );
  67. }
  68. add_action( 'init', 'register_block_core_query_pagination_next' );