PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.12
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.12
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 / rule-callbacks.php

rule-callbacks.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.0.12, at inc/functions/rule-callbacks.php

447 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Rule callback functions.
4 *
5 * @package ContentControl
6 */
7
8 namespace ContentControl\Rules;
9
10 defined( 'ABSPATH' ) || exit;
11
12 use function ContentControl\get_main_wp_query;
13 use function ContentControl\Rules\is_post_type;
14 use function ContentControl\Rules\get_rule_extra;
15 use function ContentControl\Rules\get_rule_option;
16 use function ContentControl\current_query_context;
17 use function ContentControl\Rules\allowed_user_roles;
18
19 /**
20 * Checks if a user has one of the selected roles.
21 *
22 * @return bool
23 *
24 * @since 2.0.0
25 */
26 function user_has_role() {
27 if ( ! \is_user_logged_in() ) {
28 return false;
29 }
30
31 // Get rules from the current rule.
32 $roles = get_rule_option( 'roles', [] );
33
34 if ( ! count( $roles ) ) {
35 return true;
36 }
37
38 // Get Enabled Roles to check for.
39 $user_roles = array_keys( allowed_user_roles() );
40
41 // Get the roles that are both enabled and required.
42 $required_roles = array_intersect( $user_roles, $roles );
43
44 if ( count( $required_roles ) === 0 ) {
45 return true;
46 }
47
48 // Check if the user has one of the required roles.
49 foreach ( $required_roles as $role ) {
50 if ( \current_user_can( $role ) ) {
51 return true;
52 }
53 }
54
55 return false;
56 }
57
58 /**
59 * Check if this is the home page.
60 *
61 * @uses current_query_context() To get the current query context.
62 *
63 * @return bool
64 *
65 * @since 2.0.0
66 */
67 function content_is_home_page() {
68 global $post;
69
70 $context = current_query_context();
71
72 switch ( $context ) {
73 // Check main query.
74 case 'main':
75 case 'main/blocks':
76 $main_query = get_main_wp_query();
77 return $main_query->is_front_page();
78
79 // Check based on current post.
80 default:
81 $page_id = 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) ? get_option( 'page_on_front' ) : -1;
82
83 return (int) $page_id === (int) $post->ID;
84 }
85 }
86
87 /**
88 * Check if this is the home page.
89 *
90 * @uses current_query_context() To get the current query context.
91 *
92 * @return bool
93 *
94 * @since 2.0.0
95 */
96 function content_is_blog_index() {
97 global $post;
98
99 $context = current_query_context();
100
101 switch ( $context ) {
102 // Check main query.
103 case 'main':
104 case 'main/blocks':
105 $main_query = get_main_wp_query();
106 return $main_query->is_home();
107
108 // Check based on current post.
109 default:
110 $page_for_posts = 'page' === get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ? get_option( 'page_for_posts' ) : -1;
111
112 return (int) $page_for_posts === (int) $post->ID;
113 }
114 }
115
116 /**
117 * Check if this is an archive for a specific post type.
118 *
119 * @return bool
120 *
121 * @since 2.0.0
122 */
123 function content_is_post_type_archive() {
124 $query = get_main_wp_query();
125
126 $post_type = get_rule_extra( 'post_type', '' );
127
128 // For posts we need to check a few different things.
129 if ( 'post' === $post_type ) {
130 return ( $query->is_home() || $query->is_category() || $query->is_tag() || $query->is_date() || $query->is_author() );
131 }
132
133 // Context doesn't matter for this check.
134 return $query->is_post_type_archive( $post_type );
135 }
136
137 /**
138 * Check if this is a single post for a specific post type.
139 *
140 * @return bool
141 *
142 * @since 2.0.0
143 */
144 function content_is_post_type() {
145 $context = current_query_context();
146 $post_type = get_rule_extra( 'post_type', '' );
147
148 switch ( $context ) {
149 case 'main':
150 case 'main/blocks':
151 $main_query = get_main_wp_query();
152 return $main_query->is_singular( $post_type );
153
154 default:
155 case 'main/posts':
156 case 'posts':
157 case 'blocks':
158 return is_post_type( $post_type );
159 }
160 }
161
162 /**
163 * Check if content is a selected post(s).
164 *
165 * @return bool
166 *
167 * @since 2.0.0
168 */
169 function content_is_selected_post() {
170 global $post;
171
172 $context = current_query_context();
173
174 $post_type = get_rule_extra( 'post_type', '' );
175 // Handle array of string or int, and comman list.
176 $selected = \wp_parse_id_list(
177 get_rule_option( 'selected', [] )
178 );
179
180 switch ( $context ) {
181 case 'main':
182 case 'main/blocks':
183 $main_query = get_main_wp_query();
184 return $main_query->is_singular( $post_type ) && in_array( $main_query->get_queried_object_id(), $selected, true );
185
186 default:
187 case 'main/posts':
188 case 'posts':
189 case 'blocks':
190 return is_post_type( $post_type ) && in_array( $post->ID, $selected, true );
191 }
192 }
193
194 /**
195 * Check if the current post is a child of a selected post(s).
196 *
197 * @return bool
198 *
199 * @since 2.0.0
200 */
201 function content_is_child_of_post() {
202 global $post;
203
204 $context = current_query_context();
205
206 $post_type = get_rule_extra( 'post_type', '' );
207 // Handle array of string or int, and comman list.
208 $selected = \wp_parse_id_list(
209 get_rule_option( 'selected', [] )
210 );
211
212 if ( ! \is_post_type_hierarchical( $post_type ) ) {
213 return false;
214 }
215
216 switch ( $context ) {
217 case 'main':
218 case 'main/blocks':
219 $main_query = get_main_wp_query();
220 // Check if current page is a post type.
221 if ( ! $main_query->is_singular( $post_type ) ) {
222 return false;
223 }
224 break;
225
226 default:
227 case 'main/posts':
228 case 'posts':
229 case 'blocks':
230 // Check if current post is a post type.
231 if ( ! is_post_type( $post_type ) ) {
232 return false;
233 }
234 break;
235 }
236
237 foreach ( $selected as $id ) {
238 if ( $post->post_parent === $id ) {
239 return true;
240 }
241 }
242
243 return false;
244 }
245
246 /**
247 * Check if the current post is a ancestor of a selected post(s).
248 *
249 * @return bool
250 *
251 * @since 2.0.0
252 */
253 function content_is_ancestor_of_post() {
254 global $post;
255
256 $context = current_query_context();
257
258 $post_type = get_rule_extra( 'post_type', '' );
259 // Handle array of string or int, and comman list.
260 $selected = \wp_parse_id_list(
261 get_rule_option( 'selected', [] )
262 );
263
264 if ( ! \is_post_type_hierarchical( $post_type ) ) {
265 return false;
266 }
267
268 switch ( $context ) {
269 case 'main':
270 case 'main/blocks':
271 $main_query = get_main_wp_query();
272 // Check if current page is a post type.
273 if ( ! $main_query->is_singular( $post_type ) ) {
274 return false;
275 }
276 break;
277
278 default:
279 case 'main/posts':
280 case 'posts':
281 case 'blocks':
282 // Check if current post is a post type.
283 if ( ! is_post_type( $post_type ) ) {
284 return false;
285 }
286 break;
287 }
288
289 // Ancestors of the current page.
290 $ancestors = \get_post_ancestors( $post->ID );
291
292 foreach ( $selected as $id ) {
293 if ( in_array( $id, $ancestors, true ) ) {
294 return true;
295 }
296 }
297
298 return false;
299 }
300
301 /**
302 * Check if current post uses selected template(s).
303 *
304 * @return bool
305 *
306 * @since 2.0.0
307 */
308 function content_is_post_with_template() {
309 global $post;
310
311 $context = current_query_context();
312
313 $selected = get_rule_option( 'selected', [] );
314
315 switch ( $context ) {
316 case 'main':
317 case 'main/blocks':
318 $main_query = get_main_wp_query();
319
320 $page_template = get_page_template_slug( $main_query->get_queried_object_id() );
321 break;
322
323 default:
324 case 'main/posts':
325 case 'posts':
326 case 'blocks':
327 $page_template = get_page_template_slug( $post->ID );
328 break;
329 }
330
331 if ( empty( $selected ) ) {
332 return (bool) $page_template;
333 }
334
335 foreach ( $selected as $template ) {
336 if ( $template === $page_template ) {
337 return true;
338 }
339 }
340
341 return false;
342 }
343
344 /**
345 * Check if current post has selected taxonomy term(s).
346 *
347 * @return bool
348 *
349 * @since 2.0.0
350 */
351 function content_is_post_with_tax_term() {
352 global $post;
353
354 $context = current_query_context();
355
356 $post_type = get_rule_extra( 'post_type', '' );
357 $taxonomy = get_rule_extra( 'taxonomy', '' );
358
359 // Handle array of string or int, and comman list.
360 $selected = \wp_parse_id_list(
361 get_rule_option( 'selected', [] )
362 );
363
364 switch ( $context ) {
365 case 'main':
366 case 'main/blocks':
367 $main_query = get_main_wp_query();
368
369 if ( ! $main_query->is_singular( $post_type ) ) {
370 return false;
371 }
372
373 // Ensure we are using the main query object ID.
374 $post_id = $main_query->get_queried_object_id();
375 break;
376
377 default:
378 case 'main/posts':
379 case 'posts':
380 case 'blocks':
381 if ( ! is_post_type( $post_type ) ) {
382 return false;
383 }
384
385 $post_id = $post->ID;
386 break;
387 }
388
389 switch ( $taxonomy ) {
390 case 'category':
391 return \has_category( $selected, $post_id );
392 case 'post_tag':
393 return \has_tag( $selected, $post_id );
394 default:
395 return \has_term( $selected, $taxonomy, $post_id );
396 }
397 }
398
399 /**
400 * Check if current content is a selected taxonomy(s).
401 *
402 * @return bool
403 *
404 * @since 2.0.0
405 */
406 function content_is_taxonomy_archive() {
407 $taxonomy = get_rule_extra( 'taxonomy', '' );
408
409 $main_query = get_main_wp_query();
410
411 // No context needed, always looking for main query only.
412 switch ( $taxonomy ) {
413 case 'category':
414 return $main_query->is_category();
415 case 'post_tag':
416 return $main_query->is_tag();
417 default:
418 return $main_query->is_tax( $taxonomy );
419 }
420 }
421
422 /**
423 * Check if current content is a selected taxonomy term(s).
424 *
425 * @return bool
426 *
427 * @since 2.0.0
428 */
429 function content_is_selected_term() {
430 $main_query = get_main_wp_query();
431
432 $taxonomy = get_rule_extra( 'taxonomy', '' );
433 // Handle array of string or int, and comman list.
434 $selected = \wp_parse_id_list(
435 get_rule_option( 'selected', [] )
436 );
437
438 switch ( $taxonomy ) {
439 case 'category':
440 return $main_query->is_category( $selected );
441 case 'post_tag':
442 return $main_query->is_tag( $selected );
443 default:
444 return $main_query->is_tax( $taxonomy, $selected );
445 }
446 }
447