PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 1.4.2
JetFormBuilder — Dynamic Blocks Form Builder v1.4.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
types 4 years ago action-handler.php 4 years ago action-localize.php 4 years ago condition-instance.php 4 years ago condition-manager.php 4 years ago manager.php 4 years ago
condition-instance.php
265 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_form_builder()->form_handler->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_value(
170 array(
171 'default' => $maybe_json_string,
172 )
173 );
174 }
175
176
177 /**
178 * @param $field_name
179 *
180 * @return mixed
181 * @throws Condition_Silence_Exception
182 */
183 public function get_request_field( $field_name ) {
184 $handler = jet_form_builder()->form_handler->action_handler;
185
186 if ( isset( $handler->request_data[ $field_name ] ) ) {
187 return $handler->request_data[ $field_name ];
188 }
189
190 $this->error( "empty_field::{$field_name}" );
191
192 return false;
193 }
194
195
196 public function get_compare_as_array(): array {
197 $compare = $this->get_compare();
198
199 if ( is_array( $compare ) ) {
200 return $compare;
201 }
202
203 return array_map( 'trim', explode( ',', $compare ) );
204 }
205
206 /**
207 * @return bool
208 */
209 public function check(): bool {
210 switch ( $this->get_operator() ) {
211 case 'equal':
212 return ( (string) $this->get_field_value() ) === ( (string) $this->get_compare() );
213 case 'greater':
214 return $this->get_field_value() > $this->get_compare();
215 case 'less':
216 return $this->get_field_value() < $this->get_compare();
217
218 case 'between':
219 $field = $this->get_field_value();
220 $compare_values = $this->get_compare_as_array();
221
222 if ( count( $compare_values ) !== 2 ) {
223 return false;
224 }
225
226 return ( $compare_values[0] < $field && $compare_values[1] > $field );
227 case 'one_of':
228 $field = $this->get_field_value();
229 $compare_values = $this->get_compare_as_array();
230
231 if ( ! is_array( $compare_values ) ) {
232 return false;
233 }
234
235 // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
236 return in_array( $field, $compare_values );
237 case 'contain':
238 return ( strpos( $this->get_field_value(), $this->get_compare() ) !== false );
239 default:
240 return apply_filters( 'jet-form-builder/actions/process-condition', false, $this );
241 }
242 }
243
244 /**
245 * @param $success
246 * @param mixed ...$additional
247 *
248 * @throws Condition_Silence_Exception
249 */
250 public function end_condition( $success, ...$additional ) {
251 $this->get_manager()->throw_by_method( $success, ...$additional );
252 }
253
254 /**
255 * @param mixed ...$additional
256 *
257 * @throws Condition_Silence_Exception
258 */
259 public function error( ...$additional ) {
260 $this->end_condition( false, ...$additional );
261 }
262
263
264 }
265