PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / trunk
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More vtrunk
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 trunk, at inc/functions/content.php

135 lines 3.3 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 defined( 'ABSPATH' ) || exit;
13
14 /**
15 * Get post excerpt or <!--more--> tag content for a post.
16 *
17 * This differs from get_the_excerpt in that it will return the content
18 * before the <!--more--> tag if it exists, but not generate an excerpt
19 * from the_contnet. It also doesn't filter the content.
20 *
21 * @param int|\WP_Post|null $post_id Post ID or object. Defaults to global $post.
22 * @return string
23 */
24 function get_excerpt_by_id( $post_id = null ) {
25 $post = get_post( $post_id );
26
27 $excerpt = '';
28
29 if ( $post ) {
30 if ( has_excerpt( $post ) ) {
31 // Use the excerpt if it's set.
32 $excerpt = $post->post_excerpt;
33 } else {
34 // Otherwise, use the content before the 'more' tag.
35 $content = $post->post_content;
36 $more_position = strpos( $content, '<!--more-->' );
37 if ( false !== $more_position ) {
38 // If there's a 'more' tag, return everything before it.
39 $excerpt = substr( $content, 0, $more_position );
40 }
41 }
42 }
43
44 return $excerpt;
45 }
46
47 /**
48 * Filter feed post content when needed.
49 *
50 * @param string $content Content to display.
51 * @param \ContentControl\Models\Restriction $restriction Restriction object.
52 *
53 * @return string
54 */
55 function append_post_excerpts( $content, $restriction ) {
56 global $post;
57
58 if ( $restriction->show_excerpts() ) {
59
60 // Get unfiltered excerpt.
61 $excerpt = get_excerpt_by_id( $post );
62
63 if ( ! empty( $excerpt ) ) {
64 /**
65 * Filter the allowed tags for excerpts.
66 *
67 * @param string $tags Allowed tags.
68 *
69 * @return string
70 */
71 $tags = apply_filters(
72 'content_control/excerpt_allowed_tags',
73 '<a><em><strong><blockquote><ul><ol><li><p>'
74 );
75
76 // Strip tags from excerpt.
77 $excerpt = wp_kses( $excerpt, $tags );
78
79 // Wrap excerpt in div with class.
80 $excerpt = '<div class="cc-content-excerpt">' . $excerpt . '</div>';
81
82 // Prepend excerpt to content.
83 $content = $excerpt . $content;
84 }
85 }
86
87 return $content;
88 }
89
90 /**
91 * Apply content filters for the_content without our own again.
92 *
93 * @param string $content Content to display.
94 *
95 * @return string
96 */
97 function the_content_filters( $content ) {
98 return apply_filters( 'the_content', $content );
99 }
100
101 /**
102 * Apply get_the_excerpt fitlers without our own again.
103 *
104 * @param string $excerpt Excerpt to display.
105 *
106 * @return string
107 */
108 function the_excerpt_filters( $excerpt ) {
109 global $post;
110 return apply_filters( 'get_the_excerpt', $excerpt, $post );
111 }
112
113 /**
114 * Get the current page URL.
115 *
116 * @return string
117 */
118 function get_current_page_url() {
119 global $wp;
120
121 $current_page = trailingslashit( home_url( $wp->request ) );
122 $query_args = [];
123
124 if ( isset( $_SERVER['QUERY_STRING'] ) && is_string( $_SERVER['QUERY_STRING'] ) ) {
125 // Parse before sanitizing decoded values; sanitizing the raw string removes percent encoding.
126 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
127 $query_string = wp_unslash( $_SERVER['QUERY_STRING'] );
128 wp_parse_str( $query_string, $query_args );
129 $query_args = map_deep( $query_args, 'sanitize_text_field' );
130 $query_args = map_deep( $query_args, 'rawurlencode' );
131 }
132
133 return add_query_arg( $query_args, $current_page );
134 }
135