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


Generated: Mon Jan 24 01:00:08 2022 Cross-referenced by PHPXref 0.7.1