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