content.php
3.14 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* Content helper functions.
*
* @package ContentControl
* @since 2.0.0
* @copyright (c) 2023 Code Atlantic LLC
*/
namespace ContentControl;
/**
* Get post excerpt or <!--more--> tag content for a post.
*
* This differs from get_the_excerpt in that it will return the content
* before the <!--more--> tag if it exists, but not generate an excerpt
* from the_contnet. It also doesn't filter the content.
*
* @param int|\WP_Post|null $post_id Post ID or object. Defaults to global $post.
* @return string
*/
function get_excerpt_by_id( $post_id = null ) {
$post = get_post( $post_id );
$excerpt = '';
if ( $post ) {
if ( has_excerpt( $post ) ) {
// Use the excerpt if it's set.
$excerpt = $post->post_excerpt;
} else {
// Otherwise, use the content before the 'more' tag.
$content = $post->post_content;
$more_position = strpos( $content, '<!--more-->' );
if ( false !== $more_position ) {
// If there's a 'more' tag, return everything before it.
$excerpt = substr( $content, 0, $more_position );
}
}
}
return $excerpt;
}
/**
* Filter feed post content when needed.
*
* @param string $content Content to display.
* @param \ContentControl\Models\Restriction $restriction Restriction object.
*
* @return string
*/
function append_post_excerpts( $content, $restriction ) {
global $post;
if ( $restriction->show_excerpts() ) {
// Get unfiltered excerpt.
$excerpt = get_excerpt_by_id( $post );
if ( ! empty( $excerpt ) ) {
/**
* Filter the allowed tags for excerpts.
*
* @param string $tags Allowed tags.
*
* @return string
*/
$tags = apply_filters(
'content_control/excerpt_allowed_tags',
'<a><em><strong><blockquote><ul><ol><li><p>'
);
// Strip tags from excerpt.
$excerpt = wp_kses( $excerpt, $tags );
// Wrap excerpt in div with class.
$excerpt = '<div class="cc-content-excerpt">' . $excerpt . '</div>';
// Prepend excerpt to content.
$content = $excerpt . $content;
}
}
return $content;
}
/**
* Apply content filters for the_content without our own again.
*
* @param string $content Content to display.
*
* @return string
*/
function the_content_filters( $content ) {
return apply_filters( 'the_content', $content );
}
/**
* Apply get_the_excerpt fitlers without our own again.
*
* @param string $excerpt Excerpt to display.
*
* @return string
*/
function the_excerpt_filters( $excerpt ) {
global $post;
return apply_filters( 'get_the_excerpt', $excerpt, $post );
}
/**
* Get the current page URL.
*
* @return string
*/
function get_current_page_url() {
global $wp;
$current_page = trailingslashit( home_url( $wp->request ) );
/* phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash */
return add_query_arg( $_SERVER['QUERY_STRING'], '', $current_page );
/* phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash */
}