[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/query-pagination-numbers` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/query-pagination-numbers` block on the server.
  10   *
  11   * @since 5.8.0
  12   *
  13   * @global WP_Query $wp_query WordPress Query object.
  14   *
  15   * @param array    $attributes Block attributes.
  16   * @param string   $content    Block default content.
  17   * @param WP_Block $block      Block instance.
  18   *
  19   * @return string Returns the pagination numbers for the Query.
  20   */
  21  function render_block_core_query_pagination_numbers( $attributes, $content, $block ) {
  22      $page_key            = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
  23      $enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination'];
  24      $page                = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
  25      $max_page            = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0;
  26  
  27      $wrapper_attributes = get_block_wrapper_attributes();
  28      $content            = '';
  29      global $wp_query;
  30      $mid_size = isset( $block->attributes['midSize'] ) ? (int) $block->attributes['midSize'] : null;
  31      if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
  32          // Take into account if we have set a bigger `max page`
  33          // than what the query has.
  34          $total         = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page;
  35          $paginate_args = array(
  36              'prev_next' => false,
  37              'total'     => $total,
  38          );
  39          if ( null !== $mid_size ) {
  40              $paginate_args['mid_size'] = $mid_size;
  41          }
  42          $content = paginate_links( $paginate_args );
  43      } else {
  44          $block_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
  45          // `paginate_links` works with the global $wp_query, so we have to
  46          // temporarily switch it with our custom query.
  47          $prev_wp_query = $wp_query;
  48          $wp_query      = $block_query;
  49          $total         = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page;
  50          $paginate_args = array(
  51              'base'      => '%_%',
  52              'format'    => "?$page_key=%#%",
  53              'current'   => max( 1, $page ),
  54              'total'     => $total,
  55              'prev_next' => false,
  56          );
  57          if ( null !== $mid_size ) {
  58              $paginate_args['mid_size'] = $mid_size;
  59          }
  60          if ( 1 !== $page ) {
  61              /**
  62               * `paginate_links` doesn't use the provided `format` when the page is `1`.
  63               * This is great for the main query as it removes the extra query params
  64               * making the URL shorter, but in the case of multiple custom queries is
  65               * problematic. It results in returning an empty link which ends up with
  66               * a link to the current page.
  67               *
  68               * A way to address this is to add a `fake` query arg with no value that
  69               * is the same for all custom queries. This way the link is not empty and
  70               * preserves all the other existent query args.
  71               *
  72               * @see https://developer.wordpress.org/reference/functions/paginate_links/
  73               *
  74               * The proper fix of this should be in core. Track Ticket:
  75               * @see https://core.trac.wordpress.org/ticket/53868
  76               *
  77               * TODO: After two WP versions (starting from the WP version the core patch landed),
  78               * we should remove this and call `paginate_links` with the proper new arg.
  79               */
  80              $paginate_args['add_args'] = array( 'cst' => '' );
  81          }
  82          // We still need to preserve `paged` query param if exists, as is used
  83          // for Queries that inherit from global context.
  84          $paged = empty( $_GET['paged'] ) ? null : (int) $_GET['paged'];
  85          if ( $paged ) {
  86              $paginate_args['add_args'] = array( 'paged' => $paged );
  87          }
  88          $content = paginate_links( $paginate_args );
  89          wp_reset_postdata(); // Restore original Post Data.
  90          $wp_query = $prev_wp_query;
  91      }
  92  
  93      if ( empty( $content ) ) {
  94          return '';
  95      }
  96  
  97      if ( $enhanced_pagination ) {
  98          $p         = new WP_HTML_Tag_Processor( $content );
  99          $tag_index = 0;
 100          while ( $p->next_tag(
 101              array( 'class_name' => 'page-numbers' )
 102          ) ) {
 103              if ( null === $p->get_attribute( 'data-wp-key' ) ) {
 104                  $p->set_attribute( 'data-wp-key', 'index-' . $tag_index++ );
 105              }
 106              if ( 'A' === $p->get_tag() ) {
 107                  $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' );
 108              }
 109          }
 110          $content = $p->get_updated_html();
 111      }
 112  
 113      return sprintf(
 114          '<div %1$s>%2$s</div>',
 115          $wrapper_attributes,
 116          $content
 117      );
 118  }
 119  
 120  /**
 121   * Registers the `core/query-pagination-numbers` block on the server.
 122   *
 123   * @since 5.8.0
 124   */
 125  function register_block_core_query_pagination_numbers() {
 126      register_block_type_from_metadata(
 127          __DIR__ . '/query-pagination-numbers',
 128          array(
 129              'render_callback' => 'render_block_core_query_pagination_numbers',
 130          )
 131      );
 132  }
 133  add_action( 'init', 'register_block_core_query_pagination_numbers' );


Generated : Sat Nov 23 08:20:01 2024 Cross-referenced by PHPXref