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