Plugin Directory

Changeset 2874897


Ignore:
Timestamp:
03/05/2023 09:37:56 AM (3 years ago)
Author:
alphanetbd
Message:

updated the description image

Location:
alpha-sms
Files:
49 added
2 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • alpha-sms/trunk/README.txt

    r2872897 r2874897  
    55Tested up to: 6.1
    66Requires PHP: 5.6
    7 Stable tag: 1.0.5
     7Stable tag: 1.0.6
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
  • alpha-sms/trunk/alpha_sms.php

    r2872897 r2874897  
    1717 * Plugin URI:        https://sms.net.bd/plugins/wordpress
    1818 * Description:       WP 2FA Login. SMS OTP Verification for Registration and Login forms, WooCommerce SMS Notification for your shop orders.
    19  * Version:           1.0.5
     19 * Version:           1.0.6
    2020 * Author:            Alpha Net
    2121 * Author URI:        https://sms.net.bd/
     
    3636 * Rename this for your plugin and update it as you release new versions.
    3737 */
    38 define('ALPHA_SMS_VERSION', '1.0.4');
     38define('ALPHA_SMS_VERSION', '1.0.6');
    3939
    4040// plugin constants
  • alpha-sms/trunk/public/class-alpha_sms-public.php

    r2867587 r2874897  
    22
    33// If this file is called directly, abort.
    4 if ( ! defined( 'WPINC' ) ) {
     4if (!defined('WPINC')) {
    55    die;
    66}
     
    1616 * @author     Alpha Net Developer Team <support@alpha.net.bd>
    1717 */
    18 class Alpha_sms_Public {
     18class Alpha_sms_Public
     19{
    1920
    2021    /**
     
    4950     * @since    1.0.0
    5051     */
    51     public function __construct( $plugin_name, $version ) {
     52    public function __construct($plugin_name, $version)
     53    {
    5254        $this->plugin_name  = $plugin_name;
    5355        $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']);
    5658    }
    5759
     
    6163     * start session if not started
    6264     */
    63     public function start_session_wp() {
    64         if ( ! session_id() ) {
     65    public function start_session_wp()
     66    {
     67        if (!session_id()) {
    6568            session_start();
    6669        }
     
    7275     * @since    1.0.0
    7376     */
    74     public function enqueue_styles() {
     77    public function enqueue_styles()
     78    {
    7579        /**
    7680         * This function is provided for demonstration purposes only.
     
    8791        wp_enqueue_style(
    8892            $this->plugin_name,
    89             plugin_dir_url( __FILE__ ) . 'css/alpha_sms-public.css',
     93            plugin_dir_url(__FILE__) . 'css/alpha_sms-public.css',
    9094            [],
    9195            $this->version,
     
    99103     * @since    1.0.0
    100104     */
    101     public function enqueue_scripts() {
     105    public function enqueue_scripts()
     106    {
    102107        /**
    103108         * This function is provided for demonstration purposes only.
     
    114119        wp_enqueue_script(
    115120            $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'],
    118123            $this->version,
    119124            false
     
    124129            $this->plugin_name,
    125130            $this->plugin_name . '_object',
    126             [ 'ajaxurl' => admin_url( 'admin-ajax.php' ) ]
     131            ['ajaxurl' => admin_url('admin-ajax.php')]
    127132        );
    128133    }
     
    132137     * show phone number on register page and my account
    133138     */
    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']) {
    136142            return;
    137143        }
    138144
    139145        $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'])
    141147            : $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         <?php
     148?>
     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
    152158    }
    153159
     
    156162     * show otp form in registration form
    157163     */
    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']) {
    160167            return;
    161168        }
    162169        require_once 'partials/add-otp-on-login-form.php';
    163         ?>
    164         <input type='hidden' name='action_type' id='action_type' value='wp_reg'/>
    165         <?php
     170    ?>
     171        <input type='hidden' name='action_type' id='action_type' value='wp_reg' />
     172    <?php
    166173    }
    167174
     
    170177     * show otp form in registration form
    171178     */
    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']) {
    174182            return;
    175183        }
    176184
    177185        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         <?php
     186    ?>
     187        <input type='hidden' name='action_type' id='action_type' value='wc_reg' />
     188    <?php
    181189    }
    182190
     
    185193     * ajax otp send on post phone number *
    186194     */
    187     public function send_otp_for_reg() {
     195    public function send_otp_for_reg()
     196    {
    188197        $user_phone = $user_email = '';
    189198
    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));
    202211            wp_die();
    203212            exit;
    204213        }
    205214
    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));
    209218            wp_die();
    210219            exit;
    211220        }
    212221
    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));
    216225            wp_die();
    217226            exit;
     
    223232        $body = 'Your OTP for ' . get_bloginfo() . ' registration is ' . $otp_code . '. Valid for 2 min. Contact us if you need help.';
    224233
    225         if ( ! empty( $_POST['action_type'] ) && $_POST['action_type'] === 'wc_checkout' ) {
     234        if (!empty($_POST['action_type']) && $_POST['action_type'] === 'wc_checkout') {
    226235            $body = 'Your OTP for secure order checkout on ' . get_bloginfo() . ' is ' . $otp_code . '. Use it within 2 min to complete the checkout process.';
    227236        }
    228237
    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) {
    232241            // save info in database for later verification
    233             if ( $this->log_login_register_action(
     242            if ($this->log_login_register_action(
    234243                $user_phone,
    235244                $otp_code
    236             ) ) {
     245            )) {
    237246                $response = [
    238247                    'status'  => 200,
     
    240249                ];
    241250            } 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.')];
    243252            }
    244253
    245             echo wp_kses_post( json_encode( $response ) );
     254            echo wp_kses_post(json_encode($response));
    246255            wp_die();
    247256            exit;
    248257        }
    249258
    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));
    252261        wp_die();
    253262        exit;
     
    261270     * @return false|int|string
    262271     */
    263     public function validateNumber( $num ) {
    264         if ( ! $num ) {
     272    public function validateNumber($num)
     273    {
     274        if (!$num) {
    265275            return false;
    266276        }
    267277
    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)) {
    273283            return $number;
    274284        }
     
    282292     * @return string
    283293     */
    284     public function generateOTP() {
     294    public function generateOTP()
     295    {
    285296        $otp = '';
    286297
    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);
    289300        }
    290301
     
    300311     * @return false|mixed
    301312     */
    302     public function SendSMS( $to, $body ) {
    303         if ( ! $this->pluginActive ) {
     313    public function SendSMS($to, $body)
     314    {
     315        if (!$this->pluginActive) {
    304316            return false;
    305317        }
    306318
    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']) : '';
    309321
    310322        require_once ALPHA_SMS_PATH . 'includes/sms.class.php';
    311323
    312         $sms            = new AlphaSMS( $api_key );
     324        $sms            = new AlphaSMS($api_key);
    313325        $sms->numbers   = $to;
    314326        $sms->body      = $body;
     
    330342        $otp_code
    331343    ) {
    332         $dateTime = new DateTime( ALPHA_SMS_TIMESTAMP );
    333         $dateTime->modify( '+2 minutes' );
     344        $dateTime = new DateTime(ALPHA_SMS_TIMESTAMP);
     345        $dateTime->modify('+2 minutes');
    334346
    335347        $_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'])) {
    339351            return true;
    340352        }
     
    348360     * @param $customer_id
    349361     */
    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']))) {
    355368            update_user_meta(
    356369                $customer_id,
    357370                'billing_phone',
    358                 sanitize_text_field( $this->validateNumber( $_POST['billing_phone'] ) )
     371                sanitize_text_field($this->validateNumber($_POST['billing_phone']))
    359372            );
    360373        }
     
    365378     * show phone number on register page
    366379     */
    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
    380394    }
    381395
     
    389403     * @return mixed
    390404     */
    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    {
    392407        if (
    393             $this->pluginActive && $this->options['wp_reg'] && ! empty( $_POST['action_type'] ) &&
     408            $this->pluginActive && $this->options['wp_reg'] && !empty($_POST['action_type']) &&
    394409            $_POST['action_type'] === 'wp_reg'
    395410        ) {
    396             $this->register_form_validation( $errors, $sanitized_user_login, $user_email );
     411            $this->register_form_validation($errors, $sanitized_user_login, $user_email);
    397412        }
    398413
     
    409424     * @return mixed
    410425     */
    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');
    414430        $enable_guest_checkout = $enable_guest_checkout === 'yes' ? true : false;
    415431
    416         if($enable_guest_checkout){
     432        if (!$this->pluginActive || !$this->options['otp_checkout'] || $enable_guest_checkout) {
    417433            return $errors;
    418434        }
    419        
     435
    420436        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']))
    423439        ) {
    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) {
    441457                $this->deletePastData();
    442458
     
    447463
    448464        // 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));
    450466
    451467        return $errors;
    452468    }
    453    
     469
    454470    /**
    455471     * Validate guest checkout otp
     
    461477     * @return mixed
    462478     */
    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');
    466483        $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) {
    478495                $this->deletePastData();
    479496            } else {
    480                 wc_add_notice( __( 'Please enter a valid OTP.', 'woocommerce' ), 'error' );
     497                wc_add_notice(__('Please enter a valid OTP.', 'woocommerce'), 'error');
    481498            }
    482499        } 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
    487504
    488505    /**
     
    493510     * @return bool
    494511     */
    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']) {
    502520                    return true;
    503521                }
    504522            }
    505 
    506523        }
    507524
     
    513530     *
    514531     */
    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']);
    518536        }
    519537    }
     
    528546     * @return mixed
    529547     */
    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) {
    532551            return $errors;
    533552        }
    534553
    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);
    537556        }
    538557
     
    545564     * @param $order_id
    546565     */
    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) {
    549569            return;
    550570        }
    551571
    552572        // 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']) {
    554574            return;
    555575        }
     
    558578
    559579        // send sms to all admins if enabled
    560         $order = new WC_Order( $order_id );
     580        $order = new WC_Order($order_id);
    561581
    562582        $admin_msg = $this->options['ADMIN_STATUS_SMS'];
     
    580600        ];
    581601
    582         $admin_msg = str_replace( $search, $replace, $admin_msg );
     602        $admin_msg = str_replace($search, $replace, $admin_msg);
    583603
    584604        // if admin phone is not provided then send to all admins
    585605        $admin_phones[] = $this->options['admin_phones'];
    586606
    587         if ( empty( $admin_phones ) ) {
     607        if (empty($admin_phones)) {
    588608            $admin_phones = $this->admin_phones();
    589609        }
    590610
    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        }
    596615    }
    597616
     
    604623     */
    605624
    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);
    612632
    613633        // Get the Customer billing phone
     
    615635
    616636        //we will send sms
    617         $status = str_replace( '-', '_', $order->data['status'] );
     637        $status = str_replace('-', '_', $order->data['status']);
    618638
    619639        // option not enabled
    620640        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]
    622642        ) {
    623643            return;
    624644        }
    625645
    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;
    627647
    628648        $search = [
     
    644664        ];
    645665
    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));
    659679        } 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        }
    663682    }
    664683
     
    668687     * @return array
    669688     */
    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']);
    672692        $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) {
    676696                $numbers[] = $number;
    677697            }
     
    686706     */
    687707
    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']) {
    690711            wp_enqueue_style(
    691712                $this->plugin_name,
    692                 plugin_dir_url( __FILE__ ) . 'css/otp-login-form.css',
     713                plugin_dir_url(__FILE__) . 'css/otp-login-form.css',
    693714                [],
    694715                $this->version,
     
    698719    }
    699720
    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']) {
    706728            wp_enqueue_script(
    707729                $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'],
    710732                $this->version,
    711733                false
     
    714736                $this->plugin_name,
    715737                $this->plugin_name . '_object',
    716                 [ 'ajaxurl' => admin_url( 'admin-ajax.php' ) ]
     738                ['ajaxurl' => admin_url('admin-ajax.php')]
    717739            );
    718740        }
     
    723745     *
    724746     */
    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']) {
    727750            return;
    728751        }
    729752
    730753        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' />
    733771        <?php
    734772    }
    735773
    736774    /**
    737      * Add OTP view in Wc login form
    738      *
    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         <?php
    748     }
    749 
    750     /**
    751775     * Verify number and send otp
    752776     *
    753777     */
    754     public function save_and_send_otp_login() {
     778    public function save_and_send_otp_login()
     779    {
    755780        // 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);
    757782
    758783        //Nonce is checked, get the POST data and sign user on
    759784        $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']);
    768793        }
    769794        // wp_authenticate()
    770795        $user_id = $userdata->data->ID;
    771796
    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));
    777802            wp_die();
    778803            exit;
    779804        }
    780805
    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);
    785810        }
    786811
    787812        // 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));
    791816            wp_die();
    792817            exit;
     
    799824        $body   = 'Your one time password for ' . get_bloginfo() . ' login is ' . $otp_code . ' . Only valid for 2 min.';
    800825
    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) {
    804829            // 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.'];
    809834            } else {
    810                 $response = [ 'status' => 500, 'message' => 'Something went wrong. Please try again.' ];
     835                $response = ['status' => 500, 'message' => 'Something went wrong. Please try again.'];
    811836            }
    812837
    813             echo wp_kses_post( json_encode( $response ) );
     838            echo wp_kses_post(json_encode($response));
    814839            exit;
    815840        }
    816841
    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));
    819844        wp_die();
    820845        exit;
     
    829854     * @return User|WP_Error
    830855     */
    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)) {
    833859            return $user;
    834860        }
    835         if ( ! $this->pluginActive || ( ! $this->options['wp_login'] && ! $this->options['wc_login'] ) ) {
     861        if (!$this->pluginActive || (!$this->options['wp_login'] && !$this->options['wc_login'])) {
    836862            return $user;
    837863        }
    838864
    839         if ( empty( $_POST['action_type'] ) ) {
     865        if (empty($_POST['action_type'])) {
    840866            $error = new WP_Error();
    841867
    842868            $error->add(
    843869                'empty_password',
    844                 __( '<strong>Error</strong>: Authentication Error!', $this->plugin_name )
     870                __('<strong>Error</strong>: Authentication Error!', $this->plugin_name)
    845871            );
    846872        }
    847873
    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')
    850876        ) {
    851             return $this->startOTPChallenge( $user, $username );
     877            return $this->startOTPChallenge($user, $username);
    852878        }
    853879
     
    861887     * @return mixed|WP_Error
    862888     */
    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)) {
    871898            return $user;
    872899        }
    873900
    874         if ( empty( $_REQUEST['otp_code'] ) ) {
     901        if (empty($_REQUEST['otp_code'])) {
    875902            $error = new WP_Error();
    876903
    877904            $error->add(
    878905                'empty_password',
    879                 __( '<strong>Error</strong>: Wrong OTP Code!', $this->plugin_name )
     906                __('<strong>Error</strong>: Wrong OTP Code!', $this->plugin_name)
    880907            );
    881908
     
    883910        }
    884911
    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) {
    890917            $this->deletePastData();
    891918
     
    895922        return new WP_Error(
    896923            'invalid_password',
    897             __( 'OTP is not valid', $this->plugin_name )
     924            __('OTP is not valid', $this->plugin_name)
    898925        );
    899926    }
     
    902929     * Woocommerce otp form in checkout
    903930     */
    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()) {
    910938            require_once 'partials/add-otp-checkout-form.php';
    911             ?>
    912             <input type='hidden' name='action_type' id='action_type' value='wc_checkout'/>
    913             <?php
     939        ?>
     940            <input type='hidden' name='action_type' id='action_type' value='wc_checkout' />
     941<?php
    914942        }
    915943    }
     
    920948     * @return bool
    921949     */
    922     private function checkAPI( $api_key ) {
     950    private function checkAPI($api_key)
     951    {
    923952        require_once ALPHA_SMS_PATH . 'includes/sms.class.php';
    924953
    925         $smsPortal = new AlphaSMS( $api_key );
     954        $smsPortal = new AlphaSMS($api_key);
    926955
    927956        $response = $smsPortal->getBalance();
Note: See TracChangeset for help on using the changeset viewer.