PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.12.8
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.12.8
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / includes / core / classes / class-wpfnl-ajax-handler.php

class-wpfnl-ajax-handler.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.12.8, at includes/core/classes/class-wpfnl-ajax-handler.php

873 lines 31.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Ajax handler
5 *
6 * @package
7 */
8
9 namespace WPFunnels\Ajax_Handler;
10
11 use Elementor\Plugin;
12 use Elementor\Utils;
13 use WPFunnels\Frontend\Recaptcha\Google_Recaptcha_Handler;
14 use WPFunnels\Optin\Optin_Record;
15 use WPFunnels\Wpfnl_functions;
16 use function Mollie\WooCommerce\errorNotice;
17 use WPFunnels\Conditions\Wpfnl_Condition_Checker;
18
19 /**
20 * Class Ajax_Handler
21 *
22 * @package WPFunnels\Widgets\Elementor
23 */
24 class Ajax_Handler {
25
26 /**
27 * Initializes AJAX actions and filters for handling different types of form submissions.
28 * Sets up AJAX hooks for various form submission scenarios, including shortcode and Gutenberg submissions.
29 * Also hooks into the 'mailmint_after_form_submit_response' filter for processing form submissions.
30 */
31 public function __construct() {
32 add_action( 'wp_ajax_wpfnl_optin_submission', [ $this, 'optin_form_submission' ] );
33 add_action( 'wp_ajax_nopriv_wpfnl_optin_submission', [ $this, 'optin_form_submission' ] );
34
35 add_action( 'wp_ajax_wpfnl_shortcode_optin_submission', [ $this, 'wpfnl_shortcode_optin_submission' ] );
36 add_action( 'wp_ajax_nopriv_wpfnl_shortcode_optin_submission', [ $this, 'wpfnl_shortcode_optin_submission' ] );
37
38 add_action( 'wp_ajax_wpfnl_gutenberg_optin_submission', [ $this, 'gutenberg_optin_form_submission' ] );
39 add_action( 'wp_ajax_nopriv_wpfnl_gutenberg_optin_submission', [ $this, 'gutenberg_optin_form_submission' ] );
40
41 }
42
43
44 /**
45 * Optin form submission handler for elementor
46 *
47 * @return void
48 * @since 2.5.7
49 */
50 public function optin_form_submission() {
51 // check_ajax_referer( 'optin_form_nonce', 'security' );
52
53 $step_id = isset( $_POST[ 'step_id' ] ) ? $_POST[ 'step_id' ] : '';
54 $postData = isset( $_POST[ 'postData' ] ) ? $_POST[ 'postData' ] : '';
55
56 $funnel_id = Wpfnl_functions::get_funnel_id_from_step( $step_id );
57 $post_data = array();
58 wp_parse_str( $postData, $post_data );
59
60 if ( isset( $post_data[ 'email' ] ) ) {
61 $post_data[ 'email' ] = strtolower( $post_data[ 'email' ] );
62 }
63
64 $url = isset( $_POST[ 'url' ] ) ? $_POST[ 'url' ] : '';
65 $parts = parse_url( $url );
66 $query = [];
67 if ( isset( $parts[ 'query' ] ) ) {
68 parse_str( $parts[ 'query' ], $query );
69 }
70
71 $elementor = Plugin::instance();
72 $document = $elementor->documents->get( $step_id );
73 $form = null;
74 if ( $document ) {
75 $form = Utils::find_element_recursive( $document->get_elements_data(), $post_data[ 'form_id' ] );
76 }
77
78 if ( empty( $form ) ) {
79 $results = array(
80 'message' => 'invalid_form',
81 'success' => false
82 );
83 echo json_encode( $results );
84 die();
85 }
86
87 $widget = $elementor->elements_manager->create_element_instance( $form );
88 $form[ 'settings' ] = $widget->get_settings_for_display();
89 unset( $post_data[ 'post_id' ] );
90 unset( $post_data[ 'form_id' ] );
91
92 $record = new Optin_Record( $post_data, $form );
93 $fields = $record->get_fields();
94 $name = '';
95 if ( $fields ) {
96 foreach( $fields as $key => $value ) {
97 if ( 'email' === $key ) {
98 $name = strstr( $value, '@', true );;
99 }
100 elseif ( 'last_name' === $key ) {
101 $name = $value;
102 }
103 elseif ( 'first_name' === $key ) {
104 $name = $value;
105 }
106 }
107 }
108
109 $response = array(
110 'success' => true,
111 'post_action' => 'notification',
112 'notification_text' => '',
113 'redirect_url' => '#'
114 );
115
116 $post_action = $form[ 'settings' ][ 'post_action' ];
117 $response[ 'post_action' ] = $post_action;
118 $action_type = '';
119 $response[ 'notification_text' ] = isset( $form[ 'settings' ][ 'notification_text' ] ) && $form[ 'settings' ][ 'notification_text' ] ? $form[ 'settings' ][ 'notification_text' ] : '';
120 $next_step = [];
121 switch( $post_action ) {
122 case 'notification':
123 $response[ 'redirect' ] = false;
124 break;
125 case 'redirect_to':
126 $action_type = 'redirect_to_url';
127 $response[ 'redirect' ] = true;
128 if ( !empty( $form[ 'settings' ][ 'redirect_url' ] ) ) {
129 $response[ 'redirect_url' ] = add_query_arg( $this->prepare_query_param( $name, $query ), $form[ 'settings' ][ 'redirect_url' ][ 'url' ] );
130 }
131 else {
132 $response[ 'redirect_url' ] = '#';
133 }
134 break;
135 default:
136 $action_type = 'next_step';
137 $next_step = Wpfnl_functions::get_next_step( $funnel_id, $step_id );
138 $response[ 'redirect_url' ] = add_query_arg( $this->prepare_query_param( $name, $query ), get_the_permalink( $next_step[ 'step_id' ] ) );
139 $response[ 'redirect' ] = true;
140 }
141
142 $admin_email = isset( $form[ 'settings' ][ 'admin_email' ] ) ? $form[ 'settings' ][ 'admin_email' ] : false;
143 $admin_email_subject = isset( $form[ 'settings' ][ 'admin_email_subject' ] ) ? $form[ 'settings' ][ 'admin_email_subject' ] : 'Opt-in form Submission';
144
145 $user_info = $record->form_data;
146
147 /**
148 * User registration
149 */
150 if ( !empty( $user_info[ 'optin_allow_registration' ] ) && $user_info[ 'optin_allow_registration' ] == 'yes' ) {
151
152 $this->create_user_optin_allow_registration( $name, $user_info );
153 }
154
155
156 /**
157 * Recaptcha Check
158 */
159 $is_recaptcha = isset( $post_data[ 'wpf-is-recapcha' ] ) ? $post_data[ 'wpf-is-recapcha' ] : 'no';
160 $recaptcha_site_key = isset( $post_data[ 'wpf-optin-g-token' ] ) ? $post_data[ 'wpf-optin-g-token' ] : '';
161 $recaptcha_site_secrect = isset( $post_data[ 'wpf-optin-g-secret-key' ] ) ? $post_data[ 'wpf-optin-g-secret-key' ] : '';
162
163 $response = $this->send_mail_after_checking_google_recaptcha_response( $is_recaptcha, $recaptcha_site_key, $recaptcha_site_secrect, $admin_email, $admin_email_subject, $user_info, $response );
164
165 if( \WPFunnels\Integrations\Helper::maybe_enabled() && !empty($form[ 'settings' ]['enable_mm_contact']) && 'yes' === $form[ 'settings' ]['enable_mm_contact'] ){
166 $lists = [];
167 $tags = [];
168 if ( !empty($form[ 'settings' ][ 'mm_lists' ]) ){
169 $data = \WPFunnels\Integrations\Helper::get_list_by_id($form[ 'settings' ][ 'mm_lists' ]);
170 if( isset($data['id'],$data['title'])){
171 $lists = [
172 [
173 'id' => $data['id'],
174 'title' => $data['title']
175 ]
176 ];
177 }
178 }
179 if ( !empty($form[ 'settings' ][ 'mm_tags' ]) ){
180 $data = \WPFunnels\Integrations\Helper::get_list_by_id($form[ 'settings' ][ 'mm_tags' ]);
181 if( isset($data['id'],$data['title'])){
182 $tags = [
183 [
184 'id' => $data['id'],
185 'title' => $data['title']
186 ]
187 ];
188 }
189 }
190
191 if ( !empty($form[ 'settings' ][ 'mm_contact_status' ]) ){
192 $user_info['status'] = $form[ 'settings' ][ 'mm_contact_status' ];
193 }
194
195 $user_info['meta_fields']['phone_number'] = isset( $user_info['phone'] ) ?$user_info['phone'] : '';
196
197 $mail_mint_object = new \WPFunnels\Integrations\MailMint( $user_info, $lists, $tags );
198 $mail_mint_object->create_or_update_contact();
199 $mail_mint_object->add_contact_to_lists();
200 $mail_mint_object->add_contact_to_tags();
201 }
202
203
204 /**
205 * Submit & process form data
206 */
207 ob_start();
208 do_action( 'wpfunnels/after_optin_submit', $step_id, $post_action, $action_type, $record, $post_data );
209 ob_get_clean();
210
211 if ( !empty( $next_step[ 'step_type' ] ) && 'conditional' == $next_step[ 'step_type' ] ) {
212 $next_step = $this->match_conditional_step( $next_step, $funnel_id, $step_id );
213 $response[ 'redirect_url' ] = add_query_arg( $this->prepare_query_param( $name, $query ), get_the_permalink( $next_step[ 'step_id' ] ) );
214 }
215
216 if ( isset( $next_step[ 'step_type' ] ) && 'thankyou' === $next_step[ 'step_type' ] ) {
217 $custom_url = Wpfnl_functions::custom_url_for_thankyou_page( $next_step[ 'step_id' ] );
218 if ( $custom_url ) {
219 $response[ 'redirect_url' ] = $custom_url;
220 }
221 }
222
223 echo json_encode( $response, true );
224 die();
225 }
226
227
228 /**
229 * Prepare query param for redirect url
230 *
231 * @param String $name
232 * @param array $query
233 *
234 * @return array
235 * @since 2.5.9
236 */
237 private function prepare_query_param( $name, $query ) {
238 $query_param = [ 'optin' => true, 'uname' => $name, ];
239
240 $required_keys = [ 'wpfnl-order', 'wpfnl-key', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content' ];
241
242 foreach( $required_keys as $key ) {
243 if ( !empty( $query[ $key ] ) ) {
244 $query_param[ $key ] = $query[ $key ];
245 }
246 }
247
248 return apply_filters('wpfunnels/update_query_param', $query_param);
249 }
250
251
252 /**
253 * Optin submission handling for shortcode
254 *
255 * @return void
256 * @since @since 2.5.7
257 */
258 public function wpfnl_shortcode_optin_submission( $response = [] ) {
259 $post = filter_input_array( INPUT_POST );
260 // if ( !wp_verify_nonce( htmlspecialchars( $post[ 'security' ] ?? '' ), 'optin_form_nonce' )
261 // && !wp_verify_nonce( htmlspecialchars( $post[ 'wp_nonce' ] ?? '' ), 'wp_rest' ) ) {
262 // die( '-1' );
263 // }
264
265 $return = true;
266 if ( empty( $response ) ) {
267 $response = [];
268 $return = false;
269 }
270
271 $response[ 'success' ] = true;
272 $response[ 'post_action' ] = 'notification';
273 $response[ 'notification_text' ] = '';
274 $response[ 'redirect_url' ] = '#';
275
276 $post_data = htmlspecialchars( $post[ 'postData' ] ?? '' );
277 $post_data = empty( $post_data ) ? htmlspecialchars( $post[ 'post_data' ] ?? '' ) : $post_data;
278 $post_data = !empty( $post_data ) ? str_replace( 'amp;', '&', $post_data ) : $post_data;
279 wp_parse_str( $post_data, $post_data );
280
281 if ( !empty( $post_data ) ) {
282 $post_data[ 'email' ] = !empty( $post_data[ 'email' ] ) && is_email( $post_data[ 'email' ] ) ? strtolower( sanitize_email( $post_data[ 'email' ] ) ) : '';
283 $parts = wp_parse_url( filter_input( INPUT_POST, 'url', FILTER_VALIDATE_URL ) );
284 $parts_query = $parts[ 'query' ] ?? '';
285 wp_parse_str( $parts_query, $query );
286 $step_id = $post_data[ 'step_id' ] ?? null;
287 if( isset($post_data[ 'post_id' ]) ){
288 $step_id = $step_id ? $step_id : $post_data[ 'post_id' ] ?? null;
289 }
290
291 $funnel_id = $step_id ? Wpfnl_functions::get_funnel_id_from_step( $step_id ) : null;
292 $post_action = $post_data[ 'post_action' ] ?? 'notification';
293 $response[ 'post_action' ] = $post_action;
294 $response[ 'notification_text' ] = $post_data[ 'notification_text' ] ?? '';
295 $user_info = $this->extract_user_info( $post_data );
296 $name = $user_info[ 'email' ] ? strstr( $user_info[ 'email' ], '@', true ) : '';
297 $query_params = $this->prepare_query_param( $name, $query );
298 $actions = $this->process_post_action( $post_action, $post_data, $response, $query_params, $step_id, $funnel_id );
299 $response = $actions[ 'response' ] ?? $response;
300 $next_step = $actions[ 'next_step' ] ?? [];
301 $action_type = $actions[ 'action_type' ] ?? '';
302 /**
303 * Filters the response after opt-in for submission.
304 *
305 * This filter hook allows developers to modify or override the response generated
306 * for an opt-in form submission.
307 *
308 * @param array $response The response generated after opt-in for submission.
309 * @param array $post_data The post data received from the opt-in form submission.
310 * @param array $user_info The user information extracted from the form submission.
311 * @return array Modified response array.
312 *
313 * @since 2.8.2
314 */
315 $response = apply_filters( 'wpfunnels/optin_submission_response', $this->process_recaptcha( $post_data, $response, $user_info ), $funnel_id, $step_id, $next_step, $query_params );
316 $record = new \stdClass();
317 $record->form_data = $user_info;
318
319 if ( isset( $next_step[ 'step_type' ] ) && 'thankyou' === $next_step[ 'step_type' ] ) {
320 $custom_url = Wpfnl_functions::custom_url_for_thankyou_page( $next_step[ 'step_id' ] );
321 if ( $custom_url ) {
322 $response[ 'redirect_url' ] = $custom_url;
323 }
324 }
325
326 if( \WPFunnels\Integrations\Helper::maybe_enabled() && !empty($post_data['enable_mm_contact']) && 'yes' === $post_data['enable_mm_contact'] ){
327 $lists = [];
328 $tags = [];
329 if ( !empty($post_data[ 'mm_lists' ]) ){
330 $data = \WPFunnels\Integrations\Helper::get_list_by_id($post_data[ 'mm_lists' ]);
331 if( isset($data['id'],$data['title'])){
332 $lists = [
333 [
334 'id' => $data['id'],
335 'title' => $data['title']
336 ]
337 ];
338 }
339 }
340 if ( !empty($post_data[ 'mm_tags' ]) ){
341 $data = \WPFunnels\Integrations\Helper::get_list_by_id($post_data[ 'mm_tags' ]);
342 if( isset($data['id'],$data['title'])){
343 $tags = [
344 [
345 'id' => $data['id'],
346 'title' => $data['title']
347 ]
348 ];
349 }
350 }
351
352 if ( !empty($post_data[ 'mm_contact_status' ]) ){
353 $user_info['status'] = $post_data[ 'mm_contact_status' ];
354 }
355
356 $user_info['meta_fields']['phone_number'] = isset( $user_info['phone'] ) ?$user_info['phone'] : '';
357
358 $mail_mint_object = new \WPFunnels\Integrations\MailMint( $user_info, $lists, $tags );
359 $mail_mint_object->create_or_update_contact();
360 $mail_mint_object->add_contact_to_lists();
361 $mail_mint_object->add_contact_to_tags();
362 }
363
364
365
366
367 /**
368 * Fires after an opt-in form submission in a funnel step.
369 *
370 * This action hook is triggered after the submission of an opt-in form in a funnel step.
371 * It provides the opportunity to perform additional actions or processes after the opt-in form is submitted.
372 *
373 * @param int $step_id The ID of the funnel step where the opt-in form was submitted.
374 * @param string $post_action The type of action taken after form submission (e.g., 'notification', 'redirect_to', 'next_step').
375 * @param string $action_type The type of action that determined the redirection or next step behavior.
376 * @param object $record An object containing data related to the form submission.
377 * @param array $post_data The post data received from the form submission.
378 *
379 * @since @since 2.5.7
380 */
381 ob_start();
382 do_action( 'wpfunnels/after_optin_submit', $step_id, $post_action, $action_type, $record, $post_data );
383 ob_get_clean();
384
385 if ( $return ) return $response;
386
387 echo json_encode( $response, true );
388 }
389 die();
390 }
391
392 /**
393 * Extracts user information from the provided post data.
394 *
395 * This function extracts specific user information fields from the given post data.
396 * The function iterates through a predefined list of user fields and extracts
397 * their values from the post data.
398 *
399 * @param array $post_data The post data containing user information.
400 *
401 * @return array The extracted user information as an associative array.
402 * @since 2.8.2
403 */
404 private function extract_user_info( $post_data ) {
405 $user_info = [];
406 $user_fields = [ 'email', 'last_name', 'first_name', 'phone', 'web-url', 'message', 'data_to_checkout' ];
407
408 // Iterate through user fields and extract their values from post data
409 foreach( $user_fields as $key ) {
410 if ( !empty( $post_data[ $key ] ) ) {
411 $user_info[ $key ] = $post_data[ $key ];
412 }
413 }
414
415 return $user_info;
416 }
417
418 /**
419 * Processes the post action based on the provided data and returns relevant response information.
420 *
421 * This function takes the post action, post data, response, query parameters, step ID, and funnel ID
422 * to determine the appropriate action and response details. It handles different post action cases,
423 * such as 'notification', 'redirect_to', and the default case. It prepares the necessary response data
424 * including whether a redirect is required, the action type, and the next step information.
425 *
426 * @param string $post_action The post action type.
427 * @param array $post_data The post data containing user inputs.
428 * @param array $response The current response data.
429 * @param array $query_params The query parameters to add to the URL.
430 * @param int $step_id The ID of the current step.
431 * @param int $funnel_id The ID of the current funnel.
432 *
433 * @return array An associative array containing the updated response, next step information, and action type.
434 * @since 2.8.2
435 */
436 private function process_post_action( $post_action, $post_data, $response, $query_params, $step_id, $funnel_id ) {
437 switch( $post_action ) {
438 case 'notification':
439 $response[ 'redirect' ] = false;
440 break;
441 case 'redirect_to':
442 $response[ 'redirect' ] = true;
443 $response[ 'confirmation_type' ] = 'to_a_custom_url';
444 $action_type = 'redirect_to_url';
445 if ( !empty( $post_data[ 'redirect_url' ] ) ) {
446 if ( 'next_step' !== $post_data[ 'redirect_url' ] ) {
447 $redirect_url = sanitize_url( add_query_arg( $query_params, $post_data[ 'redirect_url' ] ) );
448 $response[ 'redirect_url' ] = $redirect_url;
449 $response[ 'custom_url' ] = $redirect_url;
450 break;
451 }
452 $action_type = 'next_step';
453 $next_step = $funnel_id ? Wpfnl_functions::get_next_step( $funnel_id, $step_id ) : '';
454 if ( $next_step ) {
455 $redirect_url = sanitize_url( add_query_arg( $query_params, get_the_permalink( $next_step[ 'step_id' ] ) ) );
456 $response[ 'redirect_url' ] = $redirect_url;
457 $response[ 'custom_url' ] = $redirect_url;
458 break;
459 }
460 $response[ 'redirect_url' ] = '#';
461 break;
462 }
463 $response[ 'redirect_url' ] = '#';
464 break;
465 default:
466 $action_type = 'next_step';
467 $next_step = $funnel_id ? Wpfnl_functions::get_next_step( $funnel_id, $step_id ) : '';
468
469 if ( $next_step ) {
470 $redirect_url = sanitize_url( add_query_arg( $query_params, get_the_permalink( $next_step[ 'step_id' ] ) ) );
471 $response[ 'redirect_url' ] = $redirect_url;
472 $response[ 'custom_url' ] = $redirect_url;
473 }
474 else {
475 $response[ 'redirect_url' ] = '#';
476 }
477 $response[ 'redirect' ] = true;
478 $response[ 'confirmation_type' ] = 'to_a_custom_url';
479 }
480
481 return [
482 'response' => $response,
483 'next_step' => $next_step ?? [],
484 'action_type' => $action_type ?? ''
485 ];
486 }
487
488 /**
489 * Processes reCAPTCHA and sends an email after checking Google reCAPTCHA response.
490 *
491 * This function takes the post data, response, and user information, and processes reCAPTCHA
492 * by checking the Google reCAPTCHA response. It then sends an email with the provided user information.
493 *
494 * @param array $post_data The post data containing user inputs and reCAPTCHA details.
495 * @param array $response The current response data.
496 * @param array $user_info The user information to include in the email.
497 *
498 * @return array The updated response after processing reCAPTCHA and sending the email.
499 * @since 2.8.2
500 */
501 private function process_recaptcha( $post_data, $response, $user_info ) {
502 $admin_email = $post_data[ 'admin_email' ] ?? false;
503 $admin_email_subject = $post_data[ 'admin_email_subject' ] ?? 'Opt-in form Submission';
504 $is_recaptcha = $post_data[ 'wpf-is-recapcha' ] ?? 'false';
505 $recaptcha_site_key = $post_data[ 'wpf-optin-g-token' ] ?? '';
506 $recaptcha_site_secrect = $post_data[ 'wpf-optin-g-secret-key' ] ?? '';
507
508 return $this->send_mail_after_checking_google_recaptcha_response( $is_recaptcha, $recaptcha_site_key, $recaptcha_site_secrect, $admin_email, $admin_email_subject, $user_info, $response );
509 }
510
511 /**
512 * Gutenberg opt-in form handling
513 *
514 * @return void
515 * @since 2.5.9
516 */
517 public function gutenberg_optin_form_submission() {
518 check_ajax_referer( 'optin_form_nonce', 'security' );
519 $step_id = isset( $_POST[ 'step_id' ] ) ? $_POST[ 'step_id' ] : '';
520 $postData = isset( $_POST[ 'postData' ] ) ? $_POST[ 'postData' ] : '';
521 $funnel_id = Wpfnl_functions::get_funnel_id_from_step( $step_id );
522 $post_data = array();
523 parse_str( $postData, $post_data );
524
525 if ( isset( $post_data[ 'email' ] ) ) {
526 $post_data[ 'email' ] = strtolower( $post_data[ 'email' ] );
527 }
528
529 $post_id = $step_id;
530 $post = get_post( $post_id );
531 $all_blocks = parse_blocks( $post->post_content );
532
533 $url = isset( $_POST[ 'url' ] ) ? $_POST[ 'url' ] : '';
534 $parts = parse_url( $url );
535 $query = [];
536 if ( isset( $parts[ 'query' ] ) ) {
537 parse_str( $parts[ 'query' ], $query );
538 }
539
540 $admin_email = '';
541 $admin_email_subject = '';
542 $post_action = '';
543 $block_attr = array();
544
545 $record = new Optin_Record( $post_data );
546 $fields = $record->get_fields();
547 $name = '';
548 if ( $fields ) {
549 foreach( $fields as $key => $value ) {
550 if ( 'email' === $key ) {
551 $name = strstr( $value, '@', true );
552 }
553 elseif ( 'last_name' === $key ) {
554 $name = $value;
555 }
556 elseif ( 'first_name' === $key ) {
557 $name = $value;
558 }
559 }
560 }
561
562 $response = array(
563 'success' => true,
564 'post_action' => 'notification',
565 'notification_text' => '',
566 'redirect_url' => '#'
567 );
568 $custom_url = '';
569 $blocks = $this->search_items_by_key( $all_blocks, 'blockName' );
570 foreach( $blocks as $block ) {
571 if ( $block[ 'blockName' ] == 'wpfunnels/optin-form' ) {
572 $block_attr = $block[ 'attrs' ];
573
574 $admin_email = isset( $block_attr[ 'adminEmail' ] ) ? $block_attr[ 'adminEmail' ] : '';
575 $admin_email_subject = isset( $block_attr[ 'emailSubject' ] ) ? $block_attr[ 'emailSubject' ] : 'Opt-in form Submission';
576
577 $post_action = isset( $block_attr[ 'postAction' ] ) ? $block_attr[ 'postAction' ] : 'notification';
578 $custom_url = isset( $block_attr[ 'redirect_url' ] ) ? $block_attr[ 'redirect_url' ] : '#';
579 }
580 }
581
582 $response[ 'notification_text' ] = isset( $block_attr[ 'notification' ] ) ? $block_attr[ 'notification' ] : '';
583 $next_step = [];
584 switch( $post_action ) {
585 case 'notification':
586 $action_type = 'notification';
587 $response[ 'redirect' ] = false;
588 break;
589 case 'redirect_to':
590 $action_type = 'redirect_to_url';
591 $response[ 'redirect' ] = true;
592 $response[ 'post_action' ] = $post_action;
593 if ( $custom_url ) {
594 $response[ 'redirect_url' ] = add_query_arg( $this->prepare_query_param( $name, $query ), $custom_url );
595 }
596 else {
597 $response[ 'redirect_url' ] = '#';
598 }
599 break;
600 case 'next_step':
601 $response[ 'post_action' ] = $post_action;
602 $action_type = 'next_step';
603 $next_step = Wpfnl_functions::get_next_step( $funnel_id, $step_id );
604
605 $response[ 'redirect_url' ] = add_query_arg( $this->prepare_query_param( $name, $query ), get_the_permalink( $next_step[ 'step_id' ] ) );
606 $response[ 'redirect' ] = true;
607 break;
608 default:
609 $action_type = 'next_step';
610 $next_step = Wpfnl_functions::get_next_step( $funnel_id, $step_id );
611 $response[ 'redirect_url' ] = add_query_arg( $this->prepare_query_param( $name, $query ), get_the_permalink( $next_step[ 'step_id' ] ) );
612 $response[ 'redirect' ] = true;
613 }
614 /**
615 * User registration
616 */
617 if ( !empty( $post_data[ 'optin_allow_registration' ] ) && $post_data[ 'optin_allow_registration' ] == 'yes' ) {
618
619 $this->create_user_optin_allow_registration( $name, $post_data );
620 }
621 /**
622 * Check google Recaptcha V3
623 */
624 $is_recaptcha = isset( $post_data[ 'wpf-is-recapcha' ] ) ? $post_data[ 'wpf-is-recapcha' ] : 'false';
625 $recaptcha_site_key = isset( $post_data[ 'wpf-optin-g-token' ] ) ? $post_data[ 'wpf-optin-g-token' ] : '';
626 $recaptcha_site_secrect = isset( $post_data[ 'wpf-optin-g-secret-key' ] ) ? $post_data[ 'wpf-optin-g-secret-key' ] : '';
627
628 $response = $this->send_mail_after_checking_google_recaptcha_response( $is_recaptcha, $recaptcha_site_key, $recaptcha_site_secrect, $admin_email, $admin_email_subject, $post_data, $response );
629
630
631 $user_data = [
632 'first_name' => $post_data['first_name'] ?? '',
633 'last_name' => $post_data['last_name'] ?? '',
634 'email' => $post_data['email'] ?? '',
635 'phone' => $post_data['phone'] ?? '',
636 'web-url' => $post_data['web-url'] ?? '',
637 ];
638
639 if( \WPFunnels\Integrations\Helper::maybe_enabled() && !empty($post_data['enable_mm_contact']) && 'yes' === $post_data['enable_mm_contact'] ){
640
641 $lists = [];
642 $tags = [];
643 if ( !empty($post_data[ 'mm_lists' ]) ){
644 $data = \WPFunnels\Integrations\Helper::get_list_by_id($post_data[ 'mm_lists' ]);
645 if( isset($data['id'],$data['title'])){
646 $lists = [
647 [
648 'id' => $data['id'],
649 'title' => $data['title']
650 ]
651 ];
652 }
653 }
654 if ( !empty($post_data[ 'mm_tags' ]) ){
655 $data = \WPFunnels\Integrations\Helper::get_list_by_id($post_data[ 'mm_tags' ]);
656 if( isset($data['id'],$data['title'])){
657 $tags = [
658 [
659 'id' => $data['id'],
660 'title' => $data['title']
661 ]
662 ];
663 }
664 }
665
666 if ( !empty($post_data[ 'mm_contact_status' ]) ){
667 $user_data['status'] = $post_data[ 'mm_contact_status' ];
668 }
669
670 $user_data['meta_fields']['phone_number'] = isset( $user_data['phone'] ) ?$user_data['phone'] : '';
671
672 $mail_mint_object = new \WPFunnels\Integrations\MailMint( $user_data, $lists, $tags );
673 $mail_mint_object->create_or_update_contact();
674 $mail_mint_object->add_contact_to_lists();
675 $mail_mint_object->add_contact_to_tags();
676 }
677
678 ob_start();
679 do_action( 'wpfunnels/after_optin_submit', $step_id, $post_action, $action_type, $record, $post_data );
680 ob_get_clean();
681
682 if ( !empty( $next_step[ 'step_type' ] ) && 'conditional' == $next_step[ 'step_type' ] ) {
683 $next_step = $this->match_conditional_step( $next_step, $funnel_id, $step_id );
684 $response[ 'redirect_url' ] = add_query_arg( $this->prepare_query_param( $name, $query ), get_the_permalink( $next_step[ 'step_id' ] ) );
685 }
686
687 if ( isset( $next_step[ 'step_type' ] ) && 'thankyou' === $next_step[ 'step_type' ] ) {
688 $custom_url = Wpfnl_functions::custom_url_for_thankyou_page( $next_step[ 'step_id' ] );
689 if ( $custom_url ) {
690 $response[ 'redirect_url' ] = $custom_url;
691 }
692 }
693
694
695 echo json_encode( $response, true );
696 die();
697 }
698
699
700 /**
701 * Search array item by key
702 *
703 * @param Array $array
704 * @param String $key
705 *
706 * @return Array
707 * @since 2.5.9
708 */
709 private function search_items_by_key( $array, $key ) {
710 $results = array();
711
712 if ( is_array( $array ) ) {
713 if ( isset( $array[ $key ] ) && key( $array ) == $key )
714 if ( $array[ $key ] == 'wpfunnels/optin-form' ) {
715 $results[] = $array;
716 }
717
718 foreach( $array as $sub_array )
719 $results = array_merge( $results, $this->search_items_by_key( $sub_array, $key ) );
720 }
721
722 return $results;
723 }
724
725
726 /**
727 * Send email to admin
728 *
729 * @param String $email
730 * @param String $subject
731 * @param Array $user_info
732 *
733 * @return void
734 * @since 2.5.9
735 */
736 private function send_email_to_admin( $emails = '', $subject = '', $user_info = [] ) {
737 $email_array = explode( ",", $emails );
738 if ( is_array( $email_array ) ) {
739 foreach( $email_array as $email ) {
740
741 if ( $email ) {
742 $email = trim( $email );
743 $current_date = date( "d M Y" );
744 $time = new \DateTimeImmutable( 'now', wp_timezone() );
745 $current_time = $time->format( "h:i A" );
746 $poweredBy = __( 'WPFunnels', 'wpfnl' );
747 $info = '';
748 if ( isset( $user_info[ 'first_name' ] ) && $user_info[ 'first_name' ] ) {
749 $info .= "First Name : {$user_info['first_name']}<br>";
750 }
751 if ( isset( $user_info[ 'last_name' ] ) && $user_info[ 'last_name' ] ) {
752 $info .= "Last Name : {$user_info['last_name']}<br>";
753 }
754 if ( isset( $user_info[ 'email' ] ) && $user_info[ 'email' ] ) {
755 $info .= "Email : {$user_info['email']}<br><br>";
756 }
757 if ( isset( $user_info[ 'phone' ] ) && $user_info[ 'phone' ] ) {
758 $info .= "Phone : {$user_info['phone']}<br><br>";
759 }
760 if ( isset( $user_info[ 'web-url' ] ) && $user_info[ 'web-url' ] ) {
761 $esc_url = esc_url( $user_info[ 'web-url' ] );
762 $info .= "Website URL : {$esc_url}<br><br>";
763 }
764 if ( isset( $user_info[ 'message' ] ) && $user_info[ 'message' ] ) {
765 $esc_html = esc_html( $user_info[ 'message' ] );
766 $info .= "Message : {$esc_html}<br><br>";
767 }
768 $info .= "----<br><br>Date : {$current_date} <br>Time : {$current_time} <br>Powered by : {$poweredBy} <br>";
769
770 $email_body = apply_filters('wpfunnels/after_optin_submit_admin_email_body', $info );
771 if ( !$subject ) {
772 $subject = 'New Submission';
773 }
774 $headers = [ 'Content-Type: text/html; charset=UTF-8' ];
775 wp_mail( $email, $subject, $email_body, $headers );
776 }
777 }
778 }
779 }
780
781
782 /**
783 * Create User as a subscriber
784 * When allow the subscriber option for registration from builder
785 *
786 * @param $name
787 * @param $post_data
788 *
789 * @since 2.5.9
790 */
791 public static function create_user_optin_allow_registration( $name, $post_data ) {
792 $exist_user_by_username = get_user_by( 'login', $name );
793 $exist_user_by_email = get_user_by( 'email', $post_data[ 'email' ] );
794
795 if ( empty( $exist_user_by_email ) || empty( $exist_user_by_username ) ) {
796 $data = array(
797 'user_login' => $name,
798 'user_email' => isset( $post_data[ 'email' ] ) ? $post_data[ 'email' ] : '',
799 'first_name' => isset( $post_data[ 'first_name' ] ) ? $post_data[ 'first_name' ] : '',
800 'last_name' => isset( $post_data[ 'last_name' ] ) ? $post_data[ 'last_name' ] : '',
801 'user_pass' => wp_generate_password( 6, false ),
802 'role' => 'subscriber',
803 );
804 if ( isset( $post_data[ 'web-url' ] ) ) {
805 $data[ 'user_url' ] = $post_data[ 'web-url' ];
806 }
807 $user_id = wp_insert_user( $data );
808 if ( isset( $post_data[ 'phone' ] ) ) {
809 update_user_meta( $user_id, 'phone', $post_data[ 'phone' ] );
810 }
811 if ( !is_wp_error( $user_id ) ) {
812 wp_send_new_user_notifications( $user_id, $notify = 'both' );
813 }
814 }
815 }
816
817
818 /**
819 * Check Google recaptcha Response and Send mail
820 *
821 * @param $is_recaptcha
822 * @param $recaptcha_site_key
823 * @param $recaptcha_site_secrect
824 *
825 * @since 2.5.9
826 */
827
828 private function send_mail_after_checking_google_recaptcha_response( $is_recaptcha, $recaptcha_site_key, $recaptcha_site_secrect, $admin_email, $admin_email_subject, $post_data, $response ) {
829 $recaptcha_response = [];
830 if ( $recaptcha_site_key && $recaptcha_site_secrect && 'on' === $is_recaptcha ) {
831 $recaptcha_response = Google_Recaptcha_Handler::get_response_recaptcha( $recaptcha_site_secrect, $recaptcha_site_key );
832 }
833 if ( 'on' === $is_recaptcha ) {
834 if ( $admin_email && $admin_email_subject && isset( $post_data[ 'email' ] ) && $post_data[ 'email' ] && isset( $recaptcha_response->success ) && $recaptcha_response->success ) {
835 $this->send_email_to_admin( $admin_email, $admin_email_subject, $post_data );
836 }
837 else {
838 $response[ 'notification_text' ] = __( "reCAPTCHA is not Correct", 'wpfnl' );
839 $response[ 'success' ] = false;
840 return $response;
841 }
842 }
843 else {
844 if ( $admin_email && $admin_email_subject && isset( $post_data[ 'email' ] ) && $post_data[ 'email' ] ) {
845 $this->send_email_to_admin( $admin_email, $admin_email_subject, $post_data );
846 }
847 }
848 return $response;
849 }
850
851
852 /**
853 * Match ption submit condition
854 *
855 * @param Array $next_step
856 * @param Number $funnel_id
857 *
858 * @return Array $next_step
859 * @since 2.5.7
860 */
861 public static function match_conditional_step( $next_step, $funnel_id, $step_id ) {
862 $condition = Wpfnl_Condition_Checker::getInstance();
863 $condition_identifier = strval( $next_step[ 'step_id' ] );
864 $data[ 'step_id' ] = $step_id;
865 $condition_matched = $condition->check_condition( $funnel_id, $data, $condition_identifier );
866 $next_node = Wpfnl_functions::get_next_step( $funnel_id, $condition_identifier, $condition_matched );
867 $next_node = apply_filters( 'wpfunnels/next_step_data', $next_node );
868 return $next_node;
869 }
870
871
872 }
873