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