User Switching - Version 1.7.0

Version Description

Download this release

Release Info

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

Code changes from version 1.6.0 to 1.7.0

Files changed (3) hide show
  1. .editorconfig +0 -16
  2. readme.md +10 -7
  3. user-switching.php +203 -76
.editorconfig DELETED
@@ -1,16 +0,0 @@
1
- root = true
2
-
3
- [*]
4
- charset = utf-8
5
- end_of_line = lf
6
- insert_final_newline = true
7
- indent_style = tab
8
- indent_size = 4
9
- trim_trailing_whitespace = true
10
-
11
- [*.md]
12
- trim_trailing_whitespace = false
13
-
14
- [*.yml]
15
- indent_style = space
16
- indent_size = 2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
readme.md CHANGED
@@ -1,6 +1,6 @@
1
  # User Switching
2
 
3
- Stable tag: 1.6.0
4
  Requires at least: 3.7
5
  Tested up to: 6.0
6
  Requires PHP: 5.3
@@ -275,6 +275,15 @@ In addition, User Switching respects the following filters from WordPress core w
275
 
276
  ## Changelog ##
277
 
 
 
 
 
 
 
 
 
 
278
  ### 1.6.0 ###
279
 
280
  * Add a 'Switch To' link to the order screen in WooCommerce
@@ -451,9 +460,3 @@ In addition, User Switching respects the following filters from WordPress core w
451
  - Indonesian translation by Eko Ikhyar.
452
  - Portuguese translation by Raphael Mendonça.
453
 
454
-
455
- ### 0.8.8 ###
456
-
457
- - Spanish Translation by Marcelo Pedra.
458
- - User Switching is now a network-only plugin when used on Multisite.
459
-
1
  # User Switching
2
 
3
+ Stable tag: 1.7.0
4
  Requires at least: 3.7
5
  Tested up to: 6.0
6
  Requires PHP: 5.3
275
 
276
  ## Changelog ##
277
 
278
+ ### 1.7.0 ###
279
+
280
+ * Redirect to the current post, term, user, or comment being edited when switching off
281
+ * Clean up some user-facing messages
282
+ * Apply basic styling to the Switch Back link that appears in the footer
283
+ * Use a better placement for the Switch To menu on bbPress profiles
284
+ * Use a more appropriate HTTP response code if switching off fails
285
+ * Exclude `.editorconfig` from dist ZIP
286
+
287
  ### 1.6.0 ###
288
 
289
  * Add a 'Switch To' link to the order screen in WooCommerce
460
  - Indonesian translation by Eko Ikhyar.
461
  - Portuguese translation by Raphael Mendonça.
462
 
 
 
 
 
 
 
user-switching.php CHANGED
@@ -10,7 +10,7 @@
10
  *
11
  * Plugin Name: User Switching
12
  * Description: Instant switching between user accounts in WordPress
13
- * Version: 1.6.0
14
  * Plugin URI: https://wordpress.org/plugins/user-switching/
15
  * Author: John Blackbourn & contributors
16
  * Author URI: https://github.com/johnbillion/user-switching/graphs/contributors
@@ -46,6 +46,13 @@ class user_switching {
46
  */
47
  public static $application = 'WordPress/User Switching';
48
 
 
 
 
 
 
 
 
49
  /**
50
  * Sets up all the filters and actions.
51
  *
@@ -73,7 +80,7 @@ class user_switching {
73
  add_action( 'admin_bar_menu', array( $this, 'action_admin_bar_menu' ), 11 );
74
  add_action( 'bp_member_header_actions', array( $this, 'action_bp_button' ), 11 );
75
  add_action( 'bp_directory_members_actions', array( $this, 'action_bp_button' ), 11 );
76
- add_action( 'bbp_template_after_user_details', array( $this, 'action_bbpress_button' ) );
77
  add_action( 'woocommerce_login_form_start', array( $this, 'action_woocommerce_login_form_start' ), 10, 0 );
78
  add_action( 'woocommerce_admin_order_data_after_order_details', array( $this, 'action_woocommerce_order_details' ), 1 );
79
  add_filter( 'woocommerce_account_menu_items', array( $this, 'filter_woocommerce_account_menu_items' ), 999 );
@@ -254,7 +261,7 @@ class user_switching {
254
  // Check authentication:
255
  if ( ! $current_user || ! current_user_can( 'switch_off' ) ) {
256
  /* Translators: "switch off" means to temporarily log out */
257
- wp_die( esc_html__( 'Could not switch off.', 'user-switching' ) );
258
  }
