PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 1.1.0
JetFormBuilder — Dynamic Blocks Form Builder v1.1.0
3.6.6.1 3.6.6 3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 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 All 134 releases
jetformbuilder / includes / actions / action-handler.php

action-handler.php in JetFormBuilder — Dynamic Blocks Form Builder 1.1.0, at includes/actions/action-handler.php

216 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Jet_Form_Builder\Actions;
4
5 // If this file is called directly, abort.
6 use Jet_Form_Builder\Classes\Tools;
7 use Jet_Form_Builder\Exceptions\Action_Exception;
8 use Jet_Form_Builder\Exceptions\Condition_Exception;
9 use Jet_Form_Builder\Gateways\Gateway_Manager;
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 = null;
23 public $manager = null;
24
25
26 public $form_actions = array();
27 public $is_ajax = false;
28
29 /**
30 * Data for actions
31 */
32 public $size_all;
33 public $current_position;
34 public $response_data = array();
35
36
37 /**
38 * Constructor for the class
39 *
40 * @param $form_id
41 * @param array $request_data
42 */
43 public function __construct( $form_id, $request_data = array() ) {
44 $this->form_id = $form_id;
45 $this->set_form_actions();
46 }
47
48 public function add_request( $request ) {
49 $this->request_data = $request;
50
51 return $this;
52 }
53
54 /**
55 * Set form actions,
56 * which were saved in form meta
57 *
58 * @return $this
59 */
60 public function set_form_actions() {
61 $available_actions = Plugin::instance()->actions->get_actions();
62 $form_actions = Plugin::instance()->post_type->get_actions( $this->form_id );
63
64 foreach ( $form_actions as $form_action ) {
65 $id = $form_action['id'];
66 $type = $form_action['type'];
67 $conditions = isset( $form_action['conditions'] ) ? $form_action['conditions'] : array();
68
69 if ( isset( $available_actions[ $type ] ) ) {
70 $action = clone $available_actions[ $type ];
71 /**
72 * Save action settings to the class field,
73 * it allows to not send action settings
74 * in action hook
75 */
76 $action->_id = $id;
77 $action->settings = $form_action['settings'];
78 $action->conditions = $conditions;
79
80 $this->form_actions[ $id ] = $action;
81 }
82 }
83
84 return $this;
85 }
86
87
88 /**
89 * Unregister notification by id
90 *
91 * @param $id [description]
92 *
93 * @return void [description]
94 */
95 public function unregister_action( $key ) {
96 if ( is_numeric( $key ) && isset( $this->form_actions[ $key ] ) ) {
97 unset( $this->form_actions[ $key ] );
98
99 return;
100 }
101 foreach ( $this->form_actions as $index => $action ) {
102 if ( $key === $action->get_id() ) {
103 unset( $this->form_actions[ $index ] );
104 }
105 }
106 }
107
108 /**
109 * Returns all registered notifications
110 *
111 * @return array [description]
112 */
113 public function get_all() {
114 return $this->form_actions;
115 }
116
117 /**
118 * Send form notifications
119 *
120 * @return array [type] [description]
121 * @throws Action_Exception
122 */
123 public function do_actions() {
124
125 $this->before_run_actions();
126 $this->run_actions();
127 $this->after_run_actions();
128
129 return $this->response_data;
130 }
131
132 public function before_run_actions() {
133 $callbacks = array();
134
135 if ( Plugin::instance()->allow_gateways ) {
136 $callbacks[] = array(
137 Gateway_Manager::instance(),
138 Gateway_Manager::BEFORE_ACTIONS_CALLABLE
139 );
140 }
141
142 Tools::run_callbacks(
143 apply_filters( 'jet-form-builder/actions/before-send/callbacks', $callbacks ),
144 $this
145 );
146 }
147
148 public function after_run_actions() {
149 $callbacks = array();
150
151 if ( Plugin::instance()->allow_gateways ) {
152 $callbacks[] = array(
153 Gateway_Manager::instance(),
154 Gateway_Manager::AFTER_ACTIONS_CALLABLE
155 );
156 }
157
158 Tools::run_callbacks(
159 apply_filters( 'jet-form-builder/actions/after-send/callbacks', $callbacks ),
160 $this
161 );
162
163 }
164
165 public function run_actions() {
166 if ( empty( $this->form_actions ) ) {
167 throw new Action_Exception( 'failed' );
168 }
169
170 $this->size_all = sizeof( $this->form_actions );
171
172 foreach ( $this->form_actions as $index => $action ) {
173
174 $this->current_position = $index;
175
176 /**
177 * Check conditions for action
178 */
179 try {
180 $action->condition( $this );
181 } catch ( Condition_Exception $exception ) {
182 continue;
183 }
184 /**
185 * Process single action
186 */
187 $action->do_action( $this->request_data, $this );
188 }
189
190 }
191
192 public function get_inserted_post_id( $action_id = 0 ) {
193 if ( ! $action_id ) {
194 return absint( $this->response_data['inserted_post_id'] );
195 }
196
197 $action_id = absint( $action_id );
198
199 if ( empty( $this->response_data['inserted_posts'] ) ) {
200 return absint( $this->response_data['inserted_post_id'] );
201 }
202
203 foreach ( $this->response_data['inserted_posts'] as $posts ) {
204 if ( $action_id === $posts['action_id'] ) {
205 return $posts['post_id'];
206 }
207 }
208 }
209
210 public function add_response( $values ) {
211 Plugin::instance()->form_handler->add_response_data( $values );
212 }
213
214
215 }
216