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

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

461 lines 9.1 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 return false;
87 }
88
89 /**
90 * Check if this is the home page.
91 *
92 * @uses current_query_context() To get the current query context.
93 *
94 * @return bool
95 *
96 * @since 2.0.0
97 */
98 function content_is_blog_index() {
99 global $post;
100
101 $context = current_query_context();
102
103 switch ( $context ) {
104 // Check main query.
105 case 'main':
106 case 'main/blocks':
107 $main_query = get_main_wp_query();
108 return $main_query->is_home();
109
110 // Check based on current post.
111 default:
112 $page_for_posts = 'page' === get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ? get_option( 'page_for_posts' ) : -1;
113
114 return (int) $page_for_posts === (int) $post->ID;
115 }
116
117 return false;
118 }
119
120 /**
121 * Check if this is an archive for a specific post type.
122 *
123 * @return bool
124 *
125 * @since 2.0.0
126 */
127 function content_is_post_type_archive() {
128 $query = get_main_wp_query();
129
130 $post_type = get_rule_extra( 'post_type', '' );
131
132 // For posts we need to check a few different things.
133 if ( 'post' === $post_type ) {
134 return ( $query->is_home() || $query->is_category() || $query->is_tag() || $query->is_date() || $query->is_author() );
135 }
136
137 // Context doesn't matter for this check.
138 return $query->is_post_type_archive( $post_type );
139 }
140
141 /**
142 * Check if this is a single post for a specific post type.
143 *
144 * @return bool
145 *
146 * @since 2.0.0
147 */
148 function content_is_post_type() {
149 $context = current_query_context();
150 $post_type = get_rule_extra( 'post_type', '' );
151
152 switch ( $context ) {
153 case 'main':
154 case 'main/blocks':
155 $main_query = get_main_wp_query();
156 return $main_query->is_singular( $post_type );
157
158 default:
159 case 'main/posts':
160 case 'posts':
161 case 'blocks':
162 return is_post_type( $post_type );
163 }
164
165 return false;
166 }
167
168 /**
169 * Check if content is a selected post(s).
170 *
171 * @return bool
172 *
173 * @since 2.0.0
174 */
175 function content_is_selected_post() {
176 global $post;
177
178 $context = current_query_context();
179
180 $post_type = get_rule_extra( 'post_type', '' );
181 // Handle array of string or int, and comman list.
182 $selected = \wp_parse_id_list(
183 get_rule_option( 'selected', [] )
184 );
185
186 switch ( $context ) {
187 case 'main':
188 case 'main/blocks':
189 $main_query = get_main_wp_query();
190 return $main_query->is_singular( $post_type ) && in_array( $main_query->get_queried_object_id(), $selected, true );
191
192 default:
193 case 'main/posts':
194 case 'posts':
195 case 'blocks':
196 return is_post_type( $post_type ) && in_array( $post->ID, $selected, true );
197 }
198
199 return false;
200 }
201
202 /**
203 * Check if the current post is a child of a selected post(s).
204 *
205 * @return bool
206 *
207 * @since 2.0.0
208 */
209 function content_is_child_of_post() {
210 global $post;
211
212 $context = current_query_context();
213
214 $post_type = get_rule_extra( 'post_type', '' );
215 // Handle array of string or int, and comman list.
216 $selected = \wp_parse_id_list(
217 get_rule_option( 'selected', [] )
218 );
219
220 if ( ! \is_post_type_hierarchical( $post_type ) ) {
221 return false;
222 }
223
224 switch ( $context ) {
225 case 'main':
226 case 'main/blocks':
227 $main_query = get_main_wp_query();
228 // Check if current page is a post type.
229 if ( ! $main_query->is_singular( $post_type ) ) {
230 return false;
231 }
232 break;
233
234 default:
235 case 'main/posts':
236 case 'posts':
237 case 'blocks':
238 // Check if current post is a post type.
239 if ( ! is_post_type( $post_type ) ) {
240 return false;
241 }
242 break;
243 }
244
245 foreach ( $selected as $id ) {
246 if ( $post->post_parent === $id ) {
247 return true;
248 }
249 }
250
251 return false;
252 }
253
254 /**
255 * Check if the current post is a ancestor of a selected post(s).
256 *
257 * @return bool
258 *
259 * @since 2.0.0
260 */
261 function content_is_ancestor_of_post() {
262 global $post;
263
264 $context = current_query_context();
265
266 $post_type = get_rule_extra( 'post_type', '' );
267 // Handle array of string or int, and comman list.
268 $selected = \wp_parse_id_list(
269 get_rule_option( 'selected', [] )
270 );
271
272 if ( ! \is_post_type_hierarchical( $post_type ) ) {
273 return false;
274 }
275
276 switch ( $context ) {
277 case 'main':
278 case 'main/blocks':
279 $main_query = get_main_wp_query();
280 // Check if current page is a post type.
281 if ( ! $main_query->is_singular( $post_type ) ) {
282 return false;
283 }
284 break;
285
286 default:
287 case 'main/posts':
288 case 'posts':
289 case 'blocks':
290 // Check if current post is a post type.
291 if ( ! is_post_type( $post_type ) ) {
292 return false;
293 }
294 break;
295 }
296
297 // Ancestors of the current page.
298 $ancestors = \get_post_ancestors( $post->ID );
299
300 foreach ( $selected as $id ) {
301 if ( in_array( $id, $ancestors, true ) ) {
302 return true;
303 }
304 }
305
306 return false;
307 }
308
309 /**
310 * Check if current post uses selected template(s).
311 *
312 * @return bool
313 *
314 * @since 2.0.0
315 */
316 function content_is_post_with_template() {
317 global $post;
318
319 $context = current_query_context();
320
321 $selected = get_rule_option( 'selected', [] );
322
323 switch ( $context ) {
324 case 'main':
325 case 'main/blocks':
326 $main_query = get_main_wp_query();
327
328 $page_template = get_page_template_slug( $main_query->get_queried_object_id() );
329 break;
330
331 default:
332 case 'main/posts':
333 case 'posts':
334 case 'blocks':
335 $page_template = get_page_template_slug( $post->ID );
336 break;
337 }
338
339 if ( empty( $selected ) ) {
340 return (bool) $page_template;
341 }
342
343 foreach ( $selected as $template ) {
344 if ( $template === $page_template ) {
345 return true;
346 }
347 }
348
349 return false;
350 }
351
352 /**
353 * Check if current post has selected taxonomy term(s).
354 *
355 * @return bool
356 *
357 * @since 2.0.0
358 */
359 function content_is_post_with_tax_term() {
360 global $post;
361
362 $context = current_query_context();
363
364 $post_type = get_rule_extra( 'post_type', '' );
365 $taxonomy = get_rule_extra( 'taxonomy', '' );
366
367 // Handle array of string or int, and comman list.
368 $selected = \wp_parse_id_list(
369 get_rule_option( 'selected', [] )
370 );
371
372 switch ( $context ) {
373 case 'main':
374 case 'main/blocks':
375 $main_query = get_main_wp_query();
376
377 if ( ! $main_query->is_singular( $post_type ) ) {
378 return false;
379 }
380
381 // Ensure we are using the main query object ID.
382 $post_id = $main_query->get_queried_object_id();
383 break;
384
385 default:
386 case 'main/posts':
387 case 'posts':
388 case 'blocks':
389 if ( ! is_post_type( $post_type ) ) {
390 return false;
391 }
392
393 $post_id = $post->ID;
394 break;
395 }
396
397 switch ( $taxonomy ) {
398 case 'category':
399 return \has_category( $selected, $post_id );
400 case 'post_tag':
401 return \has_tag( $selected, $post_id );
402 default:
403 return \has_term( $selected, $taxonomy, $post_id );
404 }
405
406 return false;
407 }
408
409 /**
410 * Check if current content is a selected taxonomy(s).
411 *
412 * @return bool
413 *
414 * @since 2.0.0
415 */
416 function content_is_taxonomy_archive() {
417 $taxonomy = get_rule_extra( 'taxonomy', '' );
418
419 $main_query = get_main_wp_query();
420
421 // No context needed, always looking for main query only.
422 switch ( $taxonomy ) {
423 case 'category':
424 return $main_query->is_category();
425 case 'post_tag':
426 return $main_query->is_tag();
427 default:
428 return $main_query->is_tax( $taxonomy );
429 }
430
431 return false;
432 }
433
434 /**
435 * Check if current content is a selected taxonomy term(s).
436 *
437 * @return bool
438 *
439 * @since 2.0.0
440 */
441 function content_is_selected_term() {
442 $main_query = get_main_wp_query();
443
444 $taxonomy = get_rule_extra( 'taxonomy', '' );
445 // Handle array of string or int, and comman list.
446 $selected = \wp_parse_id_list(
447 get_rule_option( 'selected', [] )
448 );
449
450 switch ( $taxonomy ) {
451 case 'category':
452 return $main_query->is_category( $selected );
453 case 'post_tag':
454 return $main_query->is_tag( $selected );
455 default:
456 return $main_query->is_tax( $taxonomy, $selected );
457 }
458
459 return false;
460 }
461