[ Index ]

PHP Cross Reference of WordPress

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      $excerpt = get_the_excerpt();
  22  
  23      if ( empty( $excerpt ) ) {
  24          return '';
  25      }
  26  
  27      $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>' : '';
  28      $filter_excerpt_more = function( $more ) use ( $more_text ) {
  29          return empty( $more_text ) ? $more : '';
  30      };
  31      /**
  32       * Some themes might use `excerpt_more` filter to handle the
  33       * `more` link displayed after a trimmed excerpt. Since the
  34       * block has a `more text` attribute we have to check and
  35       * override if needed the return value from this filter.
  36       * So if the block's attribute is not empty override the
  37       * `excerpt_more` filter and return nothing. This will
  38       * result in showing only one `read more` link at a time.
  39       */
  40      add_filter( 'excerpt_more', $filter_excerpt_more );
  41      $classes = '';
  42      if ( isset( $attributes['textAlign'] ) ) {
  43          $classes .= "has-text-align-{$attributes['textAlign']}";
  44      }
  45      $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
  46  
  47      $content               = '<p class="wp-block-post-excerpt__excerpt">' . $excerpt;
  48      $show_more_on_new_line = ! isset( $attributes['showMoreOnNewLine'] ) || $attributes['showMoreOnNewLine'];
  49      if ( $show_more_on_new_line && ! empty( $more_text ) ) {
  50          $content .= '</p><p class="wp-block-post-excerpt__more-text">' . $more_text . '</p>';
  51      } else {
  52          $content .= " $more_text</p>";
  53      }
  54      remove_filter( 'excerpt_more', $filter_excerpt_more );
  55      return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $content );
  56  }
  57  
  58  /**
  59   * Registers the `core/post-excerpt` block on the server.
  60   */
  61  function register_block_core_post_excerpt() {
  62      register_block_type_from_metadata(
  63          __DIR__ . '/post-excerpt',
  64          array(
  65              'render_callback' => 'render_block_core_post_excerpt',
  66          )
  67      );
  68  }
  69  add_action( 'init', 'register_block_core_post_excerpt' );


Generated: Sat Apr 20 01:00:03 2024 Cross-referenced by PHPXref 0.7.1