Plugin Directory

Changeset 3424529


Ignore:
Timestamp:
12/21/2025 08:57:56 AM (3 months ago)
Author:
alphanetbd
Message:
  • Bump version number to 1.0.15 in class-alpha_sms.php
  • Add conditional checks for plugin activation before enqueuing styles and scripts in class-alpha_sms-public.php
  • Implement nonce verification for WooCommerce and WordPress registration processes in send_otp_for_reg method
  • Improve error messages for security checks and validation failures
  • Modify AJAX request handling to include action type and relevant nonces for better security
  • Update JavaScript to pass action type and checkout nonce during OTP requests
  • Refactor code for better readability and maintainability
Location:
alpha-sms
Files:
40 added
1 deleted
7 edited

Legend:

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

    r3415282 r3424529  
    55Tested up to: 6.9
    66Requires PHP: 5.6
    7 Stable tag: 1.0.14
     7Stable tag: 1.0.15
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1414
    1515= Overview =
    16 Alpha SMS makes it easy to add SMS-based two-factor authentication and transactional notifications to your WordPress site. Replace unreliable email-based logins with one-time passwords, confirm customer phone numbers during registration, and keep shoppers updated with automated WooCommerce order status messages.
     16Alpha SMS makes it easy to add SMS-based two-factor authentication and transactional notifications to your WordPress site. Replace unreliable email-based logins with one-time passwords, confirm customer phone numbers during registration, and keep shoppers updated with automated WooCommerce order status messages that are verified in order notes.
    1717
    1818= Key Features =
  • alpha-sms/trunk/admin/class-alpha_sms-admin.php

    r3415282 r3424529  
    282282        if (!$this->checkAPI($options['api_key'])) {
    283283
    284             $options['order_status'] =
    285                 $options['wp_reg'] =
     284            $options['wp_reg'] =
    286285                $options['wp_login'] =
    287286                $options['wc_reg'] =
     
    296295                $options['order_status_failed'] =
    297296                $options['order_status_admin'] = 0;
     297
     298            $options['api_key'] = '';
    298299
    299300            add_settings_error(
  • alpha-sms/trunk/admin/partials/alpha_sms-admin-display_settings.php

    r3415322 r3424529  
    145145                </th>
    146146                <td>
    147                     <input id="<?php echo esc_attr($this->plugin_name . '-api_key'); ?>" name="<?php echo esc_attr($this->plugin_name . '[api_key]'); ?>" type="text" size="55" placeholder="Enter API Key" value="<?php if (!empty($alpha_sms_api_key)) {
    148                                                                                                                                                                                                                         echo esc_attr($alpha_sms_api_key);
    149                                                                                                                                                                                                                     } ?>" />
     147                    <input id="<?php echo esc_attr($this->plugin_name . '-api_key'); ?>" name="<?php echo esc_attr($this->plugin_name . '[api_key]'); ?>" type="text" size="55" placeholder="Enter API Key" value="<?php if (!empty($alpha_sms_api_key)) { echo esc_attr($alpha_sms_api_key); } ?>" />
    150148                </td>
    151149            </tr>
     
    235233
    236234                <li>
    237                     <input class="alpha-collapse" type="checkbox" id="<?php echo esc_attr($this->plugin_name . '-order_status_admin'); ?>" name="<?php echo esc_attr($this->plugin_name . '[order_status_admin]'); ?>" <?php checked(
    238                                                                                                                                                                                                                             $alpha_sms_order_status_admin,
    239                                                                                                                                                                                                                             1
    240                                                                                                                                                                                                                         ); ?> />
     235                    <input class="alpha-collapse" type="checkbox" id="<?php echo esc_attr($this->plugin_name . '-order_status_admin'); ?>" name="<?php echo esc_attr($this->plugin_name . '[order_status_admin]'); ?>" <?php checked($alpha_sms_order_status_admin, 1); ?> />
    241236                    <label for="<?php echo esc_attr($this->plugin_name . '-order_status_admin'); ?>">
    242237                        <span class="toggle_btn"></span>
     
    254249                                    </label>
    255250                                </h4>
    256                                 <input id="<?php echo esc_attr($this->plugin_name . '-admin_phones'); ?>" name="<?php echo esc_attr($this->plugin_name . '[admin_phones]'); ?>" type="text" size="82" class="mb-2" value="<?php echo esc_attr($alpha_sms_admin_phones); ?>" />
     251                                <input id="<?php echo esc_attr($this->plugin_name . '-admin_phones'); ?>" name="<?php echo esc_attr($this->plugin_name . '[admin_phones]'); ?>" type="text" size="82" class="mb-2" value="<?php echo esc_attr($alpha_sms_admin_phones, 'alpha-sms'); ?>" />
    257252                                <span class="my-2 d-block sms_tokens"><span>[store_name]</span> |
    258253                                    <span>[billing_first_name]</span> |
  • alpha-sms/trunk/alpha_sms.php

    r3415282 r3424529  
    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.14
     19 * Version:           1.0.15
    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.14');
     38define('ALPHA_SMS_VERSION', '1.0.15');
    3939
    4040// plugin constants
  • alpha-sms/trunk/includes/class-alpha_sms.php

    r3388248 r3424529  
    7777            $this->version = ALPHA_SMS_VERSION;
    7878        } else {
    79             $this->version = '1.0.13';
     79            $this->version = '1.0.15';
    8080        }
    8181        $this->plugin_name = 'alpha_sms';
  • alpha-sms/trunk/public/class-alpha_sms-public.php

    r3415322 r3424529  
    9898         */
    9999
    100         wp_enqueue_style(
    101             $this->plugin_name,
    102             plugin_dir_url(__FILE__) . 'css/alpha_sms-public.css',
    103             [],
    104             $this->version,
    105             'all'
    106         );
     100        if ($this->pluginActive) {
     101            wp_enqueue_style(
     102                $this->plugin_name,
     103                plugin_dir_url(__FILE__) . 'css/alpha_sms-public.css',
     104                [],
     105                $this->version,
     106                'all'
     107            );
     108        }
    107109    }
    108110
     
    126128         */
    127129
    128         wp_enqueue_script(
    129             $this->plugin_name,
    130             plugin_dir_url(__FILE__) . 'js/alpha_sms-public.js',
    131             ['jquery'],
    132             $this->version,
    133             false
    134         );
    135 
    136         // adding a js variable for ajax form submit url
    137         wp_localize_script(
    138             $this->plugin_name,
    139             $this->plugin_name . '_object',
    140             ['ajaxurl' => admin_url('admin-ajax.php')]
    141         );
     130        if ($this->pluginActive) {
     131            wp_enqueue_script(
     132                $this->plugin_name,
     133                plugin_dir_url(__FILE__) . 'js/alpha_sms-public.js',
     134                ['jquery'],
     135                $this->version,
     136                false
     137            );
     138
     139            // adding a js variable for ajax form submit url
     140            wp_localize_script(
     141                $this->plugin_name,
     142                $this->plugin_name . '_object',
     143                [
     144                    'ajaxurl' => admin_url('admin-ajax.php'),
     145                    $this->plugin_name . '_checkout_nonce' => wp_create_nonce('woocommerce-process-checkout-nonce'),
     146                    'checkout_otp' => ! empty($this->options['otp_checkout']) ? 'yes' : 'no',
     147                ]
     148            );
     149        }
    142150    }
    143151
     
    187195        }
    188196        require_once 'partials/add-otp-on-login-form.php';
    189     ?>
    190         <input type='hidden' name='action_type' id='action_type' value='wp_reg' />
    191         <label for="reg_billing_phone">
    192             <?php esc_html_e('Phone', 'alpha-sms'); ?> <span class="required">*</span>
    193         </label>
    194     <?php
    195197    }
    196198
     
    218220    {
    219221        $user_phone = '';
    220 
    221         // AJAX nonce verification for WooCommerce registration
    222         // Verify nonce for WooCommerce registration AJAX request
    223         $wc_reg_phone_nonce = isset($_POST['wc_reg_phone_nonce']) ? sanitize_text_field(wp_unslash($_POST['wc_reg_phone_nonce'])) : '';
    224         if (empty($wc_reg_phone_nonce) || !wp_verify_nonce($wc_reg_phone_nonce, 'wc_reg_phone_action')) {
     222        // Require and validate nonce for AJAX requests. Fail early if missing/invalid.
     223        $action_type = isset($_POST['action_type']) ? sanitize_text_field(wp_unslash($_POST['action_type'])) : '';
     224        $nonce_ok = false;
     225
     226        // WC registration nonce
     227        if ($action_type === 'wc_reg') {
     228            $wc_reg_phone_nonce = isset($_POST['wc_reg_phone_nonce']) ? sanitize_text_field(wp_unslash($_POST['wc_reg_phone_nonce'])) : '';
     229            if (empty($wc_reg_phone_nonce) || ! wp_verify_nonce($wc_reg_phone_nonce, 'wc_reg_phone_action')) {
     230                $response = [
     231                    'status'  => 403,
     232                    'message' => __('Security Check failed. Please reload the page and try again.', 'alpha-sms'),
     233                ];
     234                echo wp_kses_post(json_encode($response));
     235                wp_die();
     236                exit;
     237            }
     238            $nonce_ok = true;
     239        }
     240
     241        // WP registration nonce
     242        if ($action_type === 'wp_reg') {
     243            $wp_reg_phone_nonce = isset($_POST['wp_reg_phone_nonce']) ? sanitize_text_field(wp_unslash($_POST['wp_reg_phone_nonce'])) : '';
     244            if (empty($wp_reg_phone_nonce) || ! wp_verify_nonce($wp_reg_phone_nonce, 'wp_reg_phone_action')) {
     245                $response = [
     246                    'status'  => 403,
     247                    'message' => __('Security Check failed. Please reload the page and try again.', 'alpha-sms'),
     248                ];
     249                echo wp_kses_post(json_encode($response));
     250                wp_die();
     251                exit;
     252            }
     253            $nonce_ok = true;
     254        }
     255
     256        // Guest checkout / other actions that rely on WooCommerce checkout nonce
     257        if ($action_type === 'wc_checkout') {
     258            $wc_checkout_nonce = isset($_POST['woocommerce-process-checkout-nonce']) ? sanitize_text_field(wp_unslash($_POST['woocommerce-process-checkout-nonce'])) : '';
     259            if (empty($wc_checkout_nonce) || ! wp_verify_nonce($wc_checkout_nonce, 'woocommerce-process-checkout-nonce')) {
     260                $response = [
     261                    'status'  => 403,
     262                    'message' => __('Security Check failed. Please reload the page and try again.', 'alpha-sms'),
     263                ];
     264                echo wp_kses_post(json_encode($response));
     265                wp_die();
     266                exit;
     267            }
     268            $nonce_ok = true;
     269        }
     270
     271        // If action_type is missing or not recognized we cannot safely continue.
     272        if (! $nonce_ok) {
    225273            $response = [
    226             'status' => 403,
    227             'message' => __('Security check failed. Please reload the page and try again.', 'alpha-sms')
     274                'status' => 403,
     275                'message' => __('Security Check failed. Missing or invalid action type.', 'alpha-sms'),
    228276            ];
    229277            echo wp_kses_post(json_encode($response));
     
    254302                'status' => 400,
    255303                /* translators: Error message shown when phone number is not valid. */
    256                 'message' => __('Please enter a valid 11-digit Bangladeshi phone number.', 'alpha-sms')
     304                'message' => __('The phone number you entered is not valid!', 'alpha-sms')
    257305            ];
    258306            echo wp_kses_post(json_encode($response));
     
    263311        $is_checkout_request = ! empty($_POST['action_type']) && $_POST['action_type'] === 'wc_checkout';
    264312
    265         if ($is_checkout_request && ! is_user_logged_in() && $this->is_checkout_rate_limited()) {
     313        if ($is_checkout_request && $this->is_checkout_rate_limited()) {
    266314            $response = [
    267315                'status'  => 429,
     
    282330            $response = [
    283331                'status'  => 400,
    284                 'message' => sprintf(__('OTP already sent to a phone number. Please try again after %s.', 'alpha-sms'), gmdate('i:s', $otp_expires_ts - $current_utc)),
     332                'message' => 'OTP already sent to a phone number. Please try again after ' . gmdate('i:s', $otp_expires_ts - $current_utc) . ' min',
    285333            ];
    286334            echo wp_kses_post(json_encode($response));
     
    320368            echo wp_kses_post(json_encode($response));
    321369            wp_die();
    322             $response = ['status' => 403, 'message' => __('Security check failed. Please reload the page and try again.', 'alpha-sms')];
     370            $response = ['status' => 403, 'message' => __('Security Check failed. Please reload the page and try again.', 'alpha-sms')];
    323371            /* translators: Error message shown when security check fails during OTP send. */
    324372        }
     
    332380    }
    333381
    334     /**
    335      * Validates and formats a Bangladeshi phone number.
    336      *
    337      * @param string $num The phone number to validate.
    338      * @return false|string Returns the formatted phone number as a string if valid, or false if invalid.
     382    /*
     383    * $response = ['status' => 403, 'message' => __('Security Check failed. Please reload the page and try again.', 'alpha-sms')];
     384     *
     385     * @param $num
     386     *
     387     * @return false|int|string
    339388     */
    340389    public function validateNumber($num)
     
    357406    /**
    358407     * Generate 6 digit otp code
    359      * Generates a 6-digit OTP (One Time Password) code.
    360      *
    361      * @return string The generated 6-digit OTP code.
     408     * $response = ['status' => 400, 'message' => __('The phone number you entered is not valid!', 'alpha-sms')];
     409     */
    362410    public function generateOTP()
    363411    {
     
    569617        }
    570618
    571         // Nonce validation for WooCommerce registration phone field
    572         if (
    573             isset($_POST['wc_reg_phone_nonce']) &&
    574             ! empty($_POST['wc_reg_phone_nonce']) &&
    575             function_exists('wp_verify_nonce') &&
    576             ! wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['wc_reg_phone_nonce'])), 'wc_reg_phone_action')
    577         ) {
    578             if (function_exists('wc_add_notice')) {
    579                 wc_add_notice(__('Security check failed. Please try again.', 'alpha-sms'), 'error');
    580             } else {
    581                 echo esc_html(__('Security check failed. Please try again.', 'alpha-sms'));
    582             }
    583             return;
     619        // Nonce validation for WooCommerce registration phone field: require nonce when wc_reg option enabled
     620        if (! empty($this->options['wc_reg'])) {
     621            $wc_reg_phone_nonce = isset($_POST['wc_reg_phone_nonce']) ? sanitize_text_field(wp_unslash($_POST['wc_reg_phone_nonce'])) : '';
     622            if (empty($wc_reg_phone_nonce) || ! function_exists('wp_verify_nonce') || ! wp_verify_nonce($wc_reg_phone_nonce, 'wc_reg_phone_action')) {
     623                if (function_exists('wc_add_notice')) {
     624                    wc_add_notice(__('Security Check failed. Please try again.', 'alpha-sms'), 'error');
     625                } else {
     626                    echo esc_html(__('Security Check failed. Please try again.', 'alpha-sms'));
     627                }
     628                return;
     629            }
     630        }
     631
     632        // Nonce validation for WP registration phone field: require nonce when wp_reg option enabled
     633        if (! empty($this->options['wp_reg'])) {
     634            $wp_reg_phone_nonce = isset($_POST['wp_reg_phone_nonce']) ? sanitize_text_field(wp_unslash($_POST['wp_reg_phone_nonce'])) : '';
     635            if (empty($wp_reg_phone_nonce) || ! function_exists('wp_verify_nonce') || ! wp_verify_nonce($wp_reg_phone_nonce, 'wp_reg_phone_action')) {
     636                if (function_exists('add_filter')) {
     637                    add_filter('registration_errors', function ($errors) {
     638                        $errors->add('security_error', __('Security Check failed. Please try again.', 'alpha-sms'));
     639                        return $errors;
     640                    });
     641                } else {
     642                    echo esc_html(__('Security Check failed. Please try again.', 'alpha-sms'));
     643                }
     644                return;
     645            }
    584646        }
    585647
     
    645707            $wp_reg_phone_nonce = isset($_POST['wp_reg_phone_nonce']) ? sanitize_text_field(wp_unslash($_POST['wp_reg_phone_nonce'])) : '';
    646708            if (empty($wp_reg_phone_nonce) || ! wp_verify_nonce($wp_reg_phone_nonce, 'wp_reg_phone_action')) {
    647                 $errors->add('security_error', __('Security check failed. Please try again.', 'alpha-sms'));
     709                $errors->add('security_error', __('Security Check failed. Please try again.', 'alpha-sms'));
    648710                return $errors;
    649711            }
     
    681743            $wp_reg_phone_nonce = isset($_POST['wp_reg_phone_nonce']) ? sanitize_text_field(wp_unslash($_POST['wp_reg_phone_nonce'])) : '';
    682744            if (empty($wp_reg_phone_nonce) || ! wp_verify_nonce($wp_reg_phone_nonce, 'wp_reg_phone_action')) {
    683                 $errors->add('security_error', __('Security check failed. Please try again.', 'alpha-sms'));
     745                $errors->add('security_error', __('Security Check failed. Please try again.', 'alpha-sms'));
    684746                return $errors;
    685747            }
     
    749811        // Nonce verification for guest checkout OTP
    750812        $wc_checkout_otp_nonce = isset($_POST['woocommerce-process-checkout-nonce']) ? sanitize_text_field(wp_unslash($_POST['woocommerce-process-checkout-nonce'])) : '';
    751         if (empty($wc_checkout_otp_nonce) || ! wp_verify_nonce($wc_checkout_otp_nonce, 'woocommerce-process_checkout')) {
    752             wc_add_notice(__('Security check failed. Please try again.', 'alpha-sms'), 'error');
     813        if (empty($wc_checkout_otp_nonce) || ! wp_verify_nonce($wc_checkout_otp_nonce, 'woocommerce-process-checkout-nonce')) {
     814            wc_add_notice(__('Security Check failed. Please try again.', 'alpha-sms'), 'error');
    753815            return;
    754816        }
     
    762824                $this->deletePastData();
    763825            } else {
     826                /* translators: Error message shown when user must enter a valid OTP. */
    764827                wc_add_notice(__('Please enter a valid OTP.', 'alpha-sms'), 'error');
    765828            }
     
    9621025            $wc_reg_phone_nonce = isset($_POST['wc_reg_phone_nonce']) ? sanitize_text_field(wp_unslash($_POST['wc_reg_phone_nonce'])) : '';
    9631026            if (empty($wc_reg_phone_nonce) || !wp_verify_nonce($wc_reg_phone_nonce, 'wc_reg_phone_action')) {
    964                 $errors->add('security_error', __('Security check failed. Please try again.', 'alpha-sms'));
     1027                $errors->add('security_error', __('Security Check failed. Please try again.', 'alpha-sms'));
    9651028                return $errors;
    9661029            }
     
    10951158
    10961159        $buyer_msg = str_replace($search, $replace, $buyer_msg);
     1160        $buyer_msg .= ' Order notifications are verified in your order notes.';
    10971161
    10981162        if (empty($buyer_msg)) {
     
    11051169
    11061170        if ($response->error === 0) {
    1107 
    1108             $order->add_order_note(__('Alpha SMS : Notified customer about his order status', 'alpha-sms'));
     1171            $order->add_order_note(__('Alpha SMS : Notified customer about order status ' . $new_status, 'alpha-sms'));
    11091172        } else {
    11101173            $order->add_order_note('Alpha SMS : ' . $response->msg);
     
    13731436        }
    13741437    }
     1438   
    13751439}
  • alpha-sms/trunk/public/js/alpha_sms-public.js

    r3415282 r3424529  
    123123   let password = wc_reg_form.find('#reg_password').val();
    124124   let wc_reg_phone_nonce = wc_reg_form.find('#wc_reg_phone_nonce').val();
     125   let action_type = wc_reg_form.find('#action_type').val();
    125126
    126127   if (!phone || !email) {
     
    141142      email: email,
    142143      wc_reg_phone_nonce: wc_reg_phone_nonce,
     144      action_type: action_type,
    143145   };
    144146
     
    211213      action_type: checkout_form.find('#action_type').val(),
    212214   };
     215   // include checkout nonce if present
     216   const checkoutNonceField = checkout_form.find('input[name="woocommerce-process-checkout-nonce"]');
     217   if (checkoutNonceField && checkoutNonceField.length) {
     218      data['woocommerce-process-checkout-nonce'] = checkoutNonceField.val();
     219   }
    213220
    214221   $.post(
Note: See TracChangeset for help on using the changeset viewer.