| 1 |
<?php |
| 2 |
namespace Elementor; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Elementor conditions. |
| 10 |
* |
| 11 |
* Elementor conditions handler class introduce the compare conditions and the |
| 12 |
* check conditions methods. |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
class Conditions { |
| 17 |
|
| 18 |
/** |
| 19 |
* Compare conditions. |
| 20 |
* |
| 21 |
* Whether the two values comply the comparison operator. |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
* @access public |
| 25 |
* @static |
| 26 |
* |
| 27 |
* @param mixed $left_value First value to compare. |
| 28 |
* @param mixed $right_value Second value to compare. |
| 29 |
* @param string $operator Comparison operator. |
| 30 |
* |
| 31 |
* @return bool Whether the two values complies the comparison operator. |
| 32 |
*/ |
| 33 |
public static function compare( $left_value, $right_value, $operator ) { |
| 34 |
switch ( $operator ) { |
| 35 |
case '==': |
| 36 |
return $left_value == $right_value; |
| 37 |
case '!=': |
| 38 |
return $left_value != $right_value; |
| 39 |
case '!==': |
| 40 |
return $left_value !== $right_value; |
| 41 |
case 'in': |
| 42 |
return in_array( $left_value, $right_value, true ); |
| 43 |
case '!in': |
| 44 |
return ! in_array( $left_value, $right_value, true ); |
| 45 |
case 'contains': |
| 46 |
return in_array( $right_value, $left_value, true ); |
| 47 |
case '!contains': |
| 48 |
return ! in_array( $right_value, $left_value, true ); |
| 49 |
case '<': |
| 50 |
return $left_value < $right_value; |
| 51 |
case '<=': |
| 52 |
return $left_value <= $right_value; |
| 53 |
case '>': |
| 54 |
return $left_value > $right_value; |
| 55 |
case '>=': |
| 56 |
return $left_value >= $right_value; |
| 57 |
default: |
| 58 |
return $left_value === $right_value; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Check conditions. |
| 64 |
* |
| 65 |
* Whether the comparison conditions comply. |
| 66 |
* |
| 67 |
* @since 1.0.0 |
| 68 |
* @access public |
| 69 |
* @static |
| 70 |
* |
| 71 |
* @param array $conditions The conditions to check. |
| 72 |
* @param array $comparison The comparison parameter. |
| 73 |
* |
| 74 |
* @return bool Whether the comparison conditions comply. |
| 75 |
*/ |
| 76 |
public static function check( array $conditions, array $comparison ) { |
| 77 |
$is_or_condition = isset( $conditions['relation'] ) && 'or' === $conditions['relation']; |
| 78 |
|
| 79 |
$condition_succeed = ! $is_or_condition; |
| 80 |
|
| 81 |
foreach ( $conditions['terms'] as $term ) { |
| 82 |
if ( ! empty( $term['terms'] ) ) { |
| 83 |
$comparison_result = self::check( $term, $comparison ); |
| 84 |
} else { |
| 85 |
preg_match( '/(\w+)(?:\[(\w+)])?/', $term['name'], $parsed_name ); |
| 86 |
|
| 87 |
$value = $comparison[ $parsed_name[1] ]; |
| 88 |
|
| 89 |
if ( ! empty( $parsed_name[2] ) ) { |
| 90 |
$value = $value[ $parsed_name[2] ]; |
| 91 |
} |
| 92 |
|
| 93 |
$operator = null; |
| 94 |
|
| 95 |
if ( ! empty( $term['operator'] ) ) { |
| 96 |
$operator = $term['operator']; |
| 97 |
} |
| 98 |
|
| 99 |
$comparison_result = self::compare( $value, $term['value'], $operator ); |
| 100 |
} |
| 101 |
|
| 102 |
if ( $is_or_condition ) { |
| 103 |
if ( $comparison_result ) { |
| 104 |
return true; |
| 105 |
} |
| 106 |
} elseif ( ! $comparison_result ) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return $condition_succeed; |
| 112 |
} |
| 113 |
} |
| 114 |
|