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


Generated : Fri Apr 19 08:20:01 2024 Cross-referenced by PHPXref