PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.1
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.1
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 / Models / RuleEngine / Query.php

Query.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.6.1, at classes/Models/RuleEngine/Query.php

170 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Rule engine query model.
4 *
5 * @package ContentControl
6 * @subpackage Models
7 */
8
9 namespace ContentControl\Models\RuleEngine;
10
11 /**
12 * Handler for condition queries.
13 *
14 * @package ContentControl
15 */
16 class Query {
17
18 /**
19 * Query logical comparison operator.
20 *
21 * @var string `and` | `or`
22 */
23 public $logical_operator;
24
25 /**
26 * Query items.
27 *
28 * @var Item[]
29 */
30 public $items;
31
32 /**
33 * Build a query.
34 *
35 * @param array{logicalOperator:string,items:array<mixed>} $query Query data.
36 */
37 public function __construct( $query ) {
38 $query = wp_parse_args( $query, [
39 'logicalOperator' => 'and',
40 'items' => [],
41 ]);
42
43 $this->logical_operator = $query['logicalOperator'];
44 $this->items = [];
45
46 foreach ( $query['items'] as $item ) {
47 $is_group = ( isset( $item['type'] ) && 'group' === $item['type'] )
48 || isset( $item['query'] );
49
50 $this->items[] = $is_group ? new Group( $item ) : new Rule( $item );
51 }
52 }
53
54 /**
55 * Check if this query has any rules.
56 *
57 * @return bool
58 */
59 public function has_rules() {
60 return ! empty( $this->items );
61 }
62
63 /**
64 * Check if this query has JS based rules.
65 *
66 * @return bool
67 */
68 public function has_js_rules() {
69 foreach ( $this->items as $item ) {
70 if ( $item instanceof Rule ) {
71 if ( $item->is_js_rule() ) {
72 return true;
73 }
74 } elseif ( $item instanceof Group ) {
75 if ( $item->has_js_rules() ) {
76 return true;
77 }
78 }
79 }
80
81 return false;
82 }
83
84 /**
85 * Check rules in a recursive nested pattern.
86 *
87 * @return bool
88 */
89 public function check_rules() {
90 $checks = [];
91
92 if ( empty( $this->items ) ) {
93 return true;
94 }
95
96 foreach ( $this->items as $item ) {
97 // Missing rules should result in restricted content.
98 $result = false;
99
100 if ( $item instanceof Rule ) {
101 $result = $item->check_rule();
102 } elseif ( $item instanceof Group ) {
103 $result = $item->check_rules();
104 }
105
106 $checks[] = $result;
107
108 // Bail as early as we can.
109 if (
110 // If we have a true result and are using `or`.
111 ( true === $result && 'or' === $this->logical_operator ) ||
112 // If we have a false result and are using `and`.
113 ( false === $result && 'and' === $this->logical_operator )
114 ) {
115 break;
116 }
117 }
118
119 /*
120 * This method ignores null values (JS conditions),
121 * if changed, null needs to be accounted for.
122 */
123 if ( 'or' === $this->logical_operator ) {
124 // If any values are true or null, return true.
125 return in_array( true, $checks, true );
126 } else {
127 // If any values are false, return false.
128 return ! in_array( false, $checks, true );
129 }
130 }
131
132 /**
133 * Return the checks as an array.
134 *
135 * Useful for debugging or passing to JS.
136 *
137 * @return array<bool|null|array<bool|null>>
138 */
139 public function get_checks() {
140 $checks = [];
141
142 foreach ( $this->items as $item ) {
143 if ( $item instanceof Rule ) {
144 $checks[] = $item->get_check();
145 } elseif ( $item instanceof Group ) {
146 $checks[] = $item->get_checks();
147 }
148 }
149
150 return $checks;
151 }
152
153 /**
154 * Return the checks as an array of information.
155 *
156 * Useful for debugging.
157 *
158 * @return array<mixed>
159 */
160 public function get_check_info() {
161 $checks = [];
162
163 foreach ( $this->items as $key => $item ) {
164 $checks[ $key ] = $item->get_check_info();
165 }
166
167 return $checks;
168 }
169 }
170