Skip to content

Commit

Permalink
Always show a 'Switch back' link in the footer when the admin toolbar…
Browse files Browse the repository at this point in the history
… isn't showing.

More intuitive redirecting after switching.

git-svn-id: http://plugins.svn.wordpress.org/user-switching/trunk@613948 b8457f37-d9ea-0310-8a92-e5e31aec5664
  • Loading branch information
johnbillion committed Oct 18, 2012
1 parent 68a705f commit 99e229c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
16 changes: 8 additions & 8 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Contributors: johnbillion
Donate link: http://lud.icro.us/donations/
Tags: user, users, profiles, switching, wpmu, multisite, buddypress, become, user control, user management, user access, developer
Requires at least: 2.7
Tested up to: 3.4.2
Requires at least: 3.1
Tested up to: 3.5
Stable tag: trunk

Instant switching between user accounts in WordPress.
Expand Down Expand Up @@ -34,7 +34,6 @@ This plugin allows you to quickly swap between user accounts in WordPress at the
* Farsi (Persian) by Amin Ab
* Slovak by Max Samael
* Polish by Bartosz Arendt
* Lithuanian by Tommixoft

== Installation ==

Expand Down Expand Up @@ -96,14 +95,15 @@ When a user switches off, the `switch_off_user` hook is called with the old user

== Upgrade Notice ==

= 0.6.3 =
* Lithuanian, Polish and Slovak translations.

= 0.6 =
* More intuitive redirecting after switching, a BuddyPress bugfix and some hooks added.
= 0.7 =
* More intuitive redirecting after switching. Always show a 'Switch back' link in the footer when the admin toolbar isn't showing.

== Changelog ==

= 0.7 =
* Always show a 'Switch back' link in the footer when the admin toolbar isn't showing.
* More intuitive redirecting after switching.

= 0.6.3 =
* Lithuanian translation by Tommixoft.

Expand Down
34 changes: 24 additions & 10 deletions user-switching.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
Plugin Name: User Switching
Description: Instant switching between user accounts in WordPress
Version: 0.6.3
Version: 0.7
Plugin URI: http://lud.icro.us/wordpress-plugin-user-switching/
Author: John Blackbourn
Author URI: http://johnblackbourn.com/
Expand Down Expand Up @@ -114,7 +114,7 @@ function init() {
return;

if ( isset( $_REQUEST['redirect_to'] ) and !empty( $_REQUEST['redirect_to'] ) )
$redirect_to = remove_query_arg( array( 'user_switched', 'switched_off', 'switched_back' ), $_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;

Expand Down Expand Up @@ -200,7 +200,10 @@ function admin_notice() {
<p><?php
if ( isset( $_GET['user_switched'] ) )
printf( __( 'Switched to %1$s (%2$s).', 'user_switching' ), $user_identity, $user_login );
printf( ' <a href="%s">%s</a>.', $this->switch_back_url(), sprintf( __( 'Switch back to %1$s (%2$s)', 'user_switching' ), $old_user->display_name, $old_user->user_login ) );
$url = add_query_arg( array(
'redirect_to' => urlencode( $this->current_url() )
), $this->switch_back_url() );
printf( ' <a href="%s">%s</a>.', $url, sprintf( __( 'Switch back to %1$s (%2$s)', 'user_switching' ), $old_user->display_name, $old_user->user_login ) );
?></p>
</div>
<?php
Expand Down Expand Up @@ -259,7 +262,9 @@ function admin_bar_menu( $wp_admin_bar ) {
'parent' => $parent,
'id' => 'wp-admin-bar-switch-back',
'title' => sprintf( __( 'Switch back to %1$s (%2$s)', 'user_switching' ), $old_user->display_name, $old_user->user_login ),
'href' => $this->switch_back_url()
'href' => add_query_arg( array(
'redirect_to' => urlencode( $this->current_url() )
), $this->switch_back_url() )
) );

}
Expand All @@ -269,7 +274,7 @@ function admin_bar_menu( $wp_admin_bar ) {
$url = $this->switch_off_url();
if ( !is_admin() ) {
$url = add_query_arg( array(
'redirect_to' => $_SERVER['REQUEST_URI']
'redirect_to' => urlencode( $this->current_url() )
), $url );
}

Expand All @@ -285,30 +290,30 @@ function admin_bar_menu( $wp_admin_bar ) {
}

/**
* Adds a 'Switch back to {user}' link to the WordPress footer if a user is switched off.
* Adds a 'Switch back to {user}' link to the WordPress footer if the admin toolbar isn't showing.
*
* @return null
*/
function switch_on() {

if ( !is_user_logged_in() and $old_user = $this->get_old_user() ) {
if ( !is_admin_bar_showing() and $old_user = $this->get_old_user() ) {
$link = sprintf( __( 'Switch back to %1$s (%2$s)', 'user_switching' ), $old_user->display_name, $old_user->user_login );
$url = add_query_arg( array(
'redirect_to' => $_SERVER['REQUEST_URI']
'redirect_to' => urlencode( $this->current_url() )
), $this->switch_back_url() );
echo '<p id="user_switching_switch_on"><a href="' . $url . '">' . $link . '</a></p>';
}

}

/**
* Adds a 'Switch back to {user}' link to the WordPress login screen if a user is switched off.
* Adds a 'Switch back to {user}' link to the WordPress login screen.
*
* @return null
*/
function login_message( $message ) {

if ( !is_user_logged_in() and $old_user = $this->get_old_user() ) {
if ( $old_user = $this->get_old_user() ) {
$link = sprintf( __( 'Switch back to %1$s (%2$s)', 'user_switching' ), $old_user->display_name, $old_user->user_login );
$url = $this->switch_back_url();
if ( isset( $_REQUEST['redirect_to'] ) and !empty( $_REQUEST['redirect_to'] ) ) {
Expand Down Expand Up @@ -419,6 +424,15 @@ function switch_off_url() {
), site_url( 'wp-login.php', 'login' ) ), 'switch_off' );
}

/**
* Returns the current URL.
*
* @return string The current URL
*/
function current_url() {
return ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}

/**
* Filter the user's capabilities so they can be added/removed on the fly.
*
Expand Down

0 comments on commit 99e229c

Please sign in to comment.