[ Index ]

PHP Cross Reference of WordPress

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( array( 'class' => $classes ) );
  32      // Set default values.
  33      $format = '%link';
  34      $link   = 'next' === $navigation_type ? _x( 'Next', 'label for next post link' ) : _x( 'Previous', 'label for previous post link' );
  35      $label  = '';
  36  
  37      // If a custom label is provided, make this a link.
  38      // `$label` is used to prepend the provided label, if we want to show the page title as well.
  39      if ( isset( $attributes['label'] ) && ! empty( $attributes['label'] ) ) {
  40          $label = "{$attributes['label']}";
  41          $link  = $label;
  42      }
  43  
  44      // If we want to also show the page title, make the page title a link and prepend the label.
  45      if ( isset( $attributes['showTitle'] ) && $attributes['showTitle'] ) {
  46          /*
  47           * If the label link option is not enabled but there is a custom label,
  48           * display the custom label as text before the linked title.
  49           */
  50          if ( ! $attributes['linkLabel'] ) {
  51              if ( $label ) {
  52                  $format = '<span class="post-navigation-link__label">' . wp_kses_post( $label ) . '</span> %link';
  53              }
  54              $link = '%title';
  55          } elseif ( isset( $attributes['linkLabel'] ) && $attributes['linkLabel'] ) {
  56              // If the label link option is enabled and there is a custom label, display it before the title.
  57              if ( $label ) {
  58                  $link = '<span class="post-navigation-link__label">' . wp_kses_post( $label ) . '</span> <span class="post-navigation-link__title">%title</span>';
  59              } else {
  60                  /*
  61                   * If the label link option is enabled and there is no custom label,
  62                   * add a colon between the label and the post title.
  63                   */
  64                  $label = 'next' === $navigation_type ? _x( 'Next:', 'label before the title of the next post' ) : _x( 'Previous:', 'label before the title of the previous post' );
  65                  $link  = sprintf(
  66                      '<span class="post-navigation-link__label">%1$s</span> <span class="post-navigation-link__title">%2$s</span>',
  67                      wp_kses_post( $label ),
  68                      '%title'
  69                  );
  70              }
  71          }
  72      }
  73  
  74      // The dynamic portion of the function name, `$navigation_type`,
  75      // refers to the type of adjacency, 'next' or 'previous'.
  76      $get_link_function = "get_{$navigation_type}_post_link";
  77      $content           = $get_link_function( $format, $link );
  78      return sprintf(
  79          '<div %1$s>%2$s</div>',
  80          $wrapper_attributes,
  81          $content
  82      );
  83  }
  84  
  85  /**
  86   * Registers the `core/post-navigation-link` block on the server.
  87   */
  88  function register_block_core_post_navigation_link() {
  89      register_block_type_from_metadata(
  90          __DIR__ . '/post-navigation-link',
  91          array(
  92              'render_callback' => 'render_block_core_post_navigation_link',
  93          )
  94      );
  95  }
  96  add_action( 'init', 'register_block_core_post_navigation_link' );


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