[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/post-excerpt` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/post-excerpt` 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   * @return string Returns the filtered post excerpt for the current post wrapped inside "p" tags.
  15   */
  16  function render_block_core_post_excerpt( $attributes, $content, $block ) {
  17      if ( ! isset( $block->context['postId'] ) ) {
  18          return '';
  19      }
  20  
  21      /*
  22      * The purpose of the excerpt length setting is to limit the length of both
  23      * automatically generated and user-created excerpts.
  24      * Because the excerpt_length filter only applies to auto generated excerpts,
  25      * wp_trim_words is used instead.
  26      */
  27      $excerpt_length = $attributes['excerptLength'];
  28      $excerpt        = get_the_excerpt( $block->context['postId'] );
  29      if ( isset( $excerpt_length ) ) {
  30          $excerpt = wp_trim_words( $excerpt, $excerpt_length );
  31      }
  32  
  33      $more_text           = ! empty( $attributes['moreText'] ) ? '<a class="wp-block-post-excerpt__more-link" href="' . esc_url( get_the_permalink( $block->context['postId'] ) ) . '">' . wp_kses_post( $attributes['moreText'] ) . '</a>' : '';
  34      $filter_excerpt_more = static function ( $more ) use ( $more_text ) {
  35          return empty( $more_text ) ? $more : '';
  36      };
  37      /**
  38       * Some themes might use `excerpt_more` filter to handle the
  39       * `more` link displayed after a trimmed excerpt. Since the
  40       * block has a `more text` attribute we have to check and
  41       * override if needed the return value from this filter.
  42       * So if the block's attribute is not empty override the
  43       * `excerpt_more` filter and return nothing. This will
  44       * result in showing only one `read more` link at a time.
  45       */
  46      add_filter( 'excerpt_more', $filter_excerpt_more );
  47      $classes = array();
  48      if ( isset( $attributes['textAlign'] ) ) {
  49          $classes[] = 'has-text-align-' . $attributes['textAlign'];
  50      }
  51      if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
  52          $classes[] = 'has-link-color';
  53      }
  54      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
  55  
  56      $content               = '<p class="wp-block-post-excerpt__excerpt">' . $excerpt;
  57      $show_more_on_new_line = ! isset( $attributes['showMoreOnNewLine'] ) || $attributes['showMoreOnNewLine'];
  58      if ( $show_more_on_new_line && ! empty( $more_text ) ) {
  59          $content .= '</p><p class="wp-block-post-excerpt__more-text">' . $more_text . '</p>';
  60      } else {
  61          $content .= " $more_text</p>";
  62      }
  63      remove_filter( 'excerpt_more', $filter_excerpt_more );
  64      return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $content );
  65  }
  66  
  67  /**
  68   * Registers the `core/post-excerpt` block on the server.
  69   */
  70  function register_block_core_post_excerpt() {
  71      register_block_type_from_metadata(
  72          __DIR__ . '/post-excerpt',
  73          array(
  74              'render_callback' => 'render_block_core_post_excerpt',
  75          )
  76      );
  77  }
  78  add_action( 'init', 'register_block_core_post_excerpt' );
  79  
  80  /**
  81   * If themes or plugins filter the excerpt_length, we need to
  82   * override the filter in the editor, otherwise
  83   * the excerpt length block setting has no effect.
  84   * Returns 100 because 100 is the max length in the setting.
  85   */
  86  if ( is_admin() ||
  87      defined( 'REST_REQUEST' ) && REST_REQUEST ) {
  88      add_filter(
  89          'excerpt_length',
  90          static function () {
  91              return 100;
  92          },
  93          PHP_INT_MAX
  94      );
  95  }


Generated : Thu Apr 25 08:20:02 2024 Cross-referenced by PHPXref