PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.1.1
JetFormBuilder — Dynamic Blocks Form Builder v2.1.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 / action-handler.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
action-handler.php
466 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
236 return $this;
237 }
238 foreach ( $this->form_actions as $index => $action ) {
239 if ( $key === $action->get_id() ) {
240 unset( $this->form_actions[ $index ] );
241 unset( $this->form_conditions[ $index ] );
242
243 return $this;
244 }
245 }
246
247 return $this;
248 }
249
250 /**
251 * Returns all registered notifications
252 *
253 * @return Base[] [description]
254 */
255 public function get_all() {
256 return $this->form_actions;
257 }
258
259 public function get_inserted_post_id( $action_id = 0 ) {
260 $default_post_id = absint( $this->response_data['inserted_post_id'] ?? 0 );
261
262 if ( ! $action_id ) {
263 return $default_post_id;
264 }
265
266 $action_id = absint( $action_id );
267
268 if ( empty( $this->response_data['inserted_posts'] ) ) {
269 return $default_post_id;
270 }
271
272 foreach ( $this->response_data['inserted_posts'] as $posts ) {
273 if ( $action_id === $posts['action_id'] ) {
274 return $posts['post_id'];
275 }
276 }
277
278 return $default_post_id;
279 }
280
281 public function add_response( $values ) {
282 Plugin::instance()->form_handler->add_response_data( $values );
283 }
284
285 public function get_context( $action_slug, $property = '' ) {
286 $context = $this->context[ $action_slug ] ?? array();
287
288 return $property ? $context[ $property ] ?? false : $context;
289 }
290
291 public function add_context( $action_slug, $context ) {
292 $this->context[ $action_slug ] = array_merge( $this->get_context( $action_slug ), $context );
293
294 return $this;
295 }
296
297 public function add_context_once( string $action_slug, array $context ) {
298 $action_context = $this->get_context( $action_slug );
299
300 if ( ! $action_context ) {
301 $this->add_context( $action_slug, $context );
302
303 return $this;
304 }
305
306 foreach ( $context as $name => $value ) {
307 if ( isset( $action_context[ $name ] ) ) {
308 unset( $context[ $name ] );
309 }
310 }
311 $this->add_context( $action_slug, $context );
312
313 return $this;
314 }
315
316 public function in_loop(): bool {
317 return false !== $this->current_position;
318 }
319
320 public function in_loop_or_die() {
321 if ( $this->in_loop() ) {
322 return;
323 }
324
325 wp_die(
326 esc_html( 'The action loop has not been started, see ' . self::class . '::run_actions()' ),
327 __METHOD__,
328 '1.4.0'
329 );
330 }
331
332 public function get_current_action(): Base {
333 $this->in_loop_or_die();
334
335 return $this->get_action_by_id( $this->get_position() );
336 }
337
338 public function get_current_condition_manager(): Condition_Manager {
339 $this->in_loop_or_die();
340
341 return $this->get_condition_by_id( $this->get_position() );
342 }
343
344 /**
345 * @param $id
346 *
347 * @return false|Base
348 */
349 public function get_action_by_id( $id ) {
350 return $this->form_actions[ $id ] ?? false;
351 }
352
353 /**
354 * @param $id
355 *
356 * @return false|Condition_Manager
357 */
358 public function get_condition_by_id( $id ) {
359 return $this->form_conditions[ $id ] ?? false;
360 }
361
362 /**
363 * @param $id
364 *
365 * @return false|Events_List
366 */
367 public function get_events_by_id( $id ) {
368 return $this->form_events[ $id ] ?? false;
369 }
370
371 /**
372 * For fix backward compatibility
373 *
374 * @param array $form_events
375 *
376 * @return $this
377 */
378 public function merge_events( array $form_events ): array {
379 foreach ( $form_events as $action_id => $event_list ) {
380 $this->form_events[ $action_id ] = $event_list;
381 }
382
383 return $this->form_events;
384 }
385
386 /**
387 * @param $slug
388 *
389 * @return false|Base
390 */
391 public function get_action_by_slug( $slug ) {
392 foreach ( $this->form_actions as $action ) {
393 /** @var Base $action */
394
395 if ( $action->get_id() !== $slug ) {
396 continue;
397 }
398
399 return $action;
400 }
401
402 return false;
403 }
404
405 public function get_refer() {
406 return $this->request_data['__refer'] ?? '';
407 }
408
409 public function get_passed_actions(): array {
410 return $this->passed;
411 }
412
413 public function get_skipped_actions(): array {
414 return $this->skipped;
415 }
416
417 public function passing_current() {
418 $this->passing_action( $this->get_position() );
419 }
420
421 public function passing_action( int $action_id ) {
422 $this->passed[] = $action_id;
423
424 return $this;
425 }
426
427 public function skipping_current() {
428 $this->skipping_action( $this->get_position() );
429 }
430
431 public function skipping_action( int $action_id ) {
432 $this->skipped[] = $action_id;
433
434 return $this;
435 }
436
437 /**
438 * @param int|bool $action_id
439 *
440 * @return $this
441 */
442 public function set_current_action( $action_id ) {
443 $this->current_position = (int) $action_id;
444
445 return $this;
446 }
447
448 public function get_position(): int {
449 return $this->current_position;
450 }
451
452 public function start_flow( string $flow_handler ) {
453 $this->current_flow_handler = $flow_handler;
454
455 return $this;
456 }
457
458 public function end_flow() {
459 $this->current_flow_handler = '';
460
461 return $this;
462 }
463
464
465 }
466