Changeset 2874897
- Timestamp:
- 03/05/2023 09:37:56 AM (3 years ago)
- Location:
- alpha-sms
- Files:
-
- 49 added
- 2 deleted
- 3 edited
-
tags/1.0.6 (added)
-
tags/1.0.6/LICENSE.txt (added)
-
tags/1.0.6/README.txt (added)
-
tags/1.0.6/admin (added)
-
tags/1.0.6/admin/class-alpha_sms-admin.php (added)
-
tags/1.0.6/admin/css (added)
-
tags/1.0.6/admin/css/alpha_sms-admin.css (added)
-
tags/1.0.6/admin/index.php (added)
-
tags/1.0.6/admin/js (added)
-
tags/1.0.6/admin/js/alpha_sms-admin.js (added)
-
tags/1.0.6/admin/partials (added)
-
tags/1.0.6/admin/partials/alpha_sms-admin-display_campaign.php (added)
-
tags/1.0.6/admin/partials/alpha_sms-admin-display_settings.php (added)
-
tags/1.0.6/alpha_sms.php (added)
-
tags/1.0.6/assets (added)
-
tags/1.0.6/assets/banner-1544x500.png (added)
-
tags/1.0.6/assets/banner-772x250.png (added)
-
tags/1.0.6/assets/icon-128x128.png (added)
-
tags/1.0.6/assets/icon-256x256.png (added)
-
tags/1.0.6/assets/screenshot-1.jpg (added)
-
tags/1.0.6/assets/screenshot-2.jpg (added)
-
tags/1.0.6/assets/smsBanner.ai (added)
-
tags/1.0.6/includes (added)
-
tags/1.0.6/includes/class-alpha_sms-activator.php (added)
-
tags/1.0.6/includes/class-alpha_sms-deactivator.php (added)
-
tags/1.0.6/includes/class-alpha_sms-i18n.php (added)
-
tags/1.0.6/includes/class-alpha_sms-loader.php (added)
-
tags/1.0.6/includes/class-alpha_sms.php (added)
-
tags/1.0.6/includes/index.php (added)
-
tags/1.0.6/includes/sms.class.php (added)
-
tags/1.0.6/index.php (added)
-
tags/1.0.6/languages (added)
-
tags/1.0.6/languages/alpha_sms.pot (added)
-
tags/1.0.6/public (added)
-
tags/1.0.6/public/class-alpha_sms-public.php (added)
-
tags/1.0.6/public/css (added)
-
tags/1.0.6/public/css/alpha_sms-public.css (added)
-
tags/1.0.6/public/css/otp-login-form.css (added)
-
tags/1.0.6/public/index.php (added)
-
tags/1.0.6/public/js (added)
-
tags/1.0.6/public/js/alpha_sms-public.js (added)
-
tags/1.0.6/public/js/otp-login-form.js (added)
-
tags/1.0.6/public/partials (added)
-
tags/1.0.6/public/partials/add-otp-checkout-form.php (added)
-
tags/1.0.6/public/partials/add-otp-on-login-form.php (added)
-
tags/1.0.6/public/partials/add-otp-on-wc-reg-form.php (added)
-
tags/1.0.6/uninstall.php (added)
-
trunk/README.txt (modified) (1 diff)
-
trunk/alpha_sms.php (modified) (2 diffs)
-
trunk/assets/screenshot-1.jpg (added)
-
trunk/assets/screenshot-1.png (deleted)
-
trunk/assets/screenshot-2.jpg (added)
-
trunk/assets/screenshot-2.png (deleted)
-
trunk/public/class-alpha_sms-public.php (modified) (46 diffs)
Legend:
- Unmodified
- Added
- Removed
-
alpha-sms/trunk/README.txt
r2872897 r2874897 5 5 Tested up to: 6.1 6 6 Requires PHP: 5.6 7 Stable tag: 1.0. 57 Stable tag: 1.0.6 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html -
alpha-sms/trunk/alpha_sms.php
r2872897 r2874897 17 17 * Plugin URI: https://sms.net.bd/plugins/wordpress 18 18 * Description: WP 2FA Login. SMS OTP Verification for Registration and Login forms, WooCommerce SMS Notification for your shop orders. 19 * Version: 1.0. 519 * Version: 1.0.6 20 20 * Author: Alpha Net 21 21 * Author URI: https://sms.net.bd/ … … 36 36 * Rename this for your plugin and update it as you release new versions. 37 37 */ 38 define('ALPHA_SMS_VERSION', '1.0. 4');38 define('ALPHA_SMS_VERSION', '1.0.6'); 39 39 40 40 // plugin constants -
alpha-sms/trunk/public/class-alpha_sms-public.php
r2867587 r2874897 2 2 3 3 // If this file is called directly, abort. 4 if ( ! defined( 'WPINC' )) {4 if (!defined('WPINC')) { 5 5 die; 6 6 } … … 16 16 * @author Alpha Net Developer Team <support@alpha.net.bd> 17 17 */ 18 class Alpha_sms_Public { 18 class Alpha_sms_Public 19 { 19 20 20 21 /** … … 49 50 * @since 1.0.0 50 51 */ 51 public function __construct( $plugin_name, $version ) { 52 public function __construct($plugin_name, $version) 53 { 52 54 $this->plugin_name = $plugin_name; 53 55 $this->version = $version; 54 $this->options = get_option( $this->plugin_name);55 $this->pluginActive = ! empty( $this->options['api_key'] ) && $this->checkAPI( $this->options['api_key']);56 $this->options = get_option($this->plugin_name); 57 $this->pluginActive = !empty($this->options['api_key']) && $this->checkAPI($this->options['api_key']); 56 58 } 57 59 … … 61 63 * start session if not started 62 64 */ 63 public function start_session_wp() { 64 if ( ! session_id() ) { 65 public function start_session_wp() 66 { 67 if (!session_id()) { 65 68 session_start(); 66 69 } … … 72 75 * @since 1.0.0 73 76 */ 74 public function enqueue_styles() { 77 public function enqueue_styles() 78 { 75 79 /** 76 80 * This function is provided for demonstration purposes only. … … 87 91 wp_enqueue_style( 88 92 $this->plugin_name, 89 plugin_dir_url( __FILE__) . 'css/alpha_sms-public.css',93 plugin_dir_url(__FILE__) . 'css/alpha_sms-public.css', 90 94 [], 91 95 $this->version, … … 99 103 * @since 1.0.0 100 104 */ 101 public function enqueue_scripts() { 105 public function enqueue_scripts() 106 { 102 107 /** 103 108 * This function is provided for demonstration purposes only. … … 114 119 wp_enqueue_script( 115 120 $this->plugin_name, 116 plugin_dir_url( __FILE__) . 'js/alpha_sms-public.js',117 [ 'jquery'],121 plugin_dir_url(__FILE__) . 'js/alpha_sms-public.js', 122 ['jquery'], 118 123 $this->version, 119 124 false … … 124 129 $this->plugin_name, 125 130 $this->plugin_name . '_object', 126 [ 'ajaxurl' => admin_url( 'admin-ajax.php' )]131 ['ajaxurl' => admin_url('admin-ajax.php')] 127 132 ); 128 133 } … … 132 137 * show phone number on register page and my account 133 138 */ 134 public function wc_phone_on_register() { 135 if ( ! $this->pluginActive || ! $this->options['wc_reg'] ) { 139 public function wc_phone_on_register() 140 { 141 if (!$this->pluginActive || !$this->options['wc_reg']) { 136 142 return; 137 143 } 138 144 139 145 $user = wp_get_current_user(); 140 $value = isset( $_POST['billing_phone'] ) ? sanitize_text_field( $_POST['billing_phone'])146 $value = isset($_POST['billing_phone']) ? sanitize_text_field($_POST['billing_phone']) 141 147 : $user->billing_phone; 142 ?>143 144 <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">145 <label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce'); ?> <span class="required">*</span>146 </label>147 <input type="tel" minlength="11" maxlength="11" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php echo esc_attr( $value ) ?>" required/>148 </p>149 <div class="clear"></div>150 151 <?php148 ?> 149 150 <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide"> 151 <label for="reg_billing_phone"><?php _e('Phone', 'woocommerce'); ?> <span class="required">*</span> 152 </label> 153 <input type="tel" minlength="11" maxlength="11" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php echo esc_attr($value) ?>" required /> 154 </p> 155 <div class="clear"></div> 156 157 <?php 152 158 } 153 159 … … 156 162 * show otp form in registration form 157 163 */ 158 public function add_otp_field_on_wp_reg_form() { 159 if ( ! $this->pluginActive || ! $this->options['wp_reg'] ) { 164 public function add_otp_field_on_wp_reg_form() 165 { 166 if (!$this->pluginActive || !$this->options['wp_reg']) { 160 167 return; 161 168 } 162 169 require_once 'partials/add-otp-on-login-form.php'; 163 ?>164 <input type='hidden' name='action_type' id='action_type' value='wp_reg'/>165 <?php170 ?> 171 <input type='hidden' name='action_type' id='action_type' value='wp_reg' /> 172 <?php 166 173 } 167 174 … … 170 177 * show otp form in registration form 171 178 */ 172 public function add_otp_field_on_wc_reg_form() { 173 if ( ! $this->pluginActive || ! $this->options['wc_reg'] ) { 179 public function add_otp_field_on_wc_reg_form() 180 { 181 if (!$this->pluginActive || !$this->options['wc_reg']) { 174 182 return; 175 183 } 176 184 177 185 require_once 'partials/add-otp-on-wc-reg-form.php'; 178 ?>179 <input type='hidden' name='action_type' id='action_type' value='wc_reg'/>180 <?php186 ?> 187 <input type='hidden' name='action_type' id='action_type' value='wc_reg' /> 188 <?php 181 189 } 182 190 … … 185 193 * ajax otp send on post phone number * 186 194 */ 187 public function send_otp_for_reg() { 195 public function send_otp_for_reg() 196 { 188 197 $user_phone = $user_email = ''; 189 198 190 if ( isset( $_POST['billing_phone'], $_POST['email'] )) {191 $user_phone = $this->validateNumber( sanitize_text_field( $_POST['billing_phone'] ));192 $user_email = sanitize_text_field( $_POST['email']);193 } 194 195 if ( ! $user_email && ! empty( $_POST['billing_email'] )) {196 $user_email = sanitize_text_field( $_POST['billing_email']);197 } 198 199 if ( ! filter_var( $user_email, FILTER_VALIDATE_EMAIL )) {200 $response = [ 'status' => 400, 'message' => __( 'The email address you entered is not valid!' )];201 echo wp_kses_post( json_encode( $response ));199 if (isset($_POST['billing_phone'], $_POST['email'])) { 200 $user_phone = $this->validateNumber(sanitize_text_field($_POST['billing_phone'])); 201 $user_email = sanitize_text_field($_POST['email']); 202 } 203 204 if (!$user_email && !empty($_POST['billing_email'])) { 205 $user_email = sanitize_text_field($_POST['billing_email']); 206 } 207 208 if (!filter_var($user_email, FILTER_VALIDATE_EMAIL)) { 209 $response = ['status' => 400, 'message' => __('The email address you entered is not valid!')]; 210 echo wp_kses_post(json_encode($response)); 202 211 wp_die(); 203 212 exit; 204 213 } 205 214 206 if ( isset( $_POST['password'] ) && empty( $_POST['password'] ) && strlen( $_POST['password'] ) < 8) {207 $response = [ 'status' => 400, 'message' => __( 'Weak - Please enter a stronger password.' )];208 echo wp_kses_post( json_encode( $response ));215 if (isset($_POST['password']) && empty($_POST['password']) && strlen($_POST['password']) < 8) { 216 $response = ['status' => 400, 'message' => __('Weak - Please enter a stronger password.')]; 217 echo wp_kses_post(json_encode($response)); 209 218 wp_die(); 210 219 exit; 211 220 } 212 221 213 if ( ! $user_phone) {214 $response = [ 'status' => 400, 'message' => __( 'The phone number you entered is not valid!' )];215 echo wp_kses_post( json_encode( $response ));222 if (!$user_phone) { 223 $response = ['status' => 400, 'message' => __('The phone number you entered is not valid!')]; 224 echo wp_kses_post(json_encode($response)); 216 225 wp_die(); 217 226 exit; … … 223 232 $body = 'Your OTP for ' . get_bloginfo() . ' registration is ' . $otp_code . '. Valid for 2 min. Contact us if you need help.'; 224 233 225 if ( ! empty( $_POST['action_type'] ) && $_POST['action_type'] === 'wc_checkout') {234 if (!empty($_POST['action_type']) && $_POST['action_type'] === 'wc_checkout') { 226 235 $body = 'Your OTP for secure order checkout on ' . get_bloginfo() . ' is ' . $otp_code . '. Use it within 2 min to complete the checkout process.'; 227 236 } 228 237 229 $sms_response = $this->SendSMS( $user_phone, $body);230 231 if ( $sms_response->error === 0) {238 $sms_response = $this->SendSMS($user_phone, $body); 239 240 if ($sms_response->error === 0) { 232 241 // save info in database for later verification 233 if ( $this->log_login_register_action(242 if ($this->log_login_register_action( 234 243 $user_phone, 235 244 $otp_code 236 ) ) {245 )) { 237 246 $response = [ 238 247 'status' => 200, … … 240 249 ]; 241 250 } else { 242 $response = [ 'status' => 400, 'message' => __( 'Error occurred while sending OTP. Please try again.' )];251 $response = ['status' => 400, 'message' => __('Error occurred while sending OTP. Please try again.')]; 243 252 } 244 253 245 echo wp_kses_post( json_encode( $response ));254 echo wp_kses_post(json_encode($response)); 246 255 wp_die(); 247 256 exit; 248 257 } 249 258 250 $response = [ 'status' => '400', 'message' => __( 'Error occurred while sending OTP. Contact Administrator.' )];251 echo wp_kses_post( json_encode( $response ));259 $response = ['status' => '400', 'message' => __('Error occurred while sending OTP. Contact Administrator.')]; 260 echo wp_kses_post(json_encode($response)); 252 261 wp_die(); 253 262 exit; … … 261 270 * @return false|int|string 262 271 */ 263 public function validateNumber( $num ) { 264 if ( ! $num ) { 272 public function validateNumber($num) 273 { 274 if (!$num) { 265 275 return false; 266 276 } 267 277 268 $num = ltrim( trim( $num ), "+88");269 $number = '88' . ltrim( $num, "88");270 271 $ext = [ "88017", "88013", "88016", "88015", "88018", "88019", "88014"];272 if ( is_numeric( $number ) && strlen( $number ) === 13 && in_array( substr( $number, 0, 5 ), $ext, true )) {278 $num = ltrim(trim($num), "+88"); 279 $number = '88' . ltrim($num, "88"); 280 281 $ext = ["88017", "88013", "88016", "88015", "88018", "88019", "88014"]; 282 if (is_numeric($number) && strlen($number) === 13 && in_array(substr($number, 0, 5), $ext, true)) { 273 283 return $number; 274 284 } … … 282 292 * @return string 283 293 */ 284 public function generateOTP() { 294 public function generateOTP() 295 { 285 296 $otp = ''; 286 297 287 for ( $i = 0; $i < 6; $i ++) {288 $otp .= mt_rand( 0, 9);298 for ($i = 0; $i < 6; $i++) { 299 $otp .= mt_rand(0, 9); 289 300 } 290 301 … … 300 311 * @return false|mixed 301 312 */ 302 public function SendSMS( $to, $body ) { 303 if ( ! $this->pluginActive ) { 313 public function SendSMS($to, $body) 314 { 315 if (!$this->pluginActive) { 304 316 return false; 305 317 } 306 318 307 $api_key = ! empty( $this->options['api_key']) ? $this->options['api_key'] : '';308 $sender_id = ! empty( $this->options['sender_id'] ) ? trim( $this->options['sender_id']) : '';319 $api_key = !empty($this->options['api_key']) ? $this->options['api_key'] : ''; 320 $sender_id = !empty($this->options['sender_id']) ? trim($this->options['sender_id']) : ''; 309 321 310 322 require_once ALPHA_SMS_PATH . 'includes/sms.class.php'; 311 323 312 $sms = new AlphaSMS( $api_key);324 $sms = new AlphaSMS($api_key); 313 325 $sms->numbers = $to; 314 326 $sms->body = $body; … … 330 342 $otp_code 331 343 ) { 332 $dateTime = new DateTime( ALPHA_SMS_TIMESTAMP);333 $dateTime->modify( '+2 minutes');344 $dateTime = new DateTime(ALPHA_SMS_TIMESTAMP); 345 $dateTime->modify('+2 minutes'); 334 346 335 347 $_SESSION['alpha_sms_otp_code'] = $otp_code; 336 $_SESSION['alpha_sms_expires'] = $dateTime->format( 'Y-m-d H:i:s');337 338 if ( ! empty( $_SESSION['alpha_sms_otp_code'] )) {348 $_SESSION['alpha_sms_expires'] = $dateTime->format('Y-m-d H:i:s'); 349 350 if (!empty($_SESSION['alpha_sms_otp_code'])) { 339 351 return true; 340 352 } … … 348 360 * @param $customer_id 349 361 */ 350 public function register_the_customer( $customer_id ) { 351 if ( ! $this->pluginActive || ( ! $this->options['wp_reg'] && ! $this->options['wc_reg'] ) ) { 352 return; 353 } 354 if ( isset( $_POST['billing_phone'] ) && $this->validateNumber( sanitize_text_field( $_POST['billing_phone'] ) ) ) { 362 public function register_the_customer($customer_id) 363 { 364 if (!$this->pluginActive || (!$this->options['wp_reg'] && !$this->options['wc_reg'])) { 365 return; 366 } 367 if (isset($_POST['billing_phone']) && $this->validateNumber(sanitize_text_field($_POST['billing_phone']))) { 355 368 update_user_meta( 356 369 $customer_id, 357 370 'billing_phone', 358 sanitize_text_field( $this->validateNumber( $_POST['billing_phone'] ))371 sanitize_text_field($this->validateNumber($_POST['billing_phone'])) 359 372 ); 360 373 } … … 365 378 * show phone number on register page 366 379 */ 367 public function wp_phone_on_register() { 368 if ( ! $this->pluginActive || ! $this->options['wp_reg'] ) { 369 return; 370 } 371 372 $billing_phone = ( ! empty( $_POST['billing_phone'] ) ) ? sanitize_text_field( $_POST['billing_phone'] ) : ''; 373 374 ?> 375 <p> 376 <label for="billing_phone"><?php _e( 'Phone', $this->plugin_name ) ?><br/> 377 <input type="text" name="billing_phone" id="reg_billing_phone" class="input" value="<?php echo esc_attr( $billing_phone ); ?>" size="25"/></label> 378 </p> 379 <?php 380 public function wp_phone_on_register() 381 { 382 if (!$this->pluginActive || !$this->options['wp_reg']) { 383 return; 384 } 385 386 $billing_phone = (!empty($_POST['billing_phone'])) ? sanitize_text_field($_POST['billing_phone']) : ''; 387 388 ?> 389 <p> 390 <label for="billing_phone"><?php _e('Phone', $this->plugin_name) ?><br /> 391 <input type="text" name="billing_phone" id="reg_billing_phone" class="input" value="<?php echo esc_attr($billing_phone); ?>" size="25" /></label> 392 </p> 393 <?php 380 394 } 381 395 … … 389 403 * @return mixed 390 404 */ 391 public function wp_register_form_validation( $errors, $sanitized_user_login, $user_email ) { 405 public function wp_register_form_validation($errors, $sanitized_user_login, $user_email) 406 { 392 407 if ( 393 $this->pluginActive && $this->options['wp_reg'] && ! empty( $_POST['action_type']) &&408 $this->pluginActive && $this->options['wp_reg'] && !empty($_POST['action_type']) && 394 409 $_POST['action_type'] === 'wp_reg' 395 410 ) { 396 $this->register_form_validation( $errors, $sanitized_user_login, $user_email);411 $this->register_form_validation($errors, $sanitized_user_login, $user_email); 397 412 } 398 413 … … 409 424 * @return mixed 410 425 */ 411 public function register_form_validation( $errors, $sanitized_user_login, $user_email ) { 412 413 $enable_guest_checkout = get_option( 'woocommerce_enable_guest_checkout' ); 426 public function register_form_validation($errors, $sanitized_user_login, $user_email) 427 { 428 429 $enable_guest_checkout = get_option('woocommerce_enable_guest_checkout'); 414 430 $enable_guest_checkout = $enable_guest_checkout === 'yes' ? true : false; 415 431 416 if ($enable_guest_checkout){432 if (!$this->pluginActive || !$this->options['otp_checkout'] || $enable_guest_checkout) { 417 433 return $errors; 418 434 } 419 435 420 436 if ( 421 empty( $_REQUEST['billing_phone'] ) || ! is_numeric( $_REQUEST['billing_phone']) ||422 ! $this->validateNumber( sanitize_text_field( $_REQUEST['billing_phone'] ))437 empty($_REQUEST['billing_phone']) || !is_numeric($_REQUEST['billing_phone']) || 438 !$this->validateNumber(sanitize_text_field($_REQUEST['billing_phone'])) 423 439 ) { 424 $errors->add( 'phone_error', __( 'You phone number is not valid.', $this->plugin_name ));425 } 426 427 $billing_phone = $this->validateNumber( sanitize_text_field( $_REQUEST['billing_phone'] ));428 429 $hasPhoneNumber = get_users( 'meta_value=' . $billing_phone);430 431 if ( ! empty( $hasPhoneNumber )) {432 $errors->add( 'duplicate_phone_error', __( 'Mobile number is already used!', $this->plugin_name ));433 } 434 435 if ( ! empty( $_REQUEST['otp_code'] )) {436 $otp_code = sanitize_text_field( $_REQUEST['otp_code']);437 438 $valid_user = $this->authenticate_otp( trim( $otp_code ));439 440 if ( $valid_user) {440 $errors->add('phone_error', __('You phone number is not valid.', $this->plugin_name)); 441 } 442 443 $billing_phone = $this->validateNumber(sanitize_text_field($_REQUEST['billing_phone'])); 444 445 $hasPhoneNumber = get_users('meta_value=' . $billing_phone); 446 447 if (!empty($hasPhoneNumber)) { 448 $errors->add('duplicate_phone_error', __('Mobile number is already used!', $this->plugin_name)); 449 } 450 451 if (!empty($_REQUEST['otp_code'])) { 452 $otp_code = sanitize_text_field($_REQUEST['otp_code']); 453 454 $valid_user = $this->authenticate_otp(trim($otp_code)); 455 456 if ($valid_user) { 441 457 $this->deletePastData(); 442 458 … … 447 463 448 464 // otp validation failed or no otp provided 449 $errors->add( 'otp_error', __( 'Invalid OTP entered!', $this->plugin_name ));465 $errors->add('otp_error', __('Invalid OTP entered!', $this->plugin_name)); 450 466 451 467 return $errors; 452 468 } 453 469 454 470 /** 455 471 * Validate guest checkout otp … … 461 477 * @return mixed 462 478 */ 463 public function validate_guest_checkout_otp(){ 464 465 $enable_guest_checkout = get_option( 'woocommerce_enable_guest_checkout' ); 479 public function validate_guest_checkout_otp() 480 { 481 482 $enable_guest_checkout = get_option('woocommerce_enable_guest_checkout'); 466 483 $enable_guest_checkout = $enable_guest_checkout === 'yes' ? true : false; 467 468 if (!$enable_guest_checkout){469 return; 470 } 471 472 if ( ! empty( $_REQUEST['otp_code'] )) {473 $otp_code = sanitize_text_field( $_REQUEST['otp_code']);474 475 $valid_user = $this->authenticate_otp( trim( $otp_code ));476 477 if ( $valid_user) {484 485 if (!$this->pluginActive || !$this->options['otp_checkout'] || !$enable_guest_checkout) { 486 return; 487 } 488 489 if (!empty($_REQUEST['otp_code'])) { 490 $otp_code = sanitize_text_field($_REQUEST['otp_code']); 491 492 $valid_user = $this->authenticate_otp(trim($otp_code)); 493 494 if ($valid_user) { 478 495 $this->deletePastData(); 479 496 } else { 480 wc_add_notice( __( 'Please enter a valid OTP.', 'woocommerce' ), 'error');497 wc_add_notice(__('Please enter a valid OTP.', 'woocommerce'), 'error'); 481 498 } 482 499 } else { 483 wc_add_notice( __( 'Please enter a valid OTP.', 'woocommerce' ), 'error');484 } 485 486 } 500 wc_add_notice(__('Please enter a valid OTP.', 'woocommerce'), 'error'); 501 } 502 } 503 487 504 488 505 /** … … 493 510 * @return bool 494 511 */ 495 public function authenticate_otp( $otp_code ) { 496 497 498 if ( ! empty( $_SESSION['alpha_sms_otp_code'] ) && ! empty( $_SESSION['alpha_sms_expires'] ) ) { 499 500 if ( strtotime( $_SESSION['alpha_sms_expires'] ) > strtotime( ALPHA_SMS_TIMESTAMP ) ) { 501 if ( $otp_code === $_SESSION['alpha_sms_otp_code'] ) { 512 public function authenticate_otp($otp_code) 513 { 514 515 516 if (!empty($_SESSION['alpha_sms_otp_code']) && !empty($_SESSION['alpha_sms_expires'])) { 517 518 if (strtotime($_SESSION['alpha_sms_expires']) > strtotime(ALPHA_SMS_TIMESTAMP)) { 519 if ($otp_code === $_SESSION['alpha_sms_otp_code']) { 502 520 return true; 503 521 } 504 522 } 505 506 523 } 507 524 … … 513 530 * 514 531 */ 515 public function deletePastData() { 516 if ( isset( $_SESSION['alpha_sms_otp_code'], $_SESSION['alpha_sms_expires'] ) ) { 517 unset( $_SESSION['alpha_sms_otp_code'], $_SESSION['alpha_sms_expires'] ); 532 public function deletePastData() 533 { 534 if (isset($_SESSION['alpha_sms_otp_code'], $_SESSION['alpha_sms_expires'])) { 535 unset($_SESSION['alpha_sms_otp_code'], $_SESSION['alpha_sms_expires']); 518 536 } 519 537 } … … 528 546 * @return mixed 529 547 */ 530 public function wc_register_form_validation( $errors, $sanitized_user_login, $user_email ) { 531 if ( ! $this->pluginActive ) { 548 public function wc_register_form_validation($errors, $sanitized_user_login, $user_email) 549 { 550 if (!$this->pluginActive) { 532 551 return $errors; 533 552 } 534 553 535 if ( $this->options['otp_checkout'] || ( $this->options['wc_reg'] && $_POST['action_type'] === 'wc_reg' )) {536 $this->register_form_validation( $errors, $sanitized_user_login, $user_email);554 if ($this->options['otp_checkout'] || ($this->options['wc_reg'] && $_POST['action_type'] === 'wc_reg')) { 555 $this->register_form_validation($errors, $sanitized_user_login, $user_email); 537 556 } 538 557 … … 545 564 * @param $order_id 546 565 */ 547 public function wc_new_order_alert( $order_id ) { 548 if ( ! $order_id ) { 566 public function wc_new_order_alert($order_id) 567 { 568 if (!$order_id) { 549 569 return; 550 570 } 551 571 552 572 // option not enabled 553 if ( ! $this->pluginActive || ! isset( $this->options['order_status_admin'] ) || ! $this->options['order_status_admin']) {573 if (!$this->pluginActive || !isset($this->options['order_status_admin']) || !$this->options['order_status_admin']) { 554 574 return; 555 575 } … … 558 578 559 579 // send sms to all admins if enabled 560 $order = new WC_Order( $order_id);580 $order = new WC_Order($order_id); 561 581 562 582 $admin_msg = $this->options['ADMIN_STATUS_SMS']; … … 580 600 ]; 581 601 582 $admin_msg = str_replace( $search, $replace, $admin_msg);602 $admin_msg = str_replace($search, $replace, $admin_msg); 583 603 584 604 // if admin phone is not provided then send to all admins 585 605 $admin_phones[] = $this->options['admin_phones']; 586 606 587 if ( empty( $admin_phones )) {607 if (empty($admin_phones)) { 588 608 $admin_phones = $this->admin_phones(); 589 609 } 590 610 591 if ( ! empty( $admin_phones ) ) { 592 $numbers = implode( ',', $admin_phones ); 593 $this->SendSMS( $numbers, $admin_msg ); 594 } 595 611 if (!empty($admin_phones)) { 612 $numbers = implode(',', $admin_phones); 613 $this->SendSMS($numbers, $admin_msg); 614 } 596 615 } 597 616 … … 604 623 */ 605 624 606 public function wc_order_status_change_alert( $order_id, $old_status, $new_status ) { 607 if ( ! $order_id ) { 608 return; 609 } 610 611 $order = new WC_Order( $order_id ); 625 public function wc_order_status_change_alert($order_id, $old_status, $new_status) 626 { 627 if (!$order_id) { 628 return; 629 } 630 631 $order = new WC_Order($order_id); 612 632 613 633 // Get the Customer billing phone … … 615 635 616 636 //we will send sms 617 $status = str_replace( '-', '_', $order->data['status']);637 $status = str_replace('-', '_', $order->data['status']); 618 638 619 639 // option not enabled 620 640 if ( 621 ! $this->pluginActive || ! isset( $this->options[ 'order_status_' . $status ] ) || ! $this->options[ 'order_status_' . $status]641 !$this->pluginActive || !isset($this->options['order_status_' . $status]) || !$this->options['order_status_' . $status] 622 642 ) { 623 643 return; 624 644 } 625 645 626 $buyer_msg = ! empty( $this->options[ 'ORDER_STATUS_' . strtoupper( $status ) . '_SMS' ] ) ? $this->options[ 'ORDER_STATUS_' . strtoupper( $status ) . '_SMS'] : NULL;646 $buyer_msg = !empty($this->options['ORDER_STATUS_' . strtoupper($status) . '_SMS']) ? $this->options['ORDER_STATUS_' . strtoupper($status) . '_SMS'] : NULL; 627 647 628 648 $search = [ … … 644 664 ]; 645 665 646 $buyer_msg = str_replace( $search, $replace, $buyer_msg);647 648 if ( empty( $buyer_msg )) {649 $order->add_order_note( __( $this->plugin_name . ': Order message not found.', $this->plugin_name ));650 651 return; 652 } 653 654 $response = $this->SendSMS( $billing_phone, $buyer_msg);655 656 if ( $response->error === 0) {657 658 $order->add_order_note( __( 'Alpha SMS : Notified customer about his order ' . $order->data['status'] . ' status', $this->plugin_name ));666 $buyer_msg = str_replace($search, $replace, $buyer_msg); 667 668 if (empty($buyer_msg)) { 669 $order->add_order_note(__($this->plugin_name . ': Order message not found.', $this->plugin_name)); 670 671 return; 672 } 673 674 $response = $this->SendSMS($billing_phone, $buyer_msg); 675 676 if ($response->error === 0) { 677 678 $order->add_order_note(__('Alpha SMS : Notified customer about his order ' . $order->data['status'] . ' status', $this->plugin_name)); 659 679 } else { 660 $order->add_order_note( __( 'Alpha SMS : ' . $response->msg, $this->plugin_name ) ); 661 } 662 680 $order->add_order_note(__('Alpha SMS : ' . $response->msg, $this->plugin_name)); 681 } 663 682 } 664 683 … … 668 687 * @return array 669 688 */ 670 public function admin_phones() { 671 $admin_ids = get_users( [ 'fields' => 'ID', 'role' => 'administrator' ] ); 689 public function admin_phones() 690 { 691 $admin_ids = get_users(['fields' => 'ID', 'role' => 'administrator']); 672 692 $numbers = []; 673 foreach ( $admin_ids as $userid) {674 $number = $this->validateNumber( get_user_meta( $userid, 'mobile_phone', true ));675 if ( $number) {693 foreach ($admin_ids as $userid) { 694 $number = $this->validateNumber(get_user_meta($userid, 'mobile_phone', true)); 695 if ($number) { 676 696 $numbers[] = $number; 677 697 } … … 686 706 */ 687 707 688 public function login_enqueue_style() { 689 if ( $this->options['wp_login'] || $this->options['wp_reg'] ) { 708 public function login_enqueue_style() 709 { 710 if ($this->options['wp_login'] || $this->options['wp_reg']) { 690 711 wp_enqueue_style( 691 712 $this->plugin_name, 692 plugin_dir_url( __FILE__) . 'css/otp-login-form.css',713 plugin_dir_url(__FILE__) . 'css/otp-login-form.css', 693 714 [], 694 715 $this->version, … … 698 719 } 699 720 700 public function login_enqueue_script() { 701 if ( ! $this->pluginActive ) { 702 return; 703 } 704 705 if ( $this->options['wp_login'] || $this->options['wp_reg'] ) { 721 public function login_enqueue_script() 722 { 723 if (!$this->pluginActive) { 724 return; 725 } 726 727 if ($this->options['wp_login'] || $this->options['wp_reg']) { 706 728 wp_enqueue_script( 707 729 $this->plugin_name, 708 plugin_dir_url( __FILE__) . 'js/otp-login-form.js',709 [ 'jquery'],730 plugin_dir_url(__FILE__) . 'js/otp-login-form.js', 731 ['jquery'], 710 732 $this->version, 711 733 false … … 714 736 $this->plugin_name, 715 737 $this->plugin_name . '_object', 716 [ 'ajaxurl' => admin_url( 'admin-ajax.php' )]738 ['ajaxurl' => admin_url('admin-ajax.php')] 717 739 ); 718 740 } … … 723 745 * 724 746 */ 725 public function add_otp_field_in_wp_login_form() { 726 if ( ! $this->pluginActive || ! $this->options['wp_login'] ) { 747 public function add_otp_field_in_wp_login_form() 748 { 749 if (!$this->pluginActive || !$this->options['wp_login']) { 727 750 return; 728 751 } 729 752 730 753 require_once 'partials/add-otp-on-login-form.php'; 731 ?> 732 <input type='hidden' name='action_type' id='action_type' value='wp_login'/> 754 ?> 755 <input type='hidden' name='action_type' id='action_type' value='wp_login' /> 756 <?php 757 } 758 759 /** 760 * Add OTP view in Wc login form 761 * 762 */ 763 public function add_otp_field_in_wc_login_form() 764 { 765 if (!$this->pluginActive || !$this->options['wc_login']) { 766 return; 767 } 768 require_once 'partials/add-otp-on-login-form.php'; 769 ?> 770 <input type='hidden' name='action_type' id='action_type' value='wc_login' /> 733 771 <?php 734 772 } 735 773 736 774 /** 737 * Add OTP view in Wc login form738 *739 */740 public function add_otp_field_in_wc_login_form() {741 if ( ! $this->pluginActive || ! $this->options['wc_login'] ) {742 return;743 }744 require_once 'partials/add-otp-on-login-form.php';745 ?>746 <input type='hidden' name='action_type' id='action_type' value='wc_login'/>747 <?php748 }749 750 /**751 775 * Verify number and send otp 752 776 * 753 777 */ 754 public function save_and_send_otp_login() { 778 public function save_and_send_otp_login() 779 { 755 780 // First check the nonce, if it fails the function will break 756 check_ajax_referer( 'ajax-login-nonce', $this->plugin_name);781 check_ajax_referer('ajax-login-nonce', $this->plugin_name); 757 782 758 783 //Nonce is checked, get the POST data and sign user on 759 784 $info = []; 760 $info['user_login'] = sanitize_text_field( $_POST['log']);761 $info['user_password'] = sanitize_text_field( $_POST['pwd']);762 $info['remember'] = sanitize_text_field( $_POST['rememberme']);763 764 $userdata = get_user_by( 'login', $info['user_login']);765 766 if ( ! $userdata) {767 $userdata = get_user_by( 'email', $info['user_login']);785 $info['user_login'] = sanitize_text_field($_POST['log']); 786 $info['user_password'] = sanitize_text_field($_POST['pwd']); 787 $info['remember'] = sanitize_text_field($_POST['rememberme']); 788 789 $userdata = get_user_by('login', $info['user_login']); 790 791 if (!$userdata) { 792 $userdata = get_user_by('email', $info['user_login']); 768 793 } 769 794 // wp_authenticate() 770 795 $user_id = $userdata->data->ID; 771 796 772 $result = wp_check_password( $info['user_password'], $userdata->data->user_pass, $user_id);773 774 if ( ! $user_id || ! $result) {775 $response = [ 'status' => 401, 'message' => __( 'Wrong username or password!' )];776 echo wp_kses_post( json_encode( $response ));797 $result = wp_check_password($info['user_password'], $userdata->data->user_pass, $user_id); 798 799 if (!$user_id || !$result) { 800 $response = ['status' => 401, 'message' => __('Wrong username or password!')]; 801 echo wp_kses_post(json_encode($response)); 777 802 wp_die(); 778 803 exit; 779 804 } 780 805 781 $user_phone = get_user_meta( $user_id, 'mobile_phone', true);782 783 if ( ! $user_phone) {784 $user_phone = get_user_meta( $user_id, 'billing_phone', true);806 $user_phone = get_user_meta($user_id, 'mobile_phone', true); 807 808 if (!$user_phone) { 809 $user_phone = get_user_meta($user_id, 'billing_phone', true); 785 810 } 786 811 787 812 // if user phone number is not valid then login without verification 788 if ( ! $user_phone || ! $this->validateNumber( $user_phone )) {789 $response = [ 'status' => 402, 'message' => __( 'No phone number found' )];790 echo wp_kses_post( json_encode( $response ));813 if (!$user_phone || !$this->validateNumber($user_phone)) { 814 $response = ['status' => 402, 'message' => __('No phone number found')]; 815 echo wp_kses_post(json_encode($response)); 791 816 wp_die(); 792 817 exit; … … 799 824 $body = 'Your one time password for ' . get_bloginfo() . ' login is ' . $otp_code . ' . Only valid for 2 min.'; 800 825 801 $sms_response = $this->SendSMS( $number, $body);802 803 if ( $sms_response->error === 0) {826 $sms_response = $this->SendSMS($number, $body); 827 828 if ($sms_response->error === 0) { 804 829 // save info in database for later verification 805 $log_info = $this->log_login_register_action( $user_phone, $otp_code);806 807 if ( $log_info) {808 $response = [ 'status' => 200, 'message' => 'Please enter the verification code sent to your phone.'];830 $log_info = $this->log_login_register_action($user_phone, $otp_code); 831 832 if ($log_info) { 833 $response = ['status' => 200, 'message' => 'Please enter the verification code sent to your phone.']; 809 834 } else { 810 $response = [ 'status' => 500, 'message' => 'Something went wrong. Please try again.'];835 $response = ['status' => 500, 'message' => 'Something went wrong. Please try again.']; 811 836 } 812 837 813 echo wp_kses_post( json_encode( $response ));838 echo wp_kses_post(json_encode($response)); 814 839 exit; 815 840 } 816 841 817 $response = [ 'status' => '400', 'message' => 'Error sending Otp Code. Please contact administrator.'];818 echo wp_kses_post( json_encode( $response ));842 $response = ['status' => '400', 'message' => 'Error sending Otp Code. Please contact administrator.']; 843 echo wp_kses_post(json_encode($response)); 819 844 wp_die(); 820 845 exit; … … 829 854 * @return User|WP_Error 830 855 */ 831 public function login_user( $user, $username ) { 832 if ( empty( $user->data ) ) { 856 public function login_user($user, $username) 857 { 858 if (empty($user->data)) { 833 859 return $user; 834 860 } 835 if ( ! $this->pluginActive || ( ! $this->options['wp_login'] && ! $this->options['wc_login'] )) {861 if (!$this->pluginActive || (!$this->options['wp_login'] && !$this->options['wc_login'])) { 836 862 return $user; 837 863 } 838 864 839 if ( empty( $_POST['action_type'] )) {865 if (empty($_POST['action_type'])) { 840 866 $error = new WP_Error(); 841 867 842 868 $error->add( 843 869 'empty_password', 844 __( '<strong>Error</strong>: Authentication Error!', $this->plugin_name)870 __('<strong>Error</strong>: Authentication Error!', $this->plugin_name) 845 871 ); 846 872 } 847 873 848 if ( ( $this->options['wp_login'] && $_POST['action_type'] == 'wp_login') ||849 ( $this->options['wc_login'] && $_POST['action_type'] == 'wc_login')874 if (($this->options['wp_login'] && $_POST['action_type'] == 'wp_login') || 875 ($this->options['wc_login'] && $_POST['action_type'] == 'wc_login') 850 876 ) { 851 return $this->startOTPChallenge( $user, $username);877 return $this->startOTPChallenge($user, $username); 852 878 } 853 879 … … 861 887 * @return mixed|WP_Error 862 888 */ 863 public function startOTPChallenge( $user, $username ) { 864 $user_phone = get_user_meta( $user->data->ID, 'mobile_phone', true ); 865 866 if ( ! $user_phone ) { 867 $user_phone = get_user_meta( $user->data->ID, 'billing_phone', true ); 868 } 869 870 if ( ! $user_phone || ! $this->validateNumber( $user_phone ) ) { 889 public function startOTPChallenge($user, $username) 890 { 891 $user_phone = get_user_meta($user->data->ID, 'mobile_phone', true); 892 893 if (!$user_phone) { 894 $user_phone = get_user_meta($user->data->ID, 'billing_phone', true); 895 } 896 897 if (!$user_phone || !$this->validateNumber($user_phone)) { 871 898 return $user; 872 899 } 873 900 874 if ( empty( $_REQUEST['otp_code'] )) {901 if (empty($_REQUEST['otp_code'])) { 875 902 $error = new WP_Error(); 876 903 877 904 $error->add( 878 905 'empty_password', 879 __( '<strong>Error</strong>: Wrong OTP Code!', $this->plugin_name)906 __('<strong>Error</strong>: Wrong OTP Code!', $this->plugin_name) 880 907 ); 881 908 … … 883 910 } 884 911 885 $otp_code = sanitize_text_field( $_REQUEST['otp_code']);886 887 $valid_user = $this->authenticate_otp( $otp_code);888 889 if ( $valid_user) {912 $otp_code = sanitize_text_field($_REQUEST['otp_code']); 913 914 $valid_user = $this->authenticate_otp($otp_code); 915 916 if ($valid_user) { 890 917 $this->deletePastData(); 891 918 … … 895 922 return new WP_Error( 896 923 'invalid_password', 897 __( 'OTP is not valid', $this->plugin_name)924 __('OTP is not valid', $this->plugin_name) 898 925 ); 899 926 } … … 902 929 * Woocommerce otp form in checkout 903 930 */ 904 public function otp_form_at_checkout() { 905 if ( ! $this->pluginActive || ! $this->options['otp_checkout'] ) { 906 return; 907 } 908 909 if ( ! is_user_logged_in()) { 931 public function otp_form_at_checkout() 932 { 933 if (!$this->pluginActive || !$this->options['otp_checkout']) { 934 return; 935 } 936 937 if (!is_user_logged_in()) { 910 938 require_once 'partials/add-otp-checkout-form.php'; 911 ?>912 <input type='hidden' name='action_type' id='action_type' value='wc_checkout'/>913 <?php939 ?> 940 <input type='hidden' name='action_type' id='action_type' value='wc_checkout' /> 941 <?php 914 942 } 915 943 } … … 920 948 * @return bool 921 949 */ 922 private function checkAPI( $api_key ) { 950 private function checkAPI($api_key) 951 { 923 952 require_once ALPHA_SMS_PATH . 'includes/sms.class.php'; 924 953 925 $smsPortal = new AlphaSMS( $api_key);954 $smsPortal = new AlphaSMS($api_key); 926 955 927 956 $response = $smsPortal->getBalance();
Note: See TracChangeset
for help on using the changeset viewer.