[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/blocks/ -> query-pagination-next.php (source)

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


Generated: Sat Apr 20 01:00:03 2024 Cross-referenced by PHPXref 0.7.1