PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.4
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.4
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 / classes / Controllers / Frontend / Restrictions / PostContent.php

PostContent.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.6.4, at classes/Controllers/Frontend/Restrictions/PostContent.php

228 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Frontend post content restrictions.
4 *
5 * @copyright (c) 2023, Code Atlantic LLC.
6 * @package ContentControl
7 */
8
9 namespace ContentControl\Controllers\Frontend\Restrictions;
10
11 use ContentControl\Base\Controller;
12
13 use function ContentControl\content_is_restricted;
14 use function ContentControl\protection_is_disabled;
15 use function ContentControl\get_applicable_restriction;
16
17 defined( 'ABSPATH' ) || exit;
18
19 /**
20 * Class for handling global restrictions of the post contents.
21 *
22 * @package ContentControl
23 */
24 class PostContent extends Controller {
25
26 /**
27 * Initiate functionality.
28 */
29 public function init() {
30 $this->enable_filters();
31 }
32
33 /**
34 * Enable filters.
35 *
36 * @return void
37 */
38 public function enable_filters() {
39 add_filter( 'the_content', [ $this, 'filter_the_content_if_restricted' ], 1000 );
40 add_filter( 'get_the_excerpt', [ $this, 'filter_the_excerpt_if_restricted' ], 1000, 2 );
41 // phpcs:disable Squiz.PHP.CommentedOutCode.Found, Squiz.Commenting.InlineComment.InvalidEndChar -- These are for future use.
42 // add_filter( 'the_title', [ $this, 'filter_the_title_if_restricted'], 1000, 2 );
43 // add_filter( 'get_the_excerpt', [ $this, 'filter_the_excerpt_if_restricted' ], 1000, 2 );
44 // add_filter( 'post_class', [ $this, 'filter_post_class_if_restricted' ], 1000, 3 );
45 // add_filter( 'post_password_required', [ $this, 'require_password_if_restricted' ], 1000, 2 );
46 // add_filter( 'the_password_form', [ $this, 'filter_password_form_if_restricted' ], 1000, 2 );
47 // phpcs:enable Squiz.PHP.CommentedOutCode.Found, Squiz.Commenting.InlineComment.InvalidEndChar
48 }
49
50 /**
51 * Disable filters.
52 *
53 * @return void
54 */
55 public function disable_filters() {
56 remove_filter( 'the_content', [ $this, 'filter_the_content_if_restricted' ], 1000 );
57 remove_filter( 'get_the_excerpt', [ $this, 'filter_the_excerpt_if_restricted' ], 1000 );
58 }
59
60 /**
61 * Filter post content when needed.
62 *
63 * NOTE: If we got this far with restricted content, this is the last attempt to protect
64 * it. This serves as the default fallback protection method if all others fail.
65 *
66 * @param string $content Content of post being checked.
67 *
68 * @return string
69 */
70 public function filter_the_content_if_restricted( $content ) {
71 $filter_name = 'content_control/restricted_post_content';
72
73 // If this isn't a post type that can be restricted, bail.
74 if ( protection_is_disabled() ) {
75 return $content;
76 }
77
78 if ( ! content_is_restricted() ) {
79 return $content;
80 }
81
82 // Ensure we don't get into an infinite loop.
83 if ( doing_filter( $filter_name ) || doing_filter( 'get_the_excerpt' ) ) {
84 return $content;
85 }
86
87 $restriction = get_applicable_restriction();
88
89 if ( false === $restriction ) {
90 return $content;
91 }
92
93 // If this is a replacement page, bail.
94 if (
95 ( 'replace' === $restriction->get_setting( 'protectionMethod' ) && 'page' === $restriction->get_setting( 'replacementType' ) && is_page( $restriction->get_setting( 'replacementPage' ) ) ) ||
96 ( 'replace_archive_page' === $restriction->get_setting( 'archiveHandling' ) && is_page( $restriction->get_setting( 'archiveReplacementPage' ) ) )
97 ) {
98 return $content;
99 }
100
101 /**
102 * Prevent the default restriction message from being shown by returning a custom
103 * message or content.
104 *
105 * @param null|string $pre_restrict_content The content to display.
106 * @param string $content The content.
107 * @param \ContentControl\Models\Restriction $restriction The restriction.
108 *
109 * @return string
110 */
111 $pre_restrict_content = apply_filters( 'content_control/pre_restrict_content', null, $content, $restriction );
112
113 if ( null !== $pre_restrict_content ) {
114 return $pre_restrict_content;
115 }
116
117 $message = \ContentControl\get_default_denial_message();
118
119 /**
120 * If the restriction has a custom message, use it.
121 *
122 * We could check $restriction->replacement_type, but we need a safe default for
123 * all cases. Further we do content filtering for all sub queries and currently
124 * don't offer a way to override the message for those.
125 *
126 * In this way currently users can change to content replacement, set the override
127 * message, then change back to page replacement and the override message will still
128 * be used for the post in sub queries.
129 */
130 if ( $restriction->get_setting( 'overrideMessage' ) ) {
131 $message = $restriction->get_message();
132 }
133
134 /**
135 * Filter the message to display when a post is restricted.
136 *
137 * @param string $message Message to display.
138 * @param \ContentControl\Models\Restriction $restriction The restriction.
139 *
140 * @return string
141 */
142 return apply_filters(
143 $filter_name,
144 // If the default message is empty, show a generic message.
145 ! empty( $message ) ? $message : __( 'This content is restricted.', 'content-control' ),
146 $restriction
147 );
148 }
149
150 /**
151 * Filter post excerpt when needed.
152 *
153 * @param string $post_excerpt The post excerpt.
154 * @param \WP_Post $post Post object.
155 *
156 * @return string
157 */
158 public function filter_the_excerpt_if_restricted( $post_excerpt, $post = null ) {
159 $filter_name = 'content_control/restricted_post_excerpt';
160
161 // If this isn't a post type that can be restricted, bail.
162 if ( protection_is_disabled() ) {
163 return $post_excerpt;
164 }
165
166 if ( ! content_is_restricted( $post->ID ) ) {
167 return $post_excerpt;
168 }
169
170 if ( doing_filter( $filter_name ) ) {
171 return $post_excerpt;
172 }
173
174 $restriction = get_applicable_restriction( $post->ID );
175
176 if ( false === $restriction ) {
177 return $post_excerpt;
178 }
179
180 /**
181 * Filter to bypass the default restriction message for excerpts.
182 *
183 * @since 2.6.2
184 *
185 * @param null|string $pre_excerpt Return a non-null value to bypass.
186 * @param string $post_excerpt The original excerpt.
187 * @param \ContentControl\Models\Restriction $restriction The restriction object.
188 */
189 $pre_restrict_excerpt = apply_filters( 'content_control/pre_restrict_excerpt', null, $post_excerpt, $restriction );
190
191 if ( null !== $pre_restrict_excerpt ) {
192 return $pre_restrict_excerpt;
193 }
194
195 $message = \ContentControl\get_default_denial_message();
196
197 /**
198 * If the restriction has a custom message, use it.
199 *
200 * We could check $restriction->replacement_type, but we need a safe default for
201 * all cases. Further we do content filtering for all sub queries and currently
202 * don't offer a way to override the message for those.
203 *
204 * In this way currently users can change to content replacement, set the override
205 * message, then change back to page replacement and the override message will still
206 * be used for the post in sub queries.
207 */
208 if ( $restriction->get_setting( 'overrideMessage' ) ) {
209 $message = $restriction->get_message();
210 }
211
212 /**
213 * Filter the excerpt message to display when a post is restricted.
214 *
215 * @param string $message Message to display.
216 * @param \ContentControl\Models\Restriction $restriction The restriction.
217 *
218 * @return string
219 */
220 return apply_filters(
221 $filter_name,
222 // If the default message is empty, show a generic message.
223 ! empty( $message ) ? $message : __( 'This content is restricted.', 'content-control' ),
224 $restriction
225 );
226 }
227 }
228