| @@ -1,356 +1,279 @@ | ||
| 1 | -<?php | |
| 2 | -if (!defined('ABSPATH')) { | |
| 3 | - exit; // Exit if accessed directly | |
| 4 | -} | |
| 5 | - | |
| 6 | -/** | |
| 7 | - * Enhanced meta box for page-level chatbot visibility and bot selection | |
| 8 | - */ | |
| 9 | -class MxChat_Meta_Box { | |
| 10 | - | |
| 11 | - public function __construct() { | |
| 12 | - add_action('add_meta_boxes', array($this, 'add_chatbot_meta_box')); | |
| 13 | - add_action('save_post', array($this, 'save_chatbot_meta_box')); | |
| 14 | - add_action('enqueue_block_editor_assets', array($this, 'enqueue_gutenberg_assets')); | |
| 15 | - add_action('init', array($this, 'register_meta_fields')); | |
| 16 | - } | |
| 17 | - | |
| 18 | - /** | |
| 19 | - * Add meta box to posts and pages | |
| 20 | - */ | |
| 21 | - public function add_chatbot_meta_box() { | |
| 22 | - $post_types = get_post_types(array('public' => true), 'names'); | |
| 23 | - | |
| 24 | - foreach ($post_types as $post_type) { | |
| 25 | - add_meta_box( | |
| 26 | - 'mxchat_visibility', | |
| 27 | - __('MxChat Settings', 'mxchat'), | |
| 28 | - array($this, 'render_meta_box'), | |
| 29 | - $post_type, | |
| 30 | - 'side', | |
| 31 | - 'default' | |
| 32 | - ); | |
| 33 | - } | |
| 34 | - } | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * Register meta fields for Gutenberg | |
| 38 | - */ | |
| 39 | - public function register_meta_fields() { | |
| 40 | - // Legacy field - kept for backward compatibility | |
| 41 | - register_post_meta('', '_mxchat_hide_chatbot', array( | |
| 42 | - 'show_in_rest' => true, | |
| 43 | - 'single' => true, | |
| 44 | - 'type' => 'string', | |
| 45 | - 'default' => '', | |
| 46 | - 'auth_callback' => function() { | |
| 47 | - return current_user_can('edit_posts'); | |
| 48 | - } | |
| 49 | - )); | |
| 50 | - | |
| 51 | - // New visibility field: '' (global), 'show', 'hide' | |
| 52 | - register_post_meta('', '_mxchat_page_visibility', array( | |
| 53 | - 'show_in_rest' => true, | |
| 54 | - 'single' => true, | |
| 55 | - 'type' => 'string', | |
| 56 | - 'default' => '', | |
| 57 | - 'auth_callback' => function() { | |
| 58 | - return current_user_can('edit_posts'); | |
| 59 | - } | |
| 60 | - )); | |
| 61 | - | |
| 62 | - register_post_meta('', '_mxchat_selected_bot', array( | |
| 63 | - 'show_in_rest' => true, | |
| 64 | - 'single' => true, | |
| 65 | - 'type' => 'string', | |
| 66 | - 'default' => '', | |
| 67 | - 'auth_callback' => function() { | |
| 68 | - return current_user_can('edit_posts'); | |
| 69 | - } | |
| 70 | - )); | |
| 71 | - } | |
| 72 | - | |
| 73 | - /** | |
| 74 | - * Get available bots | |
| 75 | - */ | |
| 76 | - private function get_available_bots() { | |
| 77 | - $bots = array(); | |
| 78 | - | |
| 79 | - // Check if multi-bot addon is active | |
| 80 | - if (class_exists('MxChat_Multi_Bot_Manager')) { | |
| 81 | - $multi_bot_manager = MxChat_Multi_Bot_Core_Manager::get_instance(); | |
| 82 | - $available_bots = $multi_bot_manager->get_available_bots(); | |
| 83 | - | |
| 84 | - // Add the available bots | |
| 85 | - foreach ($available_bots as $bot_id => $bot_name) { | |
| 86 | - $bots[$bot_id] = $bot_name; | |
| 87 | - } | |
| 88 | - } else { | |
| 89 | - // Only default bot available | |
| 90 | - $bots['default'] = __('Default Bot', 'mxchat'); | |
| 91 | - } | |
| 92 | - | |
| 93 | - return $bots; | |
| 94 | - } | |
| 95 | - | |
| 96 | - /** | |
| 97 | - * Check if global auto-show is enabled | |
| 98 | - */ | |
| 99 | - private function is_global_autoshow_enabled() { | |
| 100 | - $options = get_option('mxchat_options', array()); | |
| 101 | - return isset($options['append_to_body']) && $options['append_to_body'] === 'on'; | |
| 102 | - } | |
| 103 | - | |
| 104 | - /** | |
| 105 | - * Get the effective visibility value for a post, with backward compatibility | |
| 106 | - */ | |
| 107 | - private function get_effective_visibility($post_id) { | |
| 108 | - // Check new field first | |
| 109 | - $visibility = get_post_meta($post_id, '_mxchat_page_visibility', true); | |
| 110 | - | |
| 111 | - if (!empty($visibility)) { | |
| 112 | - return $visibility; | |
| 113 | - } | |
| 114 | - | |
| 115 | - // Backward compat: check legacy hide checkbox | |
| 116 | - $hide_chatbot = get_post_meta($post_id, '_mxchat_hide_chatbot', true); | |
| 117 | - if ($hide_chatbot === '1') { | |
| 118 | - return 'hide'; | |
| 119 | - } | |
| 120 | - | |
| 121 | - return ''; // Global default | |
| 122 | - } | |
| 123 | - | |
| 124 | - /** | |
| 125 | - * Render meta box with 3-option visibility control | |
| 126 | - */ | |
| 127 | - public function render_meta_box($post) { | |
| 128 | - wp_nonce_field('mxchat_meta_box_nonce', 'mxchat_meta_box_nonce'); | |
| 129 | - | |
| 130 | - $visibility = $this->get_effective_visibility($post->ID); | |
| 131 | - $selected_bot = get_post_meta($post->ID, '_mxchat_selected_bot', true); | |
| 132 | - $available_bots = $this->get_available_bots(); | |
| 133 | - $global_autoshow = $this->is_global_autoshow_enabled(); | |
| 134 | - $has_multibot = class_exists('MxChat_Multi_Bot_Manager'); | |
| 135 | - | |
| 136 | - ?> | |
| 137 | - <div style="padding: 10px 0;"> | |
| 138 | - | |
| 139 | - <!-- Chatbot Visibility --> | |
| 140 | - <div style="margin-bottom: 15px;"> | |
| 141 | - <label style="display: block; margin-bottom: 8px; font-weight: 600;"> | |
| 142 | - <?php _e('Chatbot Visibility', 'mxchat'); ?> | |
| 143 | - </label> | |
| 144 | - | |
| 145 | - <label style="display: flex; align-items: center; margin-bottom: 6px; cursor: pointer;"> | |
| 146 | - <input type="radio" | |
| 147 | - name="mxchat_page_visibility" | |
| 148 | - value="" | |
| 149 | - <?php checked($visibility, ''); ?> | |
| 150 | - style="margin-right: 8px;" /> | |
| 151 | - <?php _e('Use Global Setting', 'mxchat'); ?> | |
| 152 | - </label> | |
| 153 | - | |
| 154 | - <label style="display: flex; align-items: center; margin-bottom: 6px; cursor: pointer;"> | |
| 155 | - <input type="radio" | |
| 156 | - name="mxchat_page_visibility" | |
| 157 | - value="show" | |
| 158 | - <?php checked($visibility, 'show'); ?> | |
| 159 | - style="margin-right: 8px;" /> | |
| 160 | - <?php _e('Show Chatbot on this page', 'mxchat'); ?> | |
| 161 | - </label> | |
| 162 | - | |
| 163 | - <label style="display: flex; align-items: center; cursor: pointer;"> | |
| 164 | - <input type="radio" | |
| 165 | - name="mxchat_page_visibility" | |
| 166 | - value="hide" | |
| 167 | - <?php checked($visibility, 'hide'); ?> | |
| 168 | - style="margin-right: 8px;" /> | |
| 169 | - <?php _e('Hide Chatbot on this page', 'mxchat'); ?> | |
| 170 | - </label> | |
| 171 | - | |
| 172 | - <div style="margin-top: 8px; padding: 8px; background: #f8f9fa; border-left: 3px solid #007cba; font-size: 12px;"> | |
| 173 | - <?php if ($global_autoshow) : ?> | |
| 174 | - <p style="margin: 0 0 5px 0; color: #0073aa;"> | |
| 175 | - <strong><?php _e('Global Auto-Show: ON', 'mxchat'); ?></strong> | |
| 176 | - </p> | |
| 177 | - <p style="margin: 0; color: #666;"> | |
| 178 | - <?php _e('The chatbot appears on all pages by default. Use "Hide" to exclude this page, or "Show" to override post type restrictions.', 'mxchat'); ?> | |
| 179 | - </p> | |
| 180 | - <?php else : ?> | |
| 181 | - <p style="margin: 0 0 5px 0; color: #d63638;"> | |
| 182 | - <strong><?php _e('Global Auto-Show: OFF', 'mxchat'); ?></strong> | |
| 183 | - </p> | |
| 184 | - <p style="margin: 0; color: #666;"> | |
| 185 | - <?php _e('The chatbot is hidden by default. Select "Show" to display it on this page without needing a shortcode.', 'mxchat'); ?> | |
| 186 | - </p> | |
| 187 | - <?php endif; ?> | |
| 188 | - </div> | |
| 189 | - </div> | |
| 190 | - | |
| 191 | - <!-- Bot Selection (only show if multi-bot is available) --> | |
| 192 | - <?php if ($has_multibot && count($available_bots) > 1) : ?> | |
| 193 | - <div style="margin-bottom: 15px;"> | |
| 194 | - <label style="display: block; margin-bottom: 5px; font-weight: 600;"> | |
| 195 | - <?php _e('Select Bot for this Page:', 'mxchat'); ?> | |
| 196 | - </label> | |
| 197 | - <select name="mxchat_selected_bot" style="width: 100%;"> | |
| 198 | - <option value=""><?php _e('Use Default Bot', 'mxchat'); ?></option> | |
| 199 | - <?php foreach ($available_bots as $bot_id => $bot_name) : ?> | |
| 200 | - <option value="<?php echo esc_attr($bot_id); ?>" <?php selected($selected_bot, $bot_id); ?>> | |
| 201 | - <?php echo esc_html($bot_name); ?> | |
| 202 | - </option> | |
| 203 | - <?php endforeach; ?> | |
| 204 | - </select> | |
| 205 | - <p style="font-size: 12px; color: #666; margin-top: 5px; font-style: italic;"> | |
| 206 | - <?php _e('Only applies when visibility is set to "Show" or "Use Global Setting" with auto-show enabled.', 'mxchat'); ?> | |
| 207 | - </p> | |
| 208 | - </div> | |
| 209 | - <?php endif; ?> | |
| 210 | - | |
| 211 | - <!-- Preview Notice --> | |
| 212 | - <div style="margin-top: 15px; padding: 8px; background: #fff3cd; border-left: 3px solid #ffc107; font-size: 12px;"> | |
| 213 | - <p style="margin: 0; color: #856404;"> | |
| 214 | - <strong><?php _e('Important:', 'mxchat'); ?></strong> | |
| 215 | - <?php _e('Changes only take effect after saving/updating the page.', 'mxchat'); ?> | |
| 216 | - </p> | |
| 217 | - </div> | |
| 218 | - | |
| 219 | - <!-- Shortcode Examples --> | |
| 220 | - <div style="margin-top: 10px; padding: 8px; background: #e7f3ff; border-left: 3px solid #2196f3; font-size: 12px;"> | |
| 221 | - <p style="margin: 0 0 5px 0; color: #1565c0;"> | |
| 222 | - <strong><?php _e('Shortcode Examples:', 'mxchat'); ?></strong> | |
| 223 | - </p> | |
| 224 | - <p style="margin: 0; color: #666; font-family: monospace;"> | |
| 225 | - <?php _e('[mxchat_chatbot floating="yes"] - Floating chatbot', 'mxchat'); ?><br> | |
| 226 | - <?php _e('[mxchat_chatbot floating="no"] - Embedded chatbot', 'mxchat'); ?> | |
| 227 | - </p> | |
| 228 | - </div> | |
| 229 | - </div> | |
| 230 | - <?php | |
| 231 | - } | |
| 232 | - | |
| 233 | - /** | |
| 234 | - * Save meta box data | |
| 235 | - */ | |
| 236 | - public function save_chatbot_meta_box($post_id) { | |
| 237 | - if (!isset($_POST['mxchat_meta_box_nonce']) || | |
| 238 | - !wp_verify_nonce($_POST['mxchat_meta_box_nonce'], 'mxchat_meta_box_nonce')) { | |
| 239 | - return; | |
| 240 | - } | |
| 241 | - | |
| 242 | - if (!current_user_can('edit_post', $post_id)) { | |
| 243 | - return; | |
| 244 | - } | |
| 245 | - | |
| 246 | - if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { | |
| 247 | - return; | |
| 248 | - } | |
| 249 | - | |
| 250 | - // Save new visibility field | |
| 251 | - $visibility = isset($_POST['mxchat_page_visibility']) ? sanitize_text_field($_POST['mxchat_page_visibility']) : ''; | |
| 252 | - | |
| 253 | - if (in_array($visibility, array('show', 'hide'), true)) { | |
| 254 | - update_post_meta($post_id, '_mxchat_page_visibility', $visibility); | |
| 255 | - } else { | |
| 256 | - delete_post_meta($post_id, '_mxchat_page_visibility'); | |
| 257 | - } | |
| 258 | - | |
| 259 | - // Sync legacy field for backward compat with any external code | |
| 260 | - if ($visibility === 'hide') { | |
| 261 | - update_post_meta($post_id, '_mxchat_hide_chatbot', '1'); | |
| 262 | - } else { | |
| 263 | - delete_post_meta($post_id, '_mxchat_hide_chatbot'); | |
| 264 | - } | |
| 265 | - | |
| 266 | - // Save selected bot setting | |
| 267 | - if (isset($_POST['mxchat_selected_bot'])) { | |
| 268 | - $selected_bot = sanitize_text_field($_POST['mxchat_selected_bot']); | |
| 269 | - if (!empty($selected_bot)) { | |
| 270 | - update_post_meta($post_id, '_mxchat_selected_bot', $selected_bot); | |
| 271 | - } else { | |
| 272 | - delete_post_meta($post_id, '_mxchat_selected_bot'); | |
| 273 | - } | |
| 274 | - } | |
| 275 | - } | |
| 276 | - | |
| 277 | - /** | |
| 278 | - * Enqueue Gutenberg assets | |
| 279 | - */ | |
| 280 | - public function enqueue_gutenberg_assets() { | |
| 281 | - $available_bots = $this->get_available_bots(); | |
| 282 | - $global_autoshow = $this->is_global_autoshow_enabled(); | |
| 283 | - $has_multibot = class_exists('MxChat_Multi_Bot_Manager'); | |
| 284 | - | |
| 285 | - // Enqueue the JavaScript file | |
| 286 | - wp_enqueue_script( | |
| 287 | - 'mxchat-meta-box', | |
| 288 | - plugin_dir_url(__FILE__) . '../js/meta-box.js', | |
| 289 | - array('wp-edit-post', 'wp-element', 'wp-components', 'wp-data', 'wp-plugins'), | |
| 290 | - '1.0.0', | |
| 291 | - true | |
| 292 | - ); | |
| 293 | - | |
| 294 | - // Localize script with data | |
| 295 | - wp_localize_script('mxchat-meta-box', 'mxchatMetaBox', array( | |
| 296 | - 'availableBots' => $available_bots, | |
| 297 | - 'globalAutoshow' => $global_autoshow, | |
| 298 | - 'hasMultibot' => $has_multibot, | |
| 299 | - 'strings' => array( | |
| 300 | - 'panelTitle' => __('MxChat Settings', 'mxchat'), | |
| 301 | - 'visibilityLabel' => __('Chatbot Visibility', 'mxchat'), | |
| 302 | - 'useGlobalSetting' => __('Use Global Setting', 'mxchat'), | |
| 303 | - 'showChatbot' => __('Show Chatbot on this page', 'mxchat'), | |
| 304 | - 'hideChatbot' => __('Hide Chatbot on this page', 'mxchat'), | |
| 305 | - 'selectBot' => __('Select Bot for this Page', 'mxchat'), | |
| 306 | - 'useDefaultBot' => __('Use Default Bot', 'mxchat'), | |
| 307 | - 'globalAutoshowOn' => __('Global Auto-Show is ON. The chatbot appears on all pages by default. Use "Hide" to exclude this page.', 'mxchat'), | |
| 308 | - 'globalAutoshowOff' => __('Global Auto-Show is OFF. Select "Show" to display the chatbot on this page without needing a shortcode.', 'mxchat') | |
| 309 | - ) | |
| 310 | - )); | |
| 311 | - } | |
| 312 | - | |
| 313 | - /** | |
| 314 | - * Helper function to get the bot that should display on current page | |
| 315 | - * Returns: array with 'action' (hide/show/global) and optional 'bot_id' | |
| 316 | - */ | |
| 317 | - public static function get_page_bot_setting($post_id = null) { | |
| 318 | - if (!$post_id) { | |
| 319 | - $post_id = get_the_ID(); | |
| 320 | - } | |
| 321 | - | |
| 322 | - if (!$post_id) { | |
| 323 | - return null; | |
| 324 | - } | |
| 325 | - | |
| 326 | - // Check new visibility field first | |
| 327 | - $visibility = get_post_meta($post_id, '_mxchat_page_visibility', true); | |
| 328 | - | |
| 329 | - if ($visibility === 'hide') { | |
| 330 | - return array('action' => 'hide'); | |
| 331 | - } | |
| 332 | - | |
| 333 | - if ($visibility === 'show') { | |
| 334 | - $selected_bot = get_post_meta($post_id, '_mxchat_selected_bot', true); | |
| 335 | - $bot_id = !empty($selected_bot) ? $selected_bot : 'default'; | |
| 336 | - return array('action' => 'show', 'bot_id' => $bot_id); | |
| 337 | - } | |
| 338 | - | |
| 339 | - // Backward compat: check legacy hide checkbox if no new field set | |
| 340 | - if (empty($visibility)) { | |
| 341 | - $hide_chatbot = get_post_meta($post_id, '_mxchat_hide_chatbot', true); | |
| 342 | - if ($hide_chatbot === '1') { | |
| 343 | - return array('action' => 'hide'); | |
| 344 | - } | |
| 345 | - } | |
| 346 | - | |
| 347 | - // Check if specific bot is selected (legacy path) | |
| 348 | - $selected_bot = get_post_meta($post_id, '_mxchat_selected_bot', true); | |
| 349 | - if (!empty($selected_bot)) { | |
| 350 | - return array('action' => 'show', 'bot_id' => $selected_bot); | |
| 351 | - } | |
| 352 | - | |
| 353 | - // Use global setting | |
| 354 | - return array('action' => 'global'); | |
| 355 | - } | |
| 1 | +<?php | |
| 2 | +if (!defined('ABSPATH')) { | |
| 3 | + exit; // Exit if accessed directly | |
| 4 | +} | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * Enhanced meta box to hide chatbot and select specific bot on pages | |
| 8 | + */ | |
| 9 | +class MxChat_Meta_Box { | |
| 10 | + | |
| 11 | + public function __construct() { | |
| 12 | + add_action('add_meta_boxes', array($this, 'add_chatbot_meta_box')); | |
| 13 | + add_action('save_post', array($this, 'save_chatbot_meta_box')); | |
| 14 | + add_action('enqueue_block_editor_assets', array($this, 'enqueue_gutenberg_assets')); | |
| 15 | + add_action('init', array($this, 'register_meta_fields')); | |
| 16 | + } | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * Add meta box to posts and pages | |
| 20 | + */ | |
| 21 | + public function add_chatbot_meta_box() { | |
| 22 | + $post_types = get_post_types(array('public' => true), 'names'); | |
| 23 | + | |
| 24 | + foreach ($post_types as $post_type) { | |
| 25 | + add_meta_box( | |
| 26 | + 'mxchat_visibility', | |
| 27 | + __('MxChat Settings', 'mxchat'), | |
| 28 | + array($this, 'render_meta_box'), | |
| 29 | + $post_type, | |
| 30 | + 'side', | |
| 31 | + 'default' | |
| 32 | + ); | |
| 33 | + } | |
| 34 | + } | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * Register meta fields for Gutenberg | |
| 38 | + */ | |
| 39 | + public function register_meta_fields() { | |
| 40 | + register_post_meta('', '_mxchat_hide_chatbot', array( | |
| 41 | + 'show_in_rest' => true, | |
| 42 | + 'single' => true, | |
| 43 | + 'type' => 'string', | |
| 44 | + 'default' => '', | |
| 45 | + 'auth_callback' => function() { | |
| 46 | + return current_user_can('edit_posts'); | |
| 47 | + } | |
| 48 | + )); | |
| 49 | + | |
| 50 | + register_post_meta('', '_mxchat_selected_bot', array( | |
| 51 | + 'show_in_rest' => true, | |
| 52 | + 'single' => true, | |
| 53 | + 'type' => 'string', | |
| 54 | + 'default' => '', | |
| 55 | + 'auth_callback' => function() { | |
| 56 | + return current_user_can('edit_posts'); | |
| 57 | + } | |
| 58 | + )); | |
| 59 | + } | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Get available bots | |
| 63 | + */ | |
| 64 | + private function get_available_bots() { | |
| 65 | + $bots = array(); | |
| 66 | + | |
| 67 | + // Check if multi-bot addon is active | |
| 68 | + if (class_exists('MxChat_Multi_Bot_Manager')) { | |
| 69 | + $multi_bot_manager = MxChat_Multi_Bot_Core_Manager::get_instance(); | |
| 70 | + $available_bots = $multi_bot_manager->get_available_bots(); | |
| 71 | + | |
| 72 | + // Add the available bots | |
| 73 | + foreach ($available_bots as $bot_id => $bot_name) { | |
| 74 | + $bots[$bot_id] = $bot_name; | |
| 75 | + } | |
| 76 | + } else { | |
| 77 | + // Only default bot available | |
| 78 | + $bots['default'] = __('Default Bot', 'mxchat'); | |
| 79 | + } | |
| 80 | + | |
| 81 | + return $bots; | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * Check if global auto-show is enabled | |
| 86 | + */ | |
| 87 | + private function is_global_autoshow_enabled() { | |
| 88 | + $options = get_option('mxchat_options', array()); | |
| 89 | + return isset($options['append_to_body']) && $options['append_to_body'] === 'on'; | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * Updated render_meta_box method with clarified copy | |
| 94 | + */ | |
| 95 | + public function render_meta_box($post) { | |
| 96 | + wp_nonce_field('mxchat_meta_box_nonce', 'mxchat_meta_box_nonce'); | |
| 97 | + | |
| 98 | + $hide_chatbot = get_post_meta($post->ID, '_mxchat_hide_chatbot', true); | |
| 99 | + $selected_bot = get_post_meta($post->ID, '_mxchat_selected_bot', true); | |
| 100 | + $available_bots = $this->get_available_bots(); | |
| 101 | + $global_autoshow = $this->is_global_autoshow_enabled(); | |
| 102 | + $has_multibot = class_exists('MxChat_Multi_Bot_Manager'); | |
| 103 | + | |
| 104 | + ?> | |
| 105 | + <div style="padding: 10px 0;"> | |
| 106 | + | |
| 107 | + <!-- Hide Chatbot Option --> | |
| 108 | + <div style="margin-bottom: 15px;"> | |
| 109 | + <label style="display: flex; align-items: center;"> | |
| 110 | + <input type="checkbox" | |
| 111 | + name="mxchat_hide_chatbot" | |
| 112 | + value="1" | |
| 113 | + <?php checked($hide_chatbot, '1'); ?> | |
| 114 | + style="margin-right: 8px;" /> | |
| 115 | + <?php _e('Hide auto-floating chatbot on this page', 'mxchat'); ?> | |
| 116 | + </label> | |
| 117 | + <p style="font-size: 12px; color: #666; margin-top: 5px; font-style: italic;"> | |
| 118 | + <?php _e('Prevents the floating chatbot from auto-appearing. Embedded chatbots (floating="no") will still work.', 'mxchat'); ?> | |
| 119 | + </p> | |
| 120 | + </div> | |
| 121 | + | |
| 122 | + <!-- Bot Selection (only show if multi-bot is available) --> | |
| 123 | + <?php if ($has_multibot && count($available_bots) > 1) : ?> | |
| 124 | + <div style="margin-bottom: 15px;"> | |
| 125 | + <label style="display: block; margin-bottom: 5px; font-weight: 600;"> | |
| 126 | + <?php _e('Select Bot for this Page:', 'mxchat'); ?> | |
| 127 | + </label> | |
| 128 | + <select name="mxchat_selected_bot" style="width: 100%;"> | |
| 129 | + <option value=""><?php _e('Use Global Setting', 'mxchat'); ?></option> | |
| 130 | + <?php foreach ($available_bots as $bot_id => $bot_name) : ?> | |
| 131 | + <option value="<?php echo esc_attr($bot_id); ?>" <?php selected($selected_bot, $bot_id); ?>> | |
| 132 | + <?php echo esc_html($bot_name); ?> | |
| 133 | + </option> | |
| 134 | + <?php endforeach; ?> | |
| 135 | + </select> | |
| 136 | + | |
| 137 | + <div style="margin-top: 8px; padding: 8px; background: #f8f9fa; border-left: 3px solid #007cba; font-size: 12px;"> | |
| 138 | + <?php if ($global_autoshow) : ?> | |
| 139 | + <p style="margin: 0 0 5px 0; color: #0073aa;"> | |
| 140 | + <strong><?php _e('Global Auto-Show: ON', 'mxchat'); ?></strong> | |
| 141 | + </p> | |
| 142 | + <p style="margin: 0; color: #666;"> | |
| 143 | + <?php _e('• "Use Global Setting" = Default bot will show automatically', 'mxchat'); ?><br> | |
| 144 | + <?php _e('• Select specific bot = That bot will show instead', 'mxchat'); ?><br> | |
| 145 | + <?php _e('• Check "Hide auto-floating" above = No auto bot, but shortcodes still work', 'mxchat'); ?> | |
| 146 | + </p> | |
| 147 | + <?php else : ?> | |
| 148 | + <p style="margin: 0 0 5px 0; color: #d63638;"> | |
| 149 | + <strong><?php _e('Global Auto-Show: OFF', 'mxchat'); ?></strong> | |
| 150 | + </p> | |
| 151 | + <p style="margin: 0; color: #666;"> | |
| 152 | + <?php _e('• "Use Global Setting" = No bot will show (use shortcodes)', 'mxchat'); ?><br> | |
| 153 | + <?php _e('• Select specific bot = That bot will show on this page only', 'mxchat'); ?> | |
| 154 | + </p> | |
| 155 | + <?php endif; ?> | |
| 156 | + </div> | |
| 157 | + </div> | |
| 158 | + <?php endif; ?> | |
| 159 | + | |
| 160 | + <!-- Preview Notice --> | |
| 161 | + <div style="margin-top: 15px; padding: 8px; background: #fff3cd; border-left: 3px solid #ffc107; font-size: 12px;"> | |
| 162 | + <p style="margin: 0; color: #856404;"> | |
| 163 | + <strong><?php _e('Important:', 'mxchat'); ?></strong> | |
| 164 | + <?php _e('Bot changes only take effect after saving/updating the page. Preview mode will show the previously saved settings.', 'mxchat'); ?> | |
| 165 | + </p> | |
| 166 | + </div> | |
| 167 | + | |
| 168 | + <!-- Shortcode Examples --> | |
| 169 | + <div style="margin-top: 10px; padding: 8px; background: #e7f3ff; border-left: 3px solid #2196f3; font-size: 12px;"> | |
| 170 | + <p style="margin: 0 0 5px 0; color: #1565c0;"> | |
| 171 | + <strong><?php _e('Shortcode Examples:', 'mxchat'); ?></strong> | |
| 172 | + </p> | |
| 173 | + <p style="margin: 0; color: #666; font-family: monospace;"> | |
| 174 | + <?php _e('[mxchat_chatbot floating="yes"] - Floating chatbot', 'mxchat'); ?><br> | |
| 175 | + <?php _e('[mxchat_chatbot floating="no"] - Embedded chatbot (always works)', 'mxchat'); ?> | |
| 176 | + </p> | |
| 177 | + </div> | |
| 178 | + </div> | |
| 179 | + <?php | |
| 180 | + } | |
| 181 | + | |
| 182 | + /** | |
| 183 | + * Save meta box data | |
| 184 | + */ | |
| 185 | + public function save_chatbot_meta_box($post_id) { | |
| 186 | + if (!isset($_POST['mxchat_meta_box_nonce']) || | |
| 187 | + !wp_verify_nonce($_POST['mxchat_meta_box_nonce'], 'mxchat_meta_box_nonce')) { | |
| 188 | + return; | |
| 189 | + } | |
| 190 | + | |
| 191 | + if (!current_user_can('edit_post', $post_id)) { | |
| 192 | + return; | |
| 193 | + } | |
| 194 | + | |
| 195 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { | |
| 196 | + return; | |
| 197 | + } | |
| 198 | + | |
| 199 | + // Save hide chatbot setting | |
| 200 | + if (isset($_POST['mxchat_hide_chatbot'])) { | |
| 201 | + update_post_meta($post_id, '_mxchat_hide_chatbot', '1'); | |
| 202 | + } else { | |
| 203 | + delete_post_meta($post_id, '_mxchat_hide_chatbot'); | |
| 204 | + } | |
| 205 | + | |
| 206 | + // Save selected bot setting | |
| 207 | + if (isset($_POST['mxchat_selected_bot'])) { | |
| 208 | + $selected_bot = sanitize_text_field($_POST['mxchat_selected_bot']); | |
| 209 | + if (!empty($selected_bot)) { | |
| 210 | + update_post_meta($post_id, '_mxchat_selected_bot', $selected_bot); | |
| 211 | + } else { | |
| 212 | + delete_post_meta($post_id, '_mxchat_selected_bot'); | |
| 213 | + } | |
| 214 | + } | |
| 215 | + } | |
| 216 | + | |
| 217 | + /** | |
| 218 | + * Enqueue Gutenberg assets | |
| 219 | + */ | |
| 220 | + public function enqueue_gutenberg_assets() { | |
| 221 | + $available_bots = $this->get_available_bots(); | |
| 222 | + $global_autoshow = $this->is_global_autoshow_enabled(); | |
| 223 | + $has_multibot = class_exists('MxChat_Multi_Bot_Manager'); | |
| 224 | + | |
| 225 | + // Enqueue the JavaScript file | |
| 226 | + wp_enqueue_script( | |
| 227 | + 'mxchat-meta-box', | |
| 228 | + plugin_dir_url(__FILE__) . 'js/meta-box.js', | |
| 229 | + array('wp-edit-post', 'wp-element', 'wp-components', 'wp-data', 'wp-plugins'), | |
| 230 | + '1.0.0', | |
| 231 | + true | |
| 232 | + ); | |
| 233 | + | |
| 234 | + // Localize script with data | |
| 235 | + wp_localize_script('mxchat-meta-box', 'mxchatMetaBox', array( | |
| 236 | + 'availableBots' => $available_bots, | |
| 237 | + 'globalAutoshow' => $global_autoshow, | |
| 238 | + 'hasMultibot' => $has_multibot, | |
| 239 | + 'strings' => array( | |
| 240 | + 'panelTitle' => __('MxChat Settings', 'mxchat'), | |
| 241 | + 'useGlobalSetting' => __('Use Global Setting', 'mxchat'), | |
| 242 | + 'hideChatbot' => __('Hide chatbot on this page', 'mxchat'), | |
| 243 | + 'hideChatbotHelp' => __('Prevent any chatbot from appearing on this page', 'mxchat'), | |
| 244 | + 'selectBot' => __('Select Bot for this Page', 'mxchat'), | |
| 245 | + 'globalAutoshowOn' => __('Global Auto-Show: ON. Use Global Setting = Default bot shows automatically. Select specific bot = That bot shows instead.', 'mxchat'), | |
| 246 | + 'globalAutoshowOff' => __('Global Auto-Show: OFF. Use Global Setting = No bot shows. Select specific bot = That bot shows on this page only.', 'mxchat') | |
| 247 | + ) | |
| 248 | + )); | |
| 249 | + } | |
| 250 | + | |
| 251 | + /** | |
| 252 | + * Helper function to get the bot that should display on current page | |
| 253 | + * Call this from your main chatbot display logic | |
| 254 | + */ | |
| 255 | + public static function get_page_bot_setting($post_id = null) { | |
| 256 | + if (!$post_id) { | |
| 257 | + $post_id = get_the_ID(); | |
| 258 | + } | |
| 259 | + | |
| 260 | + if (!$post_id) { | |
| 261 | + return null; | |
| 262 | + } | |
| 263 | + | |
| 264 | + // Check if chatbot is hidden on this page | |
| 265 | + $hide_chatbot = get_post_meta($post_id, '_mxchat_hide_chatbot', true); | |
| 266 | + if ($hide_chatbot === '1') { | |
| 267 | + return array('action' => 'hide'); | |
| 268 | + } | |
| 269 | + | |
| 270 | + // Check if specific bot is selected | |
| 271 | + $selected_bot = get_post_meta($post_id, '_mxchat_selected_bot', true); | |
| 272 | + if (!empty($selected_bot)) { | |
| 273 | + return array('action' => 'show', 'bot_id' => $selected_bot); | |
| 274 | + } | |
| 275 | + | |
| 276 | + // Use global setting | |
| 277 | + return array('action' => 'global'); | |
| 278 | + } | |
| 356 | 279 | } |