PluginProbe
SMS Alert – SMS & OTP for WooCommerce, Order Notifications & Abandoned Cart Recovery / trunk
SMS Alert – SMS & OTP for WooCommerce, Order Notifications & Abandoned Cart Recovery vtrunk
4.0.0 3.9.9 3.9.8 3.9.7 3.9.6 trunk 3.0.0 3.5.0 3.6.0 3.7.0 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.8.8 3.8.9 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 All 26 releases
sms-alert / handler / forms / class-wpforms.php

class-wpforms.php in SMS Alert – SMS & OTP for WooCommerce, Order Notifications & Abandoned Cart Recovery trunk, at handler/forms/class-wpforms.php

541 lines 19.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This file handles wp forms via sms notification
4 *
5 * PHP version 5
6 *
7 * @category Handler
8 * @package SMSAlert
9 * @author SMS Alert <support@cozyvision.com>
10 * @license URI: http://www.gnu.org/licenses/gpl-2.0.html
11 * @link https://www.smsalert.co.in/
12 */
13
14 if (! defined('ABSPATH') ) {
15 exit;
16 }
17
18 if (! is_plugin_active('wpforms-lite/wpforms.php') && ! is_plugin_active('wpforms/wpforms.php') ) {
19 return;
20 }
21
22 /**
23 * PHP version 5
24 *
25 * @category Handler
26 * @package SMSAlert
27 * @author SMS Alert <support@cozyvision.com>
28 * @license URI: http://www.gnu.org/licenses/gpl-2.0.html
29 * @link https://www.smsalert.co.in/
30 * WpForm class.
31 */
32 class WpForm extends FormInterface
33 {
34
35 /**
36 * Form Session Variable.
37 *
38 * @return void
39 */
40 private $form_session_var = FormSessionVars::WPFORM;
41
42 /**
43 * Handle OTP form
44 *
45 * @return void
46 */
47 public function handleForm()
48 {
49 add_action('wpforms_process_complete', array( $this, 'wpfDevProcessComplete' ), 10, 4);
50 add_filter('wpforms_field_properties', array( $this, 'wpfAddPhoneClass' ), 10, 3);
51 add_filter('wpforms_display_field_after', array( $this, 'wpfDevProcessFilter' ), 10, 2);
52 add_filter('wpforms_save_form_args', array( $this, 'smsalertWpformShowWarnings' ), 10, 3);
53 add_action('wpforms_process', array( $this, 'validateFields' ), 20, 3);
54 add_filter('wpforms_process_bypass_captcha', array( $this, 'beforeValidateFields' ), 10, 3);
55 add_filter('sa_get_user_phone_no', array( $this, 'saUpdateBillingPhone' ), 10, 2);
56 $user_authorize = new smsalert_Setting_Options();
57 if ($user_authorize->is_user_authorised() ) {
58 add_action('wpforms_form_settings_panel_content', array( $this, 'customWpformsFormSettingsPanelContent' ), 10, 1);
59 add_filter('wpforms_builder_settings_sections', array( $this, 'customWpformsBuilderSettingsSections' ), 10, 2);
60 }
61 }
62
63 /**
64 * This function by Pass Fields.
65 *
66 * @param $fals Form fals
67 * @param $entry entry
68 * @param $form_data form_data
69 *
70 * @return void.
71 */
72 public function beforeValidateFields( $fals, $entry, $form_data)
73 {
74 SmsAlertUtility::checkSession();
75 if (isset($_SESSION['sa_wf_mobile_verified']) ) {
76 unset($_SESSION['sa_wf_mobile_verified']);
77 return $entry;
78 }
79
80 }
81 /**
82 * Update phone field
83 *
84 * @param string $billing_phone billing phone
85 * @param int $user_id user id
86 *
87 * @return void
88 */
89 public function saUpdateBillingPhone($billing_phone, $user_id)
90 {
91 $form_id = !empty($_POST['wpforms']['id'])?$_POST['wpforms']['id']:'';
92 if ($form_id != '') {
93 $form_data = wpforms()->get('form')->get($form_id, ['content_only' => true,]);
94 if (!empty($form_data['settings']['smsalert']['visitor_phone'])) {
95 $phone_field = $form_data['settings']['smsalert']['visitor_phone'];
96 $phone_field_id = preg_replace('/[^0-9]/', '', $phone_field);
97 $phone = '';
98 if (! empty($phone_field_id) ) {
99 $datas = array();
100 foreach ($_POST['wpforms']['complete'] as $key => $field ) {
101 $datas[ '{field_id="' . $key . '"}' ] = $field['value'];
102 if ($phone_field_id == $key ) {
103 $phone = $field['value'];
104 }
105 }
106 }
107 if (!empty($phone)) {
108 return $phone;
109 }
110 }
111 }
112 return $billing_phone;
113 }
114
115 /**
116 * This function shows validation error message.
117 *
118 * @param $fields Form fields
119 * @param $entry entry
120 * @param $form_data form_data
121 *
122 * @return void.
123 */
124 public function validateFields($fields, $entry, $form_data)
125 {
126 if (isset($_REQUEST['option']) && 'smsalert_wpforms_otp' === sanitize_text_field(wp_unslash($_REQUEST['option']))) {
127 SmsAlertUtility::initialize_transaction($this->form_session_var);
128 } else {
129 return;
130 }
131 $phone_field = !empty($form_data['settings']['smsalert']['visitor_phone'])?$form_data['settings']['smsalert']['visitor_phone']:'';
132 $phone_field_id = preg_replace('/[^0-9]/', '', $phone_field);
133 $phone = '';
134 if (! empty($phone_field_id) ) {
135 $datas = array();
136 foreach ( $fields as $key => $field ) {
137 $datas[ '{field_id="' . $key . '"}' ] = $field['value'];
138 if ($phone_field_id == $key ) {
139 $phone = $field['value'];
140 }
141 }
142 }
143 if (isset($phone) && SmsAlertUtility::isBlank($phone)) {
144 wp_send_json(SmsAlertUtility::_create_json_response(__('Please enter phone number.', 'sms-alert'), SmsAlertConstants::ERROR_JSON_TYPE));
145 exit();
146 }
147
148 return $this->processFormFields($phone);
149
150 }
151
152 /**
153 * This function processed form fields.
154 *
155 * @param string $phone User phone.
156 *
157 * @return bool
158 */
159 public function processFormFields( $phone )
160 {
161 global $phoneLogic;
162 $phone_num = preg_replace('/[^0-9]/', '', $phone);
163
164 if (! isset($phone_num) || ! SmsAlertUtility::validatePhoneNumber($phone_num) ) {
165 wp_send_json(SmsAlertUtility::_create_json_response(str_replace('##phone##', $phone_num, $phoneLogic->_get_otp_invalid_format_message()), SmsAlertConstants::ERROR_JSON_TYPE));
166 exit();
167 }
168
169 smsalert_site_challenge_otp('test', null, null, $phone_num, 'phone', null, null, 'ajax');
170 }
171
172
173 /**
174 * Show warning if phone field not selected.
175 *
176 * @param array $form form_data.
177 * @param array $data data.
178 * @param array $args args.
179 *
180 * @return void
181 */
182 public function smsalertWpformShowWarnings($form, $data, $args)
183 {
184 $is_msg_enabled = !empty($data['settings']['smsalert']['message_enable'])?$data['settings']['smsalert']['message_enable']:'';
185 $is_otp_enable = !empty($data['settings']['smsalert']['otp_enable'])?$data['settings']['smsalert']['otp_enable']:'';
186 $is_visitor_phone = !empty($data['settings']['smsalert']['visitor_phone'])?$data['settings']['smsalert']['visitor_phone']:'';
187
188 if ((!empty($is_msg_enabled) || !empty($is_otp_enable)) && empty($is_visitor_phone)) {
189 wp_send_json_error(esc_html__('Please choose SMS Alert phone field in SMS Alert tab.', 'sms-alert'));
190 }
191 return $form;
192 }
193
194
195 /**
196 * Wpf dev process filter.
197 *
198 * @param array $field field.
199 * @param array $form_data form_data.
200 *
201 * @return void
202 */
203 public function wpfDevProcessFilter( $field, $form_data )
204 {
205 $unique_class = 'sa-class-'.mt_rand(1, 100);
206 $user_authorize = new smsalert_Setting_Options();
207 $islogged = $user_authorize->is_user_authorised();
208 $phone_field = !empty($form_data['settings']['smsalert']['visitor_phone'])?$form_data['settings']['smsalert']['visitor_phone']:'';
209 $phone_field_id = preg_replace('/[^0-9]/', '', $phone_field);
210 $enabled_country = smsalert_get_option('checkout_show_country_code', 'smsalert_general', '');
211 $inline_script = 'document.addEventListener("DOMContentLoaded", function() {';
212 if (isset($form_data['settings']['smsalert']['otp_enable']) && $islogged && ($field['id'] === $phone_field_id) ) {
213
214 $otp_enable = $form_data['settings']['smsalert']['otp_enable'];
215
216 if ($otp_enable ) {
217 $inline_script .= 'jQuery("form#wpforms-form-' . esc_attr($form_data['id']) . '").each(function ()
218 {
219 if(!jQuery(this).hasClass("sa-wp-form"))
220 {
221 jQuery(this).addClass("'.$unique_class.' sa-wp-form");
222 }
223 });';
224 echo do_shortcode('[sa_verify id="" phone_selector=".smsalert-phone #wpforms-' . esc_attr($form_data['id']) . '-field_' . esc_attr($phone_field_id) . '" submit_selector= ".'.$unique_class.' .wpforms-submit" ]');
225 }
226 }
227
228 if (isset($form_data['settings']['smsalert']) && 'on' === $enabled_country && !array_key_exists('otp_enable', (array)$form_data['settings']['smsalert']) ) {
229 $inline_script .= 'jQuery(document).ready(function(){
230 initialiseCountrySelector(".smsalert-phone #wpforms-' . esc_attr($form_data['id']) . '-field_' . esc_attr($phone_field_id) . '");
231 });';
232 }
233 $inline_script .= '});';
234 if (! wp_script_is('sainlinescript-handle-footer', 'enqueued') ) {
235 wp_register_script('sainlinescript-handle-footer', '', [], '', true);
236 wp_enqueue_script('sainlinescript-handle-footer');
237 }
238 wp_add_inline_script("sainlinescript-handle-footer", $inline_script);
239 }
240
241 /**
242 * Add Tab smsalert setting in wpform builder section
243 *
244 * @param array $sections form section.
245 * @param array $form_data form datas.
246 *
247 * @return array
248 */
249 public function customWpformsBuilderSettingsSections( $sections, $form_data )
250 {
251 $sections['smsalert'] = 'SMS Alert';
252 return $sections;
253 }
254
255 /**
256 * Add Tab panel smsalert setting in wpform builder section
257 *
258 * @param object $instance tab panel object.
259 *
260 * @return void
261 */
262 public function customWpformsFormSettingsPanelContent( $instance )
263 {
264 $form_data = $instance->form_data;
265 echo '<div class="wpforms-panel-content-section wpforms-panel-content-section-smsalert">';
266
267 echo '<div class="wpforms-panel-content-section-title"><span id="wpforms-builder-settings-notifications-title">SMS Alert Message Configuration</span>
268 </div>';
269 echo '<div>
270
271 <a href="https://www.youtube.com/watch?v=iYvHz6wrBbA" target="_blank" class="btn-outline"><span class="dashicons dashicons-video-alt3" style="font-size: 21px"></span> Youtube</a>
272
273 <a href="https://kb.smsalert.co.in/knowledgebase/integrate-with-wpforms/#configuration" target="_blank" class="btn-outline"><span class="dashicons dashicons-format-aside"></span> Documentation</a>
274
275 </div>';
276 $plugin_file = is_plugin_active('wpforms-lite/wpforms.php')?'/wpforms-lite/wpforms.php':'/wpforms/wpforms.php';
277 $plugin_data = get_plugin_data(WP_PLUGIN_DIR.$plugin_file);
278 $checkbox = (!empty($plugin_data['Version']) && $plugin_data['Version'] < '1.6.2.3') ? 'checkbox':'toggle';
279 wpforms_panel_field(
280 $checkbox,
281 'smsalert',
282 'message_enable',
283 $instance->form_data,
284 esc_html__('Enable Message', 'sms-alert'),
285 array( 'parent' => 'settings' )
286 );
287 wpforms_panel_field(
288 $checkbox,
289 'smsalert',
290 'otp_enable',
291 $instance->form_data,
292 esc_html__('Enable Mobile Verification', 'sms-alert'),
293 array( 'parent' => 'settings' )
294 );
295 wpforms_panel_field(
296 'text',
297 'smsalert',
298 'admin_number',
299 $instance->form_data,
300 __('Send Admin SMS To', 'sms-alert'),
301 array(
302 'default' => '',
303 'parent' => 'settings',
304 'after' => '<p class="note">' .
305 __('Admin sms notifications will be sent to this number.', 'sms-alert') . '</p>',
306 )
307 );
308 wpforms_panel_field(
309 'textarea',
310 'smsalert',
311 'admin_message',
312 $instance->form_data,
313 __('Admin Message', 'sms-alert'),
314 array(
315 'rows' => 6,
316 'default' => sprintf(__('Dear admin, you have a new enquiry from %1$s.%2$sPowered by%3$swww.smsalert.co.in', 'sms-alert'), '[store_name]', PHP_EOL, PHP_EOL),
317 'smarttags' => array(
318 'type' => 'all',
319 ),
320 'parent' => 'settings',
321 'class' => 'email-msg',
322
323 )
324 );
325 wpforms_panel_field(
326 'text',
327 'smsalert',
328 'visitor_phone',
329 $instance->form_data,
330 __('Select Phone Field', 'sms-alert'),
331 array(
332 'default' => '',
333 'smarttags' => array(
334 'type' => 'all',
335 ),
336 'parent' => 'settings',
337 )
338 );
339 wpforms_panel_field(
340 'textarea',
341 'smsalert',
342 'visitor_message',
343 $instance->form_data,
344 __('Visitor Message', 'sms-alert'),
345 array(
346 'rows' => 6,
347 'default' => sprintf(__('Hello user, thank you for contacting with %1$s.', 'sms-alert'), '[store_name]'),
348 'smarttags' => array(
349 'type' => 'all',
350 ),
351 'parent' => 'settings',
352 'class' => 'email-msg',
353 )
354 );
355 $admin_number = isset($form_data['settings']['smsalert']['admin_number'])?$form_data['settings']['smsalert']['admin_number']:'';
356 echo '</div>';
357 echo "<script>
358 var adminnumber = '" . $admin_number . "';
359 var tagInput1 = new TagsInput({
360 selector: 'wpforms-panel-field-smsalert-admin_number',
361 duplicate : false,
362 max : 10,
363 });
364 var number = (adminnumber!='') ? adminnumber.split(',') : [];
365 if(number.length > 0){
366 tagInput1.addData(number);
367 }
368 </script>";
369 }
370
371 /**
372 * Process wp form submission and send sms
373 *
374 * @param array $properties properties.
375 * @param array $field field.
376 * @param array $form_data form data.
377 *
378 * @return void
379 */
380 public function wpfAddPhoneClass( $properties, $field, $form_data )
381 {
382 $phone_field = !empty($form_data['settings']['smsalert']['visitor_phone'])?$form_data['settings']['smsalert']['visitor_phone']:'';
383 $phone_field_id = preg_replace('/[^0-9]/', '', $phone_field);
384 if ($field['id'] === $phone_field_id) {
385 $properties['container']['class'][] = 'smsalert-phone';
386 }
387 return $properties;
388 }
389
390 /**
391 * Process wp form submission and send sms
392 *
393 * @param array $fields form fields.
394 * @param array $entry form entries.
395 * @param array $form_data form data.
396 * @param int $entry_id entity id.
397 *
398 * @return void
399 */
400 public function wpfDevProcessComplete( $fields, $entry, $form_data, $entry_id )
401 {
402
403 $user_authorize = new smsalert_Setting_Options();
404 $islogged = $user_authorize->is_user_authorised();
405 $msg_enable = !empty($form_data['settings']['smsalert']['message_enable'])?$form_data['settings']['smsalert']['message_enable']:'';
406 if ($msg_enable && $islogged ) {
407 $phone_field = $form_data['settings']['smsalert']['visitor_phone'];
408 $admin_number = $form_data['settings']['smsalert']['admin_number'];
409 $visitor_message = $form_data['settings']['smsalert']['visitor_message'];
410 $admin_message = $form_data['settings']['smsalert']['admin_message'];
411 $phone_field_id = preg_replace('/[^0-9]/', '', $phone_field);
412 if (! empty($phone_field_id) ) {
413 $phone = '';
414 $datas = array();
415 foreach ( $fields as $key => $field ) {
416 $datas[ '{field_id="' . $key . '"}' ] = $field['value'];
417 //Please do not use === triple equal to here(Key does not match after use).
418 if ($phone_field_id == $key ) {
419 $phone = $field['value'];
420 }
421 }
422 do_action('sa_send_sms', $phone, self::parseSmsContent($visitor_message, $datas));
423 if (! empty($admin_number) ) {
424 do_action('sa_send_sms', $admin_number, self::parseSmsContent($admin_message, $datas));
425 }
426 }
427 }
428 }
429
430 /**
431 * Check your otp setting is enabled or not.
432 *
433 * @return bool
434 */
435 public static function isFormEnabled()
436 {
437 $user_authorize = new smsalert_Setting_Options();
438 $islogged = $user_authorize->is_user_authorised();
439 return ( $islogged && (is_plugin_active('wpforms-lite/wpforms.php') || is_plugin_active('wpforms/wpforms.php') )) ? true : false;
440 }
441
442 /**
443 * Handle after failed verification
444 *
445 * @param object $user_login users object.
446 * @param string $user_email user email.
447 * @param string $phone_number phone number.
448 *
449 * @return void
450 */
451 public function handle_failed_verification( $user_login, $user_email, $phone_number )
452 {
453 SmsAlertUtility::checkSession();
454 if (! isset($_SESSION[ $this->form_session_var ]) ) {
455 return;
456 }
457 if (! empty($_REQUEST['option']) && sanitize_text_field(wp_unslash($_REQUEST['option'])) === 'smsalert-validate-otp-form' ) {
458 wp_send_json(SmsAlertUtility::_create_json_response(__('Invalid one time passcode. Please enter a valid passcode.', 'sms-alert'), 'error'));
459 exit();
460 } else {
461 $_SESSION[ $this->form_session_var ] = 'verification_failed';
462 }
463 }
464
465 /**
466 * Handle after post verification
467 *
468 * @param string $redirect_to redirect url.
469 * @param object $user_login user object.
470 * @param string $user_email user email.
471 * @param string $password user password.
472 * @param string $phone_number phone number.
473 * @param string $extra_data extra hidden fields.
474 *
475 * @return void
476 */
477 public function handle_post_verification( $redirect_to, $user_login, $user_email, $password, $phone_number, $extra_data )
478 {
479 SmsAlertUtility::checkSession();
480 if (! isset($_SESSION[ $this->form_session_var ]) ) {
481 return;
482 }
483 $_SESSION['sa_wf_mobile_verified'] = true;
484 if (! empty($_REQUEST['option']) && sanitize_text_field(wp_unslash($_REQUEST['option'])) === 'smsalert-validate-otp-form' ) {
485 wp_send_json(SmsAlertUtility::_create_json_response(__('OTP Validated Successfully.', 'sms-alert'), 'success'));
486 exit();
487 } else {
488 $_SESSION[ $this->form_session_var ] = 'validated';
489 }
490 }
491
492 /**
493 * Clear otp session variable
494 *
495 * @return void
496 */
497 public function unsetOTPSessionVariables()
498 {
499 unset($_SESSION[ $this->tx_session_id ]);
500 unset($_SESSION[ $this->form_session_var ]);
501 }
502
503 /**
504 * Check current form submission is ajax or not
505 *
506 * @param bool $is_ajax bool value for form type.
507 *
508 * @return bool
509 */
510 public function is_ajax_form_in_play( $is_ajax )
511 {
512 SmsAlertUtility::checkSession();
513 return isset($_SESSION[ $this->form_session_var ]) ? true : $is_ajax;
514 }
515
516 /**
517 * Replace variables for sms contennt
518 *
519 * @param string $content sms content to be sent.
520 * @param array $datas values of varibles.
521 *
522 * @return string
523 */
524 public static function parseSmsContent( $content = null, $datas = array() )
525 {
526 $find = array_keys($datas);
527 $replace = array_values($datas);
528 $content = str_replace($find, $replace, $content);
529 return $content;
530 }
531
532 /**
533 * Handle form for WordPress backend
534 *
535 * @return void
536 */
537 public function handleFormOptions()
538 {
539 }
540 }
541 new WpForm();