| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* meta box to hide chatbot on specific 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 field 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 |
|
| 51 |
/** |
| 52 |
* Render the meta box content |
| 53 |
*/ |
| 54 |
public function render_meta_box($post) { |
| 55 |
wp_nonce_field('mxchat_meta_box_nonce', 'mxchat_meta_box_nonce'); |
| 56 |
|
| 57 |
$hide_chatbot = get_post_meta($post->ID, '_mxchat_hide_chatbot', true); |
| 58 |
?> |
| 59 |
<div style="padding: 10px 0;"> |
| 60 |
<label style="display: flex; align-items: center;"> |
| 61 |
<input type="checkbox" |
| 62 |
name="mxchat_hide_chatbot" |
| 63 |
value="1" |
| 64 |
<?php checked($hide_chatbot, '1'); ?> |
| 65 |
style="margin-right: 8px;" /> |
| 66 |
<?php _e('Hide chatbot on this page', 'mxchat'); ?> |
| 67 |
</label> |
| 68 |
<p style="font-size: 12px; color: #666; margin-top: 5px; font-style: italic;"> |
| 69 |
<?php _e('Check this to prevent the chatbot from appearing on this page.', 'mxchat'); ?> |
| 70 |
</p> |
| 71 |
</div> |
| 72 |
<?php |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Save meta box data |
| 77 |
*/ |
| 78 |
public function save_chatbot_meta_box($post_id) { |
| 79 |
if (!isset($_POST['mxchat_meta_box_nonce']) || |
| 80 |
!wp_verify_nonce($_POST['mxchat_meta_box_nonce'], 'mxchat_meta_box_nonce')) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
if (!current_user_can('edit_post', $post_id)) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
if (isset($_POST['mxchat_hide_chatbot'])) { |
| 93 |
update_post_meta($post_id, '_mxchat_hide_chatbot', '1'); |
| 94 |
} else { |
| 95 |
delete_post_meta($post_id, '_mxchat_hide_chatbot'); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Enqueue Gutenberg assets |
| 101 |
*/ |
| 102 |
public function enqueue_gutenberg_assets() { |
| 103 |
$script = " |
| 104 |
(function() { |
| 105 |
if (typeof wp !== 'undefined' && wp.editPost && wp.components && wp.element) { |
| 106 |
var PluginDocumentSettingPanel = wp.editPost.PluginDocumentSettingPanel; |
| 107 |
var CheckboxControl = wp.components.CheckboxControl; |
| 108 |
var PanelRow = wp.components.PanelRow; |
| 109 |
var useSelect = wp.data.useSelect; |
| 110 |
var useDispatch = wp.data.useDispatch; |
| 111 |
|
| 112 |
function MxChatHidePanel() { |
| 113 |
var meta = useSelect(function(select) { |
| 114 |
return select('core/editor').getEditedPostAttribute('meta') || {}; |
| 115 |
}); |
| 116 |
|
| 117 |
var editPost = useDispatch('core/editor').editPost; |
| 118 |
var hideChatbot = meta._mxchat_hide_chatbot === '1'; |
| 119 |
|
| 120 |
return wp.element.createElement( |
| 121 |
PluginDocumentSettingPanel, |
| 122 |
{ |
| 123 |
name: 'mxchat-hide', |
| 124 |
title: 'MxChat Settings', |
| 125 |
className: 'mxchat-hide-panel' |
| 126 |
}, |
| 127 |
wp.element.createElement( |
| 128 |
PanelRow, |
| 129 |
null, |
| 130 |
wp.element.createElement(CheckboxControl, { |
| 131 |
label: 'Hide chatbot on this page', |
| 132 |
help: 'Prevent the chatbot from appearing on this page.', |
| 133 |
checked: hideChatbot, |
| 134 |
onChange: function(value) { |
| 135 |
editPost({ |
| 136 |
meta: Object.assign({}, meta, { |
| 137 |
_mxchat_hide_chatbot: value ? '1' : '' |
| 138 |
}) |
| 139 |
}); |
| 140 |
} |
| 141 |
}) |
| 142 |
) |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
wp.plugins.registerPlugin('mxchat-hide', { |
| 147 |
render: MxChatHidePanel |
| 148 |
}); |
| 149 |
} |
| 150 |
})(); |
| 151 |
"; |
| 152 |
|
| 153 |
wp_add_inline_script('wp-edit-post', $script); |
| 154 |
} |
| 155 |
} |