[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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      $enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination'];
  20      $page                = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
  21      $max_page            = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0;
  22  
  23      $wrapper_attributes = get_block_wrapper_attributes();
  24      $show_label         = isset( $block->context['showLabel'] ) ? (bool) $block->context['showLabel'] : true;
  25      $default_label      = __( 'Next Page' );
  26      $label_text         = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? esc_html( $attributes['label'] ) : $default_label;
  27      $label              = $show_label ? $label_text : '';
  28      $pagination_arrow   = get_query_pagination_arrow( $block, true );
  29  
  30      if ( ! $label ) {
  31          $wrapper_attributes .= ' aria-label="' . $label_text . '"';
  32      }
  33      if ( $pagination_arrow ) {
  34          $label .= $pagination_arrow;
  35      }
  36      $content = '';
  37  
  38      // Check if the pagination is for Query that inherits the global context.
  39      if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
  40          $filter_link_attributes = static function () use ( $wrapper_attributes ) {
  41              return $wrapper_attributes;
  42          };
  43          add_filter( 'next_posts_link_attributes', $filter_link_attributes );
  44          // Take into account if we have set a bigger `max page`
  45          // than what the query has.
  46          global $wp_query;
  47          if ( $max_page > $wp_query->max_num_pages ) {
  48              $max_page = $wp_query->max_num_pages;
  49          }
  50          $content = get_next_posts_link( $label, $max_page );
  51          remove_filter( 'next_posts_link_attributes', $filter_link_attributes );
  52      } elseif ( ! $max_page || $max_page > $page ) {
  53          $custom_query           = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
  54          $custom_query_max_pages = (int) $custom_query->max_num_pages;
  55          if ( $custom_query_max_pages && $custom_query_max_pages !== $page ) {
  56              $content = sprintf(
  57                  '<a href="%1$s" %2$s>%3$s</a>',
  58                  esc_url( add_query_arg( $page_key, $page + 1 ) ),
  59                  $wrapper_attributes,
  60                  $label
  61              );
  62          }
  63          wp_reset_postdata(); // Restore original Post Data.
  64      }
  65  
  66      if ( $enhanced_pagination && isset( $content ) ) {
  67          $p = new WP_HTML_Tag_Processor( $content );
  68          if ( $p->next_tag(
  69              array(
  70                  'tag_name'   => 'a',
  71                  'class_name' => 'wp-block-query-pagination-next',
  72              )
  73          ) ) {
  74              $p->set_attribute( 'data-wp-key', 'query-pagination-next' );
  75              $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' );
  76              $p->set_attribute( 'data-wp-on--mouseenter', 'core/query::actions.prefetch' );
  77              $p->set_attribute( 'data-wp-watch', 'core/query::callbacks.prefetch' );
  78              $content = $p->get_updated_html();
  79          }
  80      }
  81  
  82      return $content;
  83  }
  84  
  85  /**
  86   * Registers the `core/query-pagination-next` block on the server.
  87   */
  88  function register_block_core_query_pagination_next() {
  89      register_block_type_from_metadata(
  90          __DIR__ . '/query-pagination-next',
  91          array(
  92              'render_callback' => 'render_block_core_query_pagination_next',
  93          )
  94      );
  95  }
  96  add_action( 'init', 'register_block_core_query_pagination_next' );


Generated : Thu Apr 25 08:20:02 2024 Cross-referenced by PHPXref