protected function switch_to_user($user_id, $remember = false, $set_old_user = true)
 {
     // Verify the integrity of our wrapper methods
     $target = new ReflectionFunction('switch_to_user');
     $wrapper = new ReflectionMethod(__METHOD__);
     $this->assertSame($wrapper->getNumberOfParameters(), $target->getNumberOfParameters());
     /*
      * `switch_to_user()` and the functions it subsequently calls will trigger "headers already sent" PHP errors, so
      * we need to mute them in order to avoid phpunit throwing an exception.
      */
     $this->silence();
     $user = switch_to_user($user_id, $remember, $set_old_user);
     $this->go_forth();
     return $user;
 }
 /**
  * Load localisation files and route actions depending on the 'action' query var.
  */
 public function action_init()
 {
     load_plugin_textdomain('user-switching', false, dirname(plugin_basename(__FILE__)) . '/languages');
     if (!isset($_REQUEST['action'])) {
         return;
     }
     $current_user = is_user_logged_in() ? wp_get_current_user() : null;
     switch ($_REQUEST['action']) {
         # We're attempting to switch to another user:
         case 'switch_to_user':
             if (isset($_REQUEST['user_id'])) {
                 $user_id = absint($_REQUEST['user_id']);
             } else {
                 $user_id = 0;
             }
             # Check authentication:
             if (!current_user_can('switch_to_user', $user_id)) {
                 wp_die(esc_html__('Could not switch users.', 'user-switching'));
             }
             # Check intent:
             check_admin_referer("switch_to_user_{$user_id}");
             # Switch user:
             $user = switch_to_user($user_id, self::remember());
             if ($user) {
                 $redirect_to = self::get_redirect($user, $current_user);
                 # Redirect to the dashboard or the home URL depending on capabilities:
                 $args = array('user_switched' => 'true');
                 if ($redirect_to) {
                     wp_safe_redirect(add_query_arg($args, $redirect_to));
                 } else {
                     if (!current_user_can('read')) {
                         wp_redirect(add_query_arg($args, home_url()));
                     } else {
                         wp_redirect(add_query_arg($args, admin_url()));
                     }
                 }
                 die;
             } else {
                 wp_die(esc_html__('Could not switch users.', 'user-switching'));
             }
             break;
             # We're attempting to switch back to the originating user:
         # We're attempting to switch back to the originating user:
         case 'switch_to_olduser':
             # Fetch the originating user data:
             if (!($old_user = self::get_old_user())) {
                 wp_die(esc_html__('Could not switch users.', 'user-switching'));
             }
             # Check authentication:
             if (!self::authenticate_old_user($old_user)) {
                 wp_die(esc_html__('Could not switch users.', 'user-switching'));
             }
             # Check intent:
             check_admin_referer("switch_to_olduser_{$old_user->ID}");
             # Switch user:
             if (switch_to_user($old_user->ID, self::remember(), false)) {
                 $redirect_to = self::get_redirect($old_user, $current_user);
                 $args = array('user_switched' => 'true', 'switched_back' => 'true');
                 if ($redirect_to) {
                     wp_safe_redirect(add_query_arg($args, $redirect_to));
                 } else {
                     wp_redirect(add_query_arg($args, admin_url('users.php')));
                 }
                 die;
             } else {
                 wp_die(esc_html__('Could not switch users.', 'user-switching'));
             }
             break;
             # We're attempting to switch off the current user:
         # We're attempting to switch off the current user:
         case 'switch_off':
             # Check authentication:
             if (!current_user_can('switch_off')) {
                 /* Translators: "switch off" means to temporarily log out */
                 wp_die(esc_html__('Could not switch off.', 'user-switching'));
             }
             # Check intent:
             check_admin_referer("switch_off_{$current_user->ID}");
             # Switch off:
             if (switch_off_user()) {
                 $redirect_to = self::get_redirect(null, $current_user);
                 $args = array('switched_off' => 'true');
                 if ($redirect_to) {
                     wp_safe_redirect(add_query_arg($args, $redirect_to));
                 } else {
                     wp_redirect(add_query_arg($args, home_url()));
                 }
                 die;
             } else {
                 /* Translators: "switch off" means to temporarily log out */
                 wp_die(esc_html__('Could not switch off.', 'user-switching'));
             }
             break;
     }
 }
 /**
  * Load localisation files and route actions depending on the 'action' query var. Actions are secured
  * with WordPress' nonce system.
  *
  * @return null
  */
 function init()
 {
     load_plugin_textdomain('user_switching', false, dirname(plugin_basename(__FILE__)) . '/languages');
     if (!isset($_REQUEST['action'])) {
         return;
     }
     if (isset($_REQUEST['redirect_to']) and !empty($_REQUEST['redirect_to'])) {
         $redirect_to = remove_query_arg(array('user_switched', 'switched_off', 'switched_back', 'message', 'updated', 'settings-updated'), $_REQUEST['redirect_to']);
     } else {
         $redirect_to = false;
     }
     switch ($_REQUEST['action']) {
         # We're attempting to switch to another user:
         case 'switch_to_user':
             $user_id = absint($_REQUEST['user_id']);
             check_admin_referer("switch_to_user_{$user_id}");
             # Switch user:
             if (switch_to_user($user_id, $this->remember())) {
                 # Redirect to the dashboard or the home URL depending on capabilities:
                 if ($redirect_to) {
                     wp_safe_redirect(add_query_arg(array('user_switched' => 'true'), $redirect_to));
                 } else {
                     if (!current_user_can('read')) {
                         wp_redirect(add_query_arg(array('user_switched' => 'true'), home_url()));
                     } else {
                         wp_redirect(add_query_arg(array('user_switched' => 'true'), admin_url()));
                     }
                 }
                 die;
             } else {
                 wp_die(__('Could not switch users.', 'user_switching'));
             }
             break;
             # We're attempting to switch back to the originating user:
         # We're attempting to switch back to the originating user:
         case 'switch_to_olduser':
             check_admin_referer('switch_to_olduser');
             # Fetch the originating user data:
             if (!($old_user = $this->get_old_user())) {
                 wp_die(__('Could not switch users.', 'user_switching'));
             }
             # Switch user:
             if (switch_to_user($old_user->ID, $this->remember(), false)) {
                 if ($redirect_to) {
                     wp_safe_redirect(add_query_arg(array('user_switched' => 'true', 'switched_back' => 'true'), $redirect_to));
                 } else {
                     wp_redirect(add_query_arg(array('user_switched' => 'true', 'switched_back' => 'true'), admin_url('users.php')));
                 }
                 die;
             } else {
                 wp_die(__('Could not switch users.', 'user_switching'));
             }
             break;
             # We're attempting to switch off the current user:
         # We're attempting to switch off the current user:
         case 'switch_off':
             check_admin_referer('switch_off');
             # Switch off:
             if (switch_off_user()) {
                 if ($redirect_to) {
                     wp_safe_redirect(add_query_arg(array('switched_off' => 'true'), $redirect_to));
                 } else {
                     wp_redirect(add_query_arg(array('switched_off' => 'true'), home_url()));
                 }
                 die;
             } else {
                 wp_die(__('Could not switch off.', 'user_switching'));
             }
             break;
     }
 }