|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', ) ); ?>
—
hook_suffix, 'normal', null ); ?>
hook_suffix, 'side', null ); ?>
*/ private function stats(): array { if ( null !== $this->stats ) { return $this->stats; } // Settings: switches on, per tab. A switch is a checkbox, or a // dropdown with an "Off" choice (e.g. Duplicate Posts' scheduled // scan), which is on when set to anything else. $tabs = array(); $on = 0; $used = 0; foreach ( $this->settings->get_tabs() as $tab_id => $tab ) { $values = $this->settings->get_values( $tab_id ); $count = 0; $total = 0; foreach ( $this->settings->get_sections( $tab_id ) as $section ) { foreach ( $this->settings->get_fields( $tab_id, $section['id'] ) as $field ) { $value = $values[ $section['id'] ][ $field->id ] ?? $field->default; if ( 'checkbox' === $field->type ) { $is_on = ! empty( $value ); } elseif ( 'select' === $field->type && isset( ( (array) ( $field->args['choices'] ?? array() ) )['off'] ) ) { $is_on = 'off' !== $value; } else { continue; } ++$total; if ( $is_on ) { ++$count; } } } if ( 0 === $total ) { continue; } $tabs[ $tab_id ] = array( 'label' => $tab['label'], 'page' => $tab['page'], 'on' => $count, 'total' => $total, ); $on += $count; $used += $count > 0 ? 1 : 0; } // Database Cleanup: items in every category that deletes something. $to_clean = 0; foreach ( CleanupTasks::all() as $task ) { if ( 'optimize_tables' !== $task->id ) { $to_clean += (int) ( $task->count )(); } } // Email Log, when it's on. $emails = null; if ( EmailLogFeature::is_enabled() ) { $since = wp_date( 'Y-m-d', time() - 6 * DAY_IN_SECONDS ); $emails = array( 'sent' => LogRepository::count( array( 'date_from' => $since ) ), 'failed' => LogRepository::count( array( 'date_from' => $since, 'status' => 'failed', ) ), ); } $plugins = AboutScreen::products(); $this->stats = array( 'tabs' => $tabs, 'switches_on' => $on, 'tabs_in_use' => $used, 'to_clean' => $to_clean, 'emails' => $emails, 'plugins' => $plugins, 'plugins_active' => count( array_filter( $plugins, static function ( array $p ): bool { return 'active' === ( $p['status'] ?? '' ); } ) ), ); return $this->stats; } /** * "Settings overview" widget: every settings tab with how many of its * switches are on, grouped by page. * * @return void */ public function render_settings_widget(): void { $groups = array(); foreach ( $this->stats()['tabs'] as $tab_id => $tab ) { $is_woo = 'woo' === $tab['page']; $group = $is_woo ? __( 'WooCommerce', 'all-in-one-utilities' ) : __( 'Settings', 'all-in-one-utilities' ); $tab['url'] = admin_url( 'admin.php?page=' . ( $is_woo ? 'aiou-woo' : Menu::SLUG . '-settings' ) . '&tab=' . $tab_id ); $groups[ $group ][ $tab_id ] = $tab; } ksort( $groups ); // "Settings" before "WooCommerce". foreach ( $groups as $group => $tabs ) : ?>

stats(); ?>

→

AIOU_VERSION, __( 'WordPress', 'all-in-one-utilities' ) => get_bloginfo( 'version' ), __( 'PHP', 'all-in-one-utilities' ) => PHP_VERSION, __( 'Database', 'all-in-one-utilities' ) => (string) $wpdb->db_server_info(), ); ?>
$value ) : ?>