/**
  * Get the redirect URL for a user.
  *
  * @since 6.4.1
  *
  * @param WP_User $user User object
  * @param string $type Optional. Type of redirect. Accepts 'login'
  *                               or 'logout'. Default is 'login'.
  * @param string $default Optional. Default URL if somehow not found
  * @return string Redirect URL
  */
 public function get_redirect_for_user($user, $type = 'login', $default = '')
 {
     // Make sure we have a default
     if (empty($default)) {
         $default = admin_url('profile.php');
     }
     // Bail if $user is not a WP_User
     if (!$user instanceof WP_User) {
         return $default;
     }
     // Make sure $type is valid
     if (!('login' == $type || 'logout' == $type)) {
         $type = 'login';
     }
     // Make sure the user has a role
     if (is_multisite() && empty($user->roles)) {
         $user->roles = array('subscriber');
     }
     // Get the user's role
     $user_role = reset($user->roles);
     // Get the redirection settings for the user's role
     $redirection = $this->get_option($user_role, array());
     // Determine which redirection type is being used
     switch ($redirection["{$type}_type"]) {
         case 'referer':
             // Get the referer
             if (!($referer = wp_get_original_referer())) {
                 $referer = wp_get_referer();
             }
             // Strip unwanted arguments from the referer
             $referer = Theme_My_Login_Common::strip_query_args($referer);
             // Is the URL a single post type?
             if ($page_id = url_to_postid($referer)) {
                 // Bail if the referer is TML page
                 if (Theme_My_Login::is_tml_page(null, $page_id)) {
                     return $default;
                 }
             }
             // Send 'em back to the referer
             $redirect_to = $referer;
             break;
         case 'custom':
             // Send 'em to the specified URL
             $redirect_to = $redirection["{$type}_url"];
             // Allow a few user specific variables
             $redirect_to = str_replace(array('%user_id%', '%user_nicename%'), array($user->ID, $user->user_nicename), $redirect_to);
             break;
     }
     // Make sure $redirect_to isn't empty
     if (empty($redirect_to)) {
         $redirect_to = $default;
     }
     return $redirect_to;
 }