[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> post-navigation-link.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/post-navigation-link` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/post-navigation-link` block on the server.
  10   *
  11   * @since 5.9.0
  12   *
  13   * @param array  $attributes Block attributes.
  14   * @param string $content    Block default content.
  15   *
  16   * @return string Returns the next or previous post link that is adjacent to the current post.
  17   */
  18  function render_block_core_post_navigation_link( $attributes, $content ) {
  19      if ( ! is_singular() ) {
  20          return '';
  21      }
  22  
  23      // Get the navigation type to show the proper link. Available options are `next|previous`.
  24      $navigation_type = isset( $attributes['type'] ) ? $attributes['type'] : 'next';
  25      // Allow only `next` and `previous` in `$navigation_type`.
  26      if ( ! in_array( $navigation_type, array( 'next', 'previous' ), true ) ) {
  27          return '';
  28      }
  29      $classes = "post-navigation-link-$navigation_type";
  30      if ( isset( $attributes['textAlign'] ) ) {
  31          $classes .= " has-text-align-{$attributes['textAlign']}";
  32      }
  33      $wrapper_attributes = get_block_wrapper_attributes(
  34          array(
  35              'class' => $classes,
  36          )
  37      );
  38      // Set default values.
  39      $format = '%link';
  40      $link   = 'next' === $navigation_type ? _x( 'Next', 'label for next post link' ) : _x( 'Previous', 'label for previous post link' );
  41      $label  = '';
  42  
  43      // Only use hardcoded values here, otherwise we need to add escaping where these values are used.
  44      $arrow_map = array(
  45          'none'    => '',
  46          'arrow'   => array(
  47              'next'     => '→',
  48              'previous' => '←',
  49          ),
  50          'chevron' => array(
  51              'next'     => '»',
  52              'previous' => '«',
  53          ),
  54      );
  55  
  56      // If a custom label is provided, make this a link.
  57      // `$label` is used to prepend the provided label, if we want to show the page title as well.
  58      if ( isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ) {
  59          $label = "{$attributes['label']}";
  60          $link  = $label;
  61      }
  62  
  63      // If we want to also show the page title, make the page title a link and prepend the label.
  64      if ( isset( $attributes['showTitle'] ) && $attributes['showTitle'] ) {
  65          /*
  66           * If the label link option is not enabled but there is a custom label,
  67           * display the custom label as text before the linked title.
  68           */
  69          if ( ! $attributes['linkLabel'] ) {
  70              if ( $label ) {
  71                  $format = '<span class="post-navigation-link__label">' . wp_kses_post( $label ) . '</span> %link';
  72              }
  73              $link = '%title';
  74          } elseif ( isset( $attributes['linkLabel'] ) && $attributes['linkLabel'] ) {
  75              // If the label link option is enabled and there is a custom label, display it before the title.
  76              if ( $label ) {
  77                  $link = '<span class="post-navigation-link__label">' . wp_kses_post( $label ) . '</span> <span class="post-navigation-link__title">%title</span>';
  78              } else {
  79                  /*
  80                   * If the label link option is enabled and there is no custom label,
  81                   * add a colon between the label and the post title.
  82                   */
  83                  $label = 'next' === $navigation_type ? _x( 'Next:', 'label before the title of the next post' ) : _x( 'Previous:', 'label before the title of the previous post' );
  84                  $link  = sprintf(
  85                      '<span class="post-navigation-link__label">%1$s</span> <span class="post-navigation-link__title">%2$s</span>',
  86                      wp_kses_post( $label ),
  87                      '%title'
  88                  );
  89              }
  90          }
  91      }
  92  
  93      // Display arrows.
  94      if ( isset( $attributes['arrow'] ) && 'none' !== $attributes['arrow'] && isset( $arrow_map[ $attributes['arrow'] ] ) ) {
  95          $arrow = $arrow_map[ $attributes['arrow'] ][ $navigation_type ];
  96  
  97          if ( 'next' === $navigation_type ) {
  98              $format = '%link<span class="wp-block-post-navigation-link__arrow-next is-arrow-' . $attributes['arrow'] . '" aria-hidden="true">' . $arrow . '</span>';
  99          } else {
 100              $format = '<span class="wp-block-post-navigation-link__arrow-previous is-arrow-' . $attributes['arrow'] . '" aria-hidden="true">' . $arrow . '</span>%link';
 101          }
 102      }
 103  
 104      /*
 105       * The dynamic portion of the function name, `$navigation_type`,
 106       * Refers to the type of adjacency, 'next' or 'previous'.
 107       *
 108       * @see https://developer.wordpress.org/reference/functions/get_previous_post_link/
 109       * @see https://developer.wordpress.org/reference/functions/get_next_post_link/
 110       */
 111      $get_link_function = "get_{$navigation_type}_post_link";
 112  
 113      if ( ! empty( $attributes['taxonomy'] ) ) {
 114          $content = $get_link_function( $format, $link, true, '', $attributes['taxonomy'] );
 115      } else {
 116          $content = $get_link_function( $format, $link );
 117      }
 118  
 119      return sprintf(
 120          '<div %1$s>%2$s</div>',
 121          $wrapper_attributes,
 122          $content
 123      );
 124  }
 125  
 126  /**
 127   * Registers the `core/post-navigation-link` block on the server.
 128   *
 129   * @since 5.9.0
 130   */
 131  function register_block_core_post_navigation_link() {
 132      register_block_type_from_metadata(
 133          __DIR__ . '/post-navigation-link',
 134          array(
 135              'render_callback' => 'render_block_core_post_navigation_link',
 136          )
 137      );
 138  }
 139  add_action( 'init', 'register_block_core_post_navigation_link' );


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref