Plugin Directory

Changeset 3248145


Ignore:
Timestamp:
02/28/2025 12:30:46 AM (13 months ago)
Author:
cod24
Message:
  • Bug fixes and improvements
  • Cached wallet amount
Location:
cod24-shipping
Files:
22 added
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • cod24-shipping/trunk/cod24-shipping.php

    r3246260 r3248145  
    33Plugin Name: COD24 Shipping For Woocommerce
    44Description: This plugin is adding COD24 shipping methods to woocommerce.
    5 Version: 3.4.5
     5Version: 3.4.6
    66Author: COD24
    77Author URI: https://cod24.ir
     
    2727        public function __construct()
    2828        {
    29             define('COD24_PLUGIN_VERSION', '3.4.5');
     29            define('COD24_PLUGIN_VERSION', '3.4.6');
    3030            define('COD24_PLUGIN_DIR', plugin_dir_path(__FILE__));
    3131            define('COD24_PLUGIN_URL', plugin_dir_url(__FILE__));
  • cod24-shipping/trunk/inc/class-cod24-api.php

    r3246260 r3248145  
    66 * Package: COD24 Shipping
    77 * Author: COD24
    8  * Last Modified Time: 2024/09/16 22:53:31
     8 * Last Modified Time: 2025/02/28 03:40:53
    99 * License: GPL-2.0+
    1010 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    3131
    3232        /**
    33          * Admin bar wallet balance
    34          *
    35          * @param admin_bar
    36          * @return void
    37          */
    38         public function admin_bar_wallet_balance( $admin_bar )
    39         {
    40             if( current_user_can('manage_options') )
    41             {
    42                 global $pagenow;
    43                 $cod24_token = get_option('cod24_shipping_token');
    44                
    45                 if( ! empty( $cod24_token ) )
    46                 {
    47                     // send to api
    48                     $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled')=='yes' ) ? 'https://' : 'http://';
    49                     $url  = $api_protocol.'api.cod24.ir/api/Wallet/getWalletAmount';
    50                     $body = [];
    51                     $body = wp_json_encode( $body );
     33         * Admin bar wallet balance
     34         *
     35         * @param WP_Admin_Bar $admin_bar
     36         * @return void
     37         */
     38        public function admin_bar_wallet_balance( $admin_bar )
     39        {
     40            if ( current_user_can('manage_options') )
     41            {
     42                global $pagenow;
     43                $cod24_token = get_option('cod24_shipping_token');
     44               
     45                if ( ! empty( $cod24_token ) )
     46                {
     47                    // Check if cached data exists
     48                    $cache_key = 'cod24_wallet_amount';
     49                    $cached_wallet_amount = get_transient( $cache_key );
    5250       
    53                     // api arguments
    54                     $args = array(
    55                         'method'      => 'POST',
    56                         'timeout'     => 500,
    57                         'sslverify'   => false,
    58                         'headers'     => array(
    59                             'Authorization' => 'Bearer '.$cod24_token,
    60                             'Content-Type'  => 'application/json',
    61                         ),
    62                         'body'        => $body,
    63                     );
     51                    if ( false === $cached_wallet_amount )
     52                    {
     53                        // Cache is empty or expired, fetch data from API
     54                        $api_protocol = ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? 'https://' : 'http://';
     55                        $url  = $api_protocol . 'api.cod24.ir/api/Wallet/getWalletAmount';
     56                        $body = [];
     57                        $body = wp_json_encode( $body );
    6458       
    65                     $request = wp_remote_post( $url, $args );
     59                        // API arguments
     60                        $args = array(
     61                            'method'      => 'POST',
     62                            'timeout'     => 500,
     63                            'sslverify'   => ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? true : false,
     64                            'headers'     => array(
     65                                'Authorization' => 'Bearer ' . $cod24_token,
     66                                'Content-Type'  => 'application/json',
     67                            ),
     68                            'body'        => $body,
     69                        );
    6670       
    67                     if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 )
    68                     {
    69                         $result['code'] = wp_remote_retrieve_response_code( $request );
    70                         if( $result['code'] == 401 )
    71                         {
    72                             self::get_token();
    73                         }
    74                     }
    75                     else
    76                     {
    77                         $response = wp_remote_retrieve_body( $request );
    78                         $result = json_decode( $response, true );
    79                         $cod24_wallet_amount = number_format( $result['walletAmount'], 0 ) ." ".get_woocommerce_currency_symbol();
    80                         // Translators: %s is the amount in the COD24 wallet.
    81                         $admin_bar->add_menu( array( 'id' => 'cod24_wallet_amount' , 'title' => sprintf( esc_attr__('COD24 Wallet Amount: %s', 'cod24-shipping' ), $cod24_wallet_amount ) ) );   
    82                     }
    83                 }
    84             }
    85         }
     71                        $request = wp_remote_post( $url, $args );
     72       
     73                        if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 )
     74                        {
     75                            $result['code'] = wp_remote_retrieve_response_code( $request );
     76                            if ( $result['code'] == 401 )
     77                            {
     78                                self::get_token();
     79                            }
     80                        }
     81                        else
     82                        {
     83                            $response = wp_remote_retrieve_body( $request );
     84                            $result = json_decode( $response, true );
     85                            $cod24_wallet_amount = number_format( $result['walletAmount'], 0 ) . " " . get_woocommerce_currency_symbol();
     86       
     87                            // Cache the result for 2 minutes
     88                            set_transient( $cache_key, $cod24_wallet_amount, 2 * MINUTE_IN_SECONDS );
     89       
     90                            // Translators: %s is the amount in the COD24 wallet.
     91                            $admin_bar->add_menu( array( 'id' => 'cod24_wallet_amount', 'title' => sprintf( esc_attr__('COD24 Wallet Amount: %s', 'cod24-shipping' ), $cod24_wallet_amount ) ) );
     92                        }
     93                    }
     94                    else
     95                    {
     96                        // Use cached data
     97                        $admin_bar->add_menu( array( 'id' => 'cod24_wallet_amount', 'title' => sprintf( esc_attr__('COD24 Wallet Amount: %s', 'cod24-shipping' ), $cached_wallet_amount ) ) );
     98                    }
     99                }
     100            }
     101        }
    86102
    87103        /**
     
    110126                $args = array(
    111127                    'method'      => 'POST',
    112                     'sslverify'   => false,
     128                    'sslverify'   => ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? true : false,
    113129                    'headers'     => array(
    114130                        'Content-Type'  => 'application/json',
     
    163179                    'method'      => 'POST',
    164180                    'timeout'     => 500,
    165                     'sslverify'   => true,
     181                    'sslverify'   => ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? true : false,
    166182                    'headers'     => array(
    167183                        'Authorization' => 'Bearer '.$cod24_token,
     
    386402                    'method'      => 'POST',
    387403                    'timeout'     => 500,
    388                     'sslverify'   => true,
     404                    'sslverify'   => ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? true : false,
    389405                    'headers'     => array(
    390406                        'Authorization' => 'Bearer '.$cod24_token,
     
    465481                    'method'      => 'POST',
    466482                    'timeout'     => 500,
    467                     'sslverify'   => true,
     483                    'sslverify'   => ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? true : false,
    468484                    'headers'     => array(
    469485                        'Authorization' => 'Bearer '.$cod24_token,
     
    535551                    'method'      => 'POST',
    536552                    'timeout'     => 500,
    537                     'sslverify'   => true,
     553                    'sslverify'   => ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? true : false,
    538554                    'headers'     => array(
    539555                        'Authorization' => 'Bearer '.$cod24_token,
     
    589605                    'method'      => 'POST',
    590606                    'timeout'     => 500,
    591                     'sslverify'   => true,
     607                    'sslverify'   => ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? true : false,
    592608                    'headers'     => array(
    593609                        'Authorization' => 'Bearer '.$cod24_token,
     
    643659                    'method'      => 'POST',
    644660                    'timeout'     => 500,
    645                     'sslverify'   => true,
     661                    'sslverify'   => ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? true : false,
    646662                    'headers'     => array(
    647663                        'Authorization' => 'Bearer '.$cod24_token,
     
    696712                    'method'      => 'POST',
    697713                    'timeout'     => 500,
    698                     'sslverify'   => true,
     714                    'sslverify'   => ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? true : false,
    699715                    'headers'     => array(
    700716                        'Authorization' => 'Bearer '.$cod24_token,
     
    725741                                'method'      => 'POST',
    726742                                'timeout'     => 500,
    727                                 'sslverify'   => true,
     743                                'sslverify'   => ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? true : false,
    728744                                'headers'     => array(
    729745                                    'Authorization' => esc_attr( $result['token'] ),
     
    770786                    'method'      => 'POST',
    771787                    'timeout'     => 500,
    772                     'sslverify'   => true,
     788                    'sslverify'   => ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? true : false,
    773789                    'headers'     => array(
    774790                        'Authorization' => 'Bearer '.$cod24_token,
     
    799815                                'method'      => 'POST',
    800816                                'timeout'     => 500,
    801                                 'sslverify'   => true,
     817                                'sslverify'   => ( get_option('cod24_shipping_api_secure_connection_enabled') == 'yes' ) ? true : false,
    802818                                'headers'     => array(
    803819                                    'Authorization' => esc_attr( $result['token'] ),
  • cod24-shipping/trunk/readme.txt

    r3246260 r3248145  
    44Requires at least: 5.0
    55Tested up to: 6.7
    6 Stable tag: 3.4.5
     6Stable tag: 3.4.6
    77License: GPL-2.0+
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
Note: See TracChangeset for help on using the changeset viewer.