Plugin Directory

Changeset 3362984


Ignore:
Timestamp:
09/17/2025 08:21:58 AM (7 months ago)
Author:
alphanetbd
Message:

1.0.12

  • Added a background processor so campaign SMS messages are queued individually and sent by scheduled jobs.
  • Aggregated campaign job results into concise admin notices that highlight failed numbers and the most recent error.
Location:
alpha-sms
Files:
49 added
4 edited

Legend:

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

    r3362903 r3362984  
    33Tags: order notification, order SMS, woocommerce sms integration, sms plugin, mobile verification, OTP, SMS notifications, two-step verification, OTP verification, SMS, signup security, user verification, user security, SMS gateway, order SMS, order notifications, WordPress OTP, 2FA, login OTP, WP SMS
    44Requires at least: 3.5
    5 Tested up to: 6.6.2
     5Tested up to: 6.8.2
    66Requires PHP: 5.6
    7 Stable tag: 1.0.11
     7Stable tag: 1.0.12
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    6060== Changelog ==
    6161
     62= 1.0.12 =
     63* Added a background processor so campaign SMS messages are queued individually and sent by scheduled jobs.
     64* Aggregated campaign job results into concise admin notices that highlight failed numbers and the most recent error.
     65
    6266= 1.0.11 =
    6367* Updated the WooCommerce checkout OTP workflow to clone whichever submit button is present instead of relying on the `#place_order` ID so guest checkout themes remain compatible.
  • alpha-sms/trunk/admin/class-alpha_sms-admin.php

    r2867587 r3362984  
    4646
    4747    /**
     48     * Background processor for queued SMS jobs.
     49     *
     50     * @var Alpha_SMS_Background|null
     51     */
     52    private $background;
     53
     54    /**
    4855     * Initialize the class and set its properties.
    4956     *
    50      * @param string $plugin_name The name of this plugin.
    51      * @param string $version The version of this plugin.
    52      * @since    1.0.0
    53      */
    54     public function __construct($plugin_name, $version)
     57     * @param string                    $plugin_name The name of this plugin.
     58     * @param string                    $version     The version of this plugin.
     59     * @param Alpha_SMS_Background|null $background  Optional background processor instance.
     60     * @since    1.0.0
     61     */
     62    public function __construct($plugin_name, $version, $background = null)
    5563    {
    5664
    5765        $this->plugin_name = $plugin_name;
    5866        $this->version = $version;
     67        $this->background = $background;
     68    }
     69
     70    /**
     71     * Set the background processor instance.
     72     *
     73     * @param Alpha_SMS_Background|null $background Background processor.
     74     *
     75     * @return void
     76     */
     77    public function set_background_processor($background)
     78    {
     79        $this->background = $background;
     80    }
     81
     82    /**
     83     * Retrieve the background processor instance.
     84     *
     85     * @return Alpha_SMS_Background|null
     86     */
     87    private function get_background_processor()
     88    {
     89        if ($this->background instanceof Alpha_SMS_Background) {
     90            return $this->background;
     91        }
     92
     93        return null;
    5994    }
    6095
     
    301336
    302337            // Redirect to plugin page
    303             wp_redirect($_SERVER['HTTP_REFERER']);
     338            wp_safe_redirect(wp_get_referer());
    304339            exit();
    305340        }
     
    308343
    309344            // Redirect to plugin page
    310             wp_redirect($_SERVER['HTTP_REFERER']);
     345            wp_safe_redirect(wp_get_referer());
    311346            exit();
    312347        }
     
    322357        }
    323358
    324         // Final Numbers
    325         $numbers = implode(',', $numbersArr);
    326 
    327         require_once ALPHA_SMS_PATH . 'includes/sms.class.php';
    328 
    329         $sms = new AlphaSMS($api_key);
    330         $sms->numbers = $numbers;
    331         $sms->body = $body;
    332         $sms->sender_id = $sender_id;
    333 
    334         $response = $sms->Send();
    335 
    336         if (!$response) {
    337             $this->add_flash_notice(__("Something went wrong, please try again.", $this->plugin_name), "error");
    338 
    339         } elseif ($response->error !== 0) {
    340             $this->add_flash_notice(__($response->msg), 'error');
    341 
    342         } else {
    343             $this->add_flash_notice(__($response->msg), 'success');
     359        $numbersArr = array_map('trim', $numbersArr);
     360        $numbersArr = array_filter($numbersArr);
     361        $numbersArr = array_unique($numbersArr);
     362
     363        if (empty($numbersArr)) {
     364            $this->add_flash_notice(__("No valid recipients were provided.", $this->plugin_name), "error");
     365
     366            // Redirect to plugin page
     367            wp_safe_redirect(wp_get_referer());
     368            exit();
     369        }
     370
     371        $background = $this->get_background_processor();
     372
     373        if (!$background) {
     374            $this->add_flash_notice(__("Background processing is unavailable. Please try again later.", $this->plugin_name),
     375                "error");
     376
     377            // Redirect to plugin page
     378            wp_safe_redirect(wp_get_referer());
     379            exit();
     380        }
     381
     382        $queued = 0;
     383        $failedQueue = [];
     384
     385        foreach ($numbersArr as $number) {
     386            if ($background->dispatch($number, $body, $sender_id, $api_key)) {
     387                $queued++;
     388            } else {
     389                $failedQueue[] = $number;
     390            }
     391        }
     392
     393        if ($queued > 0) {
     394            $notice = sprintf(
     395                _n('Queued %d SMS message for background sending.', 'Queued %d SMS messages for background sending.', $queued,
     396                    $this->plugin_name),
     397                $queued
     398            );
     399            $this->add_flash_notice(esc_html($notice), 'success');
     400        }
     401
     402        if (!empty($failedQueue)) {
     403            $failedQueue = array_map('sanitize_text_field', $failedQueue);
     404            $preview = array_slice($failedQueue, 0, 5);
     405            $summary = implode(', ', $preview);
     406            if ('' === trim($summary)) {
     407                $summary = __('unknown recipients', $this->plugin_name);
     408            }
     409            $message = sprintf(
     410                __('Unable to queue %1$d recipient(s): %2$s', $this->plugin_name),
     411                count($failedQueue),
     412                $summary
     413            );
     414            if (count($failedQueue) > count($preview)) {
     415                $message .= ' ' . sprintf(__('and %d more.', $this->plugin_name), count($failedQueue) - count($preview));
     416            }
     417
     418            $this->add_flash_notice(esc_html($message), 'error');
    344419        }
    345420
    346421        // Redirect to plugin page
    347         wp_redirect($_SERVER['HTTP_REFERER']);
     422        wp_safe_redirect(wp_get_referer());
    348423        exit();
    349424    }
     
    392467
    393468    /**
     469     * Persist job results as flash notices for display in the admin area.
     470     *
     471     * @return void
     472     */
     473    private function maybe_add_job_result_notice()
     474    {
     475        $results = get_option($this->plugin_name . '_job_results', []);
     476
     477        if (empty($results) || !is_array($results)) {
     478            return;
     479        }
     480
     481        $defaults = [
     482            'success'    => 0,
     483            'failed'     => 0,
     484            'last_error' => '',
     485            'failures'   => [],
     486        ];
     487
     488        $results = wp_parse_args($results, $defaults);
     489
     490        if (!empty($results['success'])) {
     491            $success_notice = sprintf(
     492                _n('%d SMS message was sent successfully.', '%d SMS messages were sent successfully.', (int)$results['success'],
     493                    $this->plugin_name),
     494                (int)$results['success']
     495            );
     496            $this->add_flash_notice(esc_html($success_notice), 'success');
     497        }
     498
     499        if (!empty($results['failed'])) {
     500            $error_notice = sprintf(
     501                _n('%d SMS message failed to send.', '%d SMS messages failed to send.', (int)$results['failed'],
     502                    $this->plugin_name),
     503                (int)$results['failed']
     504            );
     505
     506            if (!empty($results['last_error'])) {
     507                $error_notice .= ' ' . sprintf(__('Last error: %s', $this->plugin_name), $results['last_error']);
     508            } elseif (!empty($results['failures']) && is_array($results['failures'])) {
     509                $details = [];
     510                foreach ($results['failures'] as $failure) {
     511                    $number = isset($failure['number']) ? $failure['number'] : '';
     512                    $message = isset($failure['message']) ? $failure['message'] : '';
     513
     514                    $detail_parts = [];
     515                    $trimmed_number = trim($number);
     516                    if ($trimmed_number !== '') {
     517                        $detail_parts[] = $trimmed_number;
     518                    }
     519                    if ($message !== '') {
     520                        $detail_parts[] = $message;
     521                    }
     522
     523                    if (!empty($detail_parts)) {
     524                        $details[] = implode(' - ', $detail_parts);
     525                    }
     526                }
     527
     528                if (!empty($details)) {
     529                    $error_notice .= ' ' . sprintf(
     530                        _n('Latest error: %s', 'Latest errors: %s', count($details), $this->plugin_name),
     531                        implode('; ', $details)
     532                    );
     533                }
     534            }
     535
     536            $this->add_flash_notice(esc_html($error_notice), 'error');
     537        }
     538
     539        delete_option($this->plugin_name . '_job_results');
     540    }
     541
     542    /**
    394543     * Function executed when the 'admin_notices' action is called, here we check if there are notices on
    395544     * our database and display them, after that, we remove the option to prevent notices being displayed forever.
     
    399548    public function display_flash_notices()
    400549    {
     550        $this->maybe_add_job_result_notice();
     551
    401552        $notices = get_option($this->plugin_name . '_notices', []);
    402553
  • alpha-sms/trunk/alpha_sms.php

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

    r3362903 r3362984  
    7777            $this->version = ALPHA_SMS_VERSION;
    7878        } else {
    79             $this->version = '1.0.0';
     79            $this->version = '1.0.12';
    8080        }
    8181        $this->plugin_name = 'alpha_sms';
     
    113113
    114114        /**
     115         * Background processing utilities.
     116         */
     117        require_once plugin_dir_path(dirname(__FILE__)) . 'includes/class-alpha_sms-background.php';
     118
     119        /**
    115120         * The class responsible for defining internationalization functionality
    116121         * of the plugin.
     
    159164    {
    160165
    161         $plugin_admin = new Alpha_sms_Admin($this->get_plugin_name(), $this->get_version());
     166        $background = new Alpha_SMS_Background($this->get_plugin_name());
     167        $plugin_admin = new Alpha_sms_Admin($this->get_plugin_name(), $this->get_version(), $background);
     168
     169        $this->loader->add_action(Alpha_SMS_Background::ACTION_HOOK, $background, 'alpha_sms_send_single_sms', 10, 1);
    162170
    163171        $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles');
Note: See TracChangeset for help on using the changeset viewer.