PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.5.1
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.5.1
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Core / UnifiedMetabox.php

UnifiedMetabox.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.5.1, at includes/Core/UnifiedMetabox.php

259 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Core;
4
5 use WPDeveloper\BetterDocs\Utils\Base;
6 use WPDeveloper\BetterDocs\Core\Settings;
7
8 /**
9 * Unified Metabox for BetterDocs
10 *
11 * This class creates a tabbed metabox interface that consolidates:
12 * - Article Quality Analysis
13 * - Estimated Reading Time
14 * - Related Docs (from Pro)
15 *
16 * @since 3.7.0
17 */
18 class UnifiedMetabox extends Base {
19
20 /**
21 * Settings instance
22 *
23 * @var Settings
24 */
25 private $settings;
26
27 /**
28 * Active tabs
29 *
30 * @var array
31 */
32 private $tabs = [];
33
34 /**
35 * Constructor
36 *
37 * @param Settings $settings Settings instance
38 */
39 public function __construct( Settings $settings ) {
40 $this->settings = $settings;
41
42 if ( ! is_admin() ) {
43 return;
44 }
45
46 // Register the unified metabox
47 add_action( 'add_meta_boxes', [ $this, 'register_metabox' ], 5 );
48
49 // Enqueue scripts and styles
50 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
51 }
52
53 /**
54 * Register the unified metabox
55 */
56 public function register_metabox() {
57 // Build tabs array
58 $this->build_tabs();
59
60 // Only add metabox if there are tabs to show
61 if ( empty( $this->tabs ) ) {
62 return;
63 }
64
65 add_meta_box(
66 'betterdocs-unified-metabox',
67 __( 'BetterDocs', 'betterdocs' ),
68 [ $this, 'render_metabox' ],
69 'docs',
70 'normal',
71 'high'
72 );
73 }
74
75 /**
76 * Build the tabs array based on enabled features
77 */
78 private function build_tabs() {
79 $this->tabs = [];
80
81 // Article Quality Analysis tab (always available)
82 $this->tabs['quality-analysis'] = [
83 'id' => 'quality-analysis',
84 'label' => __( 'Article Quality Analysis', 'betterdocs' ),
85 'callback' => [ $this, 'render_quality_analysis_tab' ],
86 'priority' => 10
87 ];
88
89 // Estimated Reading Time tab
90 if ( $this->settings->get( 'enable_estimated_reading_time' ) ) {
91 $this->tabs['reading-time'] = [
92 'id' => 'reading-time',
93 'label' => __( 'Estimated Reading Time', 'betterdocs' ),
94 'callback' => [ $this, 'render_reading_time_tab' ],
95 'priority' => 20
96 ];
97 }
98
99 // Attachments tab (from Pro)
100 if ( $this->settings->get( 'show_attachment' ) ) {
101 $this->tabs['attachments'] = [
102 'id' => 'attachments',
103 'label' => __( 'Attachments', 'betterdocs' ),
104 'callback' => [ $this, 'render_attachments_tab' ],
105 'priority' => 30
106 ];
107 }
108
109 // Related Docs tab (from Pro)
110 if ( $this->settings->get( 'show_related_docs' ) ) {
111 $this->tabs['related-docs'] = [
112 'id' => 'related-docs',
113 'label' => __( 'Related Docs', 'betterdocs' ),
114 'callback' => [ $this, 'render_related_docs_tab' ],
115 'priority' => 40
116 ];
117 }
118
119 // Allow other plugins/features to add tabs
120 $this->tabs = apply_filters( 'betterdocs_unified_metabox_tabs', $this->tabs );
121
122 // Sort tabs by priority
123 uasort( $this->tabs, function( $a, $b ) {
124 return ( $a['priority'] ?? 999 ) - ( $b['priority'] ?? 999 );
125 });
126 }
127
128 /**
129 * Render the unified metabox
130 *
131 * @param \WP_Post $post Current post object
132 */
133 public function render_metabox( $post ) {
134 if ( empty( $this->tabs ) ) {
135 return;
136 }
137
138 $first_tab = array_key_first( $this->tabs );
139 ?>
140 <div class="betterdocs-unified-metabox">
141 <div class="betterdocs-metabox-tabs">
142 <?php foreach ( $this->tabs as $tab_id => $tab ) : ?>
143 <button
144 type="button"
145 class="betterdocs-metabox-tab <?php echo $tab_id === $first_tab ? 'active' : ''; ?>"
146 data-tab="<?php echo esc_attr( $tab_id ); ?>"
147 >
148 <span class="tab-label"><?php echo esc_html( $tab['label'] ); ?></span>
149 </button>
150 <?php endforeach; ?>
151 </div>
152
153 <div class="betterdocs-metabox-content">
154 <?php foreach ( $this->tabs as $tab_id => $tab ) : ?>
155 <div
156 class="betterdocs-metabox-panel <?php echo $tab_id === $first_tab ? 'active' : ''; ?>"
157 data-panel="<?php echo esc_attr( $tab_id ); ?>"
158 >
159 <?php
160 if ( is_callable( $tab['callback'] ) ) {
161 call_user_func( $tab['callback'], $post );
162 }
163 ?>
164 </div>
165 <?php endforeach; ?>
166 </div>
167 </div>
168 <?php
169 }
170
171 /**
172 * Render Article Quality Analysis tab
173 *
174 * @param \WP_Post $post Current post object
175 */
176 public function render_quality_analysis_tab( $post ) {
177 // Hook for Article Quality Score to render its content
178 do_action( 'betterdocs_quality_analysis_tab_content', $post );
179 }
180
181 /**
182 * Render Estimated Reading Time tab
183 *
184 * @param \WP_Post $post Current post object
185 */
186 public function render_reading_time_tab( $post ) {
187 echo '<div class="betterdocs-reading-time-content">';
188 // Hook for Estimated Reading Time to render its content
189 do_action( 'betterdocs_reading_time_tab_content', $post );
190 echo '</div>';
191 }
192
193 /**
194 * Render Attachments tab
195 *
196 * @param \WP_Post $post Current post object
197 */
198 public function render_attachments_tab( $post ) {
199 echo '<div class="betterdocs-attachments-content">';
200 // Hook for Attachments (Pro) to render its content
201 do_action( 'betterdocs_attachments_tab_content', $post );
202 echo '</div>';
203 }
204
205 /**
206 * Render Related Docs tab
207 *
208 * @param \WP_Post $post Current post object
209 */
210 public function render_related_docs_tab( $post ) {
211 echo '<div class="betterdocs-related-docs-content">';
212 // Hook for Related Docs (Pro) to render its content
213 do_action( 'betterdocs_related_docs_tab_content', $post );
214 echo '</div>';
215 }
216
217 /**
218 * Enqueue assets for the unified metabox
219 *
220 * @param string $hook Current admin page hook
221 */
222 public function enqueue_assets( $hook ) {
223 global $post_type;
224
225 // Only load on docs edit screens
226 if ( ( $hook === 'post.php' || $hook === 'post-new.php' ) && $post_type === 'docs' ) {
227 betterdocs()->assets->enqueue( 'betterdocs-unified-metabox', 'admin/js/unified-metabox.js' );
228
229 // Enqueue styles
230 wp_enqueue_style(
231 'betterdocs-unified-metabox',
232 BETTERDOCS_ABSURL . 'assets/admin/css/unified-metabox.css',
233 [],
234 BETTERDOCS_VERSION
235 );
236 }
237 }
238
239 /**
240 * Get AI icon SVG
241 *
242 * @return string SVG icon markup
243 */
244 private function get_ai_icon() {
245 return '<svg width="16" height="16" viewBox="0 0 33 32" fill="none" xmlns="http://www.w3.org/2000/svg">
246 <g filter="url(#filter0_d_13132_3862)">
247 <path d="M4.5 15.9986C4.5 15.5068 4.89984 15.1128 5.38925 15.0642C9.59729 14.647 12.9438 11.3004 13.361 7.09237C13.4096 6.60296 13.8036 6.20312 14.2954 6.20312C14.7872 6.20312 15.1812 6.60296 15.2297 7.09237C15.6469 11.3004 18.9935 14.647 23.2015 15.0642C23.6909 15.1128 24.0907 15.5068 24.0907 15.9986C24.0907 16.4904 23.6909 16.8844 23.2015 16.9329C18.9935 17.3501 15.6469 20.6967 15.2297 24.9048C15.1812 25.3942 14.7872 25.794 14.2954 25.794C13.8036 25.794 13.4096 25.3942 13.361 24.9048C12.9438 20.6967 9.59729 17.3501 5.38925 16.9329C4.89984 16.8844 4.5 16.4904 4.5 15.9986Z" fill="url(#paint0_linear_13132_3862)"/>
248 </g>
249 <defs>
250 <linearGradient id="paint0_linear_13132_3862" x1="14.2954" y1="6.20312" x2="14.2954" y2="25.794" gradientUnits="userSpaceOnUse">
251 <stop stop-color="#6C5CE7"/>
252 <stop offset="1" stop-color="#A29BFE"/>
253 </linearGradient>
254 </defs>
255 </svg>';
256 }
257 }
258
259