Plugin Directory

Changeset 3331541


Ignore:
Timestamp:
07/21/2025 01:35:41 PM (8 months ago)
Author:
muchatai
Message:

Update to version 2.0.41 from GitHub

Location:
muchat-ai
Files:
6 edited
1 copied

Legend:

Unmodified
Added
Removed
  • muchat-ai/tags/2.0.41/includes/Frontend/Widget.php

    r3330738 r3331541  
    383383
    384384    /**
    385      * Get initial messages based on user login status
     385     * Get initial messages based on user login status.
     386     * It first checks for specific guest messages if applicable.
     387     * If not, it falls back to the default initial messages.
     388     * Returns an array of messages, or an empty array if none are configured.
    386389     *
    387390     * @return array
     
    389392    private function get_initial_messages()
    390393    {
    391         // Get guest-specific message settings
    392         $enable_guest_messages = get_option('muchat_ai_chatbot_enable_guest_messages', '0');
    393         $guest_initial_messages_json = get_option('muchat_ai_chatbot_guest_initial_messages', '');
    394 
    395         // Check if we should use guest messages
    396         if ('1' === $enable_guest_messages && !is_user_logged_in() && !empty($guest_initial_messages_json)) {
     394        // A helper function to robustly parse the message string, which can be
     395        // JSON, a legacy comma-separated string, or empty.
     396        $parse_message_string = function ($json_string) {
     397            // If the input is empty or just an empty JSON array, return empty.
     398            if (empty($json_string) || trim($json_string) === '[]') {
     399                return [];
     400            }
     401
     402            // First, try to decode as JSON. This is the modern format.
    397403            try {
    398                 $guest_messages = json_decode($guest_initial_messages_json, true);
    399                 if (is_array($guest_messages) && !empty($guest_messages)) {
    400                     return $guest_messages;
     404                $decoded = json_decode($json_string, true, 512, JSON_THROW_ON_ERROR);
     405                // If it decodes to a valid array, return it.
     406                if (is_array($decoded)) {
     407                    return $decoded;
    401408                }
    402409            } catch (\Exception $e) {
    403                 // If JSON is invalid, fall through to default messages
    404             }
    405         }
    406 
    407         // --- Fallback to default messages ---
     410                // The string is not valid JSON. We assume it's the legacy format.
     411            }
     412
     413            // Fallback to parsing the legacy comma-separated string format.
     414            $messages = array_map('trim', explode(',', $json_string));
     415
     416            // Filter out any resultant empty strings from the list.
     417            return array_filter($messages);
     418        };
     419
     420        // --- Step 1: Check for Guest-specific Messages ---
     421        $enable_guest_messages = get_option('muchat_ai_chatbot_enable_guest_messages', '0');
     422        $use_guest_logic = ('1' === $enable_guest_messages && !is_user_logged_in());
     423       
     424        if ($use_guest_logic) {
     425            $guest_messages_json = get_option('muchat_ai_chatbot_guest_initial_messages', '');
     426            // If guest messages are enabled but empty, we don't fall back to default.
     427            // We just return the (empty) result for the guest-specific setting.
     428             return $parse_message_string($guest_messages_json);
     429        }
     430
     431        // --- Step 2: Fallback to Default Messages ---
     432        // This runs if guest messages are not enabled or the user is logged in.
    408433        $initial_messages_json = get_option('muchat_ai_chatbot_interface_initial_messages', '');
    409         if (empty($initial_messages_json)) {
    410             return []; // No messages configured
    411         }
    412 
    413         try {
    414             $initial_messages = json_decode($initial_messages_json, true);
    415             // Ensure it's a non-empty array
    416             if (is_array($initial_messages) && !empty($initial_messages)) {
    417                 return $initial_messages;
    418             }
    419         } catch (\Exception $e) {
    420             // JSON might be malformed, or it might be a simple comma-separated string
    421         }
    422 
    423         // Fallback for old comma-separated format
    424         $messages = array_map('trim', explode(',', $initial_messages_json));
    425         return array_filter($messages);
     434        return $parse_message_string($initial_messages_json);
    426435    }
    427436
  • muchat-ai/tags/2.0.41/readme.txt

    r3330977 r3331541  
    44Requires at least: 5.0
    55Tested up to: 6.8
    6 Stable tag: 2.0.40
     6Stable tag: 2.0.41
    77Requires PHP: 7.4
    88License: GPLv2 or later
     
    7575
    7676== Changelog ==
     77
     78= 2.0.41 =
     79* Bugfix: Fix initialMessages issue
    7780
    7881= 2.0.40 =
  • muchat-ai/tags/2.0.41/templates/admin/widget-settings.php

    r3330738 r3331541  
    144144                                        <?php esc_html_e('Add Message', 'muchat-ai'); ?>
    145145                                    </button>
    146                                     <input type="hidden" id="muchat_ai_chatbot_interface_initial_messages" name="muchat_ai_chatbot_interface_initial_messages" value="<?php echo esc_attr(is_array($interface_initial_messages) ? '' : $interface_initial_messages); ?>" />
     146                                    <input type="hidden" id="muchat_ai_chatbot_interface_initial_messages" name="muchat_ai_chatbot_interface_initial_messages" value="<?php echo esc_attr(is_array($interface_initial_messages) ? wp_json_encode($interface_initial_messages) : $interface_initial_messages); ?>" />
    147147                                    <p class="description">
    148148                                        <?php esc_html_e('Add welcome messages that will be shown when a visitor opens the chat widget. You can include personalization variables such as', 'muchat-ai'); ?> <code>$name</code> <?php esc_html_e('and', 'muchat-ai'); ?> <code>$lastname</code> (<?php esc_html_e('example:', 'muchat-ai'); ?> <code>"Hello $name, welcome to our site!"</code>). <?php esc_html_e('Add multiple messages to create a conversation flow (each message will appear in sequence). If left empty, default settings from your Muchat dashboard will be used.', 'muchat-ai'); ?>
     
    167167                                        <?php esc_html_e('Add Message', 'muchat-ai'); ?>
    168168                                    </button>
    169                                     <input type="hidden" id="muchat_ai_chatbot_guest_initial_messages" name="muchat_ai_chatbot_guest_initial_messages" value="<?php echo esc_attr(is_array($guest_initial_messages) ? '' : $guest_initial_messages); ?>" />
     169                                    <input type="hidden" id="muchat_ai_chatbot_guest_initial_messages" name="muchat_ai_chatbot_guest_initial_messages" value="<?php echo esc_attr(is_array($guest_initial_messages) ? wp_json_encode($guest_initial_messages) : $guest_initial_messages); ?>" />
    170170                                    <p class="description">
    171171                                        <?php esc_html_e('These messages will be shown to visitors who are not logged in. If left empty, the default messages above will be used.', 'muchat-ai'); ?>
  • muchat-ai/trunk/includes/Frontend/Widget.php

    r3330738 r3331541  
    383383
    384384    /**
    385      * Get initial messages based on user login status
     385     * Get initial messages based on user login status.
     386     * It first checks for specific guest messages if applicable.
     387     * If not, it falls back to the default initial messages.
     388     * Returns an array of messages, or an empty array if none are configured.
    386389     *
    387390     * @return array
     
    389392    private function get_initial_messages()
    390393    {
    391         // Get guest-specific message settings
    392         $enable_guest_messages = get_option('muchat_ai_chatbot_enable_guest_messages', '0');
    393         $guest_initial_messages_json = get_option('muchat_ai_chatbot_guest_initial_messages', '');
    394 
    395         // Check if we should use guest messages
    396         if ('1' === $enable_guest_messages && !is_user_logged_in() && !empty($guest_initial_messages_json)) {
     394        // A helper function to robustly parse the message string, which can be
     395        // JSON, a legacy comma-separated string, or empty.
     396        $parse_message_string = function ($json_string) {
     397            // If the input is empty or just an empty JSON array, return empty.
     398            if (empty($json_string) || trim($json_string) === '[]') {
     399                return [];
     400            }
     401
     402            // First, try to decode as JSON. This is the modern format.
    397403            try {
    398                 $guest_messages = json_decode($guest_initial_messages_json, true);
    399                 if (is_array($guest_messages) && !empty($guest_messages)) {
    400                     return $guest_messages;
     404                $decoded = json_decode($json_string, true, 512, JSON_THROW_ON_ERROR);
     405                // If it decodes to a valid array, return it.
     406                if (is_array($decoded)) {
     407                    return $decoded;
    401408                }
    402409            } catch (\Exception $e) {
    403                 // If JSON is invalid, fall through to default messages
    404             }
    405         }
    406 
    407         // --- Fallback to default messages ---
     410                // The string is not valid JSON. We assume it's the legacy format.
     411            }
     412
     413            // Fallback to parsing the legacy comma-separated string format.
     414            $messages = array_map('trim', explode(',', $json_string));
     415
     416            // Filter out any resultant empty strings from the list.
     417            return array_filter($messages);
     418        };
     419
     420        // --- Step 1: Check for Guest-specific Messages ---
     421        $enable_guest_messages = get_option('muchat_ai_chatbot_enable_guest_messages', '0');
     422        $use_guest_logic = ('1' === $enable_guest_messages && !is_user_logged_in());
     423       
     424        if ($use_guest_logic) {
     425            $guest_messages_json = get_option('muchat_ai_chatbot_guest_initial_messages', '');
     426            // If guest messages are enabled but empty, we don't fall back to default.
     427            // We just return the (empty) result for the guest-specific setting.
     428             return $parse_message_string($guest_messages_json);
     429        }
     430
     431        // --- Step 2: Fallback to Default Messages ---
     432        // This runs if guest messages are not enabled or the user is logged in.
    408433        $initial_messages_json = get_option('muchat_ai_chatbot_interface_initial_messages', '');
    409         if (empty($initial_messages_json)) {
    410             return []; // No messages configured
    411         }
    412 
    413         try {
    414             $initial_messages = json_decode($initial_messages_json, true);
    415             // Ensure it's a non-empty array
    416             if (is_array($initial_messages) && !empty($initial_messages)) {
    417                 return $initial_messages;
    418             }
    419         } catch (\Exception $e) {
    420             // JSON might be malformed, or it might be a simple comma-separated string
    421         }
    422 
    423         // Fallback for old comma-separated format
    424         $messages = array_map('trim', explode(',', $initial_messages_json));
    425         return array_filter($messages);
     434        return $parse_message_string($initial_messages_json);
    426435    }
    427436
  • muchat-ai/trunk/readme.txt

    r3330977 r3331541  
    44Requires at least: 5.0
    55Tested up to: 6.8
    6 Stable tag: 2.0.40
     6Stable tag: 2.0.41
    77Requires PHP: 7.4
    88License: GPLv2 or later
     
    7575
    7676== Changelog ==
     77
     78= 2.0.41 =
     79* Bugfix: Fix initialMessages issue
    7780
    7881= 2.0.40 =
  • muchat-ai/trunk/templates/admin/widget-settings.php

    r3330738 r3331541  
    144144                                        <?php esc_html_e('Add Message', 'muchat-ai'); ?>
    145145                                    </button>
    146                                     <input type="hidden" id="muchat_ai_chatbot_interface_initial_messages" name="muchat_ai_chatbot_interface_initial_messages" value="<?php echo esc_attr(is_array($interface_initial_messages) ? '' : $interface_initial_messages); ?>" />
     146                                    <input type="hidden" id="muchat_ai_chatbot_interface_initial_messages" name="muchat_ai_chatbot_interface_initial_messages" value="<?php echo esc_attr(is_array($interface_initial_messages) ? wp_json_encode($interface_initial_messages) : $interface_initial_messages); ?>" />
    147147                                    <p class="description">
    148148                                        <?php esc_html_e('Add welcome messages that will be shown when a visitor opens the chat widget. You can include personalization variables such as', 'muchat-ai'); ?> <code>$name</code> <?php esc_html_e('and', 'muchat-ai'); ?> <code>$lastname</code> (<?php esc_html_e('example:', 'muchat-ai'); ?> <code>"Hello $name, welcome to our site!"</code>). <?php esc_html_e('Add multiple messages to create a conversation flow (each message will appear in sequence). If left empty, default settings from your Muchat dashboard will be used.', 'muchat-ai'); ?>
     
    167167                                        <?php esc_html_e('Add Message', 'muchat-ai'); ?>
    168168                                    </button>
    169                                     <input type="hidden" id="muchat_ai_chatbot_guest_initial_messages" name="muchat_ai_chatbot_guest_initial_messages" value="<?php echo esc_attr(is_array($guest_initial_messages) ? '' : $guest_initial_messages); ?>" />
     169                                    <input type="hidden" id="muchat_ai_chatbot_guest_initial_messages" name="muchat_ai_chatbot_guest_initial_messages" value="<?php echo esc_attr(is_array($guest_initial_messages) ? wp_json_encode($guest_initial_messages) : $guest_initial_messages); ?>" />
    170170                                    <p class="description">
    171171                                        <?php esc_html_e('These messages will be shown to visitors who are not logged in. If left empty, the default messages above will be used.', 'muchat-ai'); ?>
Note: See TracChangeset for help on using the changeset viewer.