[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 * @since 5.8.0 12 * 13 * @param array $attributes Block attributes. 14 * @param string $content Block default content. 15 * @param WP_Block $block Block instance. 16 * @return string Returns the filtered post excerpt for the current post wrapped inside "p" tags. 17 */ 18 function render_block_core_post_excerpt( $attributes, $content, $block ) { 19 if ( ! isset( $block->context['postId'] ) ) { 20 return ''; 21 } 22 23 $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>' : ''; 24 $filter_excerpt_more = static function ( $more ) use ( $more_text ) { 25 return empty( $more_text ) ? $more : ''; 26 }; 27 /** 28 * Some themes might use `excerpt_more` filter to handle the 29 * `more` link displayed after a trimmed excerpt. Since the 30 * block has a `more text` attribute we have to check and 31 * override if needed the return value from this filter. 32 * So if the block's attribute is not empty override the 33 * `excerpt_more` filter and return nothing. This will 34 * result in showing only one `read more` link at a time. 35 * 36 * This hook needs to be applied before the excerpt is retrieved with get_the_excerpt. 37 * Otherwise, the read more link filter from the theme is not removed. 38 */ 39 add_filter( 'excerpt_more', $filter_excerpt_more ); 40 41 /* 42 * The purpose of the excerpt length setting is to limit the length of both 43 * automatically generated and user-created excerpts. 44 * Because the excerpt_length filter only applies to auto generated excerpts, 45 * wp_trim_words is used instead. 46 */ 47 $excerpt_length = $attributes['excerptLength']; 48 $excerpt = get_the_excerpt( $block->context['postId'] ); 49 if ( isset( $excerpt_length ) ) { 50 $excerpt = wp_trim_words( $excerpt, $excerpt_length ); 51 } 52 53 $classes = array(); 54 if ( isset( $attributes['textAlign'] ) ) { 55 $classes[] = 'has-text-align-' . $attributes['textAlign']; 56 } 57 if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) { 58 $classes[] = 'has-link-color'; 59 } 60 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); 61 62 $content = '<p class="wp-block-post-excerpt__excerpt">' . $excerpt; 63 $show_more_on_new_line = ! isset( $attributes['showMoreOnNewLine'] ) || $attributes['showMoreOnNewLine']; 64 if ( $show_more_on_new_line && ! empty( $more_text ) ) { 65 $content .= '</p><p class="wp-block-post-excerpt__more-text">' . $more_text . '</p>'; 66 } else { 67 $content .= " $more_text</p>"; 68 } 69 remove_filter( 'excerpt_more', $filter_excerpt_more ); 70 return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $content ); 71 } 72 73 /** 74 * Registers the `core/post-excerpt` block on the server. 75 * 76 * @since 5.8.0 77 */ 78 function register_block_core_post_excerpt() { 79 register_block_type_from_metadata( 80 __DIR__ . '/post-excerpt', 81 array( 82 'render_callback' => 'render_block_core_post_excerpt', 83 ) 84 ); 85 } 86 add_action( 'init', 'register_block_core_post_excerpt' ); 87 88 /** 89 * If themes or plugins filter the excerpt_length, we need to 90 * override the filter in the editor, otherwise 91 * the excerpt length block setting has no effect. 92 * Returns 100 because 100 is the max length in the setting. 93 */ 94 if ( is_admin() || 95 defined( 'REST_REQUEST' ) && REST_REQUEST ) { 96 add_filter( 97 'excerpt_length', 98 static function () { 99 return 100; 100 }, 101 PHP_INT_MAX 102 ); 103 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Oct 23 08:20:05 2025 | Cross-referenced by PHPXref |