| 1 |
<?php |
| 2 |
/** |
| 3 |
* Rule engine rule model. |
| 4 |
* |
| 5 |
* @package ContentControl |
| 6 |
* @subpackage Models |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ContentControl\Models\RuleEngine; |
| 10 |
|
| 11 |
use function ContentControl\plugin; |
| 12 |
use function ContentControl\Rules\current_rule; |
| 13 |
|
| 14 |
/** |
| 15 |
* Handler for condition rules. |
| 16 |
* |
| 17 |
* @package ContentControl |
| 18 |
*/ |
| 19 |
class Rule extends Item { |
| 20 |
|
| 21 |
/** |
| 22 |
* Unique Hash ID. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
public $id; |
| 27 |
|
| 28 |
/** |
| 29 |
* Rule name. |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
public $name; |
| 34 |
|
| 35 |
/** |
| 36 |
* Rule options. |
| 37 |
* |
| 38 |
* @var array<string,mixed> |
| 39 |
*/ |
| 40 |
public $options; |
| 41 |
|
| 42 |
/** |
| 43 |
* Rule not operand. |
| 44 |
* |
| 45 |
* @var boolean |
| 46 |
*/ |
| 47 |
public $not_operand; |
| 48 |
|
| 49 |
/** |
| 50 |
* Rule extras. |
| 51 |
* |
| 52 |
* Such as post type or taxnomy like meta. |
| 53 |
* |
| 54 |
* @var array<string,mixed> |
| 55 |
*/ |
| 56 |
public $extras = []; |
| 57 |
|
| 58 |
/** |
| 59 |
* Rule is frontend only. |
| 60 |
* |
| 61 |
* @var boolean |
| 62 |
*/ |
| 63 |
public $frontend_only = false; |
| 64 |
|
| 65 |
/** |
| 66 |
* Rule definition. |
| 67 |
* |
| 68 |
* @var array<string,mixed>|null |
| 69 |
*/ |
| 70 |
public $definition; |
| 71 |
|
| 72 |
/** |
| 73 |
* Rule is deprecated. |
| 74 |
* |
| 75 |
* @var boolean |
| 76 |
*/ |
| 77 |
public $deprecated = false; |
| 78 |
|
| 79 |
/** |
| 80 |
* Build a rule. |
| 81 |
* |
| 82 |
* @param array{id:string,name:string,notOperand:bool,options:array<string,mixed>,extras:array<string,mixed>} $rule Rule data. |
| 83 |
*/ |
| 84 |
public function __construct( $rule ) { |
| 85 |
$rule = wp_parse_args( $rule, [ |
| 86 |
'id' => '', |
| 87 |
'name' => '', |
| 88 |
'notOperand' => false, |
| 89 |
'options' => [], |
| 90 |
'extras' => [], |
| 91 |
]); |
| 92 |
|
| 93 |
if ( isset( $rule['deprecated'] ) ) { |
| 94 |
$this->deprecated = $rule['deprecated']; |
| 95 |
} |
| 96 |
|
| 97 |
$name = $rule['name']; |
| 98 |
|
| 99 |
$this->definition = plugin( 'rules' )->get_rule( $name ); |
| 100 |
|
| 101 |
if ( ! $this->definition ) { |
| 102 |
/* translators: 1. Rule name. */ |
| 103 |
plugin( 'logging' )->log_unique( 'ERROR: ' . sprintf( __( 'Rule `%s` not found.', 'content-control' ), $name ) ); |
| 104 |
} |
| 105 |
|
| 106 |
$extras = isset( $this->definition['extras'] ) ? $this->definition['extras'] : []; |
| 107 |
|
| 108 |
$this->id = $rule['id']; |
| 109 |
$this->name = $name; |
| 110 |
$this->not_operand = $rule['notOperand']; |
| 111 |
$this->frontend_only = isset( $this->definition['frontend'] ) ? $this->definition['frontend'] : false; |
| 112 |
$this->options = $this->parse_options( $rule['options'] ); |
| 113 |
$this->extras = array_merge( $extras, $rule['extras'] ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Parse rule options based on rule definitions. |
| 118 |
* |
| 119 |
* @param array<string,mixed> $options Array of rule opions. |
| 120 |
* @return array<string,mixed> |
| 121 |
*/ |
| 122 |
public function parse_options( $options = [] ) { |
| 123 |
return $options; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Check the results of this rule. |
| 128 |
* |
| 129 |
* @return bool |
| 130 |
*/ |
| 131 |
public function check_rule() { |
| 132 |
if ( $this->is_js_rule() ) { |
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
$check = $this->run_check(); |
| 137 |
|
| 138 |
return $this->not_operand ? ! $check : $check; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Check the results of this rule. |
| 143 |
* |
| 144 |
* @return bool True if rule passes, false if not. |
| 145 |
*/ |
| 146 |
private function run_check() { |
| 147 |
$callback = isset( $this->definition['callback'] ) ? $this->definition['callback'] : null; |
| 148 |
|
| 149 |
if ( ! $callback ) { |
| 150 |
/* translators: 1. Rule name. */ |
| 151 |
plugin( 'logging' )->log_unique( 'ERROR: ' . esc_html( sprintf( __( 'Rule `%s` has no callback.', 'content-control' ), $this->name ) ) ); |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
if ( ! is_callable( $callback ) ) { |
| 156 |
/* translators: 1. Rule name. 2. Callback name. */ |
| 157 |
plugin( 'logging' )->log_unique( 'ERROR: ' . esc_html( sprintf( __( 'Rule `%1$s` callback is not callable (%2$s).', 'content-control' ), $this->name, $callback ) ) ); |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
// Set global current rule so it can be easily accessed. |
| 162 |
current_rule( $this ); |
| 163 |
|
| 164 |
if ( $this->deprecated ) { |
| 165 |
$settings = [ |
| 166 |
'target' => $this->name, |
| 167 |
'settings' => $this->options, |
| 168 |
]; |
| 169 |
|
| 170 |
// Old rules had the settings passed as the first argument. |
| 171 |
$check = call_user_func( $callback, $settings ); |
| 172 |
} else { |
| 173 |
/** |
| 174 |
* All rule options can be accessed via the global. |
| 175 |
* |
| 176 |
* @see \ContentControl\Rules\current_rule() |
| 177 |
*/ |
| 178 |
$check = call_user_func( $callback ); |
| 179 |
} |
| 180 |
|
| 181 |
// Clear global current rule. |
| 182 |
current_rule( null ); |
| 183 |
|
| 184 |
return $check; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Check if this rule's callback is based in JS rather than PHP. |
| 189 |
* |
| 190 |
* @return bool |
| 191 |
*/ |
| 192 |
public function is_js_rule() { |
| 193 |
return $this->frontend_only; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Return the rule check as boolean or null if the rule is JS based. |
| 198 |
* |
| 199 |
* @return bool|null |
| 200 |
*/ |
| 201 |
public function get_check() { |
| 202 |
if ( $this->is_js_rule() ) { |
| 203 |
return null; |
| 204 |
} |
| 205 |
|
| 206 |
return $this->run_check(); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Return the rule check as an array of information. |
| 211 |
* |
| 212 |
* Useful for debugging. |
| 213 |
* |
| 214 |
* @return array<string,mixed>|null |
| 215 |
*/ |
| 216 |
public function get_check_info() { |
| 217 |
if ( $this->is_js_rule() ) { |
| 218 |
return null; |
| 219 |
} |
| 220 |
|
| 221 |
return [ |
| 222 |
'result' => $this->run_check(), |
| 223 |
'id' => $this->id, |
| 224 |
'rule' => $this->name, |
| 225 |
'not' => $this->not_operand, |
| 226 |
'args' => $this->options, |
| 227 |
'def' => $this->definition, |
| 228 |
]; |
| 229 |
} |
| 230 |
} |
| 231 |
|