| 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 |
} |
| 279 |
} |