|null */ private $stats = null; /** * Settings. * * @var SettingsRegistry */ private $settings; /** * Constructor. * * @param SettingsRegistry $settings The settings registry. */ public function __construct( SettingsRegistry $settings ) { $this->settings = $settings; } /** * Wire this screen's widget registration to its own hook suffix. * Called once, from `Menu::register_menu()`, with the real value * `add_submenu_page()` returned for the Dashboard entry. * * @param string $hook_suffix This screen's real admin page hook suffix. * @return void */ public function setup( string $hook_suffix ): void { $this->hook_suffix = $hook_suffix; add_action( "load-{$hook_suffix}", array( $this, 'register_widgets' ) ); } /** * Register this screen's own widgets (as real meta boxes) and let * other plugins register theirs. Runs on `load-{$hook_suffix}` -- * before `admin-header.php` renders the Screen Options panel, which * is what makes "number of columns" and "Show on screen" work at all. * * @return void */ public function register_widgets(): void { add_screen_option( 'layout_columns', array( 'max' => 2, 'default' => 2, ) ); add_meta_box( 'aiou_dashboard_settings', __( 'Settings overview', 'all-in-one-utilities' ), array( $this, 'render_settings_widget' ), null, 'normal', 'high' ); add_meta_box( 'aiou_dashboard_maintenance', __( 'Maintenance', 'all-in-one-utilities' ), array( $this, 'render_maintenance_widget' ), null, 'normal', 'high' ); add_meta_box( 'aiou_dashboard_at_a_glance', __( 'Your plugins', 'all-in-one-utilities' ), array( $this, 'render_at_a_glance_widget' ), null, 'side', 'high' ); add_meta_box( 'aiou_dashboard_system', __( 'System', 'all-in-one-utilities' ), array( $this, 'render_system_widget' ), null, 'side', 'core' ); /** * Fires while the Dashboard screen's own widgets are being * registered, so Pro, the Woo Add-on, the Tools add-on, or any * future module can register its own via `add_meta_box()` -- * exactly how a real WordPress dashboard widget is added (see * `wp_add_dashboard_widget()`), just without that function's * dashboard-specific extras (per-widget "Configure" links, etc.) * this screen has no need for. * * @since 2.1.0 * * @param \WP_Screen $screen This screen. */ do_action( 'aiou_dashboard_widgets_setup', get_current_screen() ); $this->decorate_widgets(); wp_enqueue_script( 'postbox' ); wp_add_inline_script( 'postbox', 'jQuery(function($){ postboxes.add_postbox_toggles(pagenow); });' ); } /** * Tint every registered widget in its plugin's color and add that * plugin's badge to its title, like the Settings screen's sections. * * @return void */ private function decorate_widgets(): void { global $wp_meta_boxes; $screen = get_current_screen(); if ( ! $screen || empty( $wp_meta_boxes[ $screen->id ] ) ) { return; } foreach ( $wp_meta_boxes[ $screen->id ] as $context => $priorities ) { foreach ( (array) $priorities as $priority => $boxes ) { foreach ( (array) $boxes as $id => $box ) { if ( ! is_array( $box ) ) { continue; } $owner = 'free'; foreach ( array( 'pro', 'woo', 'tools' ) as $candidate ) { if ( str_starts_with( (string) $id, 'aiou_' . $candidate . '_' ) ) { $owner = $candidate; } } /** * Filters which plugin a Dashboard widget belongs to * ('free', 'pro', 'woo', 'tools'), for its tint and badge. * * @since 2.1.0 * * @param string $owner Owner. * @param string $id Widget (meta box) id. */ $owner = sanitize_key( (string) apply_filters( 'aiou_dashboard_widget_owner', $owner, (string) $id ) ); add_filter( "postbox_classes_{$screen->id}_{$id}", static function ( array $classes ) use ( $owner ): array { $classes[] = 'aiou-owner--' . $owner; return $classes; } ); if ( 'free' !== $owner ) { ob_start(); SettingsPage::render_owner_badge( $owner ); $wp_meta_boxes[ $screen->id ][ $context ][ $priority ][ $id ]['title'] .= (string) ob_get_clean(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- adding a badge to an already-registered meta box's title. } } } } } /** * Render the screen. * * @return void */ public function render(): void { $screen = get_current_screen(); $columns = $screen ? absint( $screen->get_columns() ) : 2; $columns_css = $columns ? " columns-{$columns}" : ''; $stats = $this->stats(); AdminShell::page_start( array( /* translators: %s: the current user's display name */ 'title' => sprintf( __( 'Hi %s,', 'all-in-one-utilities' ), wp_get_current_user()->display_name ), 'description' => __( 'Welcome to your All-In-One Utilities dashboard! See which utilities are switched on, what is waiting to be cleaned, and how your plugins are doing.', 'all-in-one-utilities' ), 'class' => 'aiou-dashboard', ) ); ?>