PluginProbe ʕ •ᴥ•ʔ
Admin Help Docs / 2.0.2
Admin Help Docs v2.0.2
2.0.2 2.0.1.1 trunk 1.4.3.2 2.0.0 2.0.0.1 2.0.0.2 2.0.1
admin-help-docs / inc / docs / controller.php
admin-help-docs / inc / docs Last commit date
page-locations 1 week ago admin-bar.php 1 week ago controller.php 1 week ago footer-text.php 1 week ago function.php 1 week ago wp-dashboard.php 1 week ago
controller.php
126 lines
1 <?php
2 /**
3 * Docs Controller
4 */
5
6 namespace PluginRx\AdminHelpDocs;
7
8 if ( ! defined( 'ABSPATH' ) ) exit;
9
10 class Controller {
11
12 /**
13 * Store the docs for each screen
14 *
15 * @var array
16 */
17 private array $screen_docs = [];
18
19
20 /**
21 * The single instance of the class
22 *
23 * @var self|null
24 */
25 private static ?Controller $instance = null;
26
27
28 /**
29 * Get the singleton instance
30 *
31 * @return self
32 */
33 public static function instance() : self {
34 return self::$instance ??= new self();
35 } // End instance()
36
37
38 /**
39 * Constructor
40 */
41 private function __construct() {
42 add_action( 'admin_init', [ $this, 'maybe_replace_dashboard' ], 1 );
43 add_action( 'current_screen', [ $this, 'render' ] );
44 } // End __construct()
45
46
47 /**
48 * Check if the dashboard should be replaced by help docs
49 */
50 public function maybe_replace_dashboard() {
51 global $pagenow;
52 if ( 'index.php' !== $pagenow || ! get_option( 'helpdocs_replace_dashboard' ) ) {
53 return;
54 }
55
56 if ( wp_doing_ajax() || wp_doing_cron() || ( defined( 'IFRAME_REQUEST' ) && IFRAME_REQUEST ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
57 return;
58 }
59
60 // Third-party plugins use /wp-admin/?foo=bar as an endpoint (Gravity Forms' ?gf_page=select_columns iframe, for one), so bail on anything that isn't a plain dashboard load
61 $allowed = apply_filters( 'helpdocs_dashboard_allowed_params', [ 'message', 'updated', 'welcome' ] );
62 $unknown = array_diff( array_keys( $_GET ), $allowed ); // phpcs:ignore
63 if ( ! empty( $unknown ) ) {
64 return;
65 }
66
67 wp_safe_redirect( admin_url( 'admin.php?page=admin-help-dashboard' ) );
68 exit;
69 } // End maybe_replace_dashboard()
70
71
72 /**
73 * Wipes the global meta boxes for the dashboard
74 */
75 public function nuclear_wipe_widgets() {
76 global $wp_meta_boxes;
77 $wp_meta_boxes[ 'dashboard' ] = [];
78 remove_action( ( 'welcome_panel' ), 'wp_welcome_panel' );
79 } // End nuclear_wipe_widgets()
80
81
82 /**
83 * Fetch docs for the current screen and render them in their appropriate locations
84 */
85 public function render( $screen ) {
86 // Ignore if on WP Dashboard or Main Docs Page
87 if ( $screen->base === 'dashboard' || Helpers::is_our_screen() ) {
88 return;
89 }
90
91 // Get all docs for the current screen, organized by location
92 $this->screen_docs = Helpers::get_current_screen_docs( $screen );
93 if ( empty( $this->screen_docs ) ) {
94 return;
95 }
96
97 // Render each doc in its appropriate location
98 $page_locations = HelpDocs::page_locations();
99
100 foreach ( $page_locations as $key => $label ) {
101 if ( empty( $this->screen_docs[ $key ] ) ) {
102 continue;
103 }
104
105 $class_name = ucfirst( $key );
106 $class_fqn = __NAMESPACE__ . '\\' . $class_name;
107
108 if ( class_exists( $class_fqn ) ) {
109 new $class_fqn( $this->screen_docs[ $key ] );
110 }
111 }
112 // Also render any docs that are hooked in
113 do_action( 'helpdocs_render_screen_docs', $this->screen_docs );
114 } // End bootstrap_docs()
115
116
117 /**
118 * Prevent cloning and unserializing
119 */
120 public function __clone() {}
121 public function __wakeup() {}
122
123 }
124
125
126 Controller::instance();