roles; foreach ( $wp_roles as $wp_role_slug => $wp_role_info ) { if ( $wp_role_slug == $viewing_admin_as ) { $viewing_admin_as_role_name = $wp_role_info['name']; } } if ( ! isset( $viewing_admin_as_role_name ) ) { $viewing_admin_as_role_name = $viewing_admin_as; } $translated_name_for_viewing_admin_as = ucfirst( $viewing_admin_as_role_name ); // Add parent menu basd on the role being set to "View as" if ( 'administrator' == $viewing_admin_as ) { // Add parent menu $wp_admin_bar->add_menu( array( 'id' => 'asenha-view-admin-as-role', 'parent' => 'top-secondary', 'title' => 'View as ▼', 'href' => '#', 'meta' => array( 'title' => 'View admin pages and the site (logged-in) as one of the following user roles.' ), ) ); } else { // Add parent menu $wp_admin_bar->add_menu( array( 'id' => 'asenha-view-admin-as-role', 'parent' => 'top-secondary', 'title' => 'Viewing as ' . $translated_name_for_viewing_admin_as . ' ▼', 'href' => '#', ) ); } // Get available role(s) to switch to $roles_to_switch_to = $this->get_roles_to_switch_to(); // Add role(s) to switch to as sub-menu if ( 'administrator' == $viewing_admin_as ) { // Add submenu for each role other than Administrator $i = 1; foreach ( $roles_to_switch_to as $role_slug => $data ) { $wp_admin_bar->add_menu( array( 'id' => 'role' . $i . '_' . $role_slug, // id based on role slug, e.g. role1_editor, role5_shop_manager 'parent' => 'asenha-view-admin-as-role', 'title' => $data['role_name'], // role name, e.g. Editor, Shop Manager 'href' => $data['nonce_url'], // nonce URL for each role ) ); $i++; } } else { // Add submenu to switch back to Administrator role foreach ( $roles_to_switch_to as $role_slug => $data ) { $wp_admin_bar->add_menu( array( 'id' => 'role_' . $role_slug, // id based on role slug, e.g. role1_editor, role5_shop_manager 'parent' => 'asenha-view-admin-as-role', 'title' => 'Switch back to ' . $data['role_name'], // role name, e.g. Editor, Shop Manager 'href' => $data['nonce_url'], // nonce URL for each role ) ); } } } /** * Get roles availble to switch to * * @since 1.8.0 */ private function get_roles_to_switch_to() { $current_user = wp_get_current_user(); $current_user_role_slugs = $current_user->roles; // indexed array of current user role slug(s) // Get full list of roles defined in WordPress $wp_roles = wp_roles()->roles; $roles_to_switch_to = array(); // Get which role slug is currently active for viewing $viewing_admin_as = get_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', true ); if ( 'administrator' == $viewing_admin_as ) { // Exclude 'Administrator' from the "View as" menu foreach ( $wp_roles as $wp_role_slug => $wp_role_info ) { if ( ! in_array( $wp_role_slug,$current_user_role_slugs ) ) { $roles_to_switch_to[$wp_role_slug] = array( 'role_name' => $wp_role_info['name'], // role name, e.g. Editor, Shop Manager 'nonce_url' => wp_nonce_url( add_query_arg( array( 'action' => 'switch_role_to', 'role' => $wp_role_slug, ) ), // add query parameters to current URl, this is the $actionurl that will be appended with the nonce action 'asenha_view_admin_as_' . $wp_role_slug, // the nonce $action name 'nonce', // the nonce url parameter name ), // will result in a URL that looks like https://www.example.com/wp-admin/index.php?action=switch_role_to&role=editor&nonce=2ced3a40df ); } } } else { // Only show switch back to Administrator in the "View as" menu $roles_to_switch_to['administrator'] = array( 'role_name' => 'Administrator', // role name, e.g. Editor, Shop Manager 'nonce_url' => wp_nonce_url( add_query_arg( array( 'action' => 'switch_back_to_administrator', 'role' => 'administrator', ) ), // add query parameters to current URl, this is the $actionurl that will be appended with the nonce action 'asenha_view_admin_as_administrator', // the nonce $action name 'nonce', // the nonce url parameter name ), // will result in a URL that looks like https://www.example.com/wp-admin/index.php?action=switch_role_to&role=editor&nonce=2ced3a40df ); } return $roles_to_switch_to; // array of $role_slug => $nonce_url } /** * Switch user role to view admin and site * * @since 1.8.0 */ public function role_switcher_to_view_admin_as() { $current_user = wp_get_current_user(); $current_user_role_slugs = $current_user->roles; // indexed array of current user role slug(s) if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['role'] ) && isset( $_REQUEST['nonce'] ) ) { $action = sanitize_text_field( $_REQUEST['action'] ); $new_role = sanitize_text_field( $_REQUEST['role'] ); $nonce = sanitize_text_field( $_REQUEST['nonce'] ); if ( 'switch_role_to' === $action ) { // Check nonce validity and role existence $wp_roles = array_keys( wp_roles()->roles ); // indexed array of all WP roles if ( ! wp_verify_nonce( $nonce, 'asenha_view_admin_as_' . $new_role ) || ! in_array( $new_role, $wp_roles ) ) { return; // cancel role switching } // Get original roles (before role switching) of the current user $original_role_slugs = get_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', true ); // Store original user role(s) before switching it to another role if ( empty( $original_role_slugs ) ) { update_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', $current_user_role_slugs ); } // Remove all current roles from current user. foreach ( $current_user_role_slugs as $current_user_role_slug ) { $current_user->remove_role( $current_user_role_slug ); } // Add new role to current user $current_user->add_role( $new_role ); // Mark that the user has switched to a non-administrator role update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', $new_role ); // Redirect to admin dashboard wp_safe_redirect( get_admin_url() ); exit; } if ( 'switch_back_to_administrator' === $action ) { // Check nonce validity if ( ! wp_verify_nonce( $nonce, 'asenha_view_admin_as_administrator' ) || ( $new_role != 'administrator' ) ) { return; // cancel role switching } // Remove all current roles from current user. foreach ( $current_user_role_slugs as $current_role_slug ) { $current_user->remove_role( $current_role_slug ); } // Get original roles (before role switching) of the current user $original_role_slugs = get_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', true ); // Add the original roles to the current user foreach ( $original_role_slugs as $original_role_slug ) { $current_user->add_role( $original_role_slug ); } // Mark that the user has switched back to an administrator role update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', 'administrator' ); } } elseif ( isset( $_REQUEST['reset-view-as'] ) ) { $reset_view_as = sanitize_text_field( $_REQUEST['reset-view-as'] ); if ( $reset_view_as == 'yes' ) { // Remove all current roles from current user. foreach ( $current_user_role_slugs as $current_role_slug ) { $current_user->remove_role( $current_role_slug ); } // Get original roles (before role switching) of the current user $original_role_slugs = get_user_meta( get_current_user_id(), '_asenha_view_admin_as_original_roles', true ); // Add the original roles to the current user foreach ( $original_role_slugs as $original_role_slug ) { $current_user->add_role( $original_role_slug ); } // Mark that the user has switched back to an administrator role update_user_meta( get_current_user_id(), '_asenha_viewing_admin_as', 'administrator' ); // Redirect to admin dashboard wp_safe_redirect( get_admin_url() ); exit; } } } /** * Show custom error page on switch failure, which causes inability to view admin dashboard/pages * * @since 1.8.0 */ public function custom_error_page_on_switch_failure( $callback ) { ?> WordPress Error
Something went wrong. Click here to go back to the admin dashboard.
add_menu( array( 'id' => 'password_protection', 'title' => '', 'href' => admin_url( 'tools.php?page=admin-site-enhancements#utilities' ), 'meta' => array( 'title' => 'Password protection is enabled for this site.', ), ) ); } } } /** * Add icon and CSS for admin bar item * * @since 4.1.0 */ public function add_wp_admin_bar_item_styles() { if ( is_user_logged_in() ) { if ( current_user_can( 'manage_options' ) ) { ?> 'view', 'source' => urlencode( $current_url ), ); $pwd_protect_login_url = add_query_arg( $args, home_url() ); nocache_headers(); wp_safe_redirect( $pwd_protect_login_url ); exit(); } } /** * Maybe process login to access protected page content * * @since 4.1.0 */ public function maybe_process_login() { global $password_protected_errors; $password_protected_errors = new WP_Error(); if ( isset( $_REQUEST['protected_page_pwd'] ) ) { $password_input = $_REQUEST['protected_page_pwd']; $options = get_option( ASENHA_SLUG_U, array() ); $stored_password = $options['password_protection_password']; if ( ! empty( $password_input ) ) { if ( $password_input == base64_decode( $stored_password ) ) { // Password is correct // Set auth cookie // $expiration = time() + DAY_IN_SECONDS; // in 24 hours $expiration = 0; // by the end of browsing session setcookie( 'asenha_password_protection', 'authenticated', $expiration, COOKIEPATH, COOKIE_DOMAIN, true, true ); // Redirect $redirect_to_url = isset( $_REQUEST['source'] ) ? $_REQUEST['source'] : ''; wp_safe_redirect( $redirect_to_url ); exit(); } else { // Password is incorrect // Add error message $password_protected_errors->add( 'incorrect_password', 'Incorrect password.' ); } } else { // Password input is empty // Add error message $password_protected_errors->add( 'empty_password', 'Password can not be empty.' ); } } } /** * Add custom login error messages * * @since 4.1.0 */ public function add_login_error_messages() { global $password_protected_errors; if ( $password_protected_errors->get_error_code() ) { $messages = ''; $errors = ''; // Extract the error message foreach( $password_protected_errors->get_error_codes() as $code ) { $severity = $password_protected_errors->get_error_data( $code ); foreach( $password_protected_errors->get_error_messages( $code ) as $error ) { if ( 'message' == $severity ) { $messages .= $error . '
'; } else { $errors .= $error . '
'; } } } // Output the error message if ( ! empty( $messages ) ) { echo '

' . $messages . '

'; } if ( ! empty( $errors ) ) { echo '
' . $errors . '
'; } } } /** * Redirect 404 to homepage * * @since 1.7.0 */ public function redirect_404_to_homepage() { if ( ! is_404() || is_admin() || ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ) { return; } else { // wp_safe_redirect( home_url(), 301 ); header( 'HTTP/1.1 301 Moved Permanently'); header( 'Location: ' . home_url() ); exit(); } } }