[ Index ]

PHP Cross Reference of WordPress

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   * @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 pagination numbers for the Query.
  16   */
  17  function render_block_core_query_pagination_numbers( $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      $content            = '';
  24      global $wp_query;
  25      if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
  26          // Take into account if we have set a bigger `max page`
  27          // than what the query has.
  28          $total         = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page;
  29          $paginate_args = array(
  30              'prev_next' => false,
  31              'total'     => $total,
  32          );
  33          $content       = paginate_links( $paginate_args );
  34      } else {
  35          $block_query = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
  36          // `paginate_links` works with the global $wp_query, so we have to
  37          // temporarily switch it with our custom query.
  38          $prev_wp_query = $wp_query;
  39          $wp_query      = $block_query;
  40          $total         = ! $max_page || $max_page > $wp_query->max_num_pages ? $wp_query->max_num_pages : $max_page;
  41          $paginate_args = array(
  42              'base'      => '%_%',
  43              'format'    => "?$page_key=%#%",
  44              'current'   => max( 1, $page ),
  45              'total'     => $total,
  46              'prev_next' => false,
  47          );
  48          if ( 1 !== $page ) {
  49              /**
  50               * `paginate_links` doesn't use the provided `format` when the page is `1`.
  51               * This is great for the main query as it removes the extra query params
  52               * making the URL shorter, but in the case of multiple custom queries is
  53               * problematic. It results in returning an empty link which ends up with
  54               * a link to the current page.
  55               *
  56               * A way to address this is to add a `fake` query arg with no value that
  57               * is the same for all custom queries. This way the link is not empty and
  58               * preserves all the other existent query args.
  59               *
  60               * @see https://developer.wordpress.org/reference/functions/paginate_links/
  61               *
  62               * The proper fix of this should be in core. Track Ticket:
  63               * @see https://core.trac.wordpress.org/ticket/53868
  64               *
  65               * TODO: After two WP versions (starting from the WP version the core patch landed),
  66               * we should remove this and call `paginate_links` with the proper new arg.
  67               */
  68              $paginate_args['add_args'] = array( 'cst' => '' );
  69          }
  70          // We still need to preserve `paged` query param if exists, as is used
  71          // for Queries that inherit from global context.
  72          $paged = empty( $_GET['paged'] ) ? null : (int) $_GET['paged'];
  73          if ( $paged ) {
  74              $paginate_args['add_args'] = array( 'paged' => $paged );
  75          }
  76          $content = paginate_links( $paginate_args );
  77          wp_reset_postdata(); // Restore original Post Data.
  78          $wp_query = $prev_wp_query;
  79      }
  80      if ( empty( $content ) ) {
  81          return '';
  82      }
  83      return sprintf(
  84          '<div %1$s>%2$s</div>',
  85          $wrapper_attributes,
  86          $content
  87      );
  88  }
  89  
  90  /**
  91   * Registers the `core/query-pagination-numbers` block on the server.
  92   */
  93  function register_block_core_query_pagination_numbers() {
  94      register_block_type_from_metadata(
  95          __DIR__ . '/query-pagination-numbers',
  96          array(
  97              'render_callback' => 'render_block_core_query_pagination_numbers',
  98          )
  99      );
 100  }
 101  add_action( 'init', 'register_block_core_query_pagination_numbers' );


Generated: Fri Apr 19 01:00:02 2024 Cross-referenced by PHPXref 0.7.1