ID && current_user_can( 'manage_options' ) && current_user_can( 'edit_user', $target->ID ) && ! self::original_user(); } /** * The "Switch to" link for a user. * * @param WP_User $target User to switch to. * @return string */ public static function switch_url( WP_User $target ): string { return wp_nonce_url( add_query_arg( array( self::ARG => 'to', 'user_id' => $target->ID, ), home_url( '/' ) ), 'aiou_switch_to_' . $target->ID ); } /** * The "Switch back" link, returning to the given page afterwards. * * @param string $return_to Page to return to. * @return string */ private static function switch_back_url( string $return_to = '' ): string { $args = array( self::ARG => 'back' ); if ( '' !== $return_to ) { $args['redirect_to'] = rawurlencode( $return_to ); } return wp_nonce_url( add_query_arg( $args, home_url( '/' ) ), 'aiou_switch_back' ); } /** * The administrator the current user was switched from, or null when * not switched (or the way back is no longer valid). * * @return WP_User|null */ public static function original_user(): ?WP_User { if ( null !== self::$original ) { return self::$original ? self::$original : null; } self::$original = false; $data = self::read_cookie(); if ( ! $data || ! is_user_logged_in() ) { return null; } list( $original_id, $secret ) = $data; $stored = get_user_meta( $original_id, self::META_KEY, true ); if ( ! is_array( $stored ) || empty( $stored['hash'] ) || (int) ( $stored['target'] ?? 0 ) !== get_current_user_id() || (int) ( $stored['expires'] ?? 0 ) < time() || ! hash_equals( (string) $stored['hash'], self::hmac( $secret ) ) ) { return null; } $user = get_userdata( $original_id ); if ( $user ) { self::$original = $user; } return $user ? $user : null; } /** * The switch-back cookie's original user id and secret, if present. * * @return array{0: int, 1: string}|null */ private static function read_cookie(): ?array { $name = self::cookie_name(); if ( empty( $_COOKIE[ $name ] ) ) { return null; } $value = sanitize_text_field( wp_unslash( $_COOKIE[ $name ] ) ); if ( ! preg_match( '/^(\d+)\|([A-Za-z0-9]{32})$/', $value, $m ) ) { return null; } return array( (int) $m[1], $m[2] ); } /** * HMAC of a switch-back secret, keyed with the site's auth salt. * * @param string $secret Secret. * @return string */ private static function hmac( string $secret ): string { return hash_hmac( 'sha256', $secret, wp_salt( 'auth' ) ); } /** * Set or expire the switch-back cookie. * * @param string $value Cookie value ('' to expire it). * @param int $expires Expiry timestamp. * @return void */ private static function set_cookie( string $value, int $expires ): void { if ( headers_sent() ) { return; } $paths = array_unique( array( COOKIEPATH, SITECOOKIEPATH ) ); foreach ( $paths as $path ) { setcookie( self::cookie_name(), $value, $expires, $path, COOKIE_DOMAIN, is_ssl(), true ); } if ( '' === $value ) { unset( $_COOKIE[ self::cookie_name() ] ); } } /** * Replace the current login with another user's. * * @param int $user_id User to log in as. * @return void */ private static function log_in_as( int $user_id ): void { wp_destroy_current_session(); wp_clear_auth_cookie(); wp_set_current_user( $user_id ); wp_set_auth_cookie( $user_id, false, is_ssl() ); } /** * Handle "Switch to" and "Switch back" links. * * @return void */ public function handle_request(): void { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- routing only; each branch verifies its own nonce. $action = isset( $_GET[ self::ARG ] ) ? sanitize_key( wp_unslash( $_GET[ self::ARG ] ) ) : ''; if ( 'to' === $action ) { $this->switch_to(); } elseif ( 'back' === $action ) { $this->switch_back(); } } /** * Stop with a message (and a way back to the Users screen). * * @param string $message Message. * @param int $status HTTP status. * @return never */ private function fail( string $message, int $status = 403 ) { wp_die( esc_html( $message ), esc_html__( 'User switching', 'all-in-one-utilities' ), array( 'response' => (int) $status, 'back_link' => true, ) ); } /** * Switch from the current administrator to the requested user. * * @return never */ private function switch_to() { $user_id = isset( $_GET['user_id'] ) ? absint( $_GET['user_id'] ) : 0; $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : ''; if ( ! is_user_logged_in() || ! wp_verify_nonce( $nonce, 'aiou_switch_to_' . $user_id ) ) { $this->fail( __( 'This switch link has expired. Go back and try again.', 'all-in-one-utilities' ) ); } $target = get_userdata( $user_id ); if ( ! $target ) { $this->fail( __( 'That user no longer exists.', 'all-in-one-utilities' ), 404 ); } if ( self::original_user() ) { $this->fail( __( 'You are already viewing the site as another user. Switch back first.', 'all-in-one-utilities' ) ); } if ( ! self::can_switch_to( $target ) ) { $this->fail( __( 'You are not allowed to switch to this user.', 'all-in-one-utilities' ) ); } $original = wp_get_current_user(); $secret = wp_generate_password( 32, false ); $expires = time() + self::LIFETIME; update_user_meta( $original->ID, self::META_KEY, array( 'hash' => self::hmac( $secret ), 'target' => $target->ID, 'expires' => $expires, ) ); self::set_cookie( $original->ID . '|' . $secret, $expires ); self::log_in_as( $target->ID ); /** * Fires after an administrator switches to another user. * * @since 2.0.0 * * @param int $target_id The user switched to. * @param int $original_id The administrator who switched. */ do_action( 'aiou_switched_to_user', $target->ID, $original->ID ); $where = 'admin' === aiou_get_setting( 'users', 'user_switching', 'redirect', 'home' ) ? admin_url() : home_url( '/' ); wp_safe_redirect( $where ); exit; } /** * Return to the administrator's own account. * * @return never */ private function switch_back() { $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'aiou_switch_back' ) ) { $this->fail( __( 'This switch-back link has expired. Reload the page and try again.', 'all-in-one-utilities' ) ); } $original = self::original_user(); if ( ! $original ) { $this->fail( __( 'There is no account to switch back to. Log out and log in again with your own account.', 'all-in-one-utilities' ) ); } $target_id = get_current_user_id(); delete_user_meta( $original->ID, self::META_KEY ); self::set_cookie( '', time() - YEAR_IN_SECONDS ); self::log_in_as( $original->ID ); self::$original = null; /** * Fires after an administrator switches back to their own account. * * @since 2.0.0 * * @param int $original_id The administrator. * @param int $target_id The user they had switched to. */ do_action( 'aiou_switched_back_user', $original->ID, $target_id ); $fallback = add_query_arg( array( 'aiou_switched_back' => $target_id, ), admin_url( 'users.php' ) ); $return = isset( $_GET['redirect_to'] ) ? esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) : ''; $return = $return ? wp_validate_redirect( $return, '' ) : ''; // Back in wp-admin, the Users screen is the natural place to land; // on the front end, stay on the same page to compare the two views. if ( '' === $return || 0 === strpos( $return, admin_url() ) ) { $return = $fallback; } wp_safe_redirect( $return ); exit; } /** * Logging out while switched ends the switch too, so the way back * can't outlive the session it belongs to. * * @return void */ public function end_on_logout(): void { $data = self::read_cookie(); if ( ! $data ) { return; } $stored = get_user_meta( $data[0], self::META_KEY, true ); if ( is_array( $stored ) && ! empty( $stored['hash'] ) && hash_equals( (string) $stored['hash'], self::hmac( $data[1] ) ) ) { delete_user_meta( $data[0], self::META_KEY ); } self::set_cookie( '', time() - YEAR_IN_SECONDS ); } /** * "Switch to" in Users → All Users. * * @param array $actions Row actions. * @param WP_User $user Row user. * @return array */ public function row_action( array $actions, WP_User $user ): array { if ( self::can_switch_to( $user ) ) { $actions['aiou_switch_to'] = sprintf( '%3$s', esc_url( self::switch_url( $user ) ), /* translators: %s: user display name */ esc_attr( sprintf( __( 'Switch to %s', 'all-in-one-utilities' ), $user->display_name ) ), esc_html__( 'Switch to', 'all-in-one-utilities' ) ); } return $actions; } /** * "Switch to this user" on another user's profile screen. * * @param WP_User $user Profile being edited. * @return void */ public function profile_button( WP_User $user ): void { if ( ! self::can_switch_to( $user ) ) { return; } ?>

add_node( array( 'id' => 'aiou-switch-back', 'parent' => 'top-secondary', /* translators: %s: administrator's display name */ 'title' => esc_html( sprintf( __( 'Switch back to %s', 'all-in-one-utilities' ), $original->display_name ) ), 'href' => self::switch_back_url( is_admin() ? '' : self::current_url() ), ) ); } /** * The "Viewing as …" bar at the bottom of every page while switched. * * @return void */ public function render_bar(): void { $original = self::original_user(); if ( ! $original ) { return; } $user = wp_get_current_user(); $roles = array_map( static function ( string $role ): string { $names = wp_roles()->get_names(); return isset( $names[ $role ] ) ? translate_user_role( $names[ $role ] ) : $role; }, $user->roles ); ?>
ID, 28 ); ?> ' . esc_html( $user->display_name ) . '', $roles ? '' . esc_html( implode( ', ', $roles ) ) . '' : '' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- literal wrapper around esc_html()'d text. ); ?> display_name ) ); ?>

%s

', /* translators: %s: display name of the user switched away from */ esc_html( sprintf( __( 'Switched back from %s to your own account.', 'all-in-one-utilities' ), $user->display_name ) ) ); } }