PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.1.5
JetFormBuilder — Dynamic Blocks Form Builder v2.1.5
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 / action-handler.php
jetformbuilder / includes / actions Last commit date
conditions 3 years ago events 3 years ago methods 3 years ago types 3 years ago action-handler.php 3 years ago action-localize.php 3 years ago events-list.php 3 years ago events-manager.php 3 years ago manager.php 3 years ago
action-handler.php
474 lines
1 <?php
2
3 namespace Jet_Form_Builder\Actions;
4
5 // If this file is called directly, abort.
6 use Jet_Form_Builder\Actions\Events\Base_Executor;
7 use Jet_Form_Builder\Actions\Executors\Action_Executor_Base;
8 use Jet_Form_Builder\Actions\Types\Base;
9 use Jet_Form_Builder\Db_Queries\Exceptions\Skip_Exception;
10 use Jet_Form_Builder\Exceptions\Action_Exception;
11 use Jet_Form_Builder\Exceptions\Condition_Exception;
12 use Jet_Form_Builder\Exceptions\Repository_Exception;
13 use Jet_Form_Builder\Exceptions\Silence_Exception;
14 use Jet_Form_Builder\Plugin;
15 use Jet_Form_Builder\Actions\Conditions\Condition_Manager;
16
17 if ( ! defined( 'WPINC' ) ) {
18 die;
19 }
20
21 /**
22 * Define Actions handler class class
23 */
24 class Action_Handler {
25
26 public $form_id = null;
27 public $request_data = array();
28 public $form_actions = array();
29 public $is_ajax = false;
30 private $form_conditions = array();
31 private $form_events = array();
32
33 /**
34 * Data for actions
35 */
36 public $size_all;
37 public $response_data = array();
38
39 public $context = array();
40
41 /** @var bool|int Current Action ID */
42 public $current_position = false;
43
44 /** @var string */
45 public $current_flow_handler = '';
46
47 /**
48 * @var array Action IDs that were executed without errors
49 */
50 protected $passed = array();
51
52 /**
53 * @var array Action IDs that were skipped due to a condition not matching
54 */
55 protected $skipped = array();
56
57 public function get_form_id() {
58 return (int) $this->form_id;
59 }
60
61 /**
62 * @param $form_id
63 *
64 * @return Action_Handler
65 */
66 public function set_form_id( $form_id ) {
67 if ( ! $form_id || $form_id === $this->form_id ) {
68 return $this;
69 }
70 $this->form_id = $form_id;
71 $this->set_form_actions();
72
73 return $this;
74 }
75
76 public function add_request( $request ): Action_Handler {
77 $this->request_data = array_merge( $this->request_data, $request );
78
79 return $this;
80 }
81
82 /**
83 * Set form actions,
84 * which were saved in form meta
85 *
86 * @return Action_Handler
87 */
88 public function set_form_actions() {
89 $form_actions = Plugin::instance()->post_type->get_actions( $this->form_id );
90
91 foreach ( $form_actions as $form_action ) {
92 try {
93 list( $action ) = $this->save_form_action( $form_action );
94 $action->on_register_in_flow();
95 } catch ( Repository_Exception $exception ) {
96 continue;
97 }
98 }
99
100 return $this;
101 }
102
103 /**
104 * @param $form_action
105 *
106 * @return Base[]|Condition_Manager[]
107 * @throws Repository_Exception
108 */
109 public function save_form_action( $form_action ): array {
110 $type = $form_action['type'];
111
112 if ( ! ( $form_action['is_execute'] ?? true ) ) {
113 throw new Repository_Exception( 'This action is turned off' );
114 }
115
116 /** @var Base $action */
117 $action = jet_form_builder()->actions->get_action_clone( $type );
118
119 $id = $form_action['id'];
120 $settings = $form_action['settings'][ $type ] ?? $form_action['settings'];
121 $conditions = $form_action['conditions'] ?? array();
122 $operator = $form_action['condition_operator'] ?? 'and';
123 $events = $form_action['events'] ?? array();
124
125 /**
126 * Save action settings to the class field,
127 * it allows to not send action settings
128 * in action hook
129 */
130 $action->_id = $id;
131 $action->settings = $settings;
132
133 $condition = new Condition_Manager();
134 $condition->set_conditions( $conditions );
135 $condition->set_condition_operator( $operator );
136
137 $this->form_conditions[ $id ] = $condition;
138 $this->form_actions[ $id ] = $action;
139 $this->form_events[ $id ] = Events_List::create(
140 array_merge( $events, $action->get_required_events() )
141 );
142
143 return array( $action, $condition );
144 }
145
146 /**
147 * Doesn't throw an exception if there are no actions
148 *
149 * Don't call manually.
150 * Use jet_fb_events()->execute() instead.
151 *
152 * @param Base_Executor $executor
153 *
154 * @return $this
155 * @throws Action_Exception
156 */
157 public function soft_run_actions( Base_Executor $executor ): Action_Handler {
158 if ( ! count( $executor ) ) {
159 return $this;
160 }
161 $this->run_actions( $executor );
162
163 return $this;
164 }
165
166 /**
167 * Don't call manually.
168 * Use jet_fb_events()->execute() instead.
169 *
170 * @param Base_Executor $executor
171 *
172 * @throws Action_Exception
173 */
174 public function run_actions( Base_Executor $executor ) {
175 if ( ! count( $executor ) ) {
176 throw new Action_Exception( 'failed', 'Empty actions' );
177 }
178
179 foreach ( $executor as $action ) {
180 $this->process_single_action( $action );
181 }
182
183 /**
184 * End the cycle
185 */
186 $this->set_current_action( false );
187 }
188
189 private function process_single_action( Base $action ) {
190 /**
191 * Start the cycle
192 *
193 * @var int current_position
194 */
195 $this->set_current_action( $action->_id );
196
197 try {
198 /**
199 * Check conditions for action
200 */
201 $this->get_current_condition_manager()->check_all();
202 } catch ( Condition_Exception $exception ) {
203 /**
204 * We save the ID of the current action,
205 * for possible logging of form entries
206 */
207 $this->skipping_current();
208
209 return;
210 }
211
212 /**
213 * Process single action
214 */
215 $action->do_action( $this->request_data, $this );
216
217 /**
218 * We save the ID of the current action,
219 * for possible logging of form entries
220 */
221 $this->passing_current();
222 }
223
224 /**
225 * Unregister notification by id
226 *
227 * @param $key
228 *
229 * @return Action_Handler [description]
230 */
231 public function unregister_action( $key ) {
232 if ( is_numeric( $key ) && isset( $this->form_actions[ $key ] ) ) {
233 unset( $this->form_actions[ $key ] );
234 unset( $this->form_conditions[ $key ] );
235 unset( $this->form_events[ $key ] );
236
237 return $this;
238 }
239 foreach ( $this->form_actions as $index => $action ) {
240 if ( $key === $action->get_id() ) {
241 unset( $this->form_actions[ $index ] );
242 unset( $this->form_conditions[ $index ] );
243 unset( $this->form_events[ $index ] );
244
245 return $this;
246 }
247 }
248
249 return $this;
250 }
251
252 /**
253 * Returns all registered notifications
254 *
255 * @return Base[] [description]
256 */
257 public function get_all() {
258 return $this->form_actions;
259 }
260
261 public function get_inserted_post_id( $action_id = 0 ) {
262 $default_post_id = absint( $this->response_data['inserted_post_id'] ?? 0 );
263
264 if ( ! $action_id ) {
265 return $default_post_id;
266 }
267
268 $action_id = absint( $action_id );
269
270 if ( empty( $this->response_data['inserted_posts'] ) ) {
271 return $default_post_id;
272 }
273
274 foreach ( $this->response_data['inserted_posts'] as $posts ) {
275 if ( $action_id === $posts['action_id'] ) {
276 return $posts['post_id'];
277 }
278 }
279
280 return $default_post_id;
281 }
282
283 public function add_response( $values ) {
284 Plugin::instance()->form_handler->add_response_data( $values );
285 }
286
287 public function get_context( $action_slug, $property = '' ) {
288 $context = $this->context[ $action_slug ] ?? array();
289
290 return $property ? $context[ $property ] ?? false : $context;
291 }
292
293 public function add_context( $action_slug, $context ) {
294 $this->context[ $action_slug ] = array_merge( $this->get_context( $action_slug ), $context );
295
296 return $this;
297 }
298
299 public function add_context_once( string $action_slug, array $context ) {
300 $action_context = $this->get_context( $action_slug );
301
302 if ( ! $action_context ) {
303 $this->add_context( $action_slug, $context );
304
305 return $this;
306 }
307
308 foreach ( $context as $name => $value ) {
309 if ( isset( $action_context[ $name ] ) ) {
310 unset( $context[ $name ] );
311 }
312 }
313 $this->add_context( $action_slug, $context );
314
315 return $this;
316 }
317
318 public function in_loop(): bool {
319 return false !== $this->current_position;
320 }
321
322 public function in_loop_or_die() {
323 if ( $this->in_loop() ) {
324 return;
325 }
326
327 wp_die(
328 esc_html( 'The action loop has not been started, see ' . self::class . '::run_actions()' ),
329 __METHOD__,
330 '1.4.0'
331 );
332 }
333
334 public function get_current_action(): Base {
335 $this->in_loop_or_die();
336
337 return $this->get_action_by_id( $this->get_position() );
338 }
339
340 public function get_current_condition_manager(): Condition_Manager {
341 $this->in_loop_or_die();
342
343 return $this->get_condition_by_id( $this->get_position() );
344 }
345
346 /**
347 * @param $id
348 *
349 * @return false|Base
350 */
351 public function get_action_by_id( $id ) {
352 return $this->form_actions[ $id ] ?? false;
353 }
354
355 /**
356 * @param $id
357 *
358 * @return false|Condition_Manager
359 */
360 public function get_condition_by_id( $id ) {
361 return $this->form_conditions[ $id ] ?? false;
362 }
363
364 /**
365 * @param $id
366 *
367 * @return false|Events_List
368 */
369 public function get_events_by_id( $id ) {
370 return $this->form_events[ $id ] ?? false;
371 }
372
373 /**
374 * For fix backward compatibility
375 *
376 * @param array $form_events
377 *
378 * @return array
379 */
380 public function merge_events( array $form_events ): array {
381 foreach ( $form_events as $action_id => $event_list ) {
382 $this->form_events[ $action_id ] = $event_list;
383 }
384
385 return $this->form_events;
386 }
387
388 /**
389 * @param $slug
390 *
391 * @return false|Base
392 */
393 public function get_action_by_slug( $slug ) {
394 foreach ( $this->form_actions as $action ) {
395 /** @var Base $action */
396
397 if ( $action->get_id() !== $slug ) {
398 continue;
399 }
400
401 return $action;
402 }
403
404 return false;
405 }
406
407 /**
408 * Use jet_fb_handler()->refer
409 * @deprecated 2.1.3
410 *
411 * @return mixed|string
412 */
413 public function get_refer() {
414 return $this->request_data['__refer'] ?? '';
415 }
416
417 public function get_passed_actions(): array {
418 return $this->passed;
419 }
420
421 public function get_skipped_actions(): array {
422 return $this->skipped;
423 }
424
425 public function passing_current() {
426 $this->passing_action( $this->get_position() );
427 }
428
429 public function passing_action( int $action_id ) {
430 $this->passed[] = $action_id;
431
432 return $this;
433 }
434
435 public function skipping_current() {
436 $this->skipping_action( $this->get_position() );
437 }
438
439 public function skipping_action( int $action_id ) {
440 $this->skipped[] = $action_id;
441
442 return $this;
443 }
444
445 /**
446 * @param int|bool $action_id
447 *
448 * @return $this
449 */
450 public function set_current_action( $action_id ) {
451 $this->current_position = (int) $action_id;
452
453 return $this;
454 }
455
456 public function get_position(): int {
457 return $this->current_position;
458 }
459
460 public function start_flow( string $flow_handler ) {
461 $this->current_flow_handler = $flow_handler;
462
463 return $this;
464 }
465
466 public function end_flow() {
467 $this->current_flow_handler = '';
468
469 return $this;
470 }
471
472
473 }
474