Plugin Directory

Changeset 3317645


Ignore:
Timestamp:
06/25/2025 12:31:54 PM (8 months ago)
Author:
mxchat
Message:

2.2.7 fixed pinecone delete all and added hyperlinking via markdown in rate limit messages.

Location:
mxchat-basic
Files:
95 added
6 edited

Legend:

Unmodified
Added
Removed
  • mxchat-basic/trunk/admin/class-pinecone-manager.php

    r3315188 r3317645  
    591591    // ========================================
    592592
    593     /**
    594      * Deletes data from Pinecone using provided API credentials
    595      */
    596      public function mxchat_delete_all_from_pinecone($pinecone_options) {
    597          $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
    598          $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
    599 
    600          if (empty($api_key) || empty($host)) {
    601              return array(
    602                  'success' => false,
    603                  'message' => 'Missing Pinecone API credentials'
    604              );
    605          }
    606 
    607          try {
    608              // First, get all vector IDs
    609              $all_vector_ids = array();
    610 
    611              // Try to get from cache first
    612              $cached_vector_ids = get_option('mxchat_pinecone_vector_ids_cache', array());
    613              if (!empty($cached_vector_ids)) {
    614                  $all_vector_ids = $cached_vector_ids;
    615              } else {
    616                  // Fallback: scan to get vector IDs
    617                  $records = $this->mxchat_scan_pinecone_vectors($pinecone_options);
    618                  foreach ($records as $record) {
    619                      if (!empty($record->id)) {
    620                          $all_vector_ids[] = $record->id;
    621                      }
    622                  }
    623              }
    624 
    625              if (empty($all_vector_ids)) {
    626                  return array(
    627                      'success' => true,
    628                      'message' => 'No vectors found to delete'
    629                  );
    630              }
    631 
    632              // Delete vectors in batches (Pinecone has limits on batch operations)
    633              $batch_size = 100;
    634              $batches = array_chunk($all_vector_ids, $batch_size);
    635              $deleted_count = 0;
    636              $failed_batches = 0;
    637 
    638              foreach ($batches as $batch) {
    639                  $result = $this->mxchat_delete_pinecone_batch($batch, $api_key, $host);
    640                  if ($result['success']) {
    641                      $deleted_count += count($batch);
    642                  } else {
    643                      $failed_batches++;
    644                      //error_log('Failed to delete Pinecone batch: ' . $result['message']);
    645                  }
    646              }
    647 
    648              if ($failed_batches > 0) {
    649                  return array(
    650                      'success' => false,
    651                      'message' => sprintf('Deleted %d vectors, but %d batches failed', $deleted_count, $failed_batches)
    652                  );
    653              }
    654 
    655              return array(
    656                  'success' => true,
    657                  'message' => "Successfully deleted {$deleted_count} vectors from Pinecone"
    658              );
    659 
    660          } catch (Exception $e) {
    661              //error_log('Pinecone delete all exception: ' . $e->getMessage());
    662              return array(
    663                  'success' => false,
    664                  'message' => $e->getMessage()
    665              );
    666          }
    667      }
     593public function mxchat_delete_all_from_pinecone($pinecone_options) {
     594    $api_key = $pinecone_options['mxchat_pinecone_api_key'] ?? '';
     595    $host = $pinecone_options['mxchat_pinecone_host'] ?? '';
     596
     597    if (empty($api_key) || empty($host)) {
     598        return array(
     599            'success' => false,
     600            'message' => 'Missing Pinecone API credentials'
     601        );
     602    }
     603
     604    try {
     605        // First, get all vector IDs
     606        $all_vector_ids = array();
     607
     608        // Try to get from cache first
     609        $cached_vector_ids = get_option('mxchat_pinecone_vector_ids_cache', array());
     610        if (!empty($cached_vector_ids)) {
     611            $all_vector_ids = $cached_vector_ids;
     612        } else {
     613            // Use the correct method name that exists in your class
     614            $records = $this->mxchat_get_recent_1k_entries($pinecone_options);
     615            foreach ($records as $record) {
     616                if (!empty($record->id)) {
     617                    $all_vector_ids[] = $record->id;
     618                }
     619            }
     620        }
     621
     622        if (empty($all_vector_ids)) {
     623            return array(
     624                'success' => true,
     625                'message' => 'No vectors found to delete'
     626            );
     627        }
     628
     629        // Delete vectors in batches (Pinecone has limits on batch operations)
     630        $batch_size = 100;
     631        $batches = array_chunk($all_vector_ids, $batch_size);
     632        $deleted_count = 0;
     633        $failed_batches = 0;
     634
     635        foreach ($batches as $batch) {
     636            $result = $this->mxchat_delete_pinecone_batch($batch, $api_key, $host);
     637            if ($result['success']) {
     638                $deleted_count += count($batch);
     639            } else {
     640                $failed_batches++;
     641                //error_log('Failed to delete Pinecone batch: ' . $result['message']);
     642            }
     643        }
     644
     645        // CLEAR ALL RELEVANT CACHES - EXACTLY like your single delete
     646        delete_transient('mxchat_pinecone_recent_1k_cache');
     647        delete_option('mxchat_pinecone_vector_ids_cache');
     648        delete_option('mxchat_pinecone_processed_cache');
     649        delete_option('mxchat_processed_content_cache');
     650       
     651        // Also force refresh for next page load - EXACTLY like your single delete
     652        $this->mxchat_refresh_after_new_content($pinecone_options);
     653
     654        if ($failed_batches > 0) {
     655            return array(
     656                'success' => false,
     657                'message' => sprintf('Deleted %d vectors, but %d batches failed', $deleted_count, $failed_batches)
     658            );
     659        }
     660
     661        return array(
     662            'success' => true,
     663            'message' => "Successfully deleted {$deleted_count} vectors from Pinecone"
     664        );
     665
     666    } catch (Exception $e) {
     667        //error_log('Pinecone delete all exception: ' . $e->getMessage());
     668        return array(
     669            'success' => false,
     670            'message' => $e->getMessage()
     671        );
     672    }
     673}
    668674
    669675
  • mxchat-basic/trunk/includes/class-mxchat-addons.php

    r3315188 r3317645  
    153153     */
    154154    public function enqueue_styles() {
    155         $plugin_version = '2.2.6';
     155        $plugin_version = '2.2.7';
    156156
    157157        wp_enqueue_style(
  • mxchat-basic/trunk/includes/class-mxchat-admin.php

    r3315188 r3317645  
    39183918}
    39193919
    3920 
    3921 
    3922 
    3923 
    39243920public function mxchat_rate_limits_callback() {
    39253921    $all_options = get_option('mxchat_options', []);
     
    39473943         esc_html__('Set message limits for each user role and customize the experience when users reach those limits. You can use {limit}, {timeframe}, {count}, and {remaining} as placeholders.', 'mxchat') .
    39483944         '</p>';
     3945
     3946    // Add markdown link documentation
     3947    echo '<div class="notice notice-info inline" style="margin-bottom: 20px; padding: 10px;">';
     3948    echo '<p><strong>' . esc_html__('Markdown Links Supported:', 'mxchat') . '</strong></p>';
     3949    echo '<p>' . esc_html__('You can include clickable links in your custom messages using markdown syntax:', 'mxchat') . '</p>';
     3950    echo '<ul style="margin-left: 20px;">';
     3951    echo '<li><code>[Link text](https://example.com)</code> - Creates a clickable link</li>';
     3952    echo '<li><code>[Visit our pricing](https://example.com/pricing)</code> - Link with custom text</li>';
     3953    echo '<li><code>Plain URLs like https://example.com will also become clickable</code></li>';
     3954    echo '</ul>';
     3955    echo '</div>';
    39493956
    39503957    // Output the controls for each role
     
    40174024                esc_textarea($custom_message) .
    40184025              '</textarea>';
     4026        echo '<p class="description">' .
     4027             esc_html__('Example: Rate limit reached! [Visit our pricing page](https://example.com/pricing) to upgrade.', 'mxchat') .
     4028             '</p>';
    40194029        echo '</div>'; // End message
    40204030
     
    40284038    echo '</div>'; // End pro-feature-wrapper
    40294039}
     4040
     4041
    40304042private function mxchat_add_option_field($id, $title, $callback = '') {
    40314043        add_settings_field(
     
    55005512public function mxchat_enqueue_admin_assets() {
    55015513    // Get plugin version (define this in your main plugin file)
    5502     $version = defined('MXCHAT_VERSION') ? MXCHAT_VERSION : '2.2.6';
     5514    $version = defined('MXCHAT_VERSION') ? MXCHAT_VERSION : '2.2.7';
    55035515   
    55045516    // Use file modification time for development (remove in production)
  • mxchat-basic/trunk/includes/class-mxchat-integrator.php

    r3315188 r3317645  
    49274927public function mxchat_enqueue_scripts_styles() {
    49284928    // Define version numbers for the styles and scripts
    4929     $chat_style_version = '2.2.6';
    4930     $chat_script_version = '2.2.6';
     4929    $chat_style_version = '2.2.7';
     4930    $chat_script_version = '2.2.7';
    49314931    // Enqueue the script
    49324932    wp_enqueue_script(
     
    51965196        );
    51975197       
     5198        // NEW: Process HTML links in the message
     5199        $message = $this->process_rate_limit_message_html($message);
     5200       
    51985201        //error_log('MXChat Rate Limit: Limit exceeded. Message: ' . $message);
    51995202       
    5200         // Return error with the custom message
     5203        // Return error with the processed message
    52015204        return [
    52025205            'error' => true,
     
    52125215    return true;
    52135216}
     5217
     5218
     5219/**
     5220 * Process HTML links in rate limit messages
     5221 *
     5222 * @param string $message The rate limit message
     5223 * @return string The processed message with safe HTML links
     5224 */
     5225private function process_rate_limit_message_html($message) {
     5226    // Return original message if empty
     5227    if (empty($message)) {
     5228        return $message;
     5229    }
     5230   
     5231    // First, convert markdown links to HTML
     5232    $message = $this->convert_markdown_links($message);
     5233   
     5234    // Then, auto-convert any remaining plain URLs to links
     5235    $message = $this->auto_link_urls($message);
     5236   
     5237    // Allow basic HTML tags for links and formatting
     5238    $allowed_tags = [
     5239        'a' => [
     5240            'href' => true,
     5241            'target' => true,
     5242            'rel' => true,
     5243            'title' => true,
     5244            'class' => true
     5245        ],
     5246        'strong' => [],
     5247        'em' => [],
     5248        'br' => [],
     5249        'b' => [],
     5250        'i' => [],
     5251        'span' => ['class' => true]
     5252    ];
     5253   
     5254    // Sanitize but allow the specified HTML tags
     5255    $processed_message = wp_kses($message, $allowed_tags);
     5256   
     5257    // If wp_kses stripped everything, return the original message as plain text
     5258    if (empty($processed_message) && !empty($message)) {
     5259        // Strip all HTML and return plain text as fallback
     5260        return wp_strip_all_tags($message);
     5261    }
     5262   
     5263    return $processed_message;
     5264}
     5265
     5266/**
     5267 * Convert markdown links to HTML
     5268 *
     5269 * @param string $text The text to process
     5270 * @return string The text with markdown links converted to HTML
     5271 */
     5272private function convert_markdown_links($text) {
     5273    // Return original text if empty
     5274    if (empty($text)) {
     5275        return $text;
     5276    }
     5277   
     5278    // Pattern to match markdown links: [text](url)
     5279    $pattern = '/\[([^\]]+)\]\(([^)]+)\)/';
     5280   
     5281    $processed_text = preg_replace_callback($pattern, function($matches) {
     5282        $link_text = $matches[1];
     5283        $url = $matches[2];
     5284       
     5285        // Clean up any trailing punctuation from the URL
     5286        $url = rtrim($url, '.,;:!?');
     5287       
     5288        // Sanitize the link text and URL
     5289        $safe_text = esc_html($link_text);
     5290        $safe_url = esc_url($url);
     5291       
     5292        // Create the HTML link
     5293        return '<a href="' . $safe_url . '" target="_blank" rel="noopener noreferrer">' . $safe_text . '</a>';
     5294    }, $text);
     5295   
     5296    // If preg_replace_callback failed, return original text
     5297    if ($processed_text === null) {
     5298        return $text;
     5299    }
     5300   
     5301    return $processed_text;
     5302}
     5303
     5304/**
     5305 * Auto-convert plain URLs to clickable links
     5306 *
     5307 * @param string $text The text to process
     5308 * @return string The text with URLs converted to links
     5309 */
     5310private function auto_link_urls($text) {
     5311    // Return original text if empty
     5312    if (empty($text)) {
     5313        return $text;
     5314    }
     5315   
     5316    // Simple pattern that avoids complex lookbehinds
     5317    // This will match URLs that are not already inside href attributes or markdown links
     5318    $pattern = '/(?<!href=["\'])(?<!\]\()https?:\/\/[^\s<>"\')\]]+/i';
     5319   
     5320    $processed_text = preg_replace_callback($pattern, function($matches) {
     5321        $url = $matches[0];
     5322        // Clean up any trailing punctuation that might have been captured
     5323        $url = rtrim($url, '.,;:!?');
     5324       
     5325        // Add target="_blank" and rel="noopener noreferrer" for security
     5326        return '<a href="' . esc_url($url) . '" target="_blank" rel="noopener noreferrer">' . esc_html($url) . '</a>';
     5327    }, $text);
     5328   
     5329    // If preg_replace_callback failed, return original text
     5330    if ($processed_text === null) {
     5331        return $text;
     5332    }
     5333   
     5334    return $processed_text;
     5335}
     5336
    52145337
    52155338// Helper function to get client IP address
  • mxchat-basic/trunk/mxchat-basic.php

    r3315188 r3317645  
    33 * Plugin Name: MxChat
    44 * Description: AI chatbot for WordPress with OpenAI, Claude, xAI, DeepSeek, live agent, PDF uploads, WooCommerce, and training on website data.
    5  * Version: 2.2.6
     5 * Version: 2.2.7
    66 * Author: MxChat
    77 * Author URI: https://mxchat.ai
     
    1717
    1818// Define plugin version constant for asset versioning
    19 define('MXCHAT_VERSION', '2.2.6');
     19define('MXCHAT_VERSION', '2.2.7');
    2020
    2121function mxchat_load_textdomain() {
  • mxchat-basic/trunk/readme.txt

    r3315188 r3317645  
    66Tested up to: 6.8
    77Requires PHP: 7.2
    8 Stable tag: 2.2.6
     8Stable tag: 2.2.7
    99License: GPLv2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    178178== Changelog ==
    179179
     180= 2.2.7 =
     181- Fixed a bug that occured when deleting all pinecone records & added support for markdown hyperlinking in rate limit messages.
     182
    180183= 2.2.6 =
    181184- Brand New Admin Debug Panel – Added a comprehensive frontend debug panel for administrators! You can now see retrieved documents, triggered actions, and other debug and setting information in real-time while testing your chatbot, making it easier than ever to fine-tune your bot's performance.
     
    524527== Upgrade Notice ==
    525528
    526 = 2.2.6 =
    527 - New admin debug panel for real-time chatbot testing and troubleshooting, plus important fixes for Pinecone deletion and WordPress knowledge base editing.
     529= 2.2.7 =
     530- Fixed a bug that occured when deleting all pinecone records & added support for markdown hyperlinking in rate limit messages.
    528531
    529532== License & Warranty ==
Note: See TracChangeset for help on using the changeset viewer.