[ Index ]

PHP Cross Reference of WordPress

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   * @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 previous posts link for the query.
  16   */
  17  function render_block_core_query_pagination_previous( $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  
  21      $wrapper_attributes = get_block_wrapper_attributes();
  22      $default_label      = __( 'Previous Page' );
  23      $label              = isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ? esc_html( $attributes['label'] ) : $default_label;
  24      $pagination_arrow   = get_query_pagination_arrow( $block, false );
  25      if ( $pagination_arrow ) {
  26          $label = $pagination_arrow . $label;
  27      }
  28      $content = '';
  29      // Check if the pagination is for Query that inherits the global context
  30      // and handle appropriately.
  31      if ( isset( $block->context['query']['inherit'] ) && $block->context['query']['inherit'] ) {
  32          $filter_link_attributes = function() use ( $wrapper_attributes ) {
  33              return $wrapper_attributes;
  34          };
  35  
  36          add_filter( 'previous_posts_link_attributes', $filter_link_attributes );
  37          $content = get_previous_posts_link( $label );
  38          remove_filter( 'previous_posts_link_attributes', $filter_link_attributes );
  39      } elseif ( 1 !== $page ) {
  40          $content = sprintf(
  41              '<a href="%1$s" %2$s>%3$s</a>',
  42              esc_url( add_query_arg( $page_key, $page - 1 ) ),
  43              $wrapper_attributes,
  44              $label
  45          );
  46      }
  47      return $content;
  48  }
  49  
  50  /**
  51   * Registers the `core/query-pagination-previous` block on the server.
  52   */
  53  function register_block_core_query_pagination_previous() {
  54      register_block_type_from_metadata(
  55          __DIR__ . '/query-pagination-previous',
  56          array(
  57              'render_callback' => 'render_block_core_query_pagination_previous',
  58          )
  59      );
  60  }
  61  add_action( 'init', 'register_block_core_query_pagination_previous' );


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1