PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 2.0.5
JetFormBuilder — Dynamic Blocks Form Builder v2.0.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
executors 4 years ago methods 4 years ago 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
action-handler.php
401 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\Executors\Action_Default_Executor;
7 use Jet_Form_Builder\Actions\Types\Base;
8 use Jet_Form_Builder\Exceptions\Condition_Exception;
9 use Jet_Form_Builder\Exceptions\Repository_Exception;
10 use Jet_Form_Builder\Plugin;
11
12 if ( ! defined( 'WPINC' ) ) {
13 die;
14 }
15
16 /**
17 * Define Actions handler class class
18 */
19 class Action_Handler {
20
21 public $form_id = null;
22 public $request_data = array();
23 public $form_actions = array();
24 private $form_conditions = array();
25 public $is_ajax = false;
26
27 /**
28 * Data for actions
29 */
30 public $size_all;
31 public $response_data = array();
32
33 public $context = array();
34
35 /** @var bool|int Current Action ID */
36 public $current_position = false;
37
38 /** @var string */
39 public $current_flow_handler = '';
40
41 /**
42 * @var array Action IDs that were executed without errors
43 */
44 protected $passed = array();
45
46 /**
47 * @var array Action IDs that were skipped due to a condition not matching
48 */
49 protected $skipped = array();
50
51 public function get_form_id() {
52 return (int) $this->form_id;
53 }
54
55 /**
56 * @param $form_id
57 *
58 * @return Action_Handler
59 */
60 public function set_form_id( $form_id ) {
61 if ( ! $form_id || $form_id === $this->form_id ) {
62 return $this;
63 }
64 $this->form_id = $form_id;
65 $this->set_form_actions();
66
67 return $this;
68 }
69
70 public function add_request( $request ): Action_Handler {
71 $this->request_data = array_merge( $this->request_data, $request );
72
73 return $this;
74 }
75
76 /**
77 * Set form actions,
78 * which were saved in form meta
79 *
80 * @return Action_Handler
81 */
82 public function set_form_actions() {
83 $form_actions = Plugin::instance()->post_type->get_actions( $this->form_id );
84
85 foreach ( $form_actions as $form_action ) {
86 try {
87 list( $action ) = $this->save_form_action( $form_action );
88 $action->on_register_in_flow();
89 } catch ( Repository_Exception $exception ) {
90 continue;
91 }
92 }
93
94 return $this;
95 }
96
97 /**
98 * @param $form_action
99 *
100 * @return Base[]|Condition_Manager[]
101 * @throws Repository_Exception
102 */
103 public function save_form_action( $form_action ): array {
104 $type = $form_action['type'];
105
106 /** @var Base $action */
107 $action = jet_form_builder()->actions->get_action_clone( $type );
108
109 $id = $form_action['id'];
110 $settings = $form_action['settings'][ $type ] ?? $form_action['settings'];
111 $conditions = $form_action['conditions'] ?? array();
112 $operator = $form_action['condition_operator'] ?? 'and';
113
114 /**
115 * Save action settings to the class field,
116 * it allows to not send action settings
117 * in action hook
118 */
119 $action->_id = $id;
120 $action->settings = $settings;
121
122 $condition = new Condition_Manager();
123 $condition->set_conditions( $conditions );
124 $condition->set_condition_operator( $operator );
125
126 $this->form_conditions[ $id ] = $condition;
127 $this->form_actions[ $id ] = $action;
128
129 return array( $action, $condition );
130 }
131
132 /**
133 * Send form notifications
134 *
135 * @return array [description]
136 */
137 public function do_actions() {
138 do_action( 'jet-form-builder/actions/before-send' );
139
140 $run_actions_callback = apply_filters(
141 'jet-form-builder/actions/run-callback',
142 array( new Action_Default_Executor(), 'run_actions' )
143 );
144
145 call_user_func( $run_actions_callback );
146
147 do_action( 'jet-form-builder/actions/after-send' );
148
149 return $this->response_data;
150 }
151
152 public function process_single_action( $index ) {
153 /**
154 * Start the cycle
155 *
156 * @var int current_position
157 */
158 $this->set_current_action( $index );
159
160 try {
161 /**
162 * Check conditions for action
163 */
164 $this->get_current_condition_manager()->check_all();
165 } catch ( Condition_Exception $exception ) {
166 /**
167 * We save the ID of the current action,
168 * for possible logging of form entries
169 */
170 $this->skipping_current();
171
172 return;
173 }
174
175 /**
176 * Process single action
177 */
178 $this->get_current_action()->do_action( $this->request_data, $this );
179
180 /**
181 * We save the ID of the current action,
182 * for possible logging of form entries
183 */
184 $this->passing_current();
185 }
186
187 /**
188 * Unregister notification by id
189 *
190 * @param $key
191 *
192 * @return Action_Handler [description]
193 */
194 public function unregister_action( $key ) {
195 if ( is_numeric( $key ) && isset( $this->form_actions[ $key ] ) ) {
196 unset( $this->form_actions[ $key ] );
197
198 return $this;
199 }
200 foreach ( $this->form_actions as $index => $action ) {
201 if ( $key === $action->get_id() ) {
202 unset( $this->form_actions[ $index ] );
203 }
204 }
205
206 return $this;
207 }
208
209 /**
210 * Returns all registered notifications
211 *
212 * @return Base[] [description]
213 */
214 public function get_all() {
215 return $this->form_actions;
216 }
217
218 public function get_inserted_post_id( $action_id = 0 ) {
219 $default_post_id = absint( $this->response_data['inserted_post_id'] ?? 0 );
220
221 if ( ! $action_id ) {
222 return $default_post_id;
223 }
224
225 $action_id = absint( $action_id );
226
227 if ( empty( $this->response_data['inserted_posts'] ) ) {
228 return $default_post_id;
229 }
230
231 foreach ( $this->response_data['inserted_posts'] as $posts ) {
232 if ( $action_id === $posts['action_id'] ) {
233 return $posts['post_id'];
234 }
235 }
236
237 return $default_post_id;
238 }
239
240 public function add_response( $values ) {
241 Plugin::instance()->form_handler->add_response_data( $values );
242 }
243
244 public function get_context( $action_slug, $property = '' ) {
245 $context = $this->context[ $action_slug ] ?? array();
246
247 return $property ? $context[ $property ] ?? false : $context;
248 }
249
250 public function add_context( $action_slug, $context ) {
251 $this->context[ $action_slug ] = array_merge( $this->get_context( $action_slug ), $context );
252
253 return $this;
254 }
255
256 public function add_context_once( string $action_slug, array $context ) {
257 $action_context = $this->get_context( $action_slug );
258
259 if ( ! $action_context ) {
260 $this->add_context( $action_slug, $context );
261
262 return $this;
263 }
264
265 foreach ( $context as $name => $value ) {
266 if ( isset( $action_context[ $name ] ) ) {
267 unset( $context[ $name ] );
268 }
269 }
270 $this->add_context( $action_slug, $context );
271
272 return $this;
273 }
274
275 public function in_loop(): bool {
276 return false !== $this->current_position;
277 }
278
279 public function in_loop_or_die() {
280 if ( $this->in_loop() ) {
281 return;
282 }
283
284 _doing_it_wrong(
285 __METHOD__,
286 esc_html( 'The action loop has not been started, see ' . self::class . '::run_actions()' ),
287 '1.4.0'
288 );
289 }
290
291 public function get_current_action(): Base {
292 $this->in_loop_or_die();
293
294 return $this->get_action_by_id( $this->get_position() );
295 }
296
297 public function get_current_condition_manager(): Condition_Manager {
298 $this->in_loop_or_die();
299
300 return $this->get_condition_by_id( $this->get_position() );
301 }
302
303 /**
304 * @param $id
305 *
306 * @return false|Base
307 */
308 public function get_action_by_id( $id ) {
309 return $this->form_actions[ $id ] ?? false;
310 }
311
312 /**
313 * @param $id
314 *
315 * @return false|Condition_Manager
316 */
317 public function get_condition_by_id( $id ) {
318 return $this->form_conditions[ $id ] ?? false;
319 }
320
321 /**
322 * @param $slug
323 *
324 * @return false|Base
325 */
326 public function get_action_by_slug( $slug ) {
327 foreach ( $this->form_actions as $action ) {
328 /** @var Base $action */
329
330 if ( $action->get_id() !== $slug ) {
331 continue;
332 }
333
334 return $action;
335 }
336
337 return false;
338 }
339
340 public function get_refer() {
341 return $this->request_data['__refer'] ?? '';
342 }
343
344 public function get_passed_actions(): array {
345 return $this->passed;
346 }
347
348 public function get_skipped_actions(): array {
349 return $this->skipped;
350 }
351
352 public function passing_current() {
353 $this->passing_action( $this->get_position() );
354 }
355
356 public function passing_action( int $action_id ) {
357 $this->passed[] = $action_id;
358
359 return $this;
360 }
361
362 public function skipping_current() {
363 $this->skipping_action( $this->get_position() );
364 }
365
366 public function skipping_action( int $action_id ) {
367 $this->skipped[] = $action_id;
368
369 return $this;
370 }
371
372 /**
373 * @param int|bool $action_id
374 *
375 * @return $this
376 */
377 public function set_current_action( $action_id ) {
378 $this->current_position = (int) $action_id;
379
380 return $this;
381 }
382
383 public function get_position(): int {
384 return $this->current_position;
385 }
386
387 public function start_flow( string $flow_handler ) {
388 $this->current_flow_handler = $flow_handler;
389
390 return $this;
391 }
392
393 public function end_flow() {
394 $this->current_flow_handler = '';
395
396 return $this;
397 }
398
399
400 }
401