PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.0.8
JetFormBuilder — Dynamic Blocks Form Builder v3.0.8
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 3 years ago condition-manager.php 3 years ago
condition-instance.php
323 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 class Condition_Instance {
11
12 private $type;
13 private $events;
14
15 private $operator;
16 private $must_be_true = true;
17 private $field_name;
18 private $compare;
19 private $compare_value_format;
20
21 private function get_manager(): Condition_Manager {
22 return jet_fb_action_handler()->get_current_condition_manager();
23 }
24
25 /**
26 * @param $condition
27 *
28 * @return $this
29 */
30 public function set_condition( $condition ): Condition_Instance {
31 $this->set_type( $condition['type'] ?? '' );
32
33 if ( ! $this->is_field_compare() ) {
34 $this->set_events( $condition['events'] ?? array() );
35
36 return $this;
37 }
38
39 $this->set_operator( $condition['operator'] ?? false );
40 $this->set_field( $condition['field'] ?? '' );
41 $this->set_compare_value_format( $condition['compare_value_format'] ?? '' );
42 $this->set_compare( $this->get_parsed_value( $condition['default'] ?? '' ) );
43 $this->set_must_be( $condition['execute'] ?? '' );
44
45 return $this;
46 }
47
48 /**
49 * @return bool
50 */
51 public function is_correct(): bool {
52 $this->sanitize_operator();
53
54 $result = $this->check();
55
56 return $this->get_must_be() ? $result : ! $result;
57 }
58
59 /**
60 * @throws Condition_Silence_Exception
61 */
62 public function is_correct_with_throw() {
63 $this->end_condition(
64 $this->is_correct(),
65 'The condition was not met.',
66 get_object_vars( $this )
67 );
68 }
69
70 public function get_compare_value_transformer() {
71 return $this->compare_value_format;
72 }
73
74 public function set_compare_value_format( $format ): Condition_Instance {
75 $this->compare_value_format = (string) $format;
76
77 return $this;
78 }
79
80
81 public function set_operator( string $operator ): Condition_Instance {
82 $this->operator = $operator;
83
84 return $this;
85 }
86
87 public function sanitize_operator(): Condition_Instance {
88 $operators = $this->get_manager()->get_operators_list();
89
90 if ( ! $this->operator || ! in_array( $this->operator, $operators, true ) ) {
91 $this->operator = '';
92 }
93
94 return $this;
95 }
96
97 public function get_operator() {
98 return $this->operator;
99 }
100
101 /**
102 * @return mixed
103 */
104 public function get_events() {
105 return $this->events;
106 }
107
108 /**
109 * @param mixed $events
110 */
111 public function set_events( array $events ) {
112 $this->events = $events;
113 }
114
115 /**
116 * @param $field_name
117 */
118 public function set_field( $field_name ) {
119 $this->field_name = $field_name;
120 }
121
122 public function get_field_name() {
123 return $this->field_name;
124 }
125
126 public function get_field_value() {
127 return jet_fb_action_handler()->request_data[ $this->get_field_name() ] ?? false;
128 }
129
130 /**
131 * @return mixed
132 */
133 public function get_type() {
134 return $this->type;
135 }
136
137 /**
138 * @param mixed $type
139 */
140 public function set_type( string $type ) {
141 $this->type = empty( $type ) ? 'field' : $type;
142 }
143
144 public function is_field_compare(): bool {
145 return 'field' === $this->get_type();
146 }
147
148 public function set_compare( $compare ): Condition_Instance {
149 return $this->set_compare_raw( $compare )->transform_compare_value();
150 }
151
152 public function set_compare_raw( $raw_compare ): Condition_Instance {
153 $this->compare = $raw_compare;
154
155 return $this;
156 }
157
158 public function transform_compare_value(): Condition_Instance {
159 $transformer = $this->get_compare_value_transformer();
160
161 if ( ! $transformer ) {
162 return $this;
163 }
164
165 $transformer = $this->get_manager()->get_transformers_find(
166 'value',
167 $transformer
168 );
169
170 $callback = $transformer['callback'] ?? false;
171
172 if ( ! is_callable( $callback ) ) {
173 return $this;
174 }
175
176 $this->set_compare_value_with_callback( $callback );
177
178 return $this;
179 }
180
181 public function set_compare_value_with_callback( $transformer_callback ) {
182 $operators_need_explode = $this->get_manager()->get_operators_filter( 'need_explode', true );
183
184 if ( ! in_array( $this->get_operator(), $operators_need_explode, true ) ) {
185 $this->set_compare_raw( call_user_func( $transformer_callback, $this->get_compare() ) );
186
187 return;
188 }
189 $this->set_compare_raw( array_map( $transformer_callback, $this->get_compare_as_array() ) );
190 }
191
192 public function get_compare() {
193 return $this->compare;
194 }
195
196 public function set_must_be( $must_be ): Condition_Instance {
197 $this->must_be_true = (bool) $must_be;
198
199 return $this;
200 }
201
202 public function get_must_be(): bool {
203 return $this->must_be_true;
204 }
205
206 public function get_parsed_value( $maybe_json_string ) {
207 return ( new Dynamic_Preset() )->parse_json( $maybe_json_string );
208 }
209
210
211 /**
212 * @param $field_name
213 *
214 * @return mixed
215 */
216 public function get_request_field( $field_name ) {
217 $handler = jet_fb_action_handler();
218
219 if ( isset( $handler->request_data[ $field_name ] ) ) {
220 return $handler->request_data[ $field_name ];
221 }
222
223 return false;
224 }
225
226
227 public function get_compare_as_array(): array {
228 $compare = $this->get_compare();
229
230 if ( is_array( $compare ) ) {
231 return $compare;
232 }
233
234 $this->compare = array_map( 'trim', explode( ',', $compare ) );
235
236 return $this->compare;
237 }
238
239 /**
240 * @return bool
241 */
242 public function check(): bool {
243 switch ( $this->get_operator() ) {
244 case 'equal':
245 return ( (string) $this->get_field_value() ) === ( (string) $this->get_compare() );
246 case 'greater':
247 return $this->get_field_value() > $this->get_compare();
248 case 'less':
249 return $this->get_field_value() < $this->get_compare();
250
251 case 'between':
252 $field = $this->get_field_value();
253 $compare_values = $this->get_compare_as_array();
254
255 if ( count( $compare_values ) !== 2 ) {
256 return false;
257 }
258
259 return ( $compare_values[0] < $field && $compare_values[1] > $field );
260 case 'one_of':
261 return $this->check_one_of();
262 case 'contain':
263 $field = Tools::to_string( $this->get_field_value() );
264 $compare = Tools::to_string( $this->get_compare() );
265
266 return ( strpos( $field, $compare ) !== false );
267 default:
268 return apply_filters( 'jet-form-builder/actions/process-condition', false, $this );
269 }
270 }
271
272 protected function check_one_of(): bool {
273 $field = $this->get_field_value();
274 $compare_values = $this->get_compare_as_array();
275
276 if ( ! is_array( $compare_values ) ) {
277 return false;
278 }
279
280 // then value is from checkbox field
281 if ( is_array( $field ) ) {
282 return $this->check_one_of_list();
283 }
284
285 // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
286 return in_array( $field, $compare_values );
287 }
288
289 public function check_one_of_list(): bool {
290 $field = $this->get_field_value();
291 $compare_values = $this->get_compare_as_array();
292
293 foreach ( $field as $current ) {
294 if ( in_array( $current, $compare_values, true ) ) {
295 return true;
296 }
297 }
298
299 return false;
300 }
301
302 /**
303 * @param $success
304 * @param mixed ...$additional
305 *
306 * @throws Condition_Silence_Exception
307 */
308 public function end_condition( $success, ...$additional ) {
309 $this->get_manager()->throw_by_method( $success, ...$additional );
310 }
311
312 /**
313 * @param mixed ...$additional
314 *
315 * @throws Condition_Silence_Exception
316 */
317 public function error( ...$additional ) {
318 $this->end_condition( false, ...$additional );
319 }
320
321
322 }
323