author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:52:52 +0200 | |
changeset 22 | 8c2e4d02f4ef |
parent 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
18 | 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 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
11 |
* @since 5.8.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
12 |
* |
18 | 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 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
23 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
24 |
* The purpose of the excerpt length setting is to limit the length of both |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
25 |
* automatically generated and user-created excerpts. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
26 |
* Because the excerpt_length filter only applies to auto generated excerpts, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
27 |
* wp_trim_words is used instead. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
28 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
29 |
$excerpt_length = $attributes['excerptLength']; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
30 |
$excerpt = get_the_excerpt( $block->context['postId'] ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
31 |
if ( isset( $excerpt_length ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
32 |
$excerpt = wp_trim_words( $excerpt, $excerpt_length ); |
19 | 33 |
} |
34 |
||
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>' : ''; |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
36 |
$filter_excerpt_more = static function ( $more ) use ( $more_text ) { |
18 | 37 |
return empty( $more_text ) ? $more : ''; |
38 |
}; |
|
39 |
/** |
|
40 |
* Some themes might use `excerpt_more` filter to handle the |
|
41 |
* `more` link displayed after a trimmed excerpt. Since the |
|
42 |
* block has a `more text` attribute we have to check and |
|
43 |
* override if needed the return value from this filter. |
|
44 |
* So if the block's attribute is not empty override the |
|
45 |
* `excerpt_more` filter and return nothing. This will |
|
46 |
* result in showing only one `read more` link at a time. |
|
47 |
*/ |
|
48 |
add_filter( 'excerpt_more', $filter_excerpt_more ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
49 |
$classes = array(); |
18 | 50 |
if ( isset( $attributes['textAlign'] ) ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
51 |
$classes[] = 'has-text-align-' . $attributes['textAlign']; |
18 | 52 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
53 |
if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
54 |
$classes[] = 'has-link-color'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
55 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
56 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => implode( ' ', $classes ) ) ); |
18 | 57 |
|
19 | 58 |
$content = '<p class="wp-block-post-excerpt__excerpt">' . $excerpt; |
18 | 59 |
$show_more_on_new_line = ! isset( $attributes['showMoreOnNewLine'] ) || $attributes['showMoreOnNewLine']; |
60 |
if ( $show_more_on_new_line && ! empty( $more_text ) ) { |
|
61 |
$content .= '</p><p class="wp-block-post-excerpt__more-text">' . $more_text . '</p>'; |
|
62 |
} else { |
|
63 |
$content .= " $more_text</p>"; |
|
64 |
} |
|
65 |
remove_filter( 'excerpt_more', $filter_excerpt_more ); |
|
66 |
return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $content ); |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Registers the `core/post-excerpt` block on the server. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
71 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
72 |
* @since 5.8.0 |
18 | 73 |
*/ |
74 |
function register_block_core_post_excerpt() { |
|
75 |
register_block_type_from_metadata( |
|
76 |
__DIR__ . '/post-excerpt', |
|
77 |
array( |
|
78 |
'render_callback' => 'render_block_core_post_excerpt', |
|
79 |
) |
|
80 |
); |
|
81 |
} |
|
82 |
add_action( 'init', 'register_block_core_post_excerpt' ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
83 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
84 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
85 |
* If themes or plugins filter the excerpt_length, we need to |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
86 |
* override the filter in the editor, otherwise |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
87 |
* the excerpt length block setting has no effect. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
88 |
* Returns 100 because 100 is the max length in the setting. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
89 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
90 |
if ( is_admin() || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
91 |
defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
92 |
add_filter( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
93 |
'excerpt_length', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
94 |
static function () { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
95 |
return 100; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
96 |
}, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
97 |
PHP_INT_MAX |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
98 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
99 |
} |