-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.php
70 lines (63 loc) · 1.67 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Server-side rendering of the `core/post-excerpt` block.
*
* @package WordPress
*/
/**
* Renders the `core/post-excerpt` block on the server.
*
* @param array $attributes The block attributes.
*
* @return string Returns the filtered post excerpt for the current post wrapped inside "p" tags.
*/
function render_block_core_post_excerpt( $attributes ) {
$post = gutenberg_get_post_from_context();
if ( ! $post ) {
return '';
}
$more_text = isset( $attributes['moreText'] ) ? '<a href="' . esc_url( get_the_permalink( $post ) ) . '">' . $attributes['moreText'] . '</a>' : '';
$filter_excerpt_length = function() use ( $attributes ) {
return isset( $attributes['wordCount'] ) ? $attributes['wordCount'] : 55;
};
add_filter(
'excerpt_length',
$filter_excerpt_length
);
$output = '<p>' . get_the_excerpt( $post );
if ( ! isset( $attributes['showMoreOnNewLine'] ) || $attributes['showMoreOnNewLine'] ) {
$output .= '</p>' . '<p>' . $more_text . '</p>';
} else {
$output .= ' ' . $more_text . '</p>';
}
remove_filter(
'excerpt_length',
$filter_excerpt_length
);
return $output;
}
/**
* Registers the `core/post-excerpt` block on the server.
*/
function register_block_core_post_excerpt() {
register_block_type(
'core/post-excerpt',
array(
'attributes' => array(
'wordCount' => array(
'type' => 'number',
'default' => 55,
),
'moreText' => array(
'type' => 'string',
),
'showMoreOnNewLine' => array(
'type' => 'boolean',
'default' => true,
),
),
'render_callback' => 'render_block_core_post_excerpt',
)
);
}
add_action( 'init', 'register_block_core_post_excerpt' );