PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.2.8
MxChat – AI Chatbot & Content Generation for WordPress v3.2.8
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
← All changes | includes/admin-settings-page.php +347 -16 3.2.33.2.8 View file →
@@ -224,9 +224,9 @@
224 224 <?php if (!$is_activated): ?>
225 225 <div class="mxch-pro-banner">
226 226 <div class="mxch-pro-banner-content">
227 227 <h3 class="mxch-pro-banner-title"><?php esc_html_e('Stop paying $50–200/month for AI chatbot tools.', 'mxchat'); ?></h3>
228 - <p class="mxch-pro-banner-text"><?php esc_html_e('One payment unlocks every MxChat Pro add-on for life — WooCommerce sales, Google Search Console insights, AI-generated chatbot themes, lead-capture forms, unlimited bots, image analysis, and more. No renewals. No surprise bills.', 'mxchat'); ?></p>
228 + <p class="mxch-pro-banner-text"><?php esc_html_e('One payment unlocks every MxChat Pro add-on for life — MCP server access for Claude / ChatGPT / Claude Code, WooCommerce sales, Google Search Console insights, AI-generated chatbot themes, lead-capture forms, unlimited bots, image analysis, and more. No renewals. No surprise bills.', 'mxchat'); ?></p>
229 229 </div>
230 230 <a href="https://mxchat.ai/" target="_blank" class="mxch-pro-banner-btn"><span><?php esc_html_e('Get Lifetime Access →', 'mxchat'); ?></span></a>
231 231 </div>
232 232 <?php endif; ?>
@@ -313,8 +313,16 @@
313 313 mxchat_render_field_wrapper('citation_links', __('Citation Links', 'mxchat'), function() use ($admin_instance) {
314 314 $admin_instance->mxchat_citation_links_toggle_callback();
315 315 }, __('Allow the AI to include citation links from your knowledge database in responses. If disabled, ensure your AI Behavior settings do not mention links or the AI may fabricate URLs.', 'mxchat'));
316 316
317 + // Satisfaction Rating Prompt
318 + // The toggle callback inline-renders the 5 customization
319 + // fields inside a single sub-options wrapper (plan-29caac).
320 + // No separate customization group needed — the wrapper handles
321 + // its own server-side display state based on the toggle value.
322 + mxchat_render_field_wrapper('satisfaction_rating', __('Satisfaction Rating Prompt', 'mxchat'), function() use ($admin_instance) {
323 + $admin_instance->mxchat_satisfaction_rating_toggle_callback();
324 + }, __('Show a 👍 / 👎 prompt after a conversation has had a couple of bot replies and the visitor has been idle for a moment. Disable to hide the prompt site-wide.', 'mxchat'));
317 325 ?>
318 326 </div>
319 327 </div>
320 328 </div>
@@ -602,8 +610,99 @@
602 610 </div>
603 611 </div>
604 612 </div>
605 613
614 + <!-- Page Cache Compatibility Card -->
615 + <div class="mxch-card">
616 + <div class="mxch-card-header">
617 + <h3 class="mxch-card-title">
618 + <svg class="mxch-card-title-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/><polyline points="3.27 6.96 12 12.01 20.73 6.96"/><line x1="12" y1="22.08" x2="12" y2="12"/></svg>
619 + <?php esc_html_e('Page Cache Compatibility', 'mxchat'); ?>
620 + </h3>
621 + </div>
622 + <div class="mxch-card-body">
623 + <p class="mxch-field-description" style="margin-top: 0;"><?php esc_html_e('MxChat\'s chat send (and stream send, plus PDF / Word upload) all POST to /wp-admin/admin-ajax.php. Page-cache plugins occasionally cache those responses, which breaks the per-session nonce flow on the first message. MxChat auto-tells the five most common cache plugins to never cache chat AJAX via their own filter APIs — no setup needed for those. For other cache plugins (or Cloudflare APO), copy the manual exclusion string into your cache plugin\'s exclusion list.', 'mxchat'); ?></p>
624 +
625 + <?php
626 + $mxch_cache_rows = array(
627 + array('name' => 'WP Rocket', 'state' => 'auto', 'exclusion' => '/wp-admin/admin-ajax\\.php\\?action=mxchat_.*'),
628 + array('name' => 'LiteSpeed Cache', 'state' => 'auto', 'exclusion' => '/wp-admin/admin-ajax.php?action=mxchat_*'),
629 + array('name' => 'W3 Total Cache', 'state' => 'auto', 'exclusion' => '/wp-admin/admin-ajax.php?action=mxchat_*'),
630 + array('name' => 'FlyingPress', 'state' => 'auto', 'exclusion' => '/wp-admin/admin-ajax.php?action=mxchat_*'),
631 + array('name' => 'WP Super Cache', 'state' => 'auto', 'exclusion' => 'wp-admin/admin-ajax.php'),
632 + array('name' => 'Cloudflare APO', 'state' => 'manual', 'exclusion' => 'Page Rule / Cache Rule: URI Path contains "/wp-admin/admin-ajax.php" → Bypass Cache'),
633 + );
634 + ?>
635 +
636 + <div class="mxch-cache-compat-grid" style="display: grid; grid-template-columns: 1fr; gap: 12px; margin-top: 16px;">
637 + <?php foreach ($mxch_cache_rows as $row) :
638 + $is_auto = ($row['state'] === 'auto');
639 + $pill_class = $is_auto ? 'mxch-status-pill-active' : 'mxch-status-pill-warning';
640 + $pill_label = $is_auto ? __('Auto-handled', 'mxchat') : __('Manual', 'mxchat');
641 + ?>
642 + <div class="mxch-cache-compat-row" style="display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 14px; background: #f8fafc; border: 1px solid var(--mxch-card-border); border-radius: var(--mxch-radius-md);">
643 + <div style="display: flex; align-items: center; gap: 10px; min-width: 0; flex: 0 0 auto;">
644 + <span style="font-weight: 500;"><?php echo esc_html($row['name']); ?></span>
645 + <span class="mxch-status-pill <?php echo esc_attr($pill_class); ?>">
646 + <span class="mxch-status-dot"></span>
647 + <?php echo esc_html($pill_label); ?>
648 + </span>
649 + </div>
650 + <div style="display: flex; align-items: center; gap: 8px; flex: 1 1 auto; min-width: 0;">
651 + <code style="background: #fff; border: 1px solid var(--mxch-card-border); padding: 4px 8px; border-radius: var(--mxch-radius-sm); font-size: 12px; color: var(--mxch-text-secondary); flex: 1 1 auto; overflow-x: auto; white-space: nowrap; min-width: 0;"><?php echo esc_html($row['exclusion']); ?></code>
652 + <button type="button" class="mxch-btn mxch-btn-secondary mxch-btn-sm" data-mxch-copy="<?php echo esc_attr($row['exclusion']); ?>" style="flex: 0 0 auto;">
653 + <?php esc_html_e('Copy', 'mxchat'); ?>
654 + </button>
655 + </div>
656 + </div>
657 + <?php endforeach; ?>
658 + </div>
659 +
660 + <div class="mxch-notice mxch-notice-info" style="margin-top: 16px;">
661 + <svg class="mxch-notice-icon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
662 + <span><?php esc_html_e('Auto-handled plugins receive a filter from MxChat that excludes chat AJAX from their cache. Even so, having the exclusion in your own exclusion list is harmless and acts as a belt-and-suspenders guarantee.', 'mxchat'); ?></span>
663 + </div>
664 + </div>
665 + </div>
666 +
667 + <!-- Provider Reliability Card -->
668 + <?php
669 + $auto_retry_enabled = !isset($options['auto_retry_on_transient_error']) ||
670 + (string) $options['auto_retry_on_transient_error'] !== '0';
671 + ?>
672 + <div class="mxch-card">
673 + <div class="mxch-card-header">
674 + <h3 class="mxch-card-title">
675 + <svg class="mxch-card-title-icon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12a9 9 0 1 1-6.219-8.56"/><polyline points="21 4 21 10 15 10"/></svg>
676 + <?php esc_html_e('Provider Reliability', 'mxchat'); ?>
677 + </h3>
678 + <span class="mxch-status-pill <?php echo $auto_retry_enabled ? 'mxch-status-pill-active' : 'mxch-status-pill-disabled'; ?>">
679 + <span class="mxch-status-dot"></span>
680 + <?php echo $auto_retry_enabled ? esc_html__('Active', 'mxchat') : esc_html__('Disabled', 'mxchat'); ?>
681 + </span>
682 + </div>
683 + <div class="mxch-card-body mxchat-autosave-section">
684 + <div class="mxch-field">
685 + <label class="mxch-field-label" for="auto_retry_on_transient_error">
686 + <?php esc_html_e('Auto-retry on provider overload', 'mxchat'); ?>
687 + </label>
688 + <div class="mxch-field-control">
689 + <label class="mxch-toggle">
690 + <input type="checkbox"
691 + id="auto_retry_on_transient_error"
692 + name="mxchat_options[auto_retry_on_transient_error]"
693 + class="mxchat-autosave-field"
694 + value="1"
695 + <?php checked($auto_retry_enabled); ?> />
696 + <span class="mxch-toggle-slider"></span>
697 + </label>
698 + </div>
699 + <p class="mxch-field-description"><?php esc_html_e('When the AI provider returns a transient error (rate-limit / overload / 429 / 503 / Gemini "model overloaded"), MxChat retries the chat-send up to twice before surfacing the error. Visitors see a brief pause instead of a raw provider error in the bot bubble.', 'mxchat'); ?></p>
700 + <p class="mxch-field-hint"><?php esc_html_e('Configuration errors (invalid API key, 401/403/404/422) still surface immediately — never retried.', 'mxchat'); ?></p>
701 + </div>
702 + </div>
703 + </div>
704 +
606 705 <!-- Debug Mode Card -->
607 706 <?php
608 707 $debug_mode = isset($options['debug_mode']) ? $options['debug_mode'] : 'off';
609 708 $debug_active = ($debug_mode === 'on');
@@ -942,8 +1041,40 @@
942 1041 <h1 class="mxch-content-title"><?php esc_html_e('Video Tutorials', 'mxchat'); ?></h1>
943 1042 <p class="mxch-content-subtitle"><?php esc_html_e('Learn how to get the most out of MxChat with our video guides.', 'mxchat'); ?></p>
944 1043 </div>
945 1044
1045 + <?php
1046 + // Setup Guide — reopen the onboarding wizard (plan-347916, moved here from Display tab).
1047 + // Only shows once the user has dismissed/graduated from the Onboarding page.
1048 + if (function_exists('mxchat_onboarding_is_dismissed') && mxchat_onboarding_is_dismissed()):
1049 + $auto_grad = function_exists('mxchat_onboarding_is_auto_graduated') && mxchat_onboarding_is_auto_graduated();
1050 + ?>
1051 + <div class="mxch-card mxch-onboarding-unhide-card" style="margin-bottom: 24px;">
1052 + <div class="mxch-card-header">
1053 + <h3 class="mxch-card-title">
1054 + <svg class="mxch-card-title-icon" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 10v6M2 10l10-5 10 5-10 5z"/><path d="M6 12v5c0 2 3 3 6 3s6-1 6-3v-5"/></svg>
1055 + <?php esc_html_e('Setup Guide', 'mxchat'); ?>
1056 + </h3>
1057 + </div>
1058 + <div class="mxch-card-body">
1059 + <p style="margin:0 0 12px;color:var(--mxch-text-secondary);font-size:13px;line-height:1.5;">
1060 + <?php if ($auto_grad): ?>
1061 + <?php esc_html_e('You finished the setup guide. Reopen it any time to revisit the steps or change a setup choice.', 'mxchat'); ?>
1062 + <?php else: ?>
1063 + <?php esc_html_e('You hid the setup guide from the MxChat menu. Reopen it any time to revisit the steps.', 'mxchat'); ?>
1064 + <?php endif; ?>
1065 + </p>
1066 + <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" style="margin:0;">
1067 + <?php wp_nonce_field('mxchat_unhide_onboarding'); ?>
1068 + <input type="hidden" name="action" value="mxchat_unhide_onboarding" />
1069 + <button type="submit" class="mxch-btn mxch-btn-secondary mxch-btn-sm">
1070 + <?php esc_html_e('Reopen setup guide', 'mxchat'); ?>
1071 + </button>
1072 + </form>
1073 + </div>
1074 + </div>
1075 + <?php endif; ?>
1076 +
946 1077 <div class="mxch-card" style="margin-bottom: 24px;">
947 1078 <div class="mxch-card-body">
948 1079 <div style="display: flex; align-items: flex-start; gap: 16px;">
949 1080 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="color: var(--mxch-warning); flex-shrink: 0;"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z"/><path d="M12 9v4"/><path d="M12 17h.01"/></svg>
@@ -1259,22 +1390,45 @@
1259 1390 }
1260 1391 });
1261 1392 });
1262 1393
1263 - // Handle initial hash
1264 - const hash = window.location.hash.slice(1);
1265 - if (hash) {
1266 - const targetLink = document.querySelector('[data-target="' + hash + '"]');
1267 - if (targetLink) {
1268 - // Expand parent if it's a sub-link
1269 - const parentItem = targetLink.closest('.mxch-nav-item');
1270 - if (parentItem && targetLink.classList.contains('mxch-nav-sub-link')) {
1271 - parentItem.classList.add('expanded');
1272 - }
1273 - showSection(hash);
1274 - setActiveNav(targetLink);
1394 + // Handle initial deep-link (?tab=<slug> takes precedence over #hash so the
1395 + // Onboarding setup-step CTAs land on the right sub-tab). Whitelist of valid
1396 + // section ids must stay in sync with the .mxch-section[id] values above.
1397 + const validTabs = [
1398 + 'chatbot-ai-models', 'chatbot-behavior', 'chatbot-display',
1399 + 'chatbot-lead-capture', 'chatbot-quick-questions', 'chatbot-rate-limits',
1400 + 'api-keys', 'optimization', 'testing',
1401 + 'integrations-toolbar', 'integrations-loops', 'integrations-brave',
1402 + 'integrations-slack', 'integrations-telegram',
1403 + 'tutorials'
1404 + ];
1405 + function activateTab(target) {
1406 + if (!target) return false;
1407 + if (validTabs.indexOf(target) === -1) return false;
1408 + const targetLink = document.querySelector('[data-target="' + target + '"]');
1409 + if (!targetLink) return false;
1410 + const parentItem = targetLink.closest('.mxch-nav-item');
1411 + if (parentItem && targetLink.classList.contains('mxch-nav-sub-link')) {
1412 + parentItem.classList.add('expanded');
1275 1413 }
1414 + showSection(target);
1415 + setActiveNav(targetLink);
1416 + return true;
1276 1417 }
1418 + const urlParams = new URLSearchParams(window.location.search);
1419 + const tabParam = urlParams.get('tab');
1420 + let tabActivated = false;
1421 + if (tabParam) {
1422 + tabActivated = activateTab(tabParam);
1423 + // Invalid ?tab=… silently falls through to default (#hash or AI Models).
1424 + }
1425 + if (!tabActivated) {
1426 + const hash = window.location.hash.slice(1);
1427 + if (hash) {
1428 + activateTab(hash);
1429 + }
1430 + }
1277 1431
1278 1432 // Rate limit accordion
1279 1433 const rateLimitHeaders = document.querySelectorAll('.mxch-rate-limit-header');
1280 1434 rateLimitHeaders.forEach(header => {
@@ -1285,8 +1439,26 @@
1285 1439 item.classList.toggle('expanded');
1286 1440 });
1287 1441 });
1288 1442
1443 + // Rate-limit "Custom…" toggle: show/hide the integer input below each
1444 + // limit <select> based on whether __custom__ is the active option.
1445 + document.querySelectorAll('.mxch-rate-limit-limit-select').forEach(sel => {
1446 + const targetId = sel.getAttribute('data-mxch-custom-target');
1447 + if (!targetId) return;
1448 + const field = document.querySelector('[data-mxch-custom-for="' + targetId + '"]');
1449 + if (!field) return;
1450 + const sync = () => {
1451 + if (sel.value === '__custom__') {
1452 + field.hidden = false;
1453 + } else {
1454 + field.hidden = true;
1455 + }
1456 + };
1457 + sel.addEventListener('change', sync);
1458 + sync();
1459 + });
1460 +
1289 1461 // =====================================================
1290 1462 // Mobile Menu Functionality
1291 1463 // =====================================================
1292 1464 const mobileMenuBtn = document.querySelector('.mxch-mobile-menu-btn');
@@ -1416,10 +1588,146 @@
1416 1588
1417 1589 $roles = wp_roles()->get_names();
1418 1590 $roles['logged_out'] = __('Logged Out Users', 'mxchat');
1419 1591
1592 + // Whole-chatbot global cap (sits above per-role; defaults to unlimited so
1593 + // existing installs are unchanged). Stored under mxchat_options['rate_limits_global'].
1594 + $global_cfg = isset($all_options['rate_limits_global']) && is_array($all_options['rate_limits_global'])
1595 + ? $all_options['rate_limits_global']
1596 + : array();
1597 + $global_limit_raw = isset($global_cfg['limit']) ? (string) $global_cfg['limit'] : 'unlimited';
1598 + $global_timeframe = isset($global_cfg['timeframe']) ? (string) $global_cfg['timeframe'] : 'daily';
1599 + $global_is_unlimited = ($global_limit_raw === '' || $global_limit_raw === 'unlimited');
1600 + $global_is_preset = !$global_is_unlimited && in_array($global_limit_raw, $rate_limits, true);
1601 + $global_select_value = $global_is_unlimited ? 'unlimited' : ($global_is_preset ? $global_limit_raw : '__custom__');
1602 + $global_custom_value = (!$global_is_unlimited && !$global_is_preset && ctype_digit($global_limit_raw))
1603 + ? $global_limit_raw
1604 + : '';
1605 +
1420 1606 echo '<div class="mxch-rate-limits">';
1421 1607
1608 + // --- Global cap card -------------------------------------------------
1609 + ?>
1610 + <div class="mxch-rate-limit-item mxch-rate-limit-global mxchat-autosave-section">
1611 + <div class="mxch-rate-limit-header">
1612 + <span class="mxch-rate-limit-role"><?php esc_html_e('Total chatbot message limit', 'mxchat'); ?></span>
1613 + <div class="mxch-rate-limit-summary">
1614 + <span class="mxch-rate-limit-badge mxch-rate-limit-global-badge">
1615 + <?php
1616 + if ($global_is_unlimited) {
1617 + esc_html_e('Unlimited', 'mxchat');
1618 + } else {
1619 + $tf_label = isset($timeframes[$global_timeframe]) ? $timeframes[$global_timeframe] : $global_timeframe;
1620 + echo esc_html(($global_custom_value !== '' ? $global_custom_value : $global_limit_raw) . ' / ' . $tf_label);
1621 + }
1622 + ?>
1623 + </span>
1624 + <svg class="mxch-rate-limit-arrow" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>
1625 + </div>
1626 + </div>
1627 + <div class="mxch-rate-limit-body">
1628 + <p class="mxch-field-description" style="margin: 0 0 12px;">
1629 + <?php esc_html_e('A single ceiling across all users and all roles, per timeframe. Independent of the per-role limits below. When both a per-role limit and this global cap are configured, whichever is hit first stops the conversation. Default is Unlimited — existing installs are unchanged.', 'mxchat'); ?>
1630 + </p>
1631 + <div class="mxch-rate-limit-controls">
1632 + <div class="mxch-field">
1633 + <label class="mxch-field-label" for="rate_limits_global_limit"><?php esc_html_e('Limit', 'mxchat'); ?></label>
1634 + <select id="rate_limits_global_limit"
1635 + name="mxchat_options[rate_limits_global][limit]"
1636 + class="mxch-select mxchat-autosave-field mxch-rate-limit-limit-select"
1637 + data-mxch-custom-target="rate_limits_global_limit_custom">
1638 + <?php foreach ($rate_limits as $limit): ?>
1639 + <option value="<?php echo esc_attr($limit); ?>" <?php selected($global_select_value, $limit); ?>>
1640 + <?php echo esc_html($limit === 'unlimited' ? __('Unlimited', 'mxchat') : $limit); ?>
1641 + </option>
1642 + <?php endforeach; ?>
1643 + <option value="__custom__" <?php selected($global_select_value, '__custom__'); ?>><?php esc_html_e('Custom…', 'mxchat'); ?></option>
1644 + </select>
1645 + </div>
1646 + <div class="mxch-field mxch-rate-limit-custom-field" data-mxch-custom-for="rate_limits_global_limit_custom" <?php echo $global_select_value === '__custom__' ? '' : 'hidden'; ?>>
1647 + <label class="mxch-field-label" for="rate_limits_global_limit_custom"><?php esc_html_e('Custom limit', 'mxchat'); ?></label>
1648 + <input type="number"
1649 + min="1"
1650 + step="1"
1651 + id="rate_limits_global_limit_custom"
1652 + name="mxchat_options[rate_limits_global][limit_custom]"
1653 + class="mxch-input mxchat-autosave-field mxch-rate-limit-custom-input"
1654 + placeholder="e.g. 250"
1655 + value="<?php echo esc_attr($global_custom_value); ?>" />
1656 + </div>
1657 + <div class="mxch-field">
1658 + <label class="mxch-field-label" for="rate_limits_global_timeframe"><?php esc_html_e('Timeframe', 'mxchat'); ?></label>
1659 + <select id="rate_limits_global_timeframe"
1660 + name="mxchat_options[rate_limits_global][timeframe]"
1661 + class="mxch-select mxchat-autosave-field">
1662 + <?php foreach ($timeframes as $value => $label): ?>
1663 + <option value="<?php echo esc_attr($value); ?>" <?php selected($global_timeframe, $value); ?>><?php echo esc_html($label); ?></option>
1664 + <?php endforeach; ?>
1665 + </select>
1666 + </div>
1667 + </div>
1668 + <?php
1669 + // --- Current usage readout (read-only; mirrors the integrator's
1670 + // window-reset math at READ time — never writes the counter here).
1671 + // v1 surfaces the DEFAULT bot's pool. TODO: per-bot selector if asked.
1672 + $global_usage_limit_int = (!$global_is_unlimited && ctype_digit($global_limit_raw))
1673 + ? (int) $global_limit_raw
1674 + : 0;
1675 + $usage_raw = get_option('mxchat_chat_limit_default_global', array('count' => 0, 'timestamp' => time()));
1676 + $usage_count = isset($usage_raw['count']) ? (int) $usage_raw['count'] : 0;
1677 + $usage_ts = isset($usage_raw['timestamp']) ? (int) $usage_raw['timestamp'] : time();
1678 + $usage_windows = array('hourly' => 3600, 'daily' => 86400, 'weekly' => 604800, 'monthly' => 2592000);
1679 + $usage_window = isset($usage_windows[$global_timeframe]) ? $usage_windows[$global_timeframe] : 86400;
1680 + $usage_now = time();
1681 + if (($usage_now - $usage_ts) >= $usage_window) {
1682 + // Window elapsed since the stored timestamp → effective count is 0.
1683 + $usage_count = 0;
1684 + $usage_reset_at = $usage_now + $usage_window;
1685 + } else {
1686 + $usage_reset_at = $usage_ts + $usage_window;
1687 + }
1688 + $usage_left = max(0, $global_usage_limit_int - $usage_count);
1689 + $usage_pct = ($global_usage_limit_int > 0)
1690 + ? min(100, (int) round(($usage_count / $global_usage_limit_int) * 100))
1691 + : 0;
1692 + $usage_reset_nonce = wp_create_nonce('mxchat_reset_global_usage');
1693 + ?>
1694 + <div class="mxch-rate-limit-usage"
1695 + id="mxch-global-usage"
1696 + data-bot-id="default"
1697 + data-reset-nonce="<?php echo esc_attr($usage_reset_nonce); ?>"
1698 + <?php echo $global_is_unlimited ? 'hidden' : ''; ?>>
1699 + <div class="mxch-rate-limit-usage-head">
1700 + <span class="mxch-field-label" style="margin:0;"><?php esc_html_e('Current usage', 'mxchat'); ?></span>
1701 + <button type="button" class="mxch-btn mxch-btn-secondary mxch-btn-sm" id="mxch-global-usage-reset"><?php esc_html_e('Reset counter', 'mxchat'); ?></button>
1702 + </div>
1703 + <div class="mxch-progress-bar">
1704 + <div class="mxch-progress-bar-fill" id="mxch-global-usage-fill" style="width: <?php echo esc_attr($usage_pct); ?>%;"></div>
1705 + </div>
1706 + <p class="mxch-progress-label" id="mxch-global-usage-text" style="margin:0;">
1707 + <?php
1708 + printf(
1709 + /* translators: 1: used count, 2: limit, 3: remaining, 4: human-readable time until reset */
1710 + esc_html__('%1$s of %2$s used · %3$s left · resets in %4$s', 'mxchat'),
1711 + esc_html(number_format_i18n($usage_count)),
1712 + esc_html(number_format_i18n($global_usage_limit_int)),
1713 + esc_html(number_format_i18n($usage_left)),
1714 + esc_html(human_time_diff($usage_now, $usage_reset_at))
1715 + );
1716 + ?>
1717 + </p>
1718 + </div>
1719 + <p class="mxch-field-description mxch-rate-limit-usage-unlimited"
1720 + id="mxch-global-usage-unlimited"
1721 + style="margin: 12px 0 0;"
1722 + <?php echo $global_is_unlimited ? '' : 'hidden'; ?>>
1723 + <?php esc_html_e('Unlimited — no cap. Usage tracking applies only when a numeric limit is set.', 'mxchat'); ?>
1724 + </p>
1725 + </div>
1726 + </div>
1727 + <?php
1728 + // ---------------------------------------------------------------------
1729 +
1422 1730 foreach ($roles as $role_id => $role_name) {
1423 1731 $default_limit = ($role_id === 'logged_out') ? '10' : '100';
1424 1732 $default_timeframe = 'daily';
1425 1733 $default_message = __('Rate limit exceeded. Please try again later.', 'mxchat');
@@ -1424,9 +1732,9 @@
1424 1732 $default_timeframe = 'daily';
1425 1733 $default_message = __('Rate limit exceeded. Please try again later.', 'mxchat');
1426 1734
1427 1735 $selected_limit = isset($all_options['rate_limits'][$role_id]['limit'])
1428 - ? $all_options['rate_limits'][$role_id]['limit']
1736 + ? (string) $all_options['rate_limits'][$role_id]['limit']
1429 1737 : $default_limit;
1430 1738
1431 1739 $selected_timeframe = isset($all_options['rate_limits'][$role_id]['timeframe'])
1432 1740 ? $all_options['rate_limits'][$role_id]['timeframe']
@@ -1436,8 +1744,16 @@
1436 1744 ? $all_options['rate_limits'][$role_id]['message']
1437 1745 : $default_message;
1438 1746
1439 1747 $timeframe_label = isset($timeframes[$selected_timeframe]) ? $timeframes[$selected_timeframe] : $selected_timeframe;
1748 +
1749 + // Custom-value handling: if the stored limit is not in the preset list
1750 + // and not 'unlimited', it's a custom integer. The select shows __custom__
1751 + // and the number input below carries the actual value.
1752 + $is_preset_limit = in_array($selected_limit, $rate_limits, true);
1753 + $role_select_value = $is_preset_limit ? $selected_limit : '__custom__';
1754 + $role_custom_value = (!$is_preset_limit && ctype_digit($selected_limit)) ? $selected_limit : '';
1755 + $custom_field_id = 'rate_limits_' . $role_id . '_limit_custom';
1440 1756 ?>
1441 1757 <div class="mxch-rate-limit-item">
1442 1758 <div class="mxch-rate-limit-header">
1443 1759 <span class="mxch-rate-limit-role"><?php echo esc_html($role_name); ?></span>
@@ -1451,13 +1767,28 @@
1451 1767 <div class="mxch-field">
1452 1768 <label class="mxch-field-label" for="rate_limits_<?php echo esc_attr($role_id); ?>_limit"><?php esc_html_e('Limit', 'mxchat'); ?></label>
1453 1769 <select id="rate_limits_<?php echo esc_attr($role_id); ?>_limit"
1454 1770 name="mxchat_options[rate_limits][<?php echo esc_attr($role_id); ?>][limit]"
1455 - class="mxch-select mxchat-autosave-field">
1771 + class="mxch-select mxchat-autosave-field mxch-rate-limit-limit-select"
1772 + data-mxch-custom-target="<?php echo esc_attr($custom_field_id); ?>">
1456 1773 <?php foreach ($rate_limits as $limit): ?>
1457 - <option value="<?php echo esc_attr($limit); ?>" <?php selected($selected_limit, $limit); ?>><?php echo esc_html($limit); ?></option>
1774 + <option value="<?php echo esc_attr($limit); ?>" <?php selected($role_select_value, $limit); ?>>
1775 + <?php echo esc_html($limit === 'unlimited' ? __('Unlimited', 'mxchat') : $limit); ?>
1776 + </option>
1458 1777 <?php endforeach; ?>
1778 + <option value="__custom__" <?php selected($role_select_value, '__custom__'); ?>><?php esc_html_e('Custom…', 'mxchat'); ?></option>
1459 1779 </select>
1780 + </div>
1781 + <div class="mxch-field mxch-rate-limit-custom-field" data-mxch-custom-for="<?php echo esc_attr($custom_field_id); ?>" <?php echo $role_select_value === '__custom__' ? '' : 'hidden'; ?>>
1782 + <label class="mxch-field-label" for="<?php echo esc_attr($custom_field_id); ?>"><?php esc_html_e('Custom limit', 'mxchat'); ?></label>
1783 + <input type="number"
1784 + min="1"
1785 + step="1"
1786 + id="<?php echo esc_attr($custom_field_id); ?>"
1787 + name="mxchat_options[rate_limits][<?php echo esc_attr($role_id); ?>][limit_custom]"
1788 + class="mxch-input mxchat-autosave-field mxch-rate-limit-custom-input"
1789 + placeholder="e.g. 250"
1790 + value="<?php echo esc_attr($role_custom_value); ?>" />
1460 1791 </div>
1461 1792 <div class="mxch-field">
1462 1793 <label class="mxch-field-label" for="rate_limits_<?php echo esc_attr($role_id); ?>_timeframe"><?php esc_html_e('Timeframe', 'mxchat'); ?></label>
1463 1794 <select id="rate_limits_<?php echo esc_attr($role_id); ?>_timeframe"