PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / 1.7.5
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster v1.7.5
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / elementor / modules / optin / actions / utils / ajax-handler.php

ajax-handler.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster 1.7.5, at includes/elementor/modules/optin/actions/utils/ajax-handler.php

297 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined( 'ABSPATH' ) || die();
4
5 use Elementor\Plugin as Elementor;
6 use Sellkit\Funnel\Contacts\Base_Contacts;
7 use Sellkit_Elementor_Optin_Module as Module;
8
9 /**
10 * Initializing the ajax handler class for handling form ajax requests.
11 *
12 * @since 1.5.0
13 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
14 */
15 class Sellkit_Elementor_Optin_Ajaxhandler {
16
17 /**
18 * Holds all the responses.
19 *
20 * @access public
21 * @var array
22 */
23 public $response = [
24 'message' => [],
25 'errors' => [],
26 'admin_errors' => [],
27 ];
28
29 /**
30 * Holds the form settings.
31 *
32 * @access public
33 * @var array
34 */
35 public $form;
36
37 /**
38 * Holds a record of the user-filled form.
39 *
40 * @access public
41 * @var array
42 */
43 public $form_data;
44
45 /**
46 * Holds the reponse state.
47 *
48 * @access public
49 * @var bool
50 */
51 public $is_success = true;
52
53 /**
54 * Initializes the AJAX handler class with registering AJAX hooks.
55 *
56 * @since 1.5.0
57 */
58 public function __construct() {
59 add_action( 'wp_ajax_sellkit_optin_frontend', [ $this, 'handle_frontend' ] );
60 add_action( 'wp_ajax_nopriv_sellkit_optin_frontend', [ $this, 'handle_frontend' ] );
61 add_action( 'wp_ajax_sellkit_optin_editor', [ $this, 'handle_editor' ] );
62 }
63
64 /**
65 * Handle the form submit in frontend.
66 *
67 * @since 1.5.0
68 * @access public
69 */
70 public function handle_frontend() {
71 if ( false === check_ajax_referer( 'sellkit_elementor', 'nonce', false ) ) {
72 $this
73 ->add_response( 'admin_errors', esc_html__( 'Error: Nonce mismatch or expired. Please reload the page and retry.', 'sellkit' ) )
74 ->set_success( false )
75 ->send_response();
76
77 return;
78 }
79
80 $post_id = filter_input( INPUT_POST, 'post_id' );
81 $form_id = filter_input( INPUT_POST, 'form_id' );
82
83 $this->form_data = $_POST; // @codingStandardsIgnoreLine
84
85 $post_elements = Elementor::$instance->documents->get( $post_id )->get_elements_data();
86 $this->form = self::find_form_recursive( $post_elements, $form_id );
87
88 $this
89 ->validate_form()
90 ->fill_form_settings()
91 ->set_custom_messages()
92 ->run_actions()
93 ->send_response();
94 }
95
96 /**
97 * Handle the form requests in editor.
98 *
99 * @since 1.5.0
100 * @access public
101 */
102 public function handle_editor() {
103 $nonce_valid = check_ajax_referer( 'sellkit_optin_editor', 'nonce', false );
104 $action = filter_input( INPUT_POST, 'service' );
105 $request = filter_input( INPUT_POST, 'request' );
106 $params = filter_input( INPUT_POST, 'params', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
107
108 if ( false === $nonce_valid ) {
109 $this->set_success( false )->send_response();
110 return;
111 }
112
113 $class_name = 'Sellkit_Elementor_Optin_Action_' . ucfirst( $action );
114 $class_name::get_instance()->$request( $this, empty( $params ) ? [] : $params );
115
116 $this->send_response();
117 }
118
119 /**
120 * Loop recursively through post meta and find the form element.
121 *
122 * @param array $post_elements elements data of the post.
123 * @param string $form_id id of the form.
124 * @return array|false data of the form in question or false on failure.
125 *
126 * @since 1.5.0
127 * @access public
128 * @static
129 */
130 public static function find_form_recursive( $post_elements, $form_id ) {
131 foreach ( $post_elements as $element ) {
132 if ( $form_id === $element['id'] ) {
133 return $element;
134 }
135
136 if ( ! empty( $element['elements'] ) ) {
137 $element = self::find_form_recursive( $element['elements'], $form_id );
138
139 if ( $element ) {
140 return $element;
141 }
142 }
143 }
144
145 return false;
146 }
147
148 /**
149 * Set form state to success/error.
150 *
151 * @param boolean $bool True or false.
152 *
153 * @since 1.5.0
154 * @access public
155 */
156 public function set_success( $bool ) {
157 $this->is_success = $bool;
158 return $this;
159 }
160
161 /**
162 * Fills "settings" key of the form by creating an instance of it.
163 *
164 * @since 1.5.0
165 * @access private
166 */
167 private function fill_form_settings() {
168 $this->form['settings'] =
169 Elementor::$instance
170 ->elements_manager
171 ->create_element_instance( $this->form )
172 ->get_settings_for_display();
173
174 return $this;
175 }
176
177 /**
178 * Validate the form based on form ID.
179 *
180 * @since 1.5.0
181 * @access public
182 */
183 public function set_custom_messages() {
184 $form = $this->form;
185
186 if ( ! $form ) {
187 return $this;
188 }
189
190 if ( empty( $form['settings']['messages_custom'] ) ) {
191 return $this;
192 }
193
194 Module::$messages = [
195 'success' => $form['settings']['messages_success'],
196 'error' => $form['settings']['messages_error'],
197 'required' => $form['settings']['messages_required'],
198 ];
199
200 return $this;
201 }
202
203 /**
204 * Validate the form based on form ID.
205 *
206 * @since 1.5.0
207 * @access private
208 */
209 private function validate_form() {
210 if ( $this->form ) {
211 return $this;
212 }
213
214 $this
215 ->add_response( 'message', esc_html__( 'There\'s something wrong. The form is not valid.', 'sellkit' ) )
216 ->set_success( false )
217 ->send_response();
218
219 return $this;
220 }
221
222 /**
223 * Run all the specified actions.
224 *
225 * @since 1.5.0
226 * @access private
227 */
228 private function run_actions() {
229 $actions = $this->form['settings']['crm_actions'];
230
231 if ( ! empty( $actions ) ) {
232 $actions = is_array( $actions ) ? $actions : [ $actions ];
233
234 foreach ( $actions as $action ) {
235 $class_name = Module::$action_types[ $action ];
236 $class_name::get_instance()->run( $this );
237 }
238 }
239
240 // Always run "After Submit Actions".
241 Sellkit_Elementor_Optin_Action_Download_Redirect::get_instance()->run( $this );
242
243 return $this;
244 }
245
246 /**
247 * Add response to ajax response.
248 *
249 * @param string $type Response type.
250 * @param string $text Response text.
251 * @param string $text_key Response text key.
252 *
253 * @since 1.5.0
254 * @access public
255 */
256 public function add_response( $type, $text = '', $text_key = '' ) {
257 if ( ! empty( $text_key ) ) {
258 $this->response[ $type ][ $text_key ] = $text;
259 return $this;
260 }
261
262 $this->response[ $type ][] = $text;
263 return $this;
264 }
265
266 /**
267 * Send success/fail response.
268 *
269 * @since 1.5.0
270 * @access public
271 */
272 public function send_response() {
273 if ( ! current_user_can( 'administrator' ) ) {
274 unset( $this->response['admin_errors'] );
275 } else {
276 // Flatten admin_errors.
277 $this->response['admin_errors'] = array_values( $this->response['admin_errors'] );
278 }
279
280 if ( $this->is_success ) {
281 if ( empty( $this->response['message'] ) ) {
282 $this->add_response( 'message', Module::$messages['success'] );
283 }
284
285 Base_Contacts::step_is_passed();
286
287 wp_send_json_success( $this->response );
288 }
289
290 if ( ! empty( $this->response['errors'] ) ) {
291 $this->add_response( 'message', Module::$messages['error'] );
292 }
293
294 wp_send_json_error( $this->response );
295 }
296 }
297