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