[ title, context ]. */ public const SEEN_OPTION = 'aiou_dashboard_widgets_seen'; /** * Widget ids starting with this are Pro's own custom widgets, which * have their own role targeting, so they are never recorded or listed. */ public const CUSTOM_PREFIX = 'aiou_cw_'; /** * Hook everything. * * @return void */ public function register(): void { // Late, so every plugin's widgets are registered first. add_action( 'wp_dashboard_setup', array( $this, 'record' ), 9998 ); add_action( 'wp_dashboard_setup', array( $this, 'hide' ), 9999 ); } /** * WordPress's own Dashboard widgets. * * @return array id => [ title, context ]. */ public static function core(): array { return array( 'dashboard_right_now' => array( __( 'At a Glance', 'all-in-one-utilities' ), 'normal' ), 'dashboard_activity' => array( __( 'Activity', 'all-in-one-utilities' ), 'normal' ), 'dashboard_site_health' => array( __( 'Site Health Status', 'all-in-one-utilities' ), 'normal' ), 'dashboard_quick_press' => array( __( 'Quick Draft', 'all-in-one-utilities' ), 'side' ), 'dashboard_primary' => array( __( 'WordPress Events and News', 'all-in-one-utilities' ), 'side' ), 'dashboard_recent_drafts' => array( __( 'Recent Drafts', 'all-in-one-utilities' ), 'side' ), 'dashboard_php_nag' => array( __( 'PHP Update Required', 'all-in-one-utilities' ), 'normal' ), ); } /** * Every widget this site is known to have: core's own plus any seen * on the Dashboard. * * @return array id => title. */ public static function known(): array { $widgets = array(); foreach ( self::core() as $id => $widget ) { $widgets[ $id ] = $widget[0]; } $seen = get_option( self::SEEN_OPTION, array() ); foreach ( is_array( $seen ) ? $seen : array() as $id => $widget ) { if ( ! isset( $widgets[ $id ] ) && is_array( $widget ) ) { $widgets[ (string) $id ] = (string) ( $widget[0] ?? $id ); } } return $widgets; } /** * Remember which widgets other plugins add (only saved when that list * changes). * * @return void */ public function record(): void { global $wp_meta_boxes; $core = self::core(); $seen = array(); foreach ( (array) ( $wp_meta_boxes['dashboard'] ?? array() ) as $context => $priorities ) { foreach ( (array) $priorities as $boxes ) { foreach ( (array) $boxes as $id => $box ) { $id = (string) $id; if ( ! is_array( $box ) || isset( $core[ $id ] ) || str_starts_with( $id, self::CUSTOM_PREFIX ) ) { continue; } // Titles may carry markup (e.g. a count bubble). $title = trim( wp_strip_all_tags( (string) preg_replace( '#]*>.*?#s', '', (string) ( $box['title'] ?? '' ) ) ) ); $seen[ $id ] = array( '' !== $title ? $title : $id, (string) $context ); } } } ksort( $seen ); if ( get_option( self::SEEN_OPTION, array() ) !== $seen ) { update_option( self::SEEN_OPTION, $seen, false ); } } /** * Remove the hidden widgets (and the Welcome panel). * * @return void */ public function hide(): void { $user = wp_get_current_user(); $hidden = (array) aiou_get_setting( 'admin', 'dashboard', 'hide_widgets', array() ); /** * Filters which Dashboard widgets are removed for a user. Pro's * role-based Dashboard layout adds that user's role's choices. * * @since 2.0.0 * * @param string[] $hidden Widget ids. * @param WP_User $user The current user. */ $hidden = (array) apply_filters( 'aiou_dashboard_hidden_widgets', $hidden, $user ); foreach ( array_unique( array_map( 'strval', $hidden ) ) as $id ) { foreach ( array( 'normal', 'side', 'column3', 'column4' ) as $context ) { remove_meta_box( $id, 'dashboard', $context ); } } if ( aiou_get_setting( 'admin', 'dashboard', 'hide_welcome_panel', 0 ) ) { remove_action( 'welcome_panel', 'wp_welcome_panel' ); } } }