PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.3.1
JetFormBuilder — Dynamic Blocks Form Builder v3.3.1
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / actions / conditions / condition-instance.php
jetformbuilder / includes / actions / conditions Last commit date
condition-instance.php 2 years ago condition-manager.php 2 years ago
condition-instance.php
311 lines
1 <?php
2
3
4 namespace Jet_Form_Builder\Actions\Conditions;
5
6 use Jet_Form_Builder\Classes\Tools;
7 use Jet_Form_Builder\Exceptions\Condition_Silence_Exception;
8 use Jet_Form_Builder\Presets\Types\Dynamic_Preset;
9
10 // If this file is called directly, abort.
11 if ( ! defined( 'WPINC' ) ) {
12 die;
13 }
14
15 class Condition_Instance {
16
17 private $type;
18 private $events;
19
20 private $operator;
21 private $must_be_true = true;
22 private $field_name;
23 private $compare;
24 private $compare_value_format;
25
26 private function get_manager(): Condition_Manager {
27 return jet_fb_action_handler()->get_current_condition_manager();
28 }
29
30 /**
31 * @param $condition
32 *
33 * @return $this
34 */
35 public function set_condition( $condition ): Condition_Instance {
36 $this->set_type( $condition['type'] ?? '' );
37
38 if ( ! $this->is_field_compare() ) {
39 $this->set_events( $condition['events'] ?? array() );
40
41 return $this;
42 }
43
44 $this->set_operator( $condition['operator'] ?? false );
45 $this->set_field( $condition['field'] ?? '' );
46 $this->set_compare_value_format( $condition['compare_value_format'] ?? '' );
47 $this->set_compare( $this->get_parsed_value( $condition['default'] ?? '' ) );
48 $this->set_must_be( $condition['execute'] ?? '' );
49
50 return $this;
51 }
52
53 /**
54 * @return bool
55 */
56 public function is_correct(): bool {
57 $this->sanitize_operator();
58
59 $result = $this->check();
60
61 return $this->get_must_be() ? $result : ! $result;
62 }
63
64 /**
65 * @throws Condition_Silence_Exception
66 */
67 public function is_correct_with_throw() {
68 $this->end_condition(
69 $this->is_correct(),
70 'The condition was not met.',
71 get_object_vars( $this )
72 );
73 }
74
75 public function get_compare_value_transformer() {
76 return $this->compare_value_format;
77 }
78
79 public function set_compare_value_format( $format ): Condition_Instance {
80 $this->compare_value_format = (string) $format;
81
82 return $this;
83 }
84
85
86 public function set_operator( string $operator ): Condition_Instance {
87 $this->operator = $operator;
88
89 return $this;
90 }
91
92 public function sanitize_operator(): Condition_Instance {
93 $operators = $this->get_manager()->get_operators_list();
94
95 if ( ! $this->operator || ! in_array( $this->operator, $operators, true ) ) {
96 $this->operator = '';
97 }
98
99 return $this;
100 }
101
102 public function get_operator() {
103 return $this->operator;
104 }
105
106 /**
107 * @return mixed
108 */
109 public function get_events() {
110 return $this->events;
111 }
112
113 /**
114 * @param mixed $events
115 */
116 public function set_events( array $events ) {
117 $this->events = $events;
118 }
119
120 /**
121 * @param $field_name
122 */
123 public function set_field( $field_name ) {
124 $this->field_name = $field_name;
125 }
126
127 public function get_field_name() {
128 return $this->field_name;
129 }
130
131 public function get_field_value() {
132 return jet_fb_context()->get_value( $this->get_field_name() );
133 }
134
135 /**
136 * @return mixed
137 */
138 public function get_type() {
139 return $this->type;
140 }
141
142 /**
143 * @param mixed $type
144 */
145 public function set_type( string $type ) {
146 $this->type = empty( $type ) ? 'field' : $type;
147 }
148
149 public function is_field_compare(): bool {
150 return 'field' === $this->get_type();
151 }
152
153 public function set_compare( $compare ): Condition_Instance {
154 return $this->set_compare_raw( $compare )->transform_compare_value();
155 }
156
157 public function set_compare_raw( $raw_compare ): Condition_Instance {
158 $this->compare = $raw_compare;
159
160 return $this;
161 }
162
163 public function transform_compare_value(): Condition_Instance {
164 $transformer = $this->get_compare_value_transformer();
165
166 if ( ! $transformer ) {
167 return $this;
168 }
169
170 $transformer = $this->get_manager()->get_transformers_find(
171 'value',
172 $transformer
173 );
174
175 $callback = $transformer['callback'] ?? false;
176
177 if ( ! is_callable( $callback ) ) {
178 return $this;
179 }
180
181 $this->set_compare_value_with_callback( $callback );
182
183 return $this;
184 }
185
186 public function set_compare_value_with_callback( $transformer_callback ) {
187 $operators_need_explode = $this->get_manager()->get_operators_filter( 'need_explode', true );
188
189 if ( ! in_array( $this->get_operator(), $operators_need_explode, true ) ) {
190 $this->set_compare_raw( call_user_func( $transformer_callback, $this->get_compare() ) );
191
192 return;
193 }
194 $this->set_compare_raw( array_map( $transformer_callback, $this->get_compare_as_array() ) );
195 }
196
197 public function get_compare() {
198 return $this->compare;
199 }
200
201 public function set_must_be( $must_be ): Condition_Instance {
202 $this->must_be_true = (bool) $must_be;
203
204 return $this;
205 }
206
207 public function get_must_be(): bool {
208 return $this->must_be_true;
209 }
210
211 public function get_parsed_value( $maybe_json_string ) {
212 return ( new Dynamic_Preset() )->parse_json( $maybe_json_string );
213 }
214
215 public function get_compare_as_array(): array {
216 $compare = $this->get_compare();
217
218 if ( is_array( $compare ) ) {
219 return $compare;
220 }
221
222 $this->compare = array_map( 'trim', explode( ',', $compare ) );
223
224 return $this->compare;
225 }
226
227 /**
228 * @return bool
229 */
230 public function check(): bool {
231 switch ( $this->get_operator() ) {
232 case 'equal':
233 return ( (string) $this->get_field_value() ) === ( (string) $this->get_compare() );
234 case 'greater':
235 return $this->get_field_value() > $this->get_compare();
236 case 'less':
237 return $this->get_field_value() < $this->get_compare();
238
239 case 'between':
240 $field = $this->get_field_value();
241 $compare_values = $this->get_compare_as_array();
242
243 if ( count( $compare_values ) !== 2 ) {
244 return false;
245 }
246
247 return ( $compare_values[0] < $field && $compare_values[1] > $field );
248 case 'one_of':
249 return $this->check_one_of();
250 case 'contain':
251 $field = Tools::to_string( $this->get_field_value() );
252 $compare = Tools::to_string( $this->get_compare() );
253
254 return ( strpos( $field, $compare ) !== false );
255 default:
256 return apply_filters( 'jet-form-builder/actions/process-condition', false, $this );
257 }
258 }
259
260 protected function check_one_of(): bool {
261 $field = $this->get_field_value();
262 $compare_values = $this->get_compare_as_array();
263
264 if ( ! is_array( $compare_values ) ) {
265 return false;
266 }
267
268 // then value is from checkbox field
269 if ( is_array( $field ) ) {
270 return $this->check_one_of_list();
271 }
272
273 // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
274 return in_array( $field, $compare_values );
275 }
276
277 public function check_one_of_list(): bool {
278 $field = $this->get_field_value();
279 $compare_values = $this->get_compare_as_array();
280
281 foreach ( $field as $current ) {
282 if ( in_array( $current, $compare_values, true ) ) {
283 return true;
284 }
285 }
286
287 return false;
288 }
289
290 /**
291 * @param $success
292 * @param mixed ...$additional
293 *
294 * @throws Condition_Silence_Exception
295 */
296 public function end_condition( $success, ...$additional ) {
297 $this->get_manager()->throw_by_method( $success, ...$additional );
298 }
299
300 /**
301 * @param mixed ...$additional
302 *
303 * @throws Condition_Silence_Exception
304 */
305 public function error( ...$additional ) {
306 $this->end_condition( false, ...$additional );
307 }
308
309
310 }
311