259
 
260
  // Check intent:
@@ -275,7 +282,7 @@ class user_switching {
275
  exit;
276
  } else {
277
  /* Translators: "switch off" means to temporarily log out */
278
- wp_die( esc_html__( 'Could not switch off.', 'user-switching' ) );
279
  }
280
  break;
281
 
@@ -290,12 +297,77 @@ class user_switching {
290
  * @return string The URL to redirect to.
291
  */
292
  protected static function get_redirect( WP_User $new_user = null, WP_User $old_user = null ) {
 
 
 
 
293
  if ( ! empty( $_REQUEST['redirect_to'] ) ) {
 
294
  $redirect_to = self::remove_query_args( wp_unslash( $_REQUEST['redirect_to'] ) );
295
  $requested_redirect_to = wp_unslash( $_REQUEST['redirect_to'] );
296
- } else {
297
- $redirect_to = '';
298
- $requested_redirect_to = '';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299
  }
300
 
301
  if ( ! $new_user ) {
@@ -306,7 +378,17 @@ class user_switching {
306
  $redirect_to = apply_filters( 'login_redirect', $redirect_to, $requested_redirect_to, $new_user );
307
  }
308
 
309
- return $redirect_to;
 
 
 
 
 
 
 
 
 
 
310
  }
311
 
312
  /**
@@ -345,12 +427,7 @@ class user_switching {
345
  $message = '';
346
  $just_switched = isset( $_GET['user_switched'] );
347
  if ( $just_switched ) {
348
- $message = esc_html( sprintf(
349
- /* Translators: 1: user display name; 2: username; */
350
- __( 'Switched to %1$s (%2$s).', 'user-switching' ),
351
- $user->display_name,
352
- $user->user_login
353
- ) );
354
  }
355
  $switch_back_url = add_query_arg( array(
356
  'redirect_to' => urlencode( self::current_url() ),
@@ -359,12 +436,7 @@ class user_switching {
359
  $message .= sprintf(
360
  ' <a href="%s">%s</a>.',
361
  esc_url( $switch_back_url ),
362
- esc_html( sprintf(
363
- /* Translators: 1: user display name; 2: username; */
364
- __( 'Switch back to %1$s (%2$s)', 'user-switching' ),
365
- $old_user->display_name,
366
- $old_user->user_login
367
- ) )
368
  );
369
 
370
  /**
@@ -398,19 +470,9 @@ class user_switching {
398
  <p>
399
  <?php
400
  if ( isset( $_GET['switched_back'] ) ) {
401
- echo esc_html( sprintf(
402
- /* Translators: 1: user display name; 2: username; */
403
- __( 'Switched back to %1$s (%2$s).', 'user-switching' ),
404
- $user->display_name,
405
- $user->user_login
406
- ) );
407
  } else {
408
- echo esc_html( sprintf(
409
- /* Translators: 1: user display name; 2: username; */
410
- __( 'Switched to %1$s (%2$s).', 'user-switching' ),
411
- $user->display_name,
412
- $user->user_login
413
- ) );
414
  }
415
  ?>
416
  </p>
