PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 2.5.5
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v2.5.5
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 / Admin.php

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

521 lines 18.6 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\Utils\Helper;
7 use WPDeveloper\BetterDocs\Utils\Enqueue;
8 use WPDeveloper\BetterDocs\Utils\Database;
9 use WPDeveloper\BetterDocs\Utils\Insights;
10 use WPDeveloper\BetterDocs\Admin\Analytics;
11 use WPDeveloper\BetterDocs\Dependencies\DI\Container;
12 use WPDeveloper\BetterDocs\Dependencies\PriyoMukul\WPNotice\Notices;
13
14 class Admin extends Base {
15 /**
16 * Admin Root Menu Slug
17 * @var string
18 */
19 private $slug = 'betterdocs-admin';
20 /**
21 * Insights
22 *
23 * @var Insights
24 */
25 private $insights = null;
26
27 /**
28 * DI\Container
29 *
30 * @var Container
31 */
32 private $container;
33
34 /**
35 * Database Wrapper
36 *
37 * @var Settings
38 */
39 private $settings;
40
41 /**
42 * Enqueue
43 * @var Enqueue
44 */
45 private $assets;
46
47 /**
48 * FAQBuilder
49 * @var FAQBuilder
50 */
51 private $faq_builder;
52
53 public function __construct( Container $container, PostType $type, Enqueue $assets, Settings $settings ) {
54 $this->container = $container;
55 $this->assets = $assets;
56 $this->settings = $settings;
57
58 add_action( 'init', [$type, 'register'], 9 );
59
60 $type->init();
61 $type->admin_init();
62
63 $this->faq_builder = $this->container->get( FAQBuilder::class );
64
65 if ( ! is_admin() ) {
66 return;
67 }
68
69 $this->plugin_insights();
70 add_action( 'admin_notices', [$this, 'compatibility_notices'] );
71 add_action( 'admin_init', [$this, 'notices'], 9 );
72
73 add_action( 'admin_menu', [$this, 'menus'] );
74 add_filter( 'plugin_action_links_' . BETTERDOCS_PLUGIN_BASENAME, [$this, 'insert_plugin_links'] );
75
76 $this->container->get( SetupWizard::class )->init();
77
78 add_action( 'admin_enqueue_scripts', [$this, 'styles'] );
79 add_action( 'admin_enqueue_scripts', [$this, 'scripts'] );
80 add_action( 'betterdocs_listing_header', [$this, 'header'], 10, 1 );
81 add_action( 'admin_bar_menu', [$this, 'toolbar_menu'], 32 );
82
83 add_filter( 'admin_body_class', [$this, 'body_classes'] );
84 add_filter( 'parent_file', [$type, 'highlight_admin_menu'] );
85 add_filter( 'submenu_file', [$type, 'highlight_admin_submenu'], 10, 2 );
86
87 /**
88 * Remove Comments Column from List Table.
89 */
90 add_filter( 'manage_docs_posts_columns', [$this, 'set_custom_edit_action_columns'] );
91 add_filter( 'manage_docs_posts_custom_column', [$this, 'manage_custom_columns'], 10, 2 );
92 }
93
94 public function compatibility_notices() {
95 if ( betterdocs()->is_pro_active() ) {
96 $plugins = Helper::get_plugins();
97 $plugin_data = $plugins['betterdocs-pro/betterdocs-pro.php'];
98
99 if ( isset( $plugin_data['Version'] ) && version_compare( $plugin_data['Version'], '2.5.0', '>=' ) ) {
100 return;
101 }
102
103 betterdocs()->views->get( 'admin/notices/compatibility', [ 'version' => $plugin_data['Version'] ] );
104 }
105 }
106
107 public function plugin_insights() {
108 $this->insights = Insights::get_instance( BETTERDOCS_PLUGIN_FILE, [
109 'opt_in' => true,
110 'goodbye_form' => true,
111 'item_id' => 'c7b16777b4f1b83f6083'
112 ] );
113
114 $this->insights->set_notice_options( [
115 'notice' => __( 'Want to help make <strong>BetterDocs</strong> even more awesome? You can get a <strong>10% discount coupon</strong> for Premium extensions if you allow us to track the usage.', 'betterdocs' ),
116 'extra_notice' => __( 'We collect non-sensitive diagnostic data and plugin usage information. Your site URL, WordPress & PHP version, plugins & themes and email address to send you the discount coupon. This data lets us make sure this plugin always stays compatible with the most popular plugins and themes. No spam, I promise.', 'betterdocs' )
117 ] );
118
119 $this->insights->init();
120 }
121
122 /**
123 * Admin notices for Review and others.
124 *
125 * @return void
126 */
127 public function notices() {
128 $notices = new Notices( [
129 'id' => 'betterdocs',
130 'store' => 'options',
131 'storage_key' => 'notices',
132 'version' => '1.0.0',
133 'lifetime' => 3,
134 'styles' => $this->assets->asset_url( 'admin/css/notices.css' )
135 // 'dev_mode' => true
136 ] );
137
138 /**
139 * Review Notice
140 * @var mixed $message
141 */
142
143 $message = __(
144 'We hope you\'re enjoying BetterDocs! Could you please do us a BIG favor and give it a 5-star rating on WordPress to help us spread the word and boost our motivation?',
145 'betterdocs'
146 );
147
148 $_review_notice = [
149 'thumbnail' => $this->assets->icon( 'betterdocs-icon.svg', true ),
150 'html' => '<p>' . $message . '</p>',
151 'links' => [
152 'later' => [
153 'link' => 'https://wordpress.org/plugins/betterdocs/#reviews',
154 'target' => '_blank',
155 'label' => __( 'Sure, you deserve it!', 'betterdocs' ),
156 'icon_class' => 'dashicons dashicons-external'
157 ],
158 'allready' => [
159 'label' => __( 'I already did', 'betterdocs' ),
160 'icon_class' => 'dashicons dashicons-smiley',
161 'attributes' => [
162 'data-dismiss' => true
163 ]
164 ],
165 'maybe_later' => [
166 'label' => __( 'Maybe Later', 'betterdocs' ),
167 'icon_class' => 'dashicons dashicons-calendar-alt',
168 'attributes' => [
169 'data-later' => true,
170 'class' => 'dismiss-btn'
171 ]
172 ],
173 'support' => [
174 'link' => 'https://wpdeveloper.com/support',
175 'attributes' => [
176 'target' => '_blank'
177 ],
178 'label' => __( 'I need help', 'betterdocs' ),
179 'icon_class' => 'dashicons dashicons-sos'
180 ],
181 'never_show_again' => [
182 'label' => __( 'Never show again', 'betterdocs' ),
183 'icon_class' => 'dashicons dashicons-dismiss',
184 'attributes' => [
185 'data-dismiss' => true
186 ]
187 ]
188 ]
189 ];
190
191 $notices->add(
192 'review',
193 $_review_notice,
194 [
195 'start' => $notices->strtotime( '+10 days' ),
196 'recurrence' => 30,
197 'dismissible' => true
198 // 'screens' => [
199 // 'dashboard', 'plugins', 'themes', 'edit-page',
200 // 'edit-post', 'users', 'tools', 'options-general',
201 // 'nav-menus', 'toplevel_page_betterdocs-admin', 'betterdocs_page_betterdocs-settings', 'betterdocs_page_betterdocs-analytics', 'betterdocs_page_betterdocs-faq', 'edit-doc_tag', 'edit-doc_category'
202 // ]
203 ]
204 );
205
206 /**
207 * Opt-In Notice
208 */
209 if ( $this->insights != null ) {
210 $notices->add(
211 'opt_in',
212 [$this->insights, 'notice'],
213 [
214 'classes' => 'updated put-dismiss-notice',
215 'start' => $notices->time(),
216 // 'start' => $notices->strtotime('+20 days'),
217 'dismissible' => true,
218 'do_action' => 'wpdeveloper_notice_clicked_for_betterdocs',
219 'display_if' => ! function_exists( 'betterdocs_pro' )
220 ]
221 );
222 }
223
224 $notices->init();
225 }
226
227 public function body_classes( $classes ) {
228 $saved_settings = get_option( 'betterdocs_settings', false );
229 $dark_mode = isset( $saved_settings['dark_mode'] ) ? $saved_settings['dark_mode'] : false;
230 $dark_mode = ! empty( $dark_mode ) ? boolval( $dark_mode ) : false;
231
232 if ( $dark_mode === true ) {
233 $classes .= ' betterdocs-dark-mode ';
234 }
235 return $classes;
236 }
237
238 /**
239 * Remove Comments Column From List Table
240 *
241 * @since 1.0.0
242 * @param array $columns
243 *
244 * @return array
245 */
246 public function set_custom_edit_action_columns( $columns ) {
247 unset( $columns['comments'] );
248 $new_columns = [];
249 foreach ( $columns as $key => $value ) {
250 if ( $key == 'date' ) {
251 $new_columns['betterdocs_word_count'] = __( 'Word Count', 'betterdocs' ); // put the tags column before it
252 }
253 $new_columns[$key] = $value;
254 }
255 return $new_columns;
256 }
257
258 public function manage_custom_columns( $column, $post_id ) {
259 switch ( $column ) {
260 case 'betterdocs_word_count':
261 $word_count = str_word_count( trim( strip_tags( get_post_field( 'post_content', $post_id ) ) ) );
262 echo '<span>' . $word_count . '</span>';
263 break;
264 }
265 }
266
267 /**
268 * Enqueue Assets for Admin ( Styles )
269 *
270 * @since 1.0.0
271 * @param string $hook
272 *
273 * @return void
274 */
275 public function styles( $hook ) {
276 $this->assets->enqueue( 'betterdocs-global', 'admin/css/global.css', [], 'all' );
277
278 if ( ! in_array( $hook, ['toplevel_page_betterdocs-admin', 'betterdocs_page_betterdocs-settings', 'betterdocs_page_betterdocs-analytics'] ) ) {
279 return;
280 }
281
282 $this->assets->enqueue( 'betterdocs-select2', 'vendor/css/select2.min.css', [], 'all' );
283 $this->assets->enqueue( 'betterdocs-daterangepicker', 'vendor/css/daterangepicker.css', [], 'all' );
284 $this->assets->enqueue( 'betterdocs', 'admin/css/betterdocs.css', [], 'all' );
285 }
286
287 /**
288 * Enqueue Assets for Admin ( Scripts )
289 *
290 * @since 1.0.0
291 * @param string $hook
292 *
293 * @return void
294 */
295 public function scripts( $hook ) {
296 if ( ( $hook === 'edit.php' || $hook === 'toplevel_page_betterdocs-admin' ) && get_post_type() == 'docs' ) {
297 $this->assets->enqueue( 'betterdocs-switcher', 'admin/js/switcher.js', [
298 'jquery'
299 ] );
300
301 $this->assets->localize( 'betterdocs-switcher', 'betterdocsSwitcher', [
302 'menu_title' => __( 'Switch to BetterDocs UI', 'betterdocs' ),
303 'site_address' => get_bloginfo( 'url' ),
304 'betterdocs_pro_plugin' => betterdocs()->is_pro_active()
305 ] );
306 }
307
308 if ( ! in_array( $hook, ['toplevel_page_betterdocs-admin', 'betterdocs_page_betterdocs-analytics'] ) ) {
309 return;
310 }
311
312 wp_enqueue_script( 'wp-color-picker' );
313
314 $this->assets->enqueue( 'betterdocs-select2', 'vendor/js/select2.min.js', [] );
315 $this->assets->enqueue( 'betterdocs-sweetalert', 'vendor/js/sweetalert.min.js', [] );
316 $this->assets->enqueue( 'moment', 'vendor/js/moment.min.js', [] );
317 $this->assets->enqueue( 'betterdocs-daterangepicker', 'vendor/js/daterangepicker.min.js', [] );
318 $this->assets->enqueue( 'betterdocs-admin', 'admin/js/betterdocs.js', ['jquery', 'jquery-ui-sortable'] );
319
320 $dark_mode = $this->settings->get( 'dark_mode', false );
321
322 $this->assets->localize( 'betterdocs-admin', 'betterdocs_admin', [
323 'ajaxurl' => admin_url( 'admin-ajax.php' ),
324 'doc_cat_order_nonce' => wp_create_nonce( 'doc_cat_order_nonce' ),
325 'knowledge_base_order_nonce' => wp_create_nonce( 'knowledge_base_order_nonce' ),
326 'paged' => isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : 0,
327 'per_page_id' => "edit_doc_category_per_page",
328 'menu_title' => __( 'Switch to BetterDocs UI', 'betterdocs' ),
329 'dark_mode' => ! empty( $dark_mode ) ? boolval( $dark_mode ) : false,
330 'text' => __( 'Copied!', 'betterdocs' ),
331 'test_report' => __( 'Test Report!', 'betterdocs' ),
332 'sending' => __( 'Sending...', 'betterdocs' )
333 ] );
334 }
335
336 /**
337 * All admin pages header
338 *
339 * @since 1.0.0
340 * @return void
341 */
342 public function header( $admin_tab_name ) {
343 $quick_links = [
344 'switch_view' => sprintf(
345 '<a href="%s" class="betterdocs-button betterdocs-button-secondary">%s</a>',
346 add_query_arg( ['post_type' => 'docs', 'bdocs_view' => 'classic'], 'edit.php' ),
347 __( 'Switch to Classic UI', 'betterdocs' )
348 ),
349 'add_new_doc' => sprintf(
350 '<a href="%s" class="betterdocs-button betterdocs-button-primary">%s</a>',
351 add_query_arg( ['post_type' => 'docs'], 'post-new.php' ),
352 __( 'Add New Doc', 'betterdocs' )
353 )
354 ];
355
356 $quick_links = apply_filters( 'betterdocs_quick_links', $quick_links );
357
358 betterdocs()->views->get( 'admin/header', [
359 'quick_links' => $quick_links,
360 'active_tab' => $admin_tab_name
361 ] );
362 }
363
364 /**
365 * Register all the menus for BetterDocs
366 *
367 * @since 1.0.0
368 * @return void
369 */
370 public function menus() {
371 $default_args = [
372 'page_title' => 'BetterDocs',
373 'menu_title' => 'BetterDocs',
374 'capability' => 'edit_posts',
375 'menu_slug' => $this->slug,
376 'callback' => [$this, 'output'],
377 'icon_url' => betterdocs()->assets->icon( 'betterdocs-icon-white.svg', true ),
378 'position' => 5
379 ];
380
381 $_menu_position = 5;
382
383 foreach ( $this->menu_list() as $key => $value ) {
384 if ( $key === 'betterdocs' ) {
385 $callable = 'add_menu_page';
386 $value = wp_parse_args( $value, $default_args );
387 } else {
388 $callable = 'add_submenu_page';
389 $default_args['callback'] = '';
390 $default_args['position'] = $_menu_position;
391 unset( $default_args['icon_url'] );
392 $value = wp_parse_args( $value, array_merge( ['parent_slug' => $this->slug], $default_args ) );
393 }
394
395 $_menu_position++;
396
397 $hookname = call_user_func_array( $callable, $value );
398 }
399 }
400
401 /**
402 * BetterDocs Admin Page Output
403 *
404 * @since 1.0.0
405 * @return void
406 */
407 public function output() {
408 betterdocs()->views->get( 'admin/main', [
409 'admin_ui' => 'dnd'
410 ] );
411 }
412
413 /**
414 * Menu creator helper
415 *
416 * @since 2.5.0
417 *
418 * @param string $title
419 * @param string $slug
420 * @param string $cap
421 * @param array $callback
422 *
423 * @return array
424 */
425 private function normalize_menu( $title, $slug, $cap = 'edit_docs', $callback = null ) {
426 return Helper::normalize_menu( $title, $slug, $cap, $callback );
427 }
428
429 /**
430 * BetterDocs Menu List
431 *
432 * @since 1.0.0
433 * @return array
434 */
435 private function menu_list() {
436 $betterdocs_admin_pages = [
437 'betterdocs' => [
438 'menu_slug' => $this->slug,
439 'page_title' => 'BetterDocs',
440 'menu_title' => 'BetterDocs',
441 'capability' => 'edit_docs',
442 'callback' => [$this, 'output'],
443 'icon_url' => betterdocs()->assets->icon( 'betterdocs-icon-white.svg', true ),
444 'position' => 5
445 ],
446 'all_docs' => $this->normalize_menu( __( 'All Docs', 'betterdocs' ), $this->slug ),
447 'add_new' => $this->normalize_menu( __( 'Add New', 'betterdocs' ), 'post-new.php?post_type=docs' ),
448 'categories' => $this->normalize_menu(
449 __( 'Categories', 'betterdocs' ),
450 'edit-tags.php?taxonomy=doc_category&post_type=docs',
451 'manage_doc_terms'
452 ),
453 'tags' => $this->normalize_menu(
454 __( 'Tags', 'betterdocs' ),
455 'edit-tags.php?taxonomy=doc_tag&post_type=docs',
456 'manage_doc_terms'
457 ),
458 'quick_setup' => $this->normalize_menu(
459 __( 'Quick Setup', 'betterdocs' ),
460 'betterdocs-setup',
461 'delete_users',
462 [$this->container->get( SetupWizard::class ), 'views']
463 ),
464 'settings' => $this->normalize_menu(
465 __( 'Settings', 'betterdocs' ),
466 'betterdocs-settings',
467 'edit_docs_settings',
468 [$this->container->get( Settings::class ), 'views']
469 ),
470 'analytics' => $this->normalize_menu(
471 __( 'Analytics', 'betterdocs' ),
472 'betterdocs-analytics',
473 'read_docs_analytics',
474 [$this->container->get( Analytics::class ), 'views']
475 ),
476 'faq' => $this->normalize_menu(
477 __( 'FAQ Builder', 'betterdocs' ),
478 'betterdocs-faq',
479 'read_docs_analytics',
480 [$this->faq_builder, 'output']
481 )
482 ];
483 return apply_filters( 'betterdocs_admin_menu', $betterdocs_admin_pages );
484 }
485
486 public function insert_plugin_links( $links ) {
487 $links[] = '<a href="admin.php?page=betterdocs-settings">' . __( 'Settings', 'betterdocs' ) . '</a>';
488 return $links;
489 }
490
491 public function toolbar_menu( $admin_bar ) {
492 if ( ! is_admin() || ! is_admin_bar_showing() ) {
493 return;
494 }
495
496 // Show only when the user is a member of this site, or they're a super admin.
497 if ( ! is_user_member_of_blog() && ! is_super_admin() ) {
498 return;
499 }
500
501 $docs_url = '';
502
503 if ( $this->settings->get( 'builtin_doc_page' ) ) {
504 $docs_url = get_post_type_archive_link( 'docs' );
505 } elseif ( intval( $docs_page = $this->settings->get( 'docs_page' ) ) ) {
506 $docs_url = ! empty( $docs_page ) ? get_page_link( $docs_page ) : false;
507 }
508
509 if ( ! $docs_url ) {
510 return;
511 }
512
513 $admin_bar->add_node( [
514 'parent' => 'site-name',
515 'id' => 'view-docs',
516 'title' => __( 'Visit Documentation', 'betterdocs' ),
517 'href' => $docs_url
518 ] );
519 }
520 }
521