PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.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 / block-library / blocks / post-excerpt.php

post-excerpt.php in Gutenberg 12.1.0, at build/block-library/blocks/post-excerpt.php

70 lines 2.5 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 * @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 gutenberg_render_block_core_post_excerpt( $attributes, $content, $block ) {
17 if ( ! isset( $block->context['postId'] ) ) {
18 return '';
19 }
20
21 $excerpt = get_the_excerpt( $block->context['postId'] );
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'] ) ) . '">' . $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 gutenberg_register_block_core_post_excerpt() {
62 register_block_type_from_metadata(
63 __DIR__ . '/post-excerpt',
64 array(
65 'render_callback' => 'gutenberg_render_block_core_post_excerpt',
66 )
67 );
68 }
69 add_action( 'init', 'gutenberg_register_block_core_post_excerpt', 20 );
70