*/ private const BACK_OFFICE_CAPS = [ 'manage_options', RoleManager::CAP_MANAGE_INSTRUCTORS, RoleManager::CAP_MANAGE_STUDENTS, RoleManager::CAP_MANAGE_OFFERINGS, RoleManager::CAP_MANAGE_QUESTIONS, RoleManager::CAP_MANAGE_POLICIES, RoleManager::CAP_MANAGE_BILLING, RoleManager::CAP_MANAGE_AVAILABILITY, RoleManager::CAP_VIEW_ALL_LESSONS, RoleManager::CAP_VIEW_ALL_PAYMENTS, RoleManager::CAP_VIEW_OWN_PAYMENTS, RoleManager::CAP_EXPORT_PAYMENTS, ]; public function register(): void { add_action( 'admin_init', [ $this, 'redirectFromDashboard' ] ); add_filter( 'show_admin_bar', [ $this, 'hideAdminBar' ] ); } /** * Redirect a front-end-only user away from any wp-admin page to the site * home, so the dashboard and profile screens are never reachable. */ public function redirectFromDashboard(): void { if ( ! $this->shouldBlockAdminAccess() ) { return; } wp_safe_redirect( home_url( '/' ) ); exit; } /** * Whether the current request into wp-admin should be bounced to the front * end. AJAX requests are always allowed through so front-end features that * call admin-ajax keep working. */ public function shouldBlockAdminAccess(): bool { if ( wp_doing_ajax() ) { return false; } if ( ! is_user_logged_in() ) { return false; } return ! $this->hasBackOfficeAccess(); } /** * Hide the admin bar for front-end-only users; leave it untouched for anyone * with back-office access. * * @param bool $show Whether WordPress would otherwise show the admin bar. */ public function hideAdminBar( bool $show ): bool { if ( is_user_logged_in() && ! $this->hasBackOfficeAccess() ) { return false; } return $show; } /** * Whether the current user holds any capability that warrants wp-admin access. */ private function hasBackOfficeAccess(): bool { foreach ( self::BACK_OFFICE_CAPS as $cap ) { if ( current_user_can( $cap ) ) { return true; } } return false; } }