page-locations
3 months ago
admin-bar.php
3 months ago
controller.php
3 months ago
footer-text.php
3 months ago
function.php
3 months ago
wp-dashboard.php
3 months ago
controller.php
120 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 || isset( $_GET[ 'page' ] ) || ! get_option( 'helpdocs_replace_dashboard' ) ) { // phpcs:ignore |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | // 1. THE NUCLEAR OPTION: Wipe out all dashboard widgets |
| 57 | add_action( ( 'wp_dashboard_setup' ), [ $this, 'nuclear_wipe_widgets' ], 9999 ); |
| 58 | |
| 59 | // 2. REDIRECT to our page |
| 60 | $redirect_url = admin_url( ( 'admin.php?page=admin-help-dashboard' ) ); |
| 61 | wp_safe_redirect( $redirect_url ); |
| 62 | exit; |
| 63 | } // End maybe_replace_dashboard() |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * Wipes the global meta boxes for the dashboard |
| 68 | */ |
| 69 | public function nuclear_wipe_widgets() { |
| 70 | global $wp_meta_boxes; |
| 71 | $wp_meta_boxes[ 'dashboard' ] = []; |
| 72 | remove_action( ( 'welcome_panel' ), 'wp_welcome_panel' ); |
| 73 | } // End nuclear_wipe_widgets() |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Fetch docs for the current screen and render them in their appropriate locations |
| 78 | */ |
| 79 | public function render( $screen ) { |
| 80 | // Ignore if on WP Dashboard or Main Docs Page |
| 81 | if ( $screen->base === 'dashboard' || Helpers::is_our_screen() ) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | // Get all docs for the current screen, organized by location |
| 86 | $this->screen_docs = Helpers::get_current_screen_docs( $screen ); |
| 87 | if ( empty( $this->screen_docs ) ) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | // Render each doc in its appropriate location |
| 92 | $page_locations = HelpDocs::page_locations(); |
| 93 | |
| 94 | foreach ( $page_locations as $key => $label ) { |
| 95 | if ( empty( $this->screen_docs[ $key ] ) ) { |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | $class_name = ucfirst( $key ); |
| 100 | $class_fqn = __NAMESPACE__ . '\\' . $class_name; |
| 101 | |
| 102 | if ( class_exists( $class_fqn ) ) { |
| 103 | new $class_fqn( $this->screen_docs[ $key ] ); |
| 104 | } |
| 105 | } |
| 106 | // Also render any docs that are hooked in |
| 107 | do_action( 'helpdocs_render_screen_docs', $this->screen_docs ); |
| 108 | } // End bootstrap_docs() |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | * Prevent cloning and unserializing |
| 113 | */ |
| 114 | public function __clone() {} |
| 115 | public function __wakeup() {} |
| 116 | |
| 117 | } |
| 118 | |
| 119 | |
| 120 | Controller::instance(); |