@@ -483,12 +545,7 @@ class user_switching {
483
  $wp_admin_bar->add_node( array(
484
  'parent' => $parent,
485
  'id' => 'switch-back',
486
- 'title' => esc_html( sprintf(
487
- /* Translators: 1: user display name; 2: username; */
488
- __( 'Switch back to %1$s (%2$s)', 'user-switching' ),
489
- $old_user->display_name,
490
- $old_user->user_login
491
- ) ),
492
  'href' => add_query_arg( array(
493
  'redirect_to' => urlencode( self::current_url() ),
494
  ), self::switch_back_url( $old_user ) ),
@@ -497,10 +554,12 @@ class user_switching {
497
 
498
  if ( current_user_can( 'switch_off' ) ) {
499
  $url = self::switch_off_url( wp_get_current_user() );
500
- if ( ! is_admin() ) {
501
- $url = add_query_arg( array(
502
- 'redirect_to' => urlencode( self::current_url() ),
503
- ), $url );
 
 
504
  }
505
 
506
  $wp_admin_bar->add_node( array(
@@ -517,12 +576,7 @@ class user_switching {
517
  $wp_admin_bar->add_node( array(
518
  'parent' => 'edit',
519
  'id' => 'author-switch-back',
520
- 'title' => esc_html( sprintf(
521
- /* Translators: 1: user display name; 2: username; */
522
- __( 'Switch back to %1$s (%2$s)', 'user-switching' ),
523
- $old_user->display_name,
524
- $old_user->user_login
525
- ) ),
526
  'href' => add_query_arg( array(
527
  'redirect_to' => urlencode( self::current_url() ),
528
  ), self::switch_back_url( $old_user ) ),
@@ -540,6 +594,39 @@ class user_switching {
540
  }
541
  }
542
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
543
  /**
544
  * Adds a 'Switch back to {user}' link to the Meta sidebar widget.
545
  *
@@ -549,19 +636,13 @@ class user_switching {
549
  $old_user = self::get_old_user();
550
 
551
  if ( $old_user instanceof WP_User ) {
552
- $link = sprintf(
553
- /* Translators: 1: user display name; 2: username; */
554
- __( 'Switch back to %1$s (%2$s)', 'user-switching' ),
555
- $old_user->display_name,
556
- $old_user->user_login
557
- );
558
  $url = add_query_arg( array(
559
  'redirect_to' => urlencode( self::current_url() ),
560
  ), self::switch_back_url( $old_user ) );
561
  printf(
562
  '<li id="user_switching_switch_on"><a href="%s">%s</a></li>',
563
  esc_url( $url ),
564
- esc_html( $link )
565
  );
566
  }
567
  }
@@ -590,19 +671,13 @@ class user_switching {
590
  $old_user = self::get_old_user();
591
 
592
  if ( $old_user instanceof WP_User ) {
593
- $link = sprintf(
594
- /* Translators: 1: user display name; 2: username; */
595
- __( 'Switch back to %1$s (%2$s)', 'user-switching' ),
596
- $old_user->display_name,
597
- $old_user->user_login
598
- );
599
  $url = add_query_arg( array(
600
  'redirect_to' => urlencode( self::current_url() ),
601
  ), self::switch_back_url( $old_user ) );
602
  printf(
603
- '<p id="user_switching_switch_on"><a href="%s">%s</a></p>',
604
  esc_url( $url ),
605
- esc_html( $link )
606
  );
607
  }
608
  }
@@ -617,12 +692,6 @@ class user_switching {
617
  $old_user = self::get_old_user();
618
 
619
  if ( $old_user instanceof WP_User ) {
620
- $link = sprintf(
621
- /* Translators: 1: user display name; 2: username; */
622
- __( 'Switch back to %1$s (%2$s)', 'user-switching' ),
623
- $old_user->display_name,
624
- $old_user->user_login
625
- );
626
  $url = self::switch_back_url( $old_user );
627
 
628
  if ( ! empty( $_REQUEST['interim-login'] ) ) {
@@ -640,7 +709,7 @@ class user_switching {
640
  $message .= sprintf(
641
  '<a href="%1$s" onclick="window.location.href=\'%1$s\';return false;">%2$s</a>',
642
  esc_url( $url ),
643
- esc_html( $link )
644
  );
645
  $message .= '</p>';
646
  }
@@ -836,6 +905,69 @@ class user_switching {
836
  ), wp_login_url() ), "switch_off_{$user->ID}" );
837
  }
838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
839
  /**
840
  * Returns the current URL.
841
  *
@@ -930,12 +1062,7 @@ class user_switching {
930
  return $items;
931
  }
932
 
933
- $items['user-switching-switch-back'] = sprintf(
934
- /* Translators: 1: user display name; 2: username; */
935
- __( 'Switch back to %1$s (%2$s)', 'user-switching' ),
936
- $old_user->display_name,
937
- $old_user->user_login
938
- );
939
 
940
  return $items;
941
  }
10
  *
11
  * Plugin Name: User Switching
12
  * Description: Instant switching between user accounts in WordPress
13
+ * Version: 1.7.0
14
  * Plugin URI: https://wordpress.org/plugins/user-switching/
15
  * Author: John Blackbourn & contributors
16
  * Author URI: https://github.com/johnbillion/user-switching/graphs/contributors
46
  */
47
  public static $application = 'WordPress/User Switching';
48
 
49
+ const REDIRECT_TYPE_NONE = null;
50
+ const REDIRECT_TYPE_URL = 'url';
51
+ const REDIRECT_TYPE_POST = 'post';
52
+ const REDIRECT_TYPE_TERM = 'term';
53
+ const REDIRECT_TYPE_USER = 'user';
54
+ const REDIRECT_TYPE_COMMENT = 'comment';
55
+
56
  /**
57
  * Sets up all the filters and actions.
58
  *
80
  add_action( 'admin_bar_menu', array( $this, 'action_admin_bar_menu' ), 11 );
81
  add_action( 'bp_member_header_actions', array( $this, 'action_bp_button' ), 11 );
82
  add_action( 'bp_directory_members_actions', array( $this, 'action_bp_button' ), 11 );
83
+ add_action( 'bbp_template_after_user_details_menu_items', array( $this, 'action_bbpress_button' ) );
84
  add_action( 'woocommerce_login_form_start', array( $this, 'action_woocommerce_login_form_start' ), 10, 0 );
85
  add_action( 'woocommerce_admin_order_data_after_order_details', array( $this, 'action_woocommerce_order_details' ), 1 );
86
  add_filter( 'woocommerce_account_menu_items', array( $this, 'filter_woocommerce_account_menu_items' ), 999 );
261
  // Check authentication:
262
  if ( ! $current_user || ! current_user_can( 'switch_off' ) ) {
263
  /* Translators: "switch off" means to temporarily log out */
264
+ wp_die( esc_html__( 'Could not switch off.', 'user-switching' ), 403 );
265
  }
266
 
267
  // Check intent:
282
  exit;
283
  } else {
284
  /* Translators: "switch off" means to temporarily log out */
285
+ wp_die( esc_html__( 'Could not switch off.', 'user-switching' ), 403 );
286
  }
287
  break;
288
 
297
  * @return string The URL to redirect to.
298
  */
299
  protected static function get_redirect( WP_User $new_user = null, WP_User $old_user = null ) {
300
+ $redirect_to = '';
301
+ $requested_redirect_to = '';
302
+ $redirect_type = self::REDIRECT_TYPE_NONE;
303
+
304
  if ( ! empty( $_REQUEST['redirect_to'] ) ) {
305
+ // URL
306
  $redirect_to = self::remove_query_args( wp_unslash( $_REQUEST['redirect_to'] ) );
307
  $requested_redirect_to = wp_unslash( $_REQUEST['redirect_to'] );
308
+ $redirect_type = self::REDIRECT_TYPE_URL;
309
+ } elseif ( ! empty( $_GET['redirect_to_post'] ) ) {
310
+ // Post
311
+ $post_id = absint( $_GET['redirect_to_post'] );
312
+
313
+ if ( function_exists( 'is_post_publicly_viewable' ) && is_post_publicly_viewable( $post_id ) ) {
314
+ $link = get_permalink( $post_id );
315
+
316
+ if ( is_string( $link ) ) {
317
+ $redirect_to = $link;
318
+ $requested_redirect_to = $link;
319
+ $redirect_type = self::REDIRECT_TYPE_POST;
320
+ }
321
+ }
322
+ } elseif ( ! empty( $_GET['redirect_to_term'] ) ) {
323
+ // Term
324
+ $term = get_term( absint( $_GET['redirect_to_term'] ) );
325
+
326
+ if ( ( $term instanceof WP_Term ) && function_exists( 'is_taxonomy_viewable' ) && is_taxonomy_viewable( $term->taxonomy ) ) {
327
+ $link = get_term_link( $term );
328
+
329
+ if ( is_string( $link ) ) {
330
+ $redirect_to = $link;
331
+ $requested_redirect_to = $link;
332
+ $redirect_type = self::REDIRECT_TYPE_TERM;
333
+ }
334
+ }
335
+ } elseif ( ! empty( $_GET['redirect_to_user'] ) ) {
336
+ // User
337
+ $user = get_userdata( absint( $_GET['redirect_to_user'] ) );
338
+
339
+ if ( $user instanceof WP_User ) {
340
+ $link = get_author_posts_url( $user->ID );
341
+
342
+ if ( is_string( $link ) ) {
343
+ $redirect_to = $link;
344
+ $requested_redirect_to = $link;
345
+ $redirect_type = self::REDIRECT_TYPE_USER;
346
+ }
347
+ }
348
+ } elseif ( ! empty( $_GET['redirect_to_comment'] ) ) {
349
+ // Comment
350
+ $comment = get_comment( absint( $_GET['redirect_to_comment'] ) );
351
+
352
+ if ( $comment instanceof WP_Comment ) {
353
+ if ( 'approved' === wp_get_comment_status( $comment ) ) {
354
+ $link = get_comment_link( $comment );
355
+
356
+ if ( is_string( $link ) ) {
357
+ $redirect_to = $link;
358
+ $requested_redirect_to = $link;
359
+ $redirect_type = self::REDIRECT_TYPE_COMMENT;
360
+ }
361
+ } elseif ( function_exists( 'is_post_publicly_viewable' ) && is_post_publicly_viewable( (int) $comment->comment_post_ID ) ) {
362
+ $link = get_permalink( (int) $comment->comment_post_ID );
363
+
364
+ if ( is_string( $link ) ) {
365
+ $redirect_to = $link;
366
+ $requested_redirect_to = $link;
367
+ $redirect_type = self::REDIRECT_TYPE_POST;
368
+ }
369
+ }
370
+ }
371
  }
372
 
373
  if ( ! $new_user ) {
378
  $redirect_to = apply_filters( 'login_redirect', $redirect_to, $requested_redirect_to, $new_user );
379
  }
380
 
381
+ /**
382
+ * Filters the redirect location after a user switches to another account or switches off.
383
+ *
384
+ * @since 1.7.0
385
+ *
386
+ * @param string $redirect_to The target redirect location, or an empty string if none is specified.
387
+ * @param string|null $redirect_type The redirect type, see the `user_switching::REDIRECT_*` constants.
388
+ * @param WP_User|null $new_user The user being switched to, or null if there is none.
389
+ * @param WP_User|null $old_user The user being switched from, or null if there is none.
390
+ */
391
+ return apply_filters( 'user_switching_redirect_to', $redirect_to, $redirect_type, $new_user, $old_user );
392
  }
393
 
394
  /**
427
  $message = '';
428
  $just_switched = isset( $_GET['user_switched'] );
429
  if ( $just_switched ) {
430
+ $message = esc_html( self::switched_to_message( $user ) );
 
 
 
 
 
431
  }
432
  $switch_back_url = add_query_arg( array(
433
  'redirect_to' => urlencode( self::current_url() ),
436
  $message .= sprintf(
437
  ' <a href="%s">%s</a>.',
438
  esc_url( $switch_back_url ),
439
+ esc_html( self::switch_back_message( $old_user ) )
 
 
 
 
 
440
  );
441
 
442
  /**
470
  <p>
471
  <?php
472
  if ( isset( $_GET['switched_back'] ) ) {
473
+ echo esc_html( self::switched_back_message( $user ) );
 
 
 
 
 
474
  } else {
475
+ echo esc_html( self::switched_to_message( $user ) );
 
 
 
 
 
476
  }
477
  ?>
478
  </p>
545
  $wp_admin_bar->add_node( array(
546
  'parent' => $parent,
547
  'id' => 'switch-back',
548
+ 'title' => esc_html( self::switch_back_message( $old_user ) ),
 
 
 
 
 
549
  'href' => add_query_arg( array(
550
  'redirect_to' => urlencode( self::current_url() ),
551
  ), self::switch_back_url( $old_user ) ),
554
 
555
  if ( current_user_can( 'switch_off' ) ) {
556
  $url = self::switch_off_url( wp_get_current_user() );
557
+ $redirect_to = is_admin() ? self::get_admin_redirect_to() : array(
558
+ 'redirect_to' => urlencode( self::current_url() ),
559
+ );
560
+
561
+ if ( is_array( $redirect_to ) ) {
562
+ $url = add_query_arg( $redirect_to, $url );
563
  }
564
 
565
  $wp_admin_bar->add_node( array(
576
  $wp_admin_bar->add_node( array(
577
  'parent' => 'edit',
578
  'id' => 'author-switch-back',
579
+ 'title' => esc_html( self::switch_back_message( $old_user ) ),
 
 
 
 
 
580
  'href' => add_query_arg( array(
581
  'redirect_to' => urlencode( self::current_url() ),
582
  ), self::switch_back_url( $old_user ) ),
594
  }
595
  }
596
 
597
+ /**
598
+ * Returns a context-aware redirect parameter for use when switching off in the admin area.
599
+ *
600
+ * This is used to redirect the user to the URL of the item they're editing at the time.
601
+ *
602
+ * @return ?array<string, int>
603
+ */
604
+ public static function get_admin_redirect_to() {
605
+ if ( ! empty( $_GET['post'] ) ) {
606
+ // Post
607
+ return array(
608
+ 'redirect_to_post' => intval( $_GET['post'] ),
609
+ );
610
+ } elseif ( ! empty( $_GET['tag_ID'] ) ) {
611
+ // Term
612
+ return array(
613
+ 'redirect_to_term' => intval( $_GET['tag_ID'] ),
614
+ );
615
+ } elseif ( ! empty( $_GET['user_id'] ) ) {
616
+ // User
617
+ return array(
618
+ 'redirect_to_user' => intval( $_GET['user_id'] ),
619
+ );
620
+ } elseif ( ! empty( $_GET['c'] ) ) {
621
+ // Comment
622
+ return array(
623
+ 'redirect_to_comment' => intval( $_GET['c'] ),
624
+ );
625
+ }
626
+
627
+ return null;
628
+ }
629
+
630
  /**
631
  * Adds a 'Switch back to {user}' link to the Meta sidebar widget.
632
  *
636
  $old_user = self::get_old_user();
637
 
638
  if ( $old_user instanceof WP_User ) {
 
 
 
 
 
 
639
  $url = add_query_arg( array(
640
  'redirect_to' => urlencode( self::current_url() ),
641
  ), self::switch_back_url( $old_user ) );
642
  printf(
643
  '<li id="user_switching_switch_on"><a href="%s">%s</a></li>',
644
  esc_url( $url ),
645
+ esc_html( self::switch_back_message( $old_user ) )
646
  );
647
  }
648
  }
671
  $old_user = self::get_old_user();
672
 
673
  if ( $old_user instanceof WP_User ) {
 
 
 
 
 
 
674
  $url = add_query_arg( array(
675
  'redirect_to' => urlencode( self::current_url() ),
676
  ), self::switch_back_url( $old_user ) );
677
  printf(
678
+ '<p id="user_switching_switch_on" style="position:fixed;bottom:40px;padding:0;margin:0;left:10px;font-size:13px;z-index:99999;"><a href="%s">%s</a></p>',
679
  esc_url( $url ),
680
+ esc_html( self::switch_back_message( $old_user ) )
681
  );
682
  }
683
  }
692
  $old_user = self::get_old_user();
693
 
694
  if ( $old_user instanceof WP_User ) {
 
 
 
 
 
 
695
  $url = self::switch_back_url( $old_user );
696
 
697
  if ( ! empty( $_REQUEST['interim-login'] ) ) {
709
  $message .= sprintf(
710
  '<a href="%1$s" onclick="window.location.href=\'%1$s\';return false;">%2$s</a>',
711
  esc_url( $url ),
712
+ esc_html( self::switch_back_message( $old_user ) )
713
  );
714
  $message .= '</p>';
715
  }
905
  ), wp_login_url() ), "switch_off_{$user->ID}" );
906
  }
907
 
908
+ /**
909
+ * Returns the message shown to the user when they've switched to a user.
910
+ *
911
+ * @param WP_User $user The concerned user.
912
+ * @return string The message.
913
+ */
914
+ public static function switched_to_message( WP_User $user ) {
915
+ $message = sprintf(
916
+ /* Translators: 1: user display name; 2: username; */
917
+ __( 'Switched to %1$s (%2$s).', 'user-switching' ),
918
+ $user->display_name,
919
+ $user->user_login
920
+ );
921
+
922
+ // Removes the user login from this message without invalidating existing translations
923
+ return str_replace( sprintf(
924
+ ' (%s)',
925
+ $user->user_login
926
+ ), '', $message );
927
+ }
928
+
929
+ /**
930
+ * Returns the message shown to the user for the link to switch back to their original user.
931
+ *
932
+ * @param WP_User $user The concerned user.
933
+ * @return string The message.
934
+ */
935
+ public static function switch_back_message( WP_User $user ) {
936
+ $message = sprintf(
937
+ /* Translators: 1: user display name; 2: username; */
938
+ __( 'Switch back to %1$s (%2$s)', 'user-switching' ),
939
+ $user->display_name,
940
+ $user->user_login
941
+ );
942
+
943
+ // Removes the user login from this message without invalidating existing translations
944
+ return str_replace( sprintf(
945
+ ' (%s)',
946
+ $user->user_login
947
+ ), '', $message );
948
+ }
949
+
950
+ /**
951
+ * Returns the message shown to the user when they've switched back to their original user.
952
+ *
953
+ * @param WP_User $user The concerned user.
954
+ * @return string The message.
955
+ */
956
+ public static function switched_back_message( WP_User $user ) {
957
+ $message = sprintf(
958
+ /* Translators: 1: user display name; 2: username; */
959
+ __( 'Switched back to %1$s (%2$s).', 'user-switching' ),
960
+ $user->display_name,
961
+ $user->user_login
962
+ );
963
+
964
+ // Removes the user login from this message without invalidating existing translations
965
+ return str_replace( sprintf(
966
+ ' (%s)',
967
+ $user->user_login
968
+ ), '', $message );
969
+ }
970
+
971
  /**
972
  * Returns the current URL.
973
  *
1062
  return $items;
1063
  }
1064
 
1065
+ $items['user-switching-switch-back'] = self::switch_back_message( $old_user );
 
 
 
 
 
1066
 
1067
  return $items;
1068
  }