[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/query-pagination-previous` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/query-pagination-previous` block on the server.
  10   *
  11   * @since 5.8.0
  12   *
  13   * @param array    $attributes Block attributes.
  14   * @param string   $content    Block default content.
  15   * @param WP_Block $block      Block instance.
  16   *
  17   * @return string Returns the previous posts link for the query.
  18   */
  19  function render_block_core_query_pagination_previous( $attributes, $content, $block ) {
  20      $page_key            = isset( $block->context['queryId'] ) ? 'query-' . $block->context['queryId'] . '-page' : 'query-page';
  21      $enhanced_pagination = isset( $block->context['enhancedPagination'] ) && $block->context['enhancedPagination'];
  22      $max_page            = isset( $block->context['query']['pages'] ) ? (int) $block->context['query']['pages'] : 0;
  23      $page                = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
  24      $wrapper_attributes  = get_block_wrapper_attributes();
  25      $show_label          = isset( $block->context['showLabel'] ) ? (bool) $block->context['showLabel'] : true;
  26      $default_label       = __( 'Previous Page' );
  27      $label_text          = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? esc_html( $attributes['label'] ) : $default_label;
  28      $label               = $show_label ? $label_text : '';
  29      $pagination_arrow    = get_query_pagination_arrow( $block, false );
  30      if ( ! $label ) {
  31          $wrapper_attributes .= ' aria-label="' . $label_text . '"';
  32      }
  33      if ( $pagination_arrow ) {
  34          $label = $pagination_arrow . $label;
  35      }
  36      $content = '';
  37      // Check if the pagination is for Query that inherits the global context
  38      // and handle appropriately.
  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  
  44          add_filter( 'previous_posts_link_attributes', $filter_link_attributes );
  45          $content = get_previous_posts_link( $label );
  46          remove_filter( 'previous_posts_link_attributes', $filter_link_attributes );
  47      } else {
  48          $block_query     = new WP_Query( build_query_vars_from_query_block( $block, $page ) );
  49          $block_max_pages = $block_query->max_num_pages;
  50          $total           = ! $max_page || $max_page > $block_max_pages ? $block_max_pages : $max_page;
  51          wp_reset_postdata();
  52  
  53          if ( 1 < $page && $page <= $total ) {
  54              $content = sprintf(
  55                  '<a href="%1$s" %2$s>%3$s</a>',
  56                  esc_url( add_query_arg( $page_key, $page - 1 ) ),
  57                  $wrapper_attributes,
  58                  $label
  59              );
  60          }
  61      }
  62  
  63      if ( $enhanced_pagination && isset( $content ) ) {
  64          $p = new WP_HTML_Tag_Processor( $content );
  65          if ( $p->next_tag(
  66              array(
  67                  'tag_name'   => 'a',
  68                  'class_name' => 'wp-block-query-pagination-previous',
  69              )
  70          ) ) {
  71              $p->set_attribute( 'data-wp-key', 'query-pagination-previous' );
  72              $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' );
  73              $p->set_attribute( 'data-wp-on-async--mouseenter', 'core/query::actions.prefetch' );
  74              $p->set_attribute( 'data-wp-watch', 'core/query::callbacks.prefetch' );
  75              $content = $p->get_updated_html();
  76          }
  77      }
  78  
  79      return $content;
  80  }
  81  
  82  /**
  83   * Registers the `core/query-pagination-previous` block on the server.
  84   *
  85   * @since 5.8.0
  86   */
  87  function register_block_core_query_pagination_previous() {
  88      register_block_type_from_metadata(
  89          __DIR__ . '/query-pagination-previous',
  90          array(
  91              'render_callback' => 'render_block_core_query_pagination_previous',
  92          )
  93      );
  94  }
  95  add_action( 'init', 'register_block_core_query_pagination_previous' );


Generated : Fri Feb 21 08:20:01 2025 Cross-referenced by PHPXref