[ 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      $page                = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ];
  23  
  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      } elseif ( 1 !== $page ) {
  48          $content = sprintf(
  49              '<a href="%1$s" %2$s>%3$s</a>',
  50              esc_url( add_query_arg( $page_key, $page - 1 ) ),
  51              $wrapper_attributes,
  52              $label
  53          );
  54      }
  55  
  56      if ( $enhanced_pagination && isset( $content ) ) {
  57          $p = new WP_HTML_Tag_Processor( $content );
  58          if ( $p->next_tag(
  59              array(
  60                  'tag_name'   => 'a',
  61                  'class_name' => 'wp-block-query-pagination-previous',
  62              )
  63          ) ) {
  64              $p->set_attribute( 'data-wp-key', 'query-pagination-previous' );
  65              $p->set_attribute( 'data-wp-on--click', 'core/query::actions.navigate' );
  66              $p->set_attribute( 'data-wp-on-async--mouseenter', 'core/query::actions.prefetch' );
  67              $p->set_attribute( 'data-wp-watch', 'core/query::callbacks.prefetch' );
  68              $content = $p->get_updated_html();
  69          }
  70      }
  71  
  72      return $content;
  73  }
  74  
  75  /**
  76   * Registers the `core/query-pagination-previous` block on the server.
  77   *
  78   * @since 5.8.0
  79   */
  80  function register_block_core_query_pagination_previous() {
  81      register_block_type_from_metadata(
  82          __DIR__ . '/query-pagination-previous',
  83          array(
  84              'render_callback' => 'render_block_core_query_pagination_previous',
  85          )
  86      );
  87  }
  88  add_action( 'init', 'register_block_core_query_pagination_previous' );


Generated : Mon Jul 15 08:20:02 2024 Cross-referenced by PHPXref