|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Helpers: Post Excerpt Helper. |
|
4
|
|
|
* |
|
5
|
|
|
* The Post Excerpt Helper provides the `get_excerpt` handy function to get the |
|
6
|
|
|
* excerpt for any post. |
|
7
|
|
|
* |
|
8
|
|
|
* While WordPress' own `get_the_excerpt` does exactly the same only since version |
|
9
|
|
|
* 4.5, the function allows to specify a post id. |
|
10
|
|
|
* |
|
11
|
|
|
* Since we need to maintain compatibility with 4.2+ we need therefore this helper |
|
12
|
|
|
* function. |
|
13
|
|
|
* |
|
14
|
|
|
* @since 3.12.0 |
|
15
|
|
|
* @package Wordlift |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Define the {@link Wordlift_Post_Excerpt_Helper} class. |
|
20
|
|
|
* |
|
21
|
|
|
* @since 3.12.0 |
|
22
|
|
|
* @package Wordlift |
|
23
|
|
|
*/ |
|
24
|
|
|
class Wordlift_Post_Excerpt_Helper { |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get the text excerpt for the provided {@link WP_Post}. |
|
28
|
|
|
* |
|
29
|
|
|
* Since anyone can hook on the excerpt generation filters, and |
|
30
|
|
|
* amend it with non textual content, we play it self and generate |
|
31
|
|
|
* the excerpt ourselves, mimicking the way wordpress core does it. |
|
32
|
|
|
* |
|
33
|
|
|
* @since 3.10.0 |
|
34
|
|
|
* |
|
35
|
|
|
* @param WP_Post $post The {@link WP_Post}. |
|
36
|
|
|
* @param int $length The desired excerpt length. |
|
37
|
|
|
* @param string $more The desired more string. |
|
38
|
|
|
* |
|
39
|
|
|
* @return string The excerpt. |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function get_text_excerpt( $post, $length = 55, $more = '...' ) { |
|
42
|
|
|
|
|
43
|
|
|
// Get the excerpt and trim it. Use the `post_excerpt` if available. |
|
44
|
|
|
$excerpt = wp_trim_words( ! empty( $post->post_excerpt ) ? $post->post_excerpt : $post->post_content, $length, $more ); |
|
45
|
|
|
|
|
46
|
|
|
// Remove shortcodes and decode html entities. |
|
47
|
|
|
return html_entity_decode( self::strip_all_shortcodes( $excerpt ) ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Remove all the shortcodes from the content. We're using our own function |
|
52
|
|
|
* because WordPress' own `strip_shortcodes` only takes into consideration |
|
53
|
|
|
* shortcodes for installed plugins/themes. |
|
54
|
|
|
* |
|
55
|
|
|
* @since 3.12.0 |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $content The content with shortcodes. |
|
58
|
|
|
* |
|
59
|
|
|
* @return string The content without shortcodes. |
|
60
|
|
|
*/ |
|
61
|
|
|
private static function strip_all_shortcodes( $content ) { |
|
62
|
|
|
|
|
63
|
|
|
return preg_replace( '/\[[^]]+\]/', '', $content ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|