User Switching - Version 0.8.8

Version Description

  • Spanish Translation by Marcelo Pedra.
  • User Switching is now a network-only plugin when used on Multisite.

=

Download this release

Release Info

Developer johnbillion
Plugin Icon 128x128 User Switching
Version 0.8.8
Comparing to
See all releases

Code changes from version 0.8.7 to 0.8.8

languages/user-switching-es_ES.mo ADDED
Binary file
languages/user-switching-es_ES.po ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ msgid ""
2
+ msgstr ""
3
+ "Project-Id-Version: User Switching v0.8.7\n"
4
+ "Report-Msgid-Bugs-To: \n"
5
+ "POT-Creation-Date: \n"
6
+ "PO-Revision-Date: 2014-03-23 11:57-0300\n"
7
+ "Last-Translator: Marcelo Pedra <marcelo@marcelopedra.com.ar>\n"
8
+ "Language-Team: \n"
9
+ "MIME-Version: 1.0\n"
10
+ "Content-Type: text/plain; charset=UTF-8\n"
11
+ "Content-Transfer-Encoding: 8bit\n"
12
+ "Plural-Forms: nplurals=2; plural=n != 1;\n"
13
+ "X-Generator: Poedit 1.6.4\n"
14
+ "X-Poedit-SourceCharset: utf-8\n"
15
+ "X-Poedit-KeywordsList: __;_e;__ngettext:1,2;_n:1,2;__ngettext_noop:1,2;"
16
+ "_n_noop:1,2;_c,_nc:4c,1,2;_x:1,2c;_ex:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2\n"
17
+ "X-Poedit-Basepath: ../\n"
18
+ "X-Textdomain-Support: yes\n"
19
+ "Language: es_ES\n"
20
+ "X-Poedit-SearchPath-0: .\n"
21
+
22
+ # user-switching
23
+ #. translators: plugin header field 'Name'
24
+ #: user-switching.php:0
25
+ msgid "User Switching"
26
+ msgstr ""
27
+
28
+ # @ user-switching
29
+ #. translators: plugin header field 'PluginURI'
30
+ #: user-switching.php:0
31
+ msgid "https://johnblackbourn.com/wordpress-plugin-user-switching/"
32
+ msgstr ""
33
+
34
+ # @ user-switching
35
+ #. translators: plugin header field 'Description'
36
+ #: user-switching.php:0
37
+ msgid "Instant switching between user accounts in WordPress"
38
+ msgstr ""
39
+
40
+ # @ user-switching
41
+ #. translators: plugin header field 'Author'
42
+ #: user-switching.php:0
43
+ msgid "John Blackbourn"
44
+ msgstr ""
45
+
46
+ # @ user-switching
47
+ #. translators: plugin header field 'AuthorURI'
48
+ #: user-switching.php:0
49
+ msgid "https://johnblackbourn.com/"
50
+ msgstr ""
51
+
52
+ # @ user-switching
53
+ #. translators: plugin header field 'Version'
54
+ #: user-switching.php:0
55
+ msgid "0.8.7"
56
+ msgstr ""
57
+
58
+ # @ user-switching
59
+ #: user-switching.php:79
60
+ msgctxt "User Switching title on user profile screen"
61
+ msgid "User Switching"
62
+ msgstr ""
63
+
64
+ # @ user-switching
65
+ #: user-switching.php:80 user-switching.php:369 user-switching.php:406
66
+ #: user-switching.php:429
67
+ msgid "Switch&nbsp;To"
68
+ msgstr "Cambiarse&nbsp;a&nbsp;este&nbsp;usuario"
69
+
70
+ # @ user-switching
71
+ #: user-switching.php:139 user-switching.php:150 user-switching.php:163
72
+ msgid "Could not switch users."
73
+ msgstr "No se pudo iniciar ese usuario."
74
+
75
+ # @ user-switching
76
+ #: user-switching.php:181
77
+ msgid "Could not switch off."
78
+ msgstr "No se pudo regresar."
79
+
80
+ # @ user-switching
81
+ #: user-switching.php:225 user-switching.php:242
82
+ #, php-format
83
+ msgid "Switched to %1$s (%2$s)."
84
+ msgstr "Iniciado como %1$s (%2$s)."
85
+
86
+ # user-switching
87
+ #: user-switching.php:229 user-switching.php:289 user-switching.php:325
88
+ #: user-switching.php:343
89
+ #, php-format
90
+ msgid "Switch back to %1$s (%2$s)"
91
+ msgstr "Volver a %1$s (%2$s)"
92
+
93
+ # @ user-switching
94
+ #: user-switching.php:240
95
+ #, php-format
96
+ msgid "Switched back to %1$s (%2$s)."
97
+ msgstr "De vuelta como %1$s (%2$s)."
98
+
99
+ # @ user-switching
100
+ #: user-switching.php:309
101
+ msgid "Switch Off"
102
+ msgstr "Volver a mi usuario"
readme.txt CHANGED
@@ -1,116 +1,122 @@
1
- === User Switching ===
2
-
3
- Contributors: johnbillion
4
- Tags: user, users, profiles, switching, wpmu, multisite, buddypress, bbpress, become, user control, user management, user access, developer
5
- Requires at least: 3.1
6
- Tested up to: 3.9
7
- Stable tag: 0.8.7
8
- License: GPL v2 or later
9
-
10
- Instant switching between user accounts in WordPress.
11
-
12
- == Description ==
13
-
14
- This plugin allows you to quickly swap between user accounts in WordPress at the click of a button. You'll be instantly logged out and logged in as your desired user. This is handy for test environments where you regularly log out and in between different accounts, or for adminstrators who need to switch between multiple accounts.
15
-
16
- = Features =
17
-
18
- * Switch user: Instantly switch to any user account from the *Users* screen.
19
- * Switch back: Instantly switch back to your originating account.
20
- * Switch off: Log out of your account but retain the ability to instantly switch back in again.
21
- * It's completely secure (see the *Security* section below).
22
- * Compatible with WordPress, WordPress Multisite, BuddyPress and bbPress.
23
-
24
- = Security =
25
-
26
- * Only users with the ability to edit other users can switch user accounts. By default this is only Administrators on single site installs, and Super Admins on Multisite installs.
27
- * Passwords are not (and cannot be) revealed.
28
- * Implements WordPress' nonce security system, meaning only those who intend to switch users can switch.
29
- * Full support for administration over SSL (if applicable).
30
-
31
- = Usage =
32
-
33
- 1. Visit the *Users* menu in WordPress and you'll see a *Switch To* link next to each user.
34
- 2. Click this and you will immediately switch into that user account.
35
- 3. You can switch back to your originating account via the *Switch back* link on each dashboard screen or in your profile menu in the WordPress toolbar.
36
-
37
- See the [FAQ](http://wordpress.org/plugins/user-switching/faq/) for information about the *Switch Off* feature.
38
-
39
- = Translations Included =
40
-
41
- * Arabic by Hassan Hisham
42
- * Chinese Simplified by Tunghsiao Liu
43
- * Farsi (Persian) by Amin Ab
44
- * German by Ralph Stenzel
45
- * Japanese by Yusuke Hayasaki
46
- * Lithuanian by Tommixoft
47
- * Polish by Bartosz Arendt
48
- * Russian by R J
49
- * Slovak by Max Samael
50
-
51
- == Screenshots ==
52
-
53
- 1. ![The *Switch To* link on the Users screen](https://raw.github.com/johnbillion/user-switching/master/assets-wp-repo/screenshot-1.png)
54
- 2. ![The *Switch To* link on a user's profile](https://raw.github.com/johnbillion/user-switching/master/assets-wp-repo/screenshot-2.png)
55
-
56
- == Installation ==
57
-
58
- If you have the [WordPress Developer plugin](http://wordpress.org/plugins/developer/) installed then User Switching is a one-click install from the Tools -> Developer screen.
59
-
60
- Alternatively, you can install this plugin directly from your WordPress dashboard:
61
-
62
- 1. Go to the *Plugins* menu and click *Add New*.
63
- 2. Search for *User Switching*.
64
- 3. Click *Install Now* next to the *User Switching* plugin.
65
- 4. Activate the plugin.
66
-
67
- == Frequently Asked Questions ==
68
-
69
- = What does "Switch off" mean? =
70
-
71
- Switching off logs you out of your account but retains your user ID in an authorisation cookie so you can switch straight back without having to log in again manually. It's akin to switching to no user, and being able to switch back.
72
-
73
- The *Switch Off* link can be found in your profile menu in the WordPress toolbar. Once you've switched off you'll see a *Switch back* link in the footer of your site.
74
-
75
- = Does this plugin work with WordPress Multisite? =
76
-
77
- Yes, and you'll also be able to switch users from the Users screen in Network Admin.
78
-
79
- = Does this plugin work with BuddyPress? =
80
-
81
- Yes, and you'll also be able to switch users from member profile screens and the member listing screen.
82
-
83
- = Does this plugin work with bbPress? =
84
-
85
- Yes, and you'll also be able to switch users from member profile screens.
86
-
87
- = Does this work as a mu-plugin? =
88
-
89
- Yes, but you'll need to install `user-switching.php` into the root of your `mu-plugins` directory, not in the `user-switching` subdirectory. This is a restriction of WordPress.
90
-
91
- = What capability does a user need in order to switch accounts? =
92
-
93
- A user needs the `edit_users` capability in order to switch user accounts. By default only Administrators have this capability, and with Multisite enabled only Super Admins have this capability.
94
-
95
- = Can regular admins on Multisite installs switch accounts? =
96
-
97
- No. This can be enabled though by installing the [User Switching for Regular Admins](https://github.com/johnbillion/user-switching-for-regular-admins) plugin.
98
-
99
- = Are any plugin hooks called when users switch accounts? =
100
-
101
- Yes. When a user switches to another account, the `switch_to_user` hook is called with the new and old user IDs passed as parameters.
102
-
103
- When a user switches back to their original account, the `switch_back_user` hook is called with the new (original) and old user IDs passed as parameters. Note that the old user ID can be boolean false if the user is switching back after they've been switched off.
104
-
105
- When a user switches off, the `switch_off_user` hook is called with the old user ID as a parameter.
 
106
 
107
  == Upgrade Notice ==
108
 
109
- = 0.8.7 =
110
- * Respect the `secure_logged_in_cookie` and `login_redirect` filters.
 
111
 
112
  == Changelog ==
113
 
 
 
 
 
114
  = 0.8.7 =
115
  * Respect the `secure_logged_in_cookie` and `login_redirect` filters.
116
 
1
+ === User Switching ===
2
+
3
+ Contributors: johnbillion
4
+ Tags: user, users, profiles, switching, wpmu, multisite, buddypress, bbpress, become, user control, user management, user access, developer
5
+ Requires at least: 3.1
6
+ Tested up to: 3.9
7
+ Stable tag: 0.8.8
8
+ License: GPL v2 or later
9
+
10
+ Instant switching between user accounts in WordPress.
11
+
12
+ == Description ==
13
+
14
+ This plugin allows you to quickly swap between user accounts in WordPress at the click of a button. You'll be instantly logged out and logged in as your desired user. This is handy for test environments where you regularly log out and in between different accounts, or for adminstrators who need to switch between multiple accounts.
15
+
16
+ = Features =
17
+
18
+ * Switch user: Instantly switch to any user account from the *Users* screen.
19
+ * Switch back: Instantly switch back to your originating account.
20
+ * Switch off: Log out of your account but retain the ability to instantly switch back in again.
21
+ * It's completely secure (see the *Security* section below).
22
+ * Compatible with WordPress, WordPress Multisite, BuddyPress and bbPress.
23
+
24
+ = Security =
25
+
26
+ * Only users with the ability to edit other users can switch user accounts. By default this is only Administrators on single site installs, and Super Admins on Multisite installs.
27
+ * Passwords are not (and cannot be) revealed.
28
+ * Implements WordPress' nonce security system, meaning only those who intend to switch users can switch.
29
+ * Full support for administration over SSL (if applicable).
30
+
31
+ = Usage =
32
+
33
+ 1. Visit the *Users* menu in WordPress and you'll see a *Switch To* link next to each user.
34
+ 2. Click this and you will immediately switch into that user account.
35
+ 3. You can switch back to your originating account via the *Switch back* link on each dashboard screen or in your profile menu in the WordPress toolbar.
36
+
37
+ See the [FAQ](http://wordpress.org/plugins/user-switching/faq/) for information about the *Switch Off* feature.
38
+
39
+ = Translations Included =
40
+
41
+ * العربية / Arabic by Hassan Hisham
42
+ * 中文 / Chinese Simplified by Tunghsiao Liu
43
+ * Deutsch / German by Ralph Stenzel
44
+ * 日本語 / Japanese by Yusuke Hayasaki
45
+ * Lietuvių kalba / Lithuanian by Tommixoft
46
+ * فارسی / Persian by Amin Ab
47
+ * Polski / Polish by Bartosz Arendt
48
+ * Русский / Russian by R J
49
+ * Slovenčina / Slovak by Max Samael
50
+ * Español / Spanish by Marcelo Pedra
51
+
52
+ == Screenshots ==
53
+
54
+ 1. ![The *Switch To* link on the Users screen](https://raw.github.com/johnbillion/user-switching/master/assets-wp-repo/screenshot-1.png)
55
+ 2. ![The *Switch To* link on a user's profile](https://raw.github.com/johnbillion/user-switching/master/assets-wp-repo/screenshot-2.png)
56
+
57
+ == Installation ==
58
+
59
+ If you have the [WordPress Developer plugin](http://wordpress.org/plugins/developer/) installed then User Switching is a one-click install from the Tools -> Developer screen.
60
+
61
+ Alternatively, you can install this plugin directly from your WordPress dashboard:
62
+
63
+ 1. Go to the *Plugins* menu and click *Add New*.
64
+ 2. Search for *User Switching*.
65
+ 3. Click *Install Now* next to the *User Switching* plugin.
66
+ 4. Activate the plugin.
67
+
68
+ == Frequently Asked Questions ==
69
+
70
+ = What does "Switch off" mean? =
71
+
72
+ Switching off logs you out of your account but retains your user ID in an authorisation cookie so you can switch straight back without having to log in again manually. It's akin to switching to no user, and being able to switch back.
73
+
74
+ The *Switch Off* link can be found in your profile menu in the WordPress toolbar. Once you've switched off you'll see a *Switch back* link in the footer of your site.
75
+
76
+ = Does this plugin work with WordPress Multisite? =
77
+
78
+ Yes, and you'll also be able to switch users from the Users screen in Network Admin.
79
+
80
+ = Does this plugin work with BuddyPress? =
81
+
82
+ Yes, and you'll also be able to switch users from member profile screens and the member listing screen.
83
+
84
+ = Does this plugin work with bbPress? =
85
+
86
+ Yes, and you'll also be able to switch users from member profile screens.
87
+
88
+ = Does this work as a mu-plugin? =
89
+
90
+ Yes, but you'll need to install `user-switching.php` into the root of your `mu-plugins` directory, not in the `user-switching` subdirectory. This is a restriction of WordPress.
91
+
92
+ = What capability does a user need in order to switch accounts? =
93
+
94
+ A user needs the `edit_users` capability in order to switch user accounts. By default only Administrators have this capability, and with Multisite enabled only Super Admins have this capability.
95
+
96
+ = Can regular admins on Multisite installs switch accounts? =
97
+
98
+ No. This can be enabled though by installing the [User Switching for Regular Admins](https://github.com/johnbillion/user-switching-for-regular-admins) plugin.
99
+
100
+ = Are any plugin hooks called when users switch accounts? =
101
+
102
+ Yes. When a user switches to another account, the `switch_to_user` hook is called with the new and old user IDs passed as parameters.
103
+
104
+ When a user switches back to their original account, the `switch_back_user` hook is called with the new (original) and old user IDs passed as parameters. Note that the old user ID can be boolean false if the user is switching back after they've been switched off.
105
+
106
+ When a user switches off, the `switch_off_user` hook is called with the old user ID as a parameter.
107
 
108
  == Upgrade Notice ==
109
 
110
+ = 0.8.8 =
111
+ * Spanish Translation by Marcelo Pedra.
112
+ * User Switching is now a network-only plugin when used on Multisite.
113
 
114
  == Changelog ==
115
 
116
+ = 0.8.8 =
117
+ * Spanish Translation by Marcelo Pedra.
118
+ * User Switching is now a network-only plugin when used on Multisite.
119
+
120
  = 0.8.7 =
121
  * Respect the `secure_logged_in_cookie` and `login_redirect` filters.
122
 
user-switching.php CHANGED
@@ -2,13 +2,14 @@
2
  /*
3
  Plugin Name: User Switching
4
  Description: Instant switching between user accounts in WordPress
5
- Version: 0.8.7
6
  Plugin URI: https://johnblackbourn.com/wordpress-plugin-user-switching/
7
  Author: John Blackbourn
8
  Author URI: https://johnblackbourn.com/
9
  Text Domain: user-switching
10
  Domain Path: /languages/
11
  License: GPL v2 or later
 
12
 
13
  Copyright © 2014 John Blackbourn
14
 
@@ -59,8 +60,9 @@ class user_switching {
59
  * @return null
60
  */
61
  public function action_plugins_loaded() {
62
- if ( !defined( 'OLDUSER_COOKIE' ) )
63
  define( 'OLDUSER_COOKIE', 'wordpress_olduser_' . COOKIEHASH );
 
64
  }
65
 
66
  /**
@@ -71,8 +73,9 @@ class user_switching {
71
  */
72
  public function action_personal_options( WP_User $user ) {
73
 
74
- if ( ! $link = self::maybe_switch_url( $user->ID ) )
75
  return;
 
76
 
77
  ?>
78
  <tr>
@@ -109,8 +112,9 @@ class user_switching {
109
 
110
  load_plugin_textdomain( 'user-switching', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
111
 
112
- if ( !isset( $_REQUEST['action'] ) )
113
  return;
 
114
 
115
  switch ( $_REQUEST['action'] ) {
116
 
@@ -127,12 +131,13 @@ class user_switching {
127
  $redirect_to = self::get_redirect( $user );
128
 
129
  # Redirect to the dashboard or the home URL depending on capabilities:
130
- if ( $redirect_to )
131
  wp_safe_redirect( add_query_arg( array( 'user_switched' => 'true' ), $redirect_to ) );
132
- else if ( !current_user_can( 'read' ) )
133
  wp_redirect( add_query_arg( array( 'user_switched' => 'true' ), home_url() ) );
134
- else
135
  wp_redirect( add_query_arg( array( 'user_switched' => 'true' ), admin_url() ) );
 
136
  die();
137
 
138
  } else {
@@ -146,18 +151,20 @@ class user_switching {
146
  check_admin_referer( 'switch_to_olduser' );
147
 
148
  # Fetch the originating user data:
149
- if ( !$old_user = self::get_old_user() )
150
  wp_die( __( 'Could not switch users.', 'user-switching' ) );
 
151
 
152
  # Switch user:
153
  if ( switch_to_user( $old_user->ID, self::remember(), false ) ) {
154
 
155
  $redirect_to = self::get_redirect();
156
 
157
- if ( $redirect_to )
158
  wp_safe_redirect( add_query_arg( array( 'user_switched' => 'true', 'switched_back' => 'true' ), $redirect_to ) );
159
- else
160
  wp_redirect( add_query_arg( array( 'user_switched' => 'true', 'switched_back' => 'true' ), admin_url( 'users.php' ) ) );
 
161
  die();
162
  } else {
163
  wp_die( __( 'Could not switch users.', 'user-switching' ) );
@@ -172,10 +179,11 @@ class user_switching {
172
  # Switch off:
173
  if ( switch_off_user() ) {
174
  $redirect_to = self::get_redirect();
175
- if ( $redirect_to )
176
  wp_safe_redirect( add_query_arg( array( 'switched_off' => 'true' ), $redirect_to ) );
177
- else
178
  wp_redirect( add_query_arg( array( 'switched_off' => 'true' ), home_url() ) );
 
179
  die();
180
  } else {
181
  wp_die( __( 'Could not switch off.', 'user-switching' ) );
@@ -221,8 +229,9 @@ class user_switching {
221
  ?>
222
  <div id="user_switching" class="updated">
223
  <p><?php
224
- if ( isset( $_GET['user_switched'] ) )
225
  printf( __( 'Switched to %1$s (%2$s).', 'user-switching' ), $user->display_name, $user->user_login );
 
226
  $url = add_query_arg( array(
227
  'redirect_to' => urlencode( self::current_url() )
228
  ), self::switch_back_url() );
@@ -236,10 +245,11 @@ class user_switching {
236
  ?>
237
  <div id="user_switching" class="updated">
238
  <p><?php
239
- if ( isset( $_GET['switched_back'] ) )
240
  printf( __( 'Switched back to %1$s (%2$s).', 'user-switching' ), $user->display_name, $user->user_login );
241
- else
242
  printf( __( 'Switched to %1$s (%2$s).', 'user-switching' ), $user->display_name, $user->user_login );
 
243
  ?></p>
244
  </div>
245
  <?php
@@ -255,8 +265,9 @@ class user_switching {
255
  public static function get_old_user() {
256
  $cookie = wp_get_olduser_cookie();
257
  if ( !empty( $cookie ) ) {
258
- if ( $old_user_id = wp_validate_auth_cookie( end( $cookie ), 'old_user' ) )
259
  return get_userdata( $old_user_id );
 
260
  }
261
  return false;
262
  }
@@ -269,17 +280,20 @@ class user_switching {
269
  */
270
  public function action_admin_bar_menu( WP_Admin_Bar $wp_admin_bar ) {
271
 
272
- if ( !function_exists( 'is_admin_bar_showing' ) )
273
  return;
274
- if ( !is_admin_bar_showing() )
 
275
  return;
 
276
 
277
- if ( method_exists( $wp_admin_bar, 'get_node' ) and $wp_admin_bar->get_node( 'user-actions' ) )
278
  $parent = 'user-actions';
279
- else if ( get_option( 'show_avatars' ) )
280
  $parent = 'my-account-with-avatar';
281
- else
282
  $parent = 'my-account';
 
283
 
284
  if ( $old_user = self::get_old_user() ) {
285
 
@@ -363,8 +377,9 @@ class user_switching {
363
  */
364
  public function filter_user_row_actions( array $actions, WP_User $user ) {
365
 
366
- if ( ! $link = self::maybe_switch_url( $user->ID ) )
367
  return $actions;
 
368
 
369
  $actions['switch_to_user'] = '<a href="' . $link . '">' . __( 'Switch&nbsp;To', 'user-switching' ) . '</a>';
370
 
@@ -380,13 +395,15 @@ class user_switching {
380
 
381
  global $bp, $members_template;
382
 
383
- if ( !empty( $members_template ) and empty( $bp->displayed_user->id ) )
384
  $id = absint( $members_template->member->id );
385
- else
386
  $id = absint( $bp->displayed_user->id );
 
387
 
388
- if ( ! $link = self::maybe_switch_url( $id ) )
389
  return;
 
390
 
391
  $link = add_query_arg( array(
392
  'redirect_to' => urlencode( bp_core_get_user_domain( $id ) )
@@ -394,10 +411,11 @@ class user_switching {
394
 
395
  # Workaround for https://buddypress.trac.wordpress.org/ticket/4212
396
  $components = array_keys( $bp->active_components );
397
- if ( !empty( $components ) )
398
  $component = reset( $components );
399
- else
400
  $component = 'core';
 
401
 
402
  echo bp_get_button( array(
403
  'id' => 'user_switching',
@@ -417,8 +435,9 @@ class user_switching {
417
 
418
  $id = bbp_get_user_id();
419
 
420
- if ( ! $link = self::maybe_switch_url( $id ) )
421
  return;
 
422
 
423
  $link = add_query_arg( array(
424
  'redirect_to' => urlencode( bbp_get_user_profile_url( $id ) )
@@ -442,12 +461,13 @@ class user_switching {
442
 
443
  $old_user = self::get_old_user();
444
 
445
- if ( $old_user and ( $old_user->ID == $user_id ) )
446
  return self::switch_back_url();
447
- else if ( current_user_can( 'switch_to_user', $user_id ) )
448
  return self::switch_to_url( $user_id );
449
- else
450
  return false;
 
451
 
452
  }
453
 
@@ -528,10 +548,11 @@ class user_switching {
528
  * @return array User's capabilities
529
  */
530
  public function filter_user_has_cap( array $user_caps, array $required_caps, array $args ) {
531
- if ( 'switch_to_user' == $args[0] )
532
  $user_caps['switch_to_user'] = ( user_can( $args[1], 'edit_user', $args[2] ) and ( $args[2] != $args[1] ) );
533
- else if ( 'switch_off' == $args[0] )
534
  $user_caps['switch_off'] = user_can( $args[1], 'edit_users' );
 
535
  return $user_caps;
536
  }
537
 
@@ -548,8 +569,9 @@ class user_switching {
548
  * @return array Required capabilities for the requested action
549
  */
550
  public function filter_map_meta_cap( array $required_caps, $cap, $user_id, array $args ) {
551
- if ( ( 'switch_to_user' == $cap ) and ( $args[0] == $user_id ) )
552
  $required_caps[] = 'do_not_allow';
 
553
  return $required_caps;
554
  }
555
 
@@ -564,9 +586,9 @@ class user_switching {
564
  if ( !function_exists( 'wp_set_olduser_cookie' ) ) {
565
  function wp_set_olduser_cookie( $old_user_id ) {
566
  $expiration = time() + 172800; # 48 hours
567
- $cookie = wp_get_olduser_cookie();
568
- $cookie[] = wp_generate_auth_cookie( $old_user_id, $expiration, 'old_user' );
569
- $secure = apply_filters( 'secure_logged_in_cookie', false, $old_user_id, is_ssl() );
570
  setcookie( OLDUSER_COOKIE, json_encode( $cookie ), $expiration, COOKIEPATH, COOKIE_DOMAIN, $secure, true );
571
  }
572
  }
@@ -598,10 +620,12 @@ function wp_clear_olduser_cookie( $clear_all = true ) {
598
  */
599
  if ( !function_exists( 'wp_get_olduser_cookie' ) ) {
600
  function wp_get_olduser_cookie() {
601
- if ( isset( $_COOKIE[OLDUSER_COOKIE] ) )
602
  $cookie = json_decode( stripslashes( $_COOKIE[OLDUSER_COOKIE] ) );
603
- if ( !isset( $cookie ) or !is_array( $cookie ) )
 
604
  $cookie = array();
 
605
  return $cookie;
606
  }
607
  }
@@ -616,8 +640,9 @@ function wp_get_olduser_cookie() {
616
  */
617
  if ( !function_exists( 'switch_to_user' ) ) {
618
  function switch_to_user( $user_id, $remember = false, $set_old_user = true ) {
619
- if ( !$user = get_userdata( $user_id ) )
620
  return false;
 
621
 
622
  if ( $set_old_user and is_user_logged_in() ) {
623
  $old_user_id = get_current_user_id();
@@ -631,10 +656,11 @@ function switch_to_user( $user_id, $remember = false, $set_old_user = true ) {
631
  wp_set_auth_cookie( $user_id, $remember );
632
  wp_set_current_user( $user_id );
633
 
634
- if ( $set_old_user )
635
  do_action( 'switch_to_user', $user_id, $old_user_id );
636
- else
637
  do_action( 'switch_back_user', $user_id, $old_user_id );
 
638
 
639
  return $user;
640
  }
@@ -648,8 +674,9 @@ function switch_to_user( $user_id, $remember = false, $set_old_user = true ) {
648
  */
649
  if ( !function_exists( 'switch_off_user' ) ) {
650
  function switch_off_user() {
651
- if ( !$old_user_id = get_current_user_id() )
652
  return false;
 
653
 
654
  wp_set_olduser_cookie( $old_user_id );
655
  wp_clear_auth_cookie();
@@ -667,8 +694,9 @@ function switch_off_user() {
667
  */
668
  if ( !function_exists( 'current_user_switched' ) ) {
669
  function current_user_switched() {
670
- if ( !is_user_logged_in() )
671
  return false;
 
672
 
673
  return user_switching::get_old_user();
674
  }
2
  /*
3
  Plugin Name: User Switching
4
  Description: Instant switching between user accounts in WordPress
5
+ Version: 0.8.8
6
  Plugin URI: https://johnblackbourn.com/wordpress-plugin-user-switching/
7
  Author: John Blackbourn
8
  Author URI: https://johnblackbourn.com/
9
  Text Domain: user-switching
10
  Domain Path: /languages/
11
  License: GPL v2 or later
12
+ Network: true
13
 
14
  Copyright © 2014 John Blackbourn
15
 
60
  * @return null
61
  */
62
  public function action_plugins_loaded() {
63
+ if ( !defined( 'OLDUSER_COOKIE' ) ) {
64
  define( 'OLDUSER_COOKIE', 'wordpress_olduser_' . COOKIEHASH );
65
+ }
66
  }
67
 
68
  /**
73
  */
74
  public function action_personal_options( WP_User $user ) {
75
 
76
+ if ( ! $link = self::maybe_switch_url( $user->ID ) ) {
77
  return;
78
+ }
79
 
80
  ?>
81
  <tr>
112
 
113
  load_plugin_textdomain( 'user-switching', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
114
 
115
+ if ( !isset( $_REQUEST['action'] ) ) {
116
  return;
117
+ }
118
 
119
  switch ( $_REQUEST['action'] ) {
120
 
131
  $redirect_to = self::get_redirect( $user );
132
 
133
  # Redirect to the dashboard or the home URL depending on capabilities:
134
+ if ( $redirect_to ) {
135
  wp_safe_redirect( add_query_arg( array( 'user_switched' => 'true' ), $redirect_to ) );
136
+ } else if ( !current_user_can( 'read' ) ) {
137
  wp_redirect( add_query_arg( array( 'user_switched' => 'true' ), home_url() ) );
138
+ } else {
139
  wp_redirect( add_query_arg( array( 'user_switched' => 'true' ), admin_url() ) );
140
+ }
141
  die();
142
 
143
  } else {
151
  check_admin_referer( 'switch_to_olduser' );
152
 
153
  # Fetch the originating user data:
154
+ if ( !$old_user = self::get_old_user() ) {
155
  wp_die( __( 'Could not switch users.', 'user-switching' ) );
156
+ }
157
 
158
  # Switch user:
159
  if ( switch_to_user( $old_user->ID, self::remember(), false ) ) {
160
 
161
  $redirect_to = self::get_redirect();
162
 
163
+ if ( $redirect_to ) {
164
  wp_safe_redirect( add_query_arg( array( 'user_switched' => 'true', 'switched_back' => 'true' ), $redirect_to ) );
165
+ } else {
166
  wp_redirect( add_query_arg( array( 'user_switched' => 'true', 'switched_back' => 'true' ), admin_url( 'users.php' ) ) );
167
+ }
168
  die();
169
  } else {
170
  wp_die( __( 'Could not switch users.', 'user-switching' ) );
179
  # Switch off:
180
  if ( switch_off_user() ) {
181
  $redirect_to = self::get_redirect();
182
+ if ( $redirect_to ) {
183
  wp_safe_redirect( add_query_arg( array( 'switched_off' => 'true' ), $redirect_to ) );
184
+ } else {
185
  wp_redirect( add_query_arg( array( 'switched_off' => 'true' ), home_url() ) );
186
+ }
187
  die();
188
  } else {
189
  wp_die( __( 'Could not switch off.', 'user-switching' ) );
229
  ?>
230
  <div id="user_switching" class="updated">
231
  <p><?php
232
+ if ( isset( $_GET['user_switched'] ) ) {
233
  printf( __( 'Switched to %1$s (%2$s).', 'user-switching' ), $user->display_name, $user->user_login );
234
+ }
235
  $url = add_query_arg( array(
236
  'redirect_to' => urlencode( self::current_url() )
237
  ), self::switch_back_url() );
245
  ?>
246
  <div id="user_switching" class="updated">
247
  <p><?php
248
+ if ( isset( $_GET['switched_back'] ) ) {
249
  printf( __( 'Switched back to %1$s (%2$s).', 'user-switching' ), $user->display_name, $user->user_login );
250
+ } else {
251
  printf( __( 'Switched to %1$s (%2$s).', 'user-switching' ), $user->display_name, $user->user_login );
252
+ }
253
  ?></p>
254
  </div>
255
  <?php
265
  public static function get_old_user() {
266
  $cookie = wp_get_olduser_cookie();
267
  if ( !empty( $cookie ) ) {
268
+ if ( $old_user_id = wp_validate_auth_cookie( end( $cookie ), 'old_user' ) ) {
269
  return get_userdata( $old_user_id );
270
+ }
271
  }
272
  return false;
273
  }
280
  */
281
  public function action_admin_bar_menu( WP_Admin_Bar $wp_admin_bar ) {
282
 
283
+ if ( !function_exists( 'is_admin_bar_showing' ) ) {
284
  return;
285
+ }
286
+ if ( !is_admin_bar_showing() ) {
287
  return;
288
+ }
289
 
290
+ if ( method_exists( $wp_admin_bar, 'get_node' ) and $wp_admin_bar->get_node( 'user-actions' ) ) {
291
  $parent = 'user-actions';
292
+ } else if ( get_option( 'show_avatars' ) ) {
293
  $parent = 'my-account-with-avatar';
294
+ } else {
295
  $parent = 'my-account';
296
+ }
297
 
298
  if ( $old_user = self::get_old_user() ) {
299
 
377
  */
378
  public function filter_user_row_actions( array $actions, WP_User $user ) {
379
 
380
+ if ( ! $link = self::maybe_switch_url( $user->ID ) ) {
381
  return $actions;
382
+ }
383
 
384
  $actions['switch_to_user'] = '<a href="' . $link . '">' . __( 'Switch&nbsp;To', 'user-switching' ) . '</a>';
385
 
395
 
396
  global $bp, $members_template;
397
 
398
+ if ( !empty( $members_template ) and empty( $bp->displayed_user->id ) ) {
399
  $id = absint( $members_template->member->id );
400
+ } else {
401
  $id = absint( $bp->displayed_user->id );
402
+ }
403
 
404
+ if ( ! $link = self::maybe_switch_url( $id ) ) {
405
  return;
406
+ }
407
 
408
  $link = add_query_arg( array(
409
  'redirect_to' => urlencode( bp_core_get_user_domain( $id ) )
411
 
412
  # Workaround for https://buddypress.trac.wordpress.org/ticket/4212
413
  $components = array_keys( $bp->active_components );
414
+ if ( !empty( $components ) ) {
415
  $component = reset( $components );
416
+ } else {
417
  $component = 'core';
418
+ }
419
 
420
  echo bp_get_button( array(
421
  'id' => 'user_switching',
435
 
436
  $id = bbp_get_user_id();
437
 
438
+ if ( ! $link = self::maybe_switch_url( $id ) ) {
439
  return;
440
+ }
441
 
442
  $link = add_query_arg( array(
443
  'redirect_to' => urlencode( bbp_get_user_profile_url( $id ) )
461
 
462
  $old_user = self::get_old_user();
463
 
464
+ if ( $old_user and ( $old_user->ID == $user_id ) ) {
465
  return self::switch_back_url();
466
+ } else if ( current_user_can( 'switch_to_user', $user_id ) ) {
467
  return self::switch_to_url( $user_id );
468
+ } else {
469
  return false;
470
+ }
471
 
472
  }
473
 
548
  * @return array User's capabilities
549
  */
550
  public function filter_user_has_cap( array $user_caps, array $required_caps, array $args ) {
551
+ if ( 'switch_to_user' == $args[0] ) {
552
  $user_caps['switch_to_user'] = ( user_can( $args[1], 'edit_user', $args[2] ) and ( $args[2] != $args[1] ) );
553
+ } else if ( 'switch_off' == $args[0] ) {
554
  $user_caps['switch_off'] = user_can( $args[1], 'edit_users' );
555
+ }
556
  return $user_caps;
557
  }
558
 
569
  * @return array Required capabilities for the requested action
570
  */
571
  public function filter_map_meta_cap( array $required_caps, $cap, $user_id, array $args ) {
572
+ if ( ( 'switch_to_user' == $cap ) and ( $args[0] == $user_id ) ) {
573
  $required_caps[] = 'do_not_allow';
574
+ }
575
  return $required_caps;
576
  }
577
 
586
  if ( !function_exists( 'wp_set_olduser_cookie' ) ) {
587
  function wp_set_olduser_cookie( $old_user_id ) {
588
  $expiration = time() + 172800; # 48 hours
589
+ $cookie = wp_get_olduser_cookie();
590
+ $cookie[] = wp_generate_auth_cookie( $old_user_id, $expiration, 'old_user' );
591
+ $secure = apply_filters( 'secure_logged_in_cookie', false, $old_user_id, is_ssl() );
592
  setcookie( OLDUSER_COOKIE, json_encode( $cookie ), $expiration, COOKIEPATH, COOKIE_DOMAIN, $secure, true );
593
  }
594
  }
620
  */
621
  if ( !function_exists( 'wp_get_olduser_cookie' ) ) {
622
  function wp_get_olduser_cookie() {
623
+ if ( isset( $_COOKIE[OLDUSER_COOKIE] ) ) {
624
  $cookie = json_decode( stripslashes( $_COOKIE[OLDUSER_COOKIE] ) );
625
+ }
626
+ if ( !isset( $cookie ) or !is_array( $cookie ) ) {
627
  $cookie = array();
628
+ }
629
  return $cookie;
630
  }
631
  }
640
  */
641
  if ( !function_exists( 'switch_to_user' ) ) {
642
  function switch_to_user( $user_id, $remember = false, $set_old_user = true ) {
643
+ if ( !$user = get_userdata( $user_id ) ) {
644
  return false;
645
+ }
646
 
647
  if ( $set_old_user and is_user_logged_in() ) {
648
  $old_user_id = get_current_user_id();
656
  wp_set_auth_cookie( $user_id, $remember );
657
  wp_set_current_user( $user_id );
658
 
659
+ if ( $set_old_user ) {
660
  do_action( 'switch_to_user', $user_id, $old_user_id );
661
+ } else {
662
  do_action( 'switch_back_user', $user_id, $old_user_id );
663
+ }
664
 
665
  return $user;
666
  }
674
  */
675
  if ( !function_exists( 'switch_off_user' ) ) {
676
  function switch_off_user() {
677
+ if ( !$old_user_id = get_current_user_id() ) {
678
  return false;
679
+ }
680
 
681
  wp_set_olduser_cookie( $old_user_id );
682
  wp_clear_auth_cookie();
694
  */
695
  if ( !function_exists( 'current_user_switched' ) ) {
696
  function current_user_switched() {
697
+ if ( !is_user_logged_in() ) {
698
  return false;
699
+ }
700
 
701
  return user_switching::get_old_user();
702
  }