PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.4.1
MxChat – AI Chatbot & Content Generation for WordPress v2.4.1
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
mxchat-basic / includes / class-mxchat-meta-box.php

class-mxchat-meta-box.php in MxChat – AI Chatbot & Content Generation for WordPress 2.4.1, at includes/class-mxchat-meta-box.php

155 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }