Plugin Directory

Changeset 3447989


Ignore:
Timestamp:
01/27/2026 03:09:10 PM (2 months ago)
Author:
lazychat
Message:

Update LazyChat to version 1.4.21

Location:
lazychat/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • lazychat/trunk/includes/class-admin.php

    r3440474 r3447989  
    102102    /**
    103103     * Check connection to LazyChat on every settings page visit
    104      * Sends connection check event notification and logout if fails
     104     * Sends connection check event notification (does not logout on failure to avoid false positives)
    105105     */
    106106    private function check_connection_on_page_load() {
     
    109109        if (empty($bearer_token)) {
    110110            return; // Not logged in, no need to check
     111        }
     112       
     113        // Grace period: Skip connection check for 30 seconds after login/token save
     114        // This prevents logout during the page reload after successful login
     115        $last_token_save = get_option('lazychat_token_saved_time', 0);
     116        if (!empty($last_token_save) && (time() - (int)$last_token_save) < 30) {
     117            return; // Within grace period, skip check
    111118        }
    112119       
     
    149156       
    150157        // Check if connection was successful
    151         if (!is_wp_error($response)) {
    152             $response_code = wp_remote_retrieve_response_code($response);
    153            
    154             if ($response_code === 200) {
    155                 // Connection successful
    156                 return;
     158        if (is_wp_error($response)) {
     159            // Network error - log but don't logout (could be temporary)
     160            if (defined('WP_DEBUG') && WP_DEBUG && defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) {
     161                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Intentional debug logging
     162                error_log('[LazyChat] Connection check failed (network error): ' . $response->get_error_message());
    157163            }
    158         }
    159        
    160         // Connection failed - logout user
    161         $this->force_logout();
     164            return; // Don't logout on network errors
     165        }
     166       
     167        $response_code = wp_remote_retrieve_response_code($response);
     168       
     169        // Accept any 2xx response as successful
     170        if ($response_code >= 200 && $response_code < 300) {
     171            // Connection successful
     172            return;
     173        }
     174       
     175        // Only logout on explicit authentication failures (401 Unauthorized)
     176        if ($response_code === 401) {
     177            if (defined('WP_DEBUG') && WP_DEBUG && defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) {
     178                // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Intentional debug logging
     179                error_log('[LazyChat] Connection check failed: 401 Unauthorized - logging out user');
     180            }
     181            $this->force_logout();
     182            return;
     183        }
     184       
     185        // For other error codes (4xx, 5xx), log but don't logout
     186        // This prevents logout due to temporary server issues
     187        if (defined('WP_DEBUG') && WP_DEBUG && defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) {
     188            // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Intentional debug logging
     189            error_log('[LazyChat] Connection check returned non-success code: ' . $response_code . ' (not logging out)');
     190        }
    162191    }
    163192   
     
    166195     */
    167196    private function force_logout() {
     197        // Send logout event before clearing credentials
     198        $this->send_logout_event('connection_check_failed');
     199       
    168200        delete_option('lazychat_bearer_token');
     201        delete_option('lazychat_token_saved_time');
    169202        delete_option('lazychat_selected_shop_id');
    170203        delete_option('lazychat_selected_shop_name');
     
    174207        wp_safe_redirect(admin_url('options-general.php?page=lazychat_settings&connection_failed=1'));
    175208        exit;
     209    }
     210   
     211    /**
     212     * Send logout event to LazyChat API
     213     *
     214     * @param string $reason Reason for logout (e.g., 'connection_check_failed', 'user_initiated', 'token_expired')
     215     */
     216    private function send_logout_event($reason = 'unknown') {
     217        $bearer_token = get_option('lazychat_bearer_token', '');
     218        $shop_id = get_option('lazychat_selected_shop_id', '');
     219       
     220        // Can't send event without token
     221        if (empty($bearer_token)) {
     222            return;
     223        }
     224       
     225        $api_url = 'https://app.lazychat.io/api/woocommerce-plugin/events';
     226       
     227        $payload = array(
     228            'event_type' => 'settings.logout',
     229            'event_data' => array(
     230                'reason' => $reason,
     231                'user_id' => get_current_user_id(),
     232                'user_email' => wp_get_current_user()->user_email,
     233                'logout_time' => current_time('mysql')
     234            ),
     235            'site_info' => array(
     236                'site_url' => get_site_url(),
     237                'site_name' => get_bloginfo('name'),
     238                'wordpress_version' => get_bloginfo('version'),
     239                'woocommerce_version' => defined('WC_VERSION') ? WC_VERSION : 'N/A',
     240                'plugin_version' => LAZYCHAT_VERSION,
     241                'php_version' => phpversion(),
     242                'timestamp' => current_time('mysql')
     243            )
     244        );
     245       
     246        // Non-blocking request - we don't wait for response since we're logging out anyway
     247        wp_remote_post($api_url, array(
     248            'body' => wp_json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
     249            'headers' => array(
     250                'Authorization' => 'Bearer ' . $bearer_token,
     251                'Content-Type' => 'application/json',
     252                'X-Event-Type' => 'settings.logout',
     253                'X-Plugin-Version' => LAZYCHAT_VERSION,
     254                'X-Lazychat-Shop-Id' => $shop_id,
     255                'X-Event-Timestamp' => time()
     256            ),
     257            'timeout' => 5,
     258            'blocking' => false, // Non-blocking - don't wait for response
     259            'data_format' => 'body'
     260        ));
    176261    }
    177262   
  • lazychat/trunk/includes/class-ajax-handlers.php

    r3440474 r3447989  
    339339
    340340        update_option('lazychat_bearer_token', $auth_token);
     341        update_option('lazychat_token_saved_time', time()); // Track when token was saved for grace period
    341342        update_option('lazychat_selected_shop_id', $shop_id);
    342343        update_option('lazychat_selected_shop_name', $shop_name);
  • lazychat/trunk/lazychat.php

    r3440503 r3447989  
    44 * Plugin URI: https://app.lazychat.io
    55 * Description: Connect your WooCommerce store with LazyChat's AI-powered customer support platform. Automatically sync products and orders via webhooks.
    6  * Version: 1.4.19
     6 * Version: 1.4.21
    77 * Author: LazyChat
    88 * License: GPL v2 or later
     
    2525
    2626// Define plugin constants
    27 define('LAZYCHAT_VERSION', '1.4.19');
     27define('LAZYCHAT_VERSION', '1.4.21');
    2828define('LAZYCHAT_PLUGIN_DIR', plugin_dir_path(__FILE__));
    2929define('LAZYCHAT_PLUGIN_URL', plugin_dir_url(__FILE__));
  • lazychat/trunk/readme.txt

    r3440503 r3447989  
    55Tested up to: 6.9
    66Requires PHP: 7.4
    7 Stable tag: 1.4.19
     7Stable tag: 1.4.21
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
Note: See TracChangeset for help on using the changeset viewer.