PluginProbe
Gutenberg / 22.9.0
Gutenberg v22.9.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / post-excerpt.php

post-excerpt.php in Gutenberg 22.9.0, at build/scripts/block-library/post-excerpt.php

118 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 gutenberg_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 * To ensure the block's excerptLength setting works correctly for auto-generated
48 * excerpts, we temporarily override excerpt_length to 101 (the max block setting)
49 * so that wp_trim_excerpt doesn't pre-trim the content before wp_trim_words can
50 * apply the user's desired length.
51 */
52 $excerpt_length = $attributes['excerptLength'];
53 add_filter( 'excerpt_length', 'gutenberg_block_core_post_excerpt_excerpt_length', PHP_INT_MAX );
54
55 $excerpt = get_the_excerpt( $block->context['postId'] );
56
57 remove_filter( 'excerpt_length', 'gutenberg_block_core_post_excerpt_excerpt_length', PHP_INT_MAX );
58
59 if ( isset( $excerpt_length ) ) {
60 $excerpt = wp_trim_words( $excerpt, $excerpt_length );
61 }
62
63 $classes = array();
64 if ( isset( $attributes['textAlign'] ) ) {
65 $classes[] = 'has-text-align-' . $attributes['textAlign'];
66 }
67 if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
68 $classes[] = 'has-link-color';
69 }
70 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) );
71
72 $content = '<p class="wp-block-post-excerpt__excerpt">' . $excerpt;
73 $show_more_on_new_line = ! isset( $attributes['showMoreOnNewLine'] ) || $attributes['showMoreOnNewLine'];
74 if ( $show_more_on_new_line && ! empty( $more_text ) ) {
75 $content .= '</p><p class="wp-block-post-excerpt__more-text">' . $more_text . '</p>';
76 } else {
77 $content .= " $more_text</p>";
78 }
79 remove_filter( 'excerpt_more', $filter_excerpt_more );
80 return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $content );
81 }
82
83 /**
84 * Registers the `core/post-excerpt` block on the server.
85 *
86 * @since 5.8.0
87 */
88 function gutenberg_register_block_core_post_excerpt() {
89 register_block_type_from_metadata(
90 __DIR__ . '/post-excerpt',
91 array(
92 'render_callback' => 'gutenberg_render_block_core_post_excerpt',
93 )
94 );
95 }
96 add_action( 'init', 'gutenberg_register_block_core_post_excerpt', 20 );
97
98 /**
99 * Callback for the excerpt_length filter to override the excerpt length.
100 *
101 * If themes or plugins filter the excerpt_length, we need to
102 * override the filter in the editor, otherwise
103 * the excerpt length block setting has no effect.
104 * Returns 101 (one more than the max block setting of 100) to ensure
105 * wp_trim_words can detect when trimming is needed and add the ellipsis.
106 *
107 * @since 7.0.0
108 *
109 * @return int The excerpt length.
110 */
111 function gutenberg_block_core_post_excerpt_excerpt_length() {
112 return 101;
113 }
114
115 if ( is_admin() ) {
116 add_filter( 'excerpt_length', 'gutenberg_block_core_post_excerpt_excerpt_length', PHP_INT_MAX );
117 }
118