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