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