conditions.php 2.6 KB

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