PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0
Elementor Website Builder – more than just a page builder v4.3.0
4.3.2 4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 All 455 releases
elementor / modules / atomic-widgets / children-dependencies / children-dependency-evaluator.php

children-dependency-evaluator.php in Elementor Website Builder – more than just a page builder 4.3.0, at modules/atomic-widgets/children-dependencies/children-dependency-evaluator.php

132 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\AtomicWidgets\ChildrenDependencies;
4
5 use Elementor\Modules\AtomicWidgets\PropDependencies\Manager as Dependency_Manager;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 class Children_Dependency_Evaluator {
12 public static function is_met( ?array $when, array $resolved_settings ): bool {
13 if ( empty( $when['terms'] ) ) {
14 return true;
15 }
16
17 $relation = $when['relation'] ?? 'or';
18 $results = array_map(
19 fn( $term ) => self::evaluate_term( $term, $resolved_settings ),
20 $when['terms']
21 );
22
23 if ( 'and' === $relation ) {
24 return ! in_array( false, $results, true );
25 }
26
27 return in_array( true, $results, true );
28 }
29
30 private static function evaluate_term( array $term, array $resolved_settings ): bool {
31 if ( isset( $term['terms'] ) ) {
32 return self::is_met( $term, $resolved_settings );
33 }
34
35 $path = $term['path'] ?? [];
36 $actual_value = self::extract_path( $resolved_settings, $path );
37
38 return self::compare( $term['operator'] ?? 'eq', $actual_value, $term['value'] ?? null );
39 }
40
41 private static function extract_path( array $settings, array $path ) {
42 $value = $settings;
43
44 foreach ( $path as $segment ) {
45 if ( ! is_array( $value ) || ! array_key_exists( $segment, $value ) ) {
46 return null;
47 }
48
49 $value = $value[ $segment ];
50 }
51
52 return $value;
53 }
54
55 private static function compare( string $operator, $actual_value, $expected_value ): bool {
56 // Fail closed: an operator outside `Manager::OPERATORS` hides the conditional child
57 // instead of silently keeping it visible.
58 if ( ! in_array( $operator, Dependency_Manager::OPERATORS, true ) ) {
59 return false;
60 }
61
62 switch ( $operator ) {
63 case 'eq':
64 case 'ne':
65 return ( $actual_value === $expected_value ) === ( 'eq' === $operator );
66
67 case 'gt':
68 case 'lte':
69 if ( ! is_numeric( $actual_value ) || ! is_numeric( $expected_value ) ) {
70 return false;
71 }
72
73 return ( (float) $actual_value > (float) $expected_value ) === ( 'gt' === $operator );
74
75 case 'lt':
76 case 'gte':
77 if ( ! is_numeric( $actual_value ) || ! is_numeric( $expected_value ) ) {
78 return false;
79 }
80
81 return ( (float) $actual_value < (float) $expected_value ) === ( 'lt' === $operator );
82
83 case 'in':
84 case 'nin':
85 if ( ! is_array( $expected_value ) ) {
86 return false;
87 }
88
89 return in_array( $actual_value, $expected_value, true ) === ( 'in' === $operator );
90
91 case 'contains':
92 case 'ncontains':
93 $contains = self::contains( $actual_value, $expected_value );
94
95 if ( null === $contains ) {
96 return false;
97 }
98
99 return ( 'contains' === $operator ) === $contains;
100
101 case 'exists':
102 case 'not_exist':
103 $exists = (bool) $actual_value || 0 === $actual_value || false === $actual_value;
104
105 return ( 'exists' === $operator ) === $exists;
106
107 default:
108 return false;
109 }
110 }
111
112 /**
113 * @return bool|null Null when the values are not comparable.
114 */
115 private static function contains( $actual_value, $expected_value ): ?bool {
116 if ( is_array( $actual_value ) ) {
117 $haystack = array_map(
118 fn( $item ) => is_array( $item ) && array_key_exists( 'value', $item ) ? $item['value'] : $item,
119 $actual_value
120 );
121
122 return in_array( $expected_value, $haystack, true );
123 }
124
125 if ( is_string( $actual_value ) && is_string( $expected_value ) ) {
126 return false !== strpos( $actual_value, $expected_value );
127 }
128
129 return null;
130 }
131 }
132