array(), 'hidden_widgets' => array(), 'hide_bar_roles' => array(), 'block_admin_roles' => array(), 'hide_wp_logo' => false, 'hide_update_nag' => false, 'footer_text' => '', ); } /** * Settings fields. * * @return array */ public function get_fields() { $roles = $this->role_options(); unset( $roles['administrator'] ); return array( 'hidden_menus' => array( 'type' => 'checkboxes', 'label' => __( 'Hide menu items for non-administrators', 'toolvane' ), 'options' => $this->menu_options(), 'description' => __( 'Administrators always see the full menu. This only hides links; access is still controlled by user capabilities.', 'toolvane' ), ), 'hidden_widgets' => array( 'type' => 'checkboxes', 'label' => __( 'Hide dashboard widgets for non-administrators', 'toolvane' ), 'options' => array( 'dashboard_activity' => __( 'Activity', 'toolvane' ), 'dashboard_right_now' => __( 'At a Glance', 'toolvane' ), 'dashboard_quick_press' => __( 'Quick Draft', 'toolvane' ), 'dashboard_primary' => __( 'WordPress Events and News', 'toolvane' ), 'dashboard_site_health' => __( 'Site Health Status', 'toolvane' ), 'welcome_panel' => __( 'Welcome panel', 'toolvane' ), ), ), 'hide_bar_roles' => array( 'type' => 'checkboxes', 'label' => __( 'Hide front-end toolbar for', 'toolvane' ), 'options' => $roles, ), 'block_admin_roles' => array( 'type' => 'checkboxes', 'label' => __( 'Block wp-admin for', 'toolvane' ), 'options' => $roles, 'description' => __( 'These users are sent to the homepage, or to WooCommerce My Account, when they open wp-admin. AJAX and uploads keep working.', 'toolvane' ), ), 'hide_wp_logo' => array( 'type' => 'checkbox', 'label' => __( 'WordPress logo', 'toolvane' ), 'checkbox_label' => __( 'Remove the WordPress logo menu from the toolbar', 'toolvane' ), ), 'hide_update_nag' => array( 'type' => 'checkbox', 'label' => __( 'Update notices', 'toolvane' ), 'checkbox_label' => __( 'Hide WordPress update notices from non-administrators', 'toolvane' ), ), 'footer_text' => array( 'type' => 'text', 'label' => __( 'Admin footer text', 'toolvane' ), ), ); } /** * Registers hooks. */ public function init() { add_action( 'admin_menu', array( $this, 'hide_menus' ), 9999 ); add_action( 'wp_dashboard_setup', array( $this, 'hide_widgets' ), 9999 ); add_filter( 'show_admin_bar', array( $this, 'show_admin_bar' ), 20 ); add_action( 'admin_init', array( $this, 'block_admin' ), 1 ); add_filter( 'admin_footer_text', array( $this, 'footer_text' ), 20 ); add_action( 'admin_bar_menu', array( $this, 'admin_bar' ), 999 ); add_action( 'admin_head', array( $this, 'hide_update_nag' ), 1 ); } /** * Top-level menu items available for hiding. * * @return array slug => title */ private function menu_options() { global $menu; $options = array(); foreach ( (array) $menu as $item ) { if ( empty( $item[2] ) || empty( $item[0] ) || Admin::SLUG === $item[2] || ( isset( $item[4] ) && false !== strpos( $item[4], 'wp-menu-separator' ) ) ) { continue; } // Remove update and notification bubbles before stripping tags. $title = trim( wp_strip_all_tags( preg_replace( '#]*>.*?#s', '', $item[0] ) ) ); $options[ $item[2] ] = $title ? $title : $item[2]; } // Keep previously saved slugs selectable even if a plugin is currently inactive. foreach ( (array) $this->get( 'hidden_menus', array() ) as $slug ) { if ( ! isset( $options[ $slug ] ) ) { $options[ $slug ] = $slug; } } return $options; } /** * Removes selected menus. */ public function hide_menus() { if ( current_user_can( 'manage_options' ) ) { return; } foreach ( (array) $this->get( 'hidden_menus', array() ) as $slug ) { remove_menu_page( $slug ); } } /** * Removes selected dashboard widgets. */ public function hide_widgets() { if ( current_user_can( 'manage_options' ) ) { return; } foreach ( (array) $this->get( 'hidden_widgets', array() ) as $widget ) { if ( 'welcome_panel' === $widget ) { remove_action( 'welcome_panel', 'wp_welcome_panel' ); continue; } remove_meta_box( $widget, 'dashboard', 'normal' ); remove_meta_box( $widget, 'dashboard', 'side' ); } } /** * Toolbar visibility. * * @param bool $show Whether to show the toolbar. * @return bool */ public function show_admin_bar( $show ) { if ( $show && ! current_user_can( 'manage_options' ) && $this->current_user_has_role( (array) $this->get( 'hide_bar_roles', array() ) ) ) { return false; } return $show; } /** * Redirects blocked roles out of wp-admin. */ public function block_admin() { global $pagenow; if ( wp_doing_ajax() || wp_doing_cron() || in_array( $pagenow, array( 'admin-post.php', 'async-upload.php', 'admin-ajax.php' ), true ) || current_user_can( 'manage_options' ) ) { return; } if ( ! $this->current_user_has_role( (array) $this->get( 'block_admin_roles', array() ) ) ) { return; } $target = function_exists( 'wc_get_page_permalink' ) ? wc_get_page_permalink( 'myaccount' ) : home_url( '/' ); wp_safe_redirect( $target ? $target : home_url( '/' ) ); exit; } /** * Footer text. * * @param string $text Default text. * @return string */ public function footer_text( $text ) { $custom = $this->get( 'footer_text' ); return $custom ? esc_html( $custom ) : $text; } /** * Removes the WordPress logo menu. * * @param \WP_Admin_Bar $bar Toolbar. */ public function admin_bar( $bar ) { if ( $this->get( 'hide_wp_logo' ) ) { $bar->remove_node( 'wp-logo' ); } } /** * Hides core update notices from non-administrators. */ public function hide_update_nag() { if ( $this->get( 'hide_update_nag' ) && ! current_user_can( 'update_core' ) ) { remove_action( 'admin_notices', 'update_nag', 3 ); remove_action( 'network_admin_notices', 'update_nag', 3 ); remove_action( 'admin_notices', 'maintenance_nag', 10 ); } } }