[ 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 = $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  
  34      // Set default values.
  35      $format = '%link';
  36      $link   = 'next' === $navigation_type ? _x( 'Next', 'label for next post link' ) : _x( 'Previous', 'label for previous post link' );
  37      $label  = '';
  38  
  39      // Only use hardcoded values here, otherwise we need to add escaping where these values are used.
  40      $arrow_map = array(
  41          'none'    => '',
  42          'arrow'   => array(
  43              'next'     => '→',
  44              'previous' => '←',
  45          ),
  46          'chevron' => array(
  47              'next'     => '»',
  48              'previous' => '«',
  49          ),
  50      );
  51  
  52      // If a custom label is provided, make this a link.
  53      // `$label` is used to prepend the provided label, if we want to show the page title as well.
  54      if ( isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ) {
  55          $label = "{$attributes['label']}";
  56          $link  = wp_kses_post( $label );
  57      }
  58  
  59      // If we want to also show the page title, make the page title a link and prepend the label.
  60      if ( isset( $attributes['showTitle'] ) && $attributes['showTitle'] ) {
  61          /*
  62           * If the label link option is not enabled but there is a custom label,
  63           * display the custom label as text before the linked title.
  64           */
  65          if ( ! $attributes['linkLabel'] ) {
  66              if ( $label ) {
  67                  $format = '<span class="post-navigation-link__label">' . wp_kses_post( $label ) . '</span> %link';
  68              }
  69              $link = '%title';
  70          } elseif ( isset( $attributes['linkLabel'] ) && $attributes['linkLabel'] ) {
  71              // If the label link option is enabled and there is a custom label, display it before the title.
  72              if ( $label ) {
  73                  $link = '<span class="post-navigation-link__label">' . wp_kses_post( $label ) . '</span> <span class="post-navigation-link__title">%title</span>';
  74              } else {
  75                  /*
  76                   * If the label link option is enabled and there is no custom label,
  77                   * add a colon between the label and the post title.
  78                   */
  79                  $label = 'next' === $navigation_type ? _x( 'Next:', 'label before the title of the next post' ) : _x( 'Previous:', 'label before the title of the previous post' );
  80                  $link  = sprintf(
  81                      '<span class="post-navigation-link__label">%1$s</span> <span class="post-navigation-link__title">%2$s</span>',
  82                      wp_kses_post( $label ),
  83                      '%title'
  84                  );
  85              }
  86          }
  87      }
  88  
  89      // Display arrows.
  90      if ( isset( $attributes['arrow'] ) && 'none' !== $attributes['arrow'] && isset( $arrow_map[ $attributes['arrow'] ] ) ) {
  91          $arrow = $arrow_map[ $attributes['arrow'] ][ $navigation_type ];
  92  
  93          if ( 'next' === $navigation_type ) {
  94              $format = '%link<span class="wp-block-post-navigation-link__arrow-next is-arrow-' . $attributes['arrow'] . '" aria-hidden="true">' . $arrow . '</span>';
  95          } else {
  96              $format = '<span class="wp-block-post-navigation-link__arrow-previous is-arrow-' . $attributes['arrow'] . '" aria-hidden="true">' . $arrow . '</span>%link';
  97          }
  98      }
  99  
 100      /*
 101       * The dynamic portion of the function name, `$navigation_type`,
 102       * Refers to the type of adjacency, 'next' or 'previous'.
 103       *
 104       * @see https://developer.wordpress.org/reference/functions/get_previous_post_link/
 105       * @see https://developer.wordpress.org/reference/functions/get_next_post_link/
 106       */
 107      $get_link_function = "get_{$navigation_type}_post_link";
 108  
 109      if ( ! empty( $attributes['taxonomy'] ) ) {
 110          $content = $get_link_function( $format, $link, true, '', $attributes['taxonomy'] );
 111      } else {
 112          $content = $get_link_function( $format, $link );
 113      }
 114  
 115      // The `{next,previous}_post_link` filters can return null, which renders as no link.
 116      $content = (string) $content;
 117  
 118      /*
 119       * Background, border, dimensions, shadow and spacing serialization is
 120       * skipped for this block so those styles can be withheld from the empty
 121       * wrapper rendered when there is no adjacent post. The wrapper itself is
 122       * kept for backward compatibility.
 123       */
 124      $support_styles = '' === $content
 125          ? array(
 126              'class' => '',
 127              'style' => '',
 128          )
 129          : block_core_post_navigation_link_get_support_styles( $attributes );
 130  
 131      if ( '' !== $support_styles['class'] ) {
 132          $classes .= " {$support_styles['class']}";
 133      }
 134  
 135      $wrapper_attributes = get_block_wrapper_attributes(
 136          array(
 137              'class' => $classes,
 138              'style' => $support_styles['style'],
 139          )
 140      );
 141  
 142      return sprintf(
 143          '<div %1$s>%2$s</div>',
 144          $wrapper_attributes,
 145          $content
 146      );
 147  }
 148  
 149  /**
 150   * Generates the background, border, dimensions, shadow and spacing class
 151   * names and inline styles for the `core/post-navigation-link` block.
 152   *
 153   * Mirrors the default background, border, dimensions, shadow and spacing
 154   * block support serialization, which the block opts out of so the styles are
 155   * only applied when a link is rendered.
 156   *
 157   * @since 7.2.0
 158   *
 159   * @param array $attributes The block attributes.
 160   * @return array {
 161   *     Background, border, dimensions, shadow and spacing class names and
 162   *     inline styles.
 163   *
 164   *     @type string $class Class names generated by the style engine, or an empty string.
 165   *     @type string $style Inline styles generated by the style engine, or an empty string.
 166   * }
 167   */
 168  function block_core_post_navigation_link_get_support_styles( $attributes ) {
 169      $block_styles  = $attributes['style'] ?? array();
 170      $border        = $block_styles['border'] ?? array();
 171      $border_styles = array();
 172  
 173      // Border radius and width accept unitless numbers from the original implementation.
 174      if ( isset( $border['radius'] ) ) {
 175          $border_styles['radius'] = is_numeric( $border['radius'] ) ? "{$border['radius']}px" : $border['radius'];
 176      }
 177  
 178      if ( isset( $border['width'] ) ) {
 179          $border_styles['width'] = is_numeric( $border['width'] ) ? "{$border['width']}px" : $border['width'];
 180      }
 181  
 182      if ( isset( $border['style'] ) ) {
 183          $border_styles['style'] = $border['style'];
 184      }
 185  
 186      // A preset border color is stored in its own attribute rather than under `style`.
 187      $border_styles['color'] = isset( $attributes['borderColor'] )
 188          ? "var:preset|color|{$attributes['borderColor']}"
 189          : ( $border['color'] ?? null );
 190  
 191      // Individual border sides e.g. top, left etc.
 192      foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
 193          $border_styles[ $side ] = array(
 194              'width' => $border[ $side ]['width'] ?? null,
 195              'color' => $border[ $side ]['color'] ?? null,
 196              'style' => $border[ $side ]['style'] ?? null,
 197          );
 198      }
 199  
 200      $background        = $block_styles['background'] ?? array();
 201      $background_styles = array(
 202          'backgroundImage'      => $background['backgroundImage'] ?? null,
 203          'backgroundSize'       => $background['backgroundSize'] ?? null,
 204          'backgroundPosition'   => $background['backgroundPosition'] ?? null,
 205          'backgroundRepeat'     => $background['backgroundRepeat'] ?? null,
 206          'backgroundAttachment' => $background['backgroundAttachment'] ?? null,
 207          'gradient'             => $background['gradient'] ?? null,
 208      );
 209  
 210      // Mirrors the background image defaults of the background block support.
 211      if ( ! empty( $background_styles['backgroundImage'] ) ) {
 212          $background_styles['backgroundSize'] = $background_styles['backgroundSize'] ?? 'cover';
 213          if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
 214              $background_styles['backgroundPosition'] = '50% 50%';
 215          }
 216      }
 217  
 218      $styles = wp_style_engine_get_styles(
 219          array(
 220              'background' => $background_styles,
 221              'border'     => $border_styles,
 222              'dimensions' => array(
 223                  'minHeight' => $block_styles['dimensions']['minHeight'] ?? null,
 224                  'minWidth'  => $block_styles['dimensions']['minWidth'] ?? null,
 225              ),
 226              'shadow'     => $block_styles['shadow'] ?? null,
 227              'spacing'    => array(
 228                  'margin'  => $block_styles['spacing']['margin'] ?? null,
 229                  'padding' => $block_styles['spacing']['padding'] ?? null,
 230              ),
 231          )
 232      );
 233  
 234      $class_names = $styles['classnames'] ?? '';
 235  
 236      // The style engine only adds `has-background` for a gradient, the background block support adds it for an image too.
 237      if ( ! empty( $background_styles['backgroundImage'] ) && ! in_array( 'has-background', explode( ' ', $class_names ), true ) ) {
 238          $class_names = trim( "{$class_names} has-background" );
 239      }
 240  
 241      return array(
 242          'class' => $class_names,
 243          'style' => $styles['css'] ?? '',
 244      );
 245  }
 246  
 247  /**
 248   * Registers the `core/post-navigation-link` block on the server.
 249   *
 250   * @since 5.9.0
 251   */
 252  function register_block_core_post_navigation_link() {
 253      register_block_type_from_metadata(
 254          __DIR__ . '/post-navigation-link',
 255          array(
 256              'render_callback' => 'render_block_core_post_navigation_link',
 257          )
 258      );
 259  }
 260  add_action( 'init', 'register_block_core_post_navigation_link' );


Generated : Thu Oct 29 08:20:35 2026 Cross-referenced by PHPXref