[ 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'] ) ? $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          add_filter( 'previous_posts_link_attributes', $filter_link_attributes );
  36          $content = get_previous_posts_link( $label );
  37          remove_filter( 'previous_posts_link_attributes', $filter_link_attributes );
  38      } elseif ( 1 !== $page ) {
  39          $content = sprintf(
  40              '<a href="%1$s" %2$s>%3$s</a>',
  41              esc_url( add_query_arg( $page_key, $page - 1 ) ),
  42              $wrapper_attributes,
  43              $label
  44          );
  45      }
  46      return $content;
  47  }
  48  
  49  /**
  50   * Registers the `core/query-pagination-previous` block on the server.
  51   */
  52  function register_block_core_query_pagination_previous() {
  53      register_block_type_from_metadata(
  54          __DIR__ . '/query-pagination-previous',
  55          array(
  56              'render_callback' => 'render_block_core_query_pagination_previous',
  57          )
  58      );
  59  }
  60  add_action( 'init', 'register_block_core_query_pagination_previous' );


Generated: Tue Jan 25 01:00:04 2022 Cross-referenced by PHPXref 0.7.1