Rule.php
4.65 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
<?php
/**
* Rule engine rule model.
*
* @package ContentControl
* @subpackage Models
*/
namespace ContentControl\Models\RuleEngine;
use function ContentControl\plugin;
use function ContentControl\Rules\current_rule;
/**
* Handler for condition rules.
*
* @package ContentControl
*/
class Rule extends Item {
/**
* Unique Hash ID.
*
* @var string
*/
public $id;
/**
* Rule name.
*
* @var string
*/
public $name;
/**
* Rule options.
*
* @var array<string,mixed>
*/
public $options;
/**
* Rule not operand.
*
* @var boolean
*/
public $not_operand;
/**
* Rule extras.
*
* Such as post type or taxnomy like meta.
*
* @var array<string,mixed>
*/
public $extras = [];
/**
* Rule is frontend only.
*
* @var boolean
*/
public $frontend_only = false;
/**
* Rule definition.
*
* @var array<string,mixed>|null
*/
public $definition;
/**
* Rule is deprecated.
*
* @var boolean
*/
public $deprecated = false;
/**
* Build a rule.
*
* @param array{id:string,name:string,notOperand:bool,options:array<string,mixed>,extras:array<string,mixed>} $rule Rule data.
*/
public function __construct( $rule ) {
$rule = wp_parse_args( $rule, [
'id' => '',
'name' => '',
'notOperand' => false,
'options' => [],
'extras' => [],
]);
if ( isset( $rule['deprecated'] ) ) {
$this->deprecated = $rule['deprecated'];
}
$name = $rule['name'];
$this->definition = plugin( 'rules' )->get_rule( $name );
if ( ! $this->definition ) {
/* translators: 1. Rule name. */
plugin( 'logging' )->log_unique( 'ERROR: ' . sprintf( __( 'Rule `%s` not found.', 'content-control' ), $name ) );
}
$extras = isset( $this->definition['extras'] ) ? $this->definition['extras'] : [];
$this->id = $rule['id'];
$this->name = $name;
$this->not_operand = $rule['notOperand'];
$this->frontend_only = isset( $this->definition['frontend'] ) ? $this->definition['frontend'] : false;
$this->options = $this->parse_options( $rule['options'] );
$this->extras = array_merge( $extras, $rule['extras'] );
}
/**
* Parse rule options based on rule definitions.
*
* @param array<string,mixed> $options Array of rule opions.
* @return array<string,mixed>
*/
public function parse_options( $options = [] ) {
return $options;
}
/**
* Check the results of this rule.
*
* @return bool
*/
public function check_rule() {
if ( $this->is_js_rule() ) {
return true;
}
$check = $this->run_check();
return $this->not_operand ? ! $check : $check;
}
/**
* Check the results of this rule.
*
* @return bool True if rule passes, false if not.
*/
private function run_check() {
$callback = isset( $this->definition['callback'] ) ? $this->definition['callback'] : null;
if ( ! $callback ) {
/* translators: 1. Rule name. */
plugin( 'logging' )->log_unique( 'ERROR: ' . esc_html( sprintf( __( 'Rule `%s` has no callback.', 'content-control' ), $this->name ) ) );
return false;
}
if ( ! is_callable( $callback ) ) {
/* translators: 1. Rule name. 2. Callback name. */
plugin( 'logging' )->log_unique( 'ERROR: ' . esc_html( sprintf( __( 'Rule `%1$s` callback is not callable (%2$s).', 'content-control' ), $this->name, $callback ) ) );
return false;
}
// Set global current rule so it can be easily accessed.
current_rule( $this );
if ( $this->deprecated ) {
$settings = [
'target' => $this->name,
'settings' => $this->options,
];
// Old rules had the settings passed as the first argument.
$check = call_user_func( $callback, $settings );
} else {
/**
* All rule options can be accessed via the global.
*
* @see \ContentControl\Rules\current_rule()
*/
$check = call_user_func( $callback );
}
// Clear global current rule.
current_rule( null );
return $check;
}
/**
* Check if this rule's callback is based in JS rather than PHP.
*
* @return bool
*/
public function is_js_rule() {
return $this->frontend_only;
}
/**
* Return the rule check as boolean or null if the rule is JS based.
*
* @return bool|null
*/
public function get_check() {
if ( $this->is_js_rule() ) {
return null;
}
return $this->run_check();
}
/**
* Return the rule check as an array of information.
*
* Useful for debugging.
*
* @return array<string,mixed>|null
*/
public function get_check_info() {
if ( $this->is_js_rule() ) {
return null;
}
return [
'result' => $this->run_check(),
'id' => $this->id,
'rule' => $this->name,
'not' => $this->not_operand,
'args' => $this->options,
'def' => $this->definition,
];
}
}