PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.1
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.1
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / inc / functions / content.php

content.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.6.1, at inc/functions/content.php

125 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Content helper functions.
4 *
5 * @package ContentControl
6 * @since 2.0.0
7 * @copyright (c) 2023 Code Atlantic LLC
8 */
9
10 namespace ContentControl;
11
12 /**
13 * Get post excerpt or <!--more--> tag content for a post.
14 *
15 * This differs from get_the_excerpt in that it will return the content
16 * before the <!--more--> tag if it exists, but not generate an excerpt
17 * from the_contnet. It also doesn't filter the content.
18 *
19 * @param int|\WP_Post|null $post_id Post ID or object. Defaults to global $post.
20 * @return string
21 */
22 function get_excerpt_by_id( $post_id = null ) {
23 $post = get_post( $post_id );
24
25 $excerpt = '';
26
27 if ( $post ) {
28 if ( has_excerpt( $post ) ) {
29 // Use the excerpt if it's set.
30 $excerpt = $post->post_excerpt;
31 } else {
32 // Otherwise, use the content before the 'more' tag.
33 $content = $post->post_content;
34 $more_position = strpos( $content, '<!--more-->' );
35 if ( false !== $more_position ) {
36 // If there's a 'more' tag, return everything before it.
37 $excerpt = substr( $content, 0, $more_position );
38 }
39 }
40 }
41
42 return $excerpt;
43 }
44
45 /**
46 * Filter feed post content when needed.
47 *
48 * @param string $content Content to display.
49 * @param \ContentControl\Models\Restriction $restriction Restriction object.
50 *
51 * @return string
52 */
53 function append_post_excerpts( $content, $restriction ) {
54 global $post;
55
56 if ( $restriction->show_excerpts() ) {
57
58 // Get unfiltered excerpt.
59 $excerpt = get_excerpt_by_id( $post );
60
61 if ( ! empty( $excerpt ) ) {
62 /**
63 * Filter the allowed tags for excerpts.
64 *
65 * @param string $tags Allowed tags.
66 *
67 * @return string
68 */
69 $tags = apply_filters(
70 'content_control/excerpt_allowed_tags',
71 '<a><em><strong><blockquote><ul><ol><li><p>'
72 );
73
74 // Strip tags from excerpt.
75 $excerpt = wp_kses( $excerpt, $tags );
76
77 // Wrap excerpt in div with class.
78 $excerpt = '<div class="cc-content-excerpt">' . $excerpt . '</div>';
79
80 // Prepend excerpt to content.
81 $content = $excerpt . $content;
82 }
83 }
84
85 return $content;
86 }
87
88 /**
89 * Apply content filters for the_content without our own again.
90 *
91 * @param string $content Content to display.
92 *
93 * @return string
94 */
95 function the_content_filters( $content ) {
96 return apply_filters( 'the_content', $content );
97 }
98
99 /**
100 * Apply get_the_excerpt fitlers without our own again.
101 *
102 * @param string $excerpt Excerpt to display.
103 *
104 * @return string
105 */
106 function the_excerpt_filters( $excerpt ) {
107 global $post;
108 return apply_filters( 'get_the_excerpt', $excerpt, $post );
109 }
110
111 /**
112 * Get the current page URL.
113 *
114 * @return string
115 */
116 function get_current_page_url() {
117 global $wp;
118
119 $current_page = trailingslashit( home_url( $wp->request ) );
120
121 /* phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash */
122 return add_query_arg( $_SERVER['QUERY_STRING'], '', $current_page );
123 /* phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash */
124 }
125