rule-callbacks.php
17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
<?php
/**
* Rule callback functions.
*
* @package ContentControl
*/
namespace ContentControl\Rules;
defined( 'ABSPATH' ) || exit;
use function ContentControl\get_main_wp_query;
use function ContentControl\Rules\is_post_type;
use function ContentControl\Rules\get_rule_extra;
use function ContentControl\Rules\get_rule_option;
use function ContentControl\current_query_context;
use function ContentControl\get_rest_api_intent;
use function ContentControl\Rules\allowed_user_roles;
/**
* Checks if a user has one of the selected roles.
*
* @return bool
*
* @since 2.0.0
*/
function user_has_role() {
if ( ! \is_user_logged_in() ) {
return false;
}
// Get rules from the current rule.
$roles = get_rule_option( 'roles', [] );
if ( ! count( $roles ) ) {
return true;
}
// Get Enabled Roles to check for.
$user_roles = array_keys( allowed_user_roles() );
/**
* Get the roles that are both enabled and required.
*
* @var string[] $required_roles
*/
$required_roles = array_intersect( $user_roles, $roles );
if ( count( $required_roles ) === 0 ) {
return true;
}
// Check if the user has one of the required roles.
foreach ( $required_roles as $role ) {
if ( \current_user_can( $role ) ) {
return true;
}
}
return false;
}
/**
* Check if this is the home page.
*
* @uses current_query_context() To get the current query context.
*
* @return bool
*
* @since 2.0.0
*/
function content_is_home_page() {
global $post;
$context = current_query_context();
switch ( $context ) {
// Check main query.
case 'main':
case 'main/blocks':
$main_query = get_main_wp_query();
return $main_query->is_front_page();
// Check based on current post.
default:
$page_id = 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) ? get_option( 'page_on_front' ) : -1;
$post_id = $post && is_a( $post, '\WP_Post' ) ? $post->ID : 0;
return (int) $page_id === (int) $post_id;
}
}
/**
* Check if this is the home page.
*
* @uses current_query_context() To get the current query context.
*
* @return bool
*
* @since 2.0.0
*/
function content_is_blog_index() {
global $post;
$context = current_query_context();
switch ( $context ) {
// Check main query.
case 'main':
case 'main/blocks':
$main_query = get_main_wp_query();
return $main_query->is_home();
// Check based on current post.
default:
$page_for_posts = 'page' === get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ? get_option( 'page_for_posts' ) : -1;
$post_id = $post && is_a( $post, '\WP_Post' ) ? $post->ID : 0;
return (int) $page_for_posts === (int) $post_id;
}
}
/**
* Check if this is an archive for a specific post type.
*
* @return bool
*
* @since 2.0.0
* @since 2.2.0 Added support for REST API.
*/
function content_is_post_type_archive() {
$context = current_query_context();
$query = get_main_wp_query();
$post_type = get_rule_extra( 'post_type', '' );
switch ( $context ) {
default:
// For posts we need to check a few different things.
if ( 'post' === $post_type ) {
return ( $query->is_home() || $query->is_category() || $query->is_tag() || $query->is_date() || $query->is_author() );
}
// Context doesn't matter for this check.
return $query->is_post_type_archive( $post_type );
case 'restapi':
$rest_intent = get_rest_api_intent();
if ( 'unknown' === $rest_intent['type'] ) {
return false;
}
if ( ! $rest_intent['index'] ) {
return false;
}
// First be sure this is for a post type of the right kind.
if ( ! rest_intent_matches_post_type( $post_type, $rest_intent ) ) {
return false;
}
return true;
case 'restapi/posts':
// This is for main query only.
return false;
}
}
/**
* Check if this is a single post for a specific post type.
*
* @return bool
*
* @since 2.0.0
* @since 2.2.0 Added support for REST API.
*/
function content_is_post_type() {
$context = current_query_context();
$post_type = get_rule_extra( 'post_type', '' );
switch ( $context ) {
case 'main':
case 'main/blocks':
$main_query = get_main_wp_query();
return $main_query->is_singular( $post_type );
default:
case 'main/posts':
case 'posts':
case 'blocks':
return is_post_type( $post_type );
case 'restapi':
$rest_intent = get_rest_api_intent();
if ( 'unknown' === $rest_intent['type'] ) {
return false;
}
// First be sure this is for a singular post type of the right kind.
if ( ! rest_intent_matches_post_type( $post_type, $rest_intent ) ) {
return false;
}
if ( true === $rest_intent['index'] ) {
return true;
}
$post_id = $rest_intent['id'] > 0 ? $rest_intent['id'] : 0;
return get_post_type( $post_id ) === $post_type;
case 'restapi/posts':
global $post;
return $post && $post->post_type === $post_type;
}
}
/**
* Check if content is a selected post(s).
*
* @return bool
*
* @since 2.0.0
* @since 2.2.0 Added support for REST API.
*/
function content_is_selected_post() {
global $post;
$context = current_query_context();
$post_type = get_rule_extra( 'post_type', '' );
// Handle array of string or int, and comman list.
$selected = \wp_parse_id_list(
get_rule_option( 'selected', [] )
);
$post_id = $post && is_a( $post, '\WP_Post' ) ? $post->ID : null;
switch ( $context ) {
case 'main':
case 'main/blocks':
$main_query = get_main_wp_query();
return $main_query->is_singular( $post_type ) && in_array( $main_query->get_queried_object_id(), $selected, true );
default:
case 'main/posts':
case 'posts':
case 'blocks':
return is_post_type( $post_type ) && in_array( $post_id, $selected, true );
case 'restapi':
case 'restapi/posts':
$rest_intent = get_rest_api_intent();
if ( 'unknown' === $rest_intent['type'] ) {
return false;
}
if ( ! rest_intent_matches_post_type( $post_type, $rest_intent ) ) {
return false;
}
if ( 'restapi' === $context ) {
if ( $rest_intent['id'] > 0 ) {
$post_id = (int) $rest_intent['id'];
}
}
return in_array( $post_id, $selected, true );
}
}
/**
* Check if the current post is a child of a selected post(s).
*
* @return bool
*
* @since 2.0.0
* @since 2.2.0 Added support for REST API.
*/
function content_is_child_of_post() {
global $post;
$context = current_query_context();
$post_type = get_rule_extra( 'post_type', '' );
// Handle array of string or int, and comman list.
$selected = \wp_parse_id_list(
get_rule_option( 'selected', [] )
);
if ( ! \is_post_type_hierarchical( $post_type ) ) {
return false;
}
$the_post = isset( $post ) ? $post : null;
switch ( $context ) {
case 'main':
case 'main/blocks':
$main_query = get_main_wp_query();
// Check if current page is a post type.
if ( ! $main_query->is_singular( $post_type ) ) {
return false;
}
break;
default:
case 'main/posts':
case 'posts':
case 'blocks':
case 'restapi/posts':
// Check if current post is a post type.
if ( ! is_post_type( $post_type ) ) {
return false;
}
break;
case 'restapi':
$rest_intent = get_rest_api_intent();
if ( 'unknown' === $rest_intent['type'] ) {
return false;
}
if ( ! rest_intent_matches_post_type( $post_type, $rest_intent ) ) {
return false;
}
if ( $rest_intent['id'] > 0 ) {
$the_post = get_post( (int) $rest_intent['id'] );
}
break;
}
if ( ! $the_post ) {
return false;
}
foreach ( $selected as $id ) {
if ( $the_post->post_parent === $id ) {
return true;
}
}
return false;
}
/**
* Check if the current post is a ancestor of a selected post(s).
*
* @return bool
*
* @since 2.0.0
* @since 2.2.0 Added support for REST API.
*/
function content_is_ancestor_of_post() {
global $post;
$context = current_query_context();
$post_type = get_rule_extra( 'post_type', '' );
// Handle array of string or int, and comman list.
$selected = \wp_parse_id_list(
get_rule_option( 'selected', [] )
);
if ( ! \is_post_type_hierarchical( $post_type ) ) {
return false;
}
$the_post = isset( $post ) ? $post : null;
switch ( $context ) {
case 'main':
case 'main/blocks':
$main_query = get_main_wp_query();
// Check if current page is a post type.
if ( ! $main_query->is_singular( $post_type ) ) {
return false;
}
break;
default:
case 'main/posts':
case 'posts':
case 'blocks':
case 'restapi/posts':
// Check if current post is a post type.
if ( ! is_post_type( $post_type ) ) {
return false;
}
break;
case 'restapi':
$rest_intent = get_rest_api_intent();
if ( 'unknown' === $rest_intent['type'] ) {
return false;
}
if ( ! rest_intent_matches_post_type( $post_type, $rest_intent ) ) {
return false;
}
if ( $rest_intent['id'] > 0 ) {
$the_post = get_post( (int) $rest_intent['id'] );
}
break;
}
// Ancestors of the current page.
$ancestors = $the_post ? \get_post_ancestors( $the_post->ID ) : [];
foreach ( $selected as $id ) {
if ( in_array( $id, $ancestors, true ) ) {
return true;
}
}
return false;
}
/**
* Check if current post uses selected template(s).
*
* @return bool
*
* @since 2.0.0
*/
function content_is_post_with_template() {
$context = current_query_context();
$selected = get_rule_option( 'selected', [] );
$page_template = '';
switch ( $context ) {
case 'main':
case 'main/blocks':
$main_query = get_main_wp_query();
$page_template = get_page_template_slug( $main_query->get_queried_object_id() );
break;
default:
case 'main/posts':
case 'posts':
case 'blocks':
global $post;
$post_id = $post && is_a( $post, '\WP_Post' ) ? $post->ID : null;
if ( $post_id ) {
$page_template = get_page_template_slug( $post_id );
}
break;
}
if ( empty( $selected ) ) {
return (bool) $page_template;
}
foreach ( $selected as $template ) {
if ( $template === $page_template ) {
return true;
}
}
return false;
}
/**
* Check if current post has selected taxonomy term(s).
*
* @return bool
*
* @since 2.0.0
* @since 2.2.0 Added support for REST API.
*/
function content_is_post_with_tax_term() {
global $post;
$context = current_query_context();
$post_type = get_rule_extra( 'post_type', '' );
$taxonomy = get_rule_extra( 'taxonomy', '' );
// Handle array of string or int, and comman list.
$selected = \wp_parse_id_list(
get_rule_option( 'selected', [] )
);
$post_id = $post && is_a( $post, '\WP_Post' ) ? $post->ID : null;
switch ( $context ) {
case 'main':
case 'main/blocks':
$main_query = get_main_wp_query();
if ( ! $main_query->is_singular( $post_type ) ) {
return false;
}
// Ensure we are using the main query object ID.
$post_id = $main_query->get_queried_object_id();
break;
case 'restapi':
case 'restapi/posts':
$rest_intent = get_rest_api_intent();
if ( 'unknown' === $rest_intent['type'] ) {
return false;
}
// Bail if we didn't detect a post type in the intent, or the post type is not the same as the one we're checking.
if ( ! rest_intent_matches_post_type( $post_type, $rest_intent ) ) {
return false;
}
if ( 'restapi' === $context ) {
if ( $rest_intent['id'] > 0 ) {
$post_id = (int) $rest_intent['id'];
}
}
break;
default:
case 'main/posts':
case 'posts':
case 'blocks':
if ( ! is_post_type( $post_type ) ) {
return false;
}
break;
}
switch ( $taxonomy ) {
case 'category':
return \has_category( $selected, $post_id );
case 'post_tag':
return \has_tag( $selected, $post_id );
default:
return \has_term( $selected, $taxonomy, $post_id );
}
}
/**
* Check if current content is a selected taxonomy(s).
*
* @return bool
*
* @since 2.0.0
* @since 2.2.0 Added support for REST API.
*/
function content_is_taxonomy_archive() {
// Get settings from the current rule.
$taxonomy = get_rule_extra( 'taxonomy', '' );
// Get query context.
$context = current_query_context();
switch ( $context ) {
// Handle detection of rest taxonomy endpoint.
case 'restapi':
$rest_intent = get_rest_api_intent();
if ( 'unknown' === $rest_intent['type'] ) {
return false;
}
return rest_intent_matches_taxonomy( $taxonomy, $rest_intent );
case 'restapi/terms':
// This is a term query, so we we aren't checking against a taxonomy archive.
return false;
default:
$main_query = get_main_wp_query();
// No context needed, always looking for main query only.
switch ( $taxonomy ) {
case 'category':
return $main_query->is_category();
case 'post_tag':
return $main_query->is_tag();
default:
return $main_query->is_tax( $taxonomy );
}
}
}
/**
* Check if current content is a selected taxonomy term(s).
*
* @return bool
*
* @since 2.0.0
* @since 2.2.0 Added support for REST API.
*/
function content_is_selected_term() {
// Get settings from the current rule.
$taxonomy = get_rule_extra( 'taxonomy', '' );
// Handle array of string or int, and comman list.
$selected = \wp_parse_id_list(
get_rule_option( 'selected', [] )
);
// Get query context.
$context = current_query_context();
// Handle detection of rest taxonomy endpoint.
switch ( $context ) {
default:
// Get main query for 'main' context.
$main_query = get_main_wp_query();
// Handle everything else.
switch ( $taxonomy ) {
case 'category':
return $main_query->is_category( $selected );
case 'post_tag':
return $main_query->is_tag( $selected );
default:
return $main_query->is_tax( $taxonomy, $selected );
}
case 'terms':
$term = get_global( 'term' ); // Used instead of global $cc_term.
// Check if we have a term object from the term query.
if ( $term && $term->term_id > 0 ) {
return in_array( (int) $term->term_id, $selected, true );
}
break;
// Handles REST API querys.
case 'restapi':
case 'restapi/terms':
$rest_intent = get_rest_api_intent();
$term = get_global( 'term' ); // Used instead of global $cc_term.
if ( 'unknown' === $rest_intent['type'] ) {
return false;
}
if ( ! rest_intent_matches_taxonomy( $taxonomy, $rest_intent ) ) {
return false;
}
// Check if we have a term ID from the rest intent.
if ( $rest_intent['id'] > 0 ) {
return in_array( (int) $rest_intent['id'], $selected, true );
}
// Check if we have a term object from the term query.
if ( $term && $term->term_id > 0 ) {
return in_array( (int) $term->term_id, $selected, true );
}
// Always return false if no ID is set.
return false;
}
return false;
}
/**
* Check if post type matches.
*
* @param string $type Type to check (post type or taxonomy key).
* @param string|string[] $matches Type matches against. Array or string of comma separated values.
*
* @return bool
*
* @since 2.3.0
*/
function check_type_match( $type, $matches ) {
// Deal with 'any' and 'all' values.
if (
// Check if any or all is in $mathces array.
(
is_array( $matches ) &&
(
in_array( 'any', $matches, true ) ||
in_array( 'all', $matches, true )
)
) ||
'any' === $matches ||
'all' === $matches
) {
return true;
}
if ( is_string( $matches ) ) {
$matches = explode( ',', $matches );
}
return in_array( $type, $matches, true );
}
/**
* Simplifies checking if a post type matches a rest intent.
*
* @param string $post_type Post type to check.
* @param array<string,bool|string|int>|null $rest_intent Rest intent to check.
*
* @return bool
*
* @since 2.3.0
*/
function rest_intent_matches_post_type( $post_type, $rest_intent = null ) {
if ( ! $rest_intent ) {
$rest_intent = get_rest_api_intent();
}
// Fill in defaults to prevent errors.
wp_parse_args( $rest_intent, [
'type' => '',
'name' => '',
] );
// Check if this is a post type intent.
if ( 'post_type' !== $rest_intent['type'] ) {
return false;
}
$post_type_object = get_post_type_object( $post_type );
// Check that rest_base or selected name match.
return // Check the rest_base for the selected post type matches the intent.
check_type_match( $post_type_object->rest_base, $rest_intent['name'] ) ||
// Check the post type matches the intent.
check_type_match( $post_type, $rest_intent['name'] );
}
/**
* Simplifies checking if a taxonomy matches a rest intent.
*
* @param string $taxonomy Taxonomy to check.
* @param array<string,bool|string|int>|null $rest_intent Rest intent to check.
*
* @return bool
*
* @since 2.3.0
*/
function rest_intent_matches_taxonomy( $taxonomy, $rest_intent = null ) {
if ( ! $rest_intent ) {
$rest_intent = get_rest_api_intent();
}
// Fill in defaults to prevent errors.
wp_parse_args( $rest_intent, [
'type' => '',
'name' => '',
] );
// Check if this is a taxonomy intent.
if ( 'taxonomy' !== $rest_intent['type'] ) {
return false;
}
$taxonomy_object = get_taxonomy( $taxonomy );
// Check that rest_base or selected name match.
return // Check the rest_base for the selected taxonomy matches the intent.
check_type_match( $taxonomy_object->rest_base, $rest_intent['name'] ) ||
// Check the taxonomy matches the intent.
check_type_match( $taxonomy, $rest_intent['name'] );
}