',
- esc_attr( $lang_attr )
- );
- } else {
- echo '
';
- }
+ if ( $lang_attr ) {
+ printf(
+ '
',
+ esc_attr( $lang_attr )
+ );
+ } else {
+ echo '
';
+ }
?>
display_name,
- $user->user_login
- ) );
- }
- $switch_back_url = add_query_arg( array(
- 'redirect_to' => urlencode( self::current_url() ),
- ), self::switch_back_url( $old_user ) );
-
- $message .= sprintf(
- ' %s.',
- esc_url( $switch_back_url ),
- esc_html( sprintf(
- /* Translators: 1: user display name; 2: username; */
- __( 'Switch back to %1$s (%2$s)', 'user-switching' ),
- $old_user->display_name,
- $old_user->user_login
- ) )
- );
-
- /**
- * Filters the contents of the message that's displayed to switched users in the admin area.
- *
- * @since 1.1.0
- *
- * @param string $message The message displayed to the switched user.
- * @param WP_User $user The current user object.
- * @param WP_User $old_user The old user object.
- * @param string $switch_back_url The switch back URL.
- * @param bool $just_switched Whether the user made the switch on this page request.
- */
- $message = apply_filters( 'user_switching_switched_message', $message, $user, $old_user, $switch_back_url, $just_switched );
-
- echo wp_kses( $message, array(
- 'a' => array(
- 'href' => array(),
- ),
- ) );
+ $message = '';
+ $just_switched = isset( $_GET['user_switched'] );
+ if ( $just_switched ) {
+ $message = esc_html( self::switched_to_message( $user ) );
+ }
+ $switch_back_url = add_query_arg( array(
+ 'redirect_to' => rawurlencode( self::current_url() ),
+ ), self::switch_back_url( $old_user ) );
+
+ $message .= sprintf(
+ ' %s.',
+ esc_url( $switch_back_url ),
+ esc_html( self::switch_back_message( $old_user ) )
+ );
+
+ /**
+ * Filters the contents of the message that's displayed to switched users in the admin area.
+ *
+ * @since 1.1.0
+ *
+ * @param string $message The message displayed to the switched user.
+ * @param WP_User $user The current user object.
+ * @param WP_User $old_user The old user object.
+ * @param string $switch_back_url The switch back URL.
+ * @param bool $just_switched Whether the user made the switch on this page request.
+ */
+ $message = apply_filters( 'user_switching_switched_message', $message, $user, $old_user, $switch_back_url, $just_switched );
+
+ echo wp_kses( $message, array(
+ 'a' => array(
+ 'href' => array(),
+ ),
+ ) );
?>
@@ -379,24 +459,14 @@ public function action_admin_notices() {
}
} elseif ( isset( $_GET['user_switched'] ) ) {
?>
-
+
display_name,
- $user->user_login
- ) );
- } else {
- echo esc_html( sprintf(
- /* Translators: 1: user display name; 2: username; */
- __( 'Switched to %1$s (%2$s).', 'user-switching' ),
- $user->display_name,
- $user->user_login
- ) );
- }
+ if ( isset( $_GET['switched_back'] ) ) {
+ echo esc_html( self::switched_back_message( $user ) );
+ } else {
+ echo esc_html( self::switched_to_message( $user ) );
+ }
?>
@@ -456,16 +526,10 @@ public function action_admin_bar_menu( WP_Admin_Bar $wp_admin_bar ) {
return;
}
- if ( method_exists( $wp_admin_bar, 'get_node' ) ) {
- if ( $wp_admin_bar->get_node( 'user-actions' ) ) {
- $parent = 'user-actions';
- } else {
- return;
- }
- } elseif ( get_option( 'show_avatars' ) ) {
- $parent = 'my-account-with-avatar';
+ if ( $wp_admin_bar->get_node( 'user-actions' ) ) {
+ $parent = 'user-actions';
} else {
- $parent = 'my-account';
+ return;
}
$old_user = self::get_old_user();
@@ -473,33 +537,30 @@ public function action_admin_bar_menu( WP_Admin_Bar $wp_admin_bar ) {
if ( $old_user ) {
$wp_admin_bar->add_node( array(
'parent' => $parent,
- 'id' => 'switch-back',
- 'title' => esc_html( sprintf(
- /* Translators: 1: user display name; 2: username; */
- __( 'Switch back to %1$s (%2$s)', 'user-switching' ),
- $old_user->display_name,
- $old_user->user_login
- ) ),
- 'href' => add_query_arg( array(
- 'redirect_to' => urlencode( self::current_url() ),
+ 'id' => 'switch-back',
+ 'title' => esc_html( self::switch_back_message( $old_user ) ),
+ 'href' => add_query_arg( array(
+ 'redirect_to' => rawurlencode( self::current_url() ),
), self::switch_back_url( $old_user ) ),
) );
}
if ( current_user_can( 'switch_off' ) ) {
$url = self::switch_off_url( wp_get_current_user() );
- if ( ! is_admin() ) {
- $url = add_query_arg( array(
- 'redirect_to' => urlencode( self::current_url() ),
- ), $url );
+ $redirect_to = is_admin() ? self::get_admin_redirect_to() : array(
+ 'redirect_to' => rawurlencode( self::current_url() ),
+ );
+
+ if ( is_array( $redirect_to ) ) {
+ $url = add_query_arg( $redirect_to, $url );
}
$wp_admin_bar->add_node( array(
'parent' => $parent,
- 'id' => 'switch-off',
+ 'id' => 'switch-off',
/* Translators: "switch off" means to temporarily log out */
- 'title' => esc_html__( 'Switch Off', 'user-switching' ),
- 'href' => $url,
+ 'title' => esc_html__( 'Switch Off', 'user-switching' ),
+ 'href' => $url,
) );
}
@@ -507,30 +568,58 @@ public function action_admin_bar_menu( WP_Admin_Bar $wp_admin_bar ) {
if ( $old_user ) {
$wp_admin_bar->add_node( array(
'parent' => 'edit',
- 'id' => 'author-switch-back',
- 'title' => esc_html( sprintf(
- /* Translators: 1: user display name; 2: username; */
- __( 'Switch back to %1$s (%2$s)', 'user-switching' ),
- $old_user->display_name,
- $old_user->user_login
- ) ),
- 'href' => add_query_arg( array(
- 'redirect_to' => urlencode( self::current_url() ),
+ 'id' => 'author-switch-back',
+ 'title' => esc_html( self::switch_back_message( $old_user ) ),
+ 'href' => add_query_arg( array(
+ 'redirect_to' => rawurlencode( self::current_url() ),
), self::switch_back_url( $old_user ) ),
) );
} elseif ( current_user_can( 'switch_to_user', get_queried_object_id() ) ) {
$wp_admin_bar->add_node( array(
'parent' => 'edit',
- 'id' => 'author-switch-to',
- 'title' => esc_html__( 'Switch To', 'user-switching' ),
- 'href' => add_query_arg( array(
- 'redirect_to' => urlencode( self::current_url() ),
+ 'id' => 'author-switch-to',
+ 'title' => esc_html__( 'Switch To', 'user-switching' ),
+ 'href' => add_query_arg( array(
+ 'redirect_to' => rawurlencode( self::current_url() ),
), self::switch_to_url( get_queried_object() ) ),
) );
}
}
}
+ /**
+ * Returns a context-aware redirect parameter for use when switching off in the admin area.
+ *
+ * This is used to redirect the user to the URL of the item they're editing at the time.
+ *
+ * @return ?array
+ */
+ public static function get_admin_redirect_to() {
+ if ( ! empty( $_GET['post'] ) ) {
+ // Post
+ return array(
+ 'redirect_to_post' => intval( $_GET['post'] ),
+ );
+ } elseif ( ! empty( $_GET['tag_ID'] ) ) {
+ // Term
+ return array(
+ 'redirect_to_term' => intval( $_GET['tag_ID'] ),
+ );
+ } elseif ( ! empty( $_GET['user_id'] ) ) {
+ // User
+ return array(
+ 'redirect_to_user' => intval( $_GET['user_id'] ),
+ );
+ } elseif ( ! empty( $_GET['c'] ) ) {
+ // Comment
+ return array(
+ 'redirect_to_comment' => intval( $_GET['c'] ),
+ );
+ }
+
+ return null;
+ }
+
/**
* Adds a 'Switch back to {user}' link to the Meta sidebar widget.
*
@@ -540,19 +629,13 @@ public function action_wp_meta() {
$old_user = self::get_old_user();
if ( $old_user instanceof WP_User ) {
- $link = sprintf(
- /* Translators: 1: user display name; 2: username; */
- __( '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( self::current_url() ),
+ 'redirect_to' => rawurlencode( self::current_url() ),
), self::switch_back_url( $old_user ) );
printf(
'%s',
esc_url( $url ),
- esc_html( $link )
+ esc_html( self::switch_back_message( $old_user ) )
);
}
}
@@ -581,19 +664,13 @@ public function action_wp_footer() {
$old_user = self::get_old_user();
if ( $old_user instanceof WP_User ) {
- $link = sprintf(
- /* Translators: 1: user display name; 2: username; */
- __( '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( self::current_url() ),
+ 'redirect_to' => rawurlencode( self::current_url() ),
), self::switch_back_url( $old_user ) );
printf(
- '%s
',
+ '%s
',
esc_url( $url ),
- esc_html( $link )
+ esc_html( self::switch_back_message( $old_user ) )
);
}
}
@@ -608,12 +685,6 @@ public function filter_login_message( $message ) {
$old_user = self::get_old_user();
if ( $old_user instanceof WP_User ) {
- $link = sprintf(
- /* Translators: 1: user display name; 2: username; */
- __( 'Switch back to %1$s (%2$s)', 'user-switching' ),
- $old_user->display_name,
- $old_user->user_login
- );
$url = self::switch_back_url( $old_user );
if ( ! empty( $_REQUEST['interim-login'] ) ) {
@@ -622,7 +693,7 @@ public function filter_login_message( $message ) {
), $url );
} elseif ( ! empty( $_REQUEST['redirect_to'] ) ) {
$url = add_query_arg( array(
- 'redirect_to' => urlencode( wp_unslash( $_REQUEST['redirect_to'] ) ),
+ 'redirect_to' => rawurlencode( wp_unslash( $_REQUEST['redirect_to'] ) ),
), $url );
}
@@ -631,7 +702,7 @@ public function filter_login_message( $message ) {
$message .= sprintf(
'%2$s',
esc_url( $url ),
- esc_html( $link )
+ esc_html( self::switch_back_message( $old_user ) )
);
$message .= '';
}
@@ -642,9 +713,9 @@ public function filter_login_message( $message ) {
/**
* Adds a 'Switch To' link to each list of user actions on the Users screen.
*
- * @param string[] $actions Array of actions to display for this user row.
- * @param WP_User $user The user object displayed in this row.
- * @return string[] Array of actions to display for this user row.
+ * @param array $actions Array of actions to display for this user row.
+ * @param WP_User $user The user object displayed in this row.
+ * @return array Array of actions to display for this user row.
*/
public function filter_user_row_actions( array $actions, WP_User $user ) {
$link = self::maybe_switch_url( $user );
@@ -687,16 +758,16 @@ public function action_bp_button() {
}
$link = add_query_arg( array(
- 'redirect_to' => urlencode( bp_core_get_user_domain( $user->ID ) ),
+ 'redirect_to' => rawurlencode( bp_core_get_user_domain( $user->ID ) ),
), $link );
$components = array_keys( buddypress()->active_components );
echo bp_get_button( array(
- 'id' => 'user_switching',
- 'component' => reset( $components ),
- 'link_href' => esc_url( $link ),
- 'link_text' => esc_html__( 'Switch To', 'user-switching' ),
+ 'id' => 'user_switching',
+ 'component' => reset( $components ),
+ 'link_href' => esc_url( $link ),
+ 'link_text' => esc_html__( 'Switch To', 'user-switching' ),
'wrapper_id' => 'user_switching_switch_to',
) );
}
@@ -720,7 +791,7 @@ public function action_bbpress_button() {
}
$link = add_query_arg( array(
- 'redirect_to' => urlencode( bbp_get_user_profile_url( $user->ID ) ),
+ 'redirect_to' => rawurlencode( bbp_get_user_profile_url( $user->ID ) ),
), $link );
echo '';
@@ -732,13 +803,34 @@ public function action_bbpress_button() {
echo '
';
}
+ /**
+ * Filters the array of row meta for each plugin in the Plugins list table.
+ *
+ * @param array $plugin_meta An array of the plugin row's meta data.
+ * @param string $plugin_file Path to the plugin file relative to the plugins directory.
+ * @return array An array of the plugin row's meta data.
+ */
+ public function filter_plugin_row_meta( array $plugin_meta, $plugin_file ) {
+ if ( 'user-switching/user-switching.php' !== $plugin_file ) {
+ return $plugin_meta;
+ }
+
+ $plugin_meta[] = sprintf(
+ '%2$s',
+ 'https://github.com/sponsors/johnbillion',
+ esc_html_x( 'Sponsor', 'verb', 'user-switching' )
+ );
+
+ return $plugin_meta;
+ }
+
/**
* Filters the list of query arguments which get removed from admin area URLs in WordPress.
*
* @link https://core.trac.wordpress.org/ticket/23367
*
- * @param string[] $args Array of removable query arguments.
- * @return string[] Updated array of removable query arguments.
+ * @param array $args Array of removable query arguments.
+ * @return array Updated array of removable query arguments.
*/
public function filter_removable_query_args( array $args ) {
return array_merge( $args, array(
@@ -774,9 +866,9 @@ public static function maybe_switch_url( WP_User $user ) {
*/
public static function switch_to_url( WP_User $user ) {
return wp_nonce_url( add_query_arg( array(
- 'action' => 'switch_to_user',
+ 'action' => 'switch_to_user',
'user_id' => $user->ID,
- 'nr' => 1,
+ 'nr' => 1,
), wp_login_url() ), "switch_to_user_{$user->ID}" );
}
@@ -789,7 +881,7 @@ public static function switch_to_url( WP_User $user ) {
public static function switch_back_url( WP_User $user ) {
return wp_nonce_url( add_query_arg( array(
'action' => 'switch_to_olduser',
- 'nr' => 1,
+ 'nr' => 1,
), wp_login_url() ), "switch_to_olduser_{$user->ID}" );
}
@@ -802,10 +894,73 @@ public static function switch_back_url( WP_User $user ) {
public static function switch_off_url( WP_User $user ) {
return wp_nonce_url( add_query_arg( array(
'action' => 'switch_off',
- 'nr' => 1,
+ 'nr' => 1,
), wp_login_url() ), "switch_off_{$user->ID}" );
}
+ /**
+ * Returns the message shown to the user when they've switched to a user.
+ *
+ * @param WP_User $user The concerned user.
+ * @return string The message.
+ */
+ public static function switched_to_message( WP_User $user ) {
+ $message = sprintf(
+ /* Translators: 1: user display name; 2: username; */
+ __( 'Switched to %1$s (%2$s).', 'user-switching' ),
+ $user->display_name,
+ $user->user_login
+ );
+
+ // Removes the user login from this message without invalidating existing translations
+ return str_replace( sprintf(
+ ' (%s)',
+ $user->user_login
+ ), '', $message );
+ }
+
+ /**
+ * Returns the message shown to the user for the link to switch back to their original user.
+ *
+ * @param WP_User $user The concerned user.
+ * @return string The message.
+ */
+ public static function switch_back_message( WP_User $user ) {
+ $message = sprintf(
+ /* Translators: 1: user display name; 2: username; */
+ __( 'Switch back to %1$s (%2$s)', 'user-switching' ),
+ $user->display_name,
+ $user->user_login
+ );
+
+ // Removes the user login from this message without invalidating existing translations
+ return str_replace( sprintf(
+ ' (%s)',
+ $user->user_login
+ ), '', $message );
+ }
+
+ /**
+ * Returns the message shown to the user when they've switched back to their original user.
+ *
+ * @param WP_User $user The concerned user.
+ * @return string The message.
+ */
+ public static function switched_back_message( WP_User $user ) {
+ $message = sprintf(
+ /* Translators: 1: user display name; 2: username; */
+ __( 'Switched back to %1$s (%2$s).', 'user-switching' ),
+ $user->display_name,
+ $user->user_login
+ );
+
+ // Removes the user login from this message without invalidating existing translations
+ return str_replace( sprintf(
+ ' (%s)',
+ $user->user_login
+ ), '', $message );
+ }
+
/**
* Returns the current URL.
*
@@ -818,15 +973,11 @@ public static function current_url() {
/**
* Removes a list of common confirmation-style query args from a URL.
*
- * @param string $url A URL.
+ * @param string $url A URL.
* @return string The URL with query args removed.
*/
public static function remove_query_args( $url ) {
- if ( function_exists( 'wp_removable_query_args' ) ) {
- $url = remove_query_arg( wp_removable_query_args(), $url );
- }
-
- return $url;
+ return remove_query_arg( wp_removable_query_args(), $url );
}
/**
@@ -839,7 +990,7 @@ public static function remove_query_args( $url ) {
* @return bool Should the old user cookie be secure?
*/
public static function secure_olduser_cookie() {
- return ( is_ssl() && ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) ) );
+ return ( is_ssl() && ( 'https' === wp_parse_url( home_url(), PHP_URL_SCHEME ) ) );
}
/**
@@ -850,7 +1001,80 @@ public static function secure_olduser_cookie() {
* @return bool Whether the auth cookie should be secure.
*/
public static function secure_auth_cookie() {
- return ( is_ssl() && ( 'https' === parse_url( wp_login_url(), PHP_URL_SCHEME ) ) );
+ return ( is_ssl() && ( 'https' === wp_parse_url( wp_login_url(), PHP_URL_SCHEME ) ) );
+ }
+
+ /**
+ * Adds a 'Switch back to {user}' link to the WooCommerce login screen.
+ *
+ * @return void
+ */
+ public function action_woocommerce_login_form_start() {
+ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
+ echo $this->filter_login_message( '' );
+ }
+
+ /**
+ * Adds a 'Switch To' link to the WooCommerce order screen.
+ *
+ * @param WC_Order $order The WooCommerce order object.
+ * @return void
+ */
+ public function action_woocommerce_order_details( WC_Order $order ) {
+ $user = $order->get_user();
+
+ if ( ! $user || ! current_user_can( 'switch_to_user', $user->ID ) ) {
+ return;
+ }
+
+ $url = add_query_arg( array(
+ 'redirect_to' => rawurlencode( $order->get_view_order_url() ),
+ ), self::switch_to_url( $user ) );
+
+ printf(
+ '%2$s
',
+ esc_url( $url ),
+ esc_html__( 'Switch To', 'user-switching' )
+ );
+ }
+
+ /**
+ * Adds a 'Switch back to {user}' link to the My Account screen in WooCommerce.
+ *
+ * @param array $items Menu items.
+ * @return array Menu items.
+ */
+ public function filter_woocommerce_account_menu_items( array $items ) {
+ $old_user = self::get_old_user();
+
+ if ( ! $old_user ) {
+ return $items;
+ }
+
+ $items['user-switching-switch-back'] = self::switch_back_message( $old_user );
+
+ return $items;
+ }
+
+ /**
+ * Sets the URL of the 'Switch back to {user}' link in the My Account screen in WooCommerce.
+ *
+ * @param string $url The URL for the menu item.
+ * @param string $endpoint The endpoint slug for the menu item.
+ * @return string The URL for the menu item.
+ */
+ public function filter_woocommerce_get_endpoint_url( $url, $endpoint ) {
+ if ( 'user-switching-switch-back' !== $endpoint ) {
+ return $url;
+ }
+
+ $old_user = self::get_old_user();
+
+ if ( ! $old_user ) {
+ return $url;
+ }
+
+ return self::switch_back_url( $old_user );
}
/**
@@ -880,24 +1104,25 @@ public function forget_woocommerce_session() {
* Filters a user's capabilities so they can be altered at runtime.
*
* This is used to:
+ *
* - Grant the 'switch_to_user' capability to the user if they have the ability to edit the user they're trying to
* switch to (and that user is not themselves).
* - Grant the 'switch_off' capability to the user if they can edit other users.
*
* Important: This does not get called for Super Admins. See filter_map_meta_cap() below.
*
- * @param bool[] $user_caps Array of key/value pairs where keys represent a capability name and boolean values
- * represent whether the user has that capability.
- * @param string[] $required_caps Array of required primitive capabilities for the requested capability.
- * @param mixed[] $args {
+ * @param array $user_caps Array of key/value pairs where keys represent a capability name and boolean values
+ * represent whether the user has that capability.
+ * @param array $required_caps Array of required primitive capabilities for the requested capability.
+ * @param array $args {
* Arguments that accompany the requested capability check.
*
* @type string $0 Requested capability.
* @type int $1 Concerned user ID.
* @type mixed ...$2 Optional second and further parameters.
* }
- * @param WP_User $user Concerned user object.
- * @return bool[] Array of concerned user's capabilities.
+ * @param WP_User $user Concerned user object.
+ * @return array Array of concerned user's capabilities.
*/
public function filter_user_has_cap( array $user_caps, array $required_caps, array $args, WP_User $user ) {
if ( 'switch_to_user' === $args[0] ) {
@@ -927,20 +1152,21 @@ public function filter_user_has_cap( array $user_caps, array $required_caps, arr
* Filters the required primitive capabilities for the given primitive or meta capability.
*
* This is used to:
+ *
* - Add the 'do_not_allow' capability to the list of required capabilities when a Super Admin is trying to switch
* to themselves.
*
* It affects nothing else as Super Admins can do everything by default.
*
- * @param string[] $required_caps Array of required primitive capabilities for the requested capability.
- * @param string $cap Capability or meta capability being checked.
- * @param int $user_id Concerned user ID.
- * @param mixed[] $args {
+ * @param array $required_caps Array of required primitive capabilities for the requested capability.
+ * @param string $cap Capability or meta capability being checked.
+ * @param int $user_id Concerned user ID.
+ * @param array $args {
* Arguments that accompany the requested capability check.
*
* @type mixed ...$0 Optional second and further parameters.
* }
- * @return string[] Array of required capabilities for the requested action.
+ * @return array Array of required capabilities for the requested action.
*/
public function filter_map_meta_cap( array $required_caps, $cap, $user_id, array $args ) {
if ( 'switch_to_user' === $cap ) {
@@ -969,8 +1195,7 @@ public static function get_instance() {
/**
* Private class constructor. Use `get_instance()` to get the instance.
*/
- final private function __construct() {}
-
+ private function __construct() {}
}
if ( ! function_exists( 'user_switching_set_olduser_cookie' ) ) {
@@ -985,18 +1210,18 @@ final private function __construct() {}
* @return void
*/
function user_switching_set_olduser_cookie( $old_user_id, $pop = false, $token = '' ) {
- $secure_auth_cookie = user_switching::secure_auth_cookie();
+ $secure_auth_cookie = user_switching::secure_auth_cookie();
$secure_olduser_cookie = user_switching::secure_olduser_cookie();
- $expiration = time() + 172800; // 48 hours
- $auth_cookie = user_switching_get_auth_cookie();
- $olduser_cookie = wp_generate_auth_cookie( $old_user_id, $expiration, 'logged_in', $token );
+ $expiration = time() + 172800; // 48 hours
+ $auth_cookie = user_switching_get_auth_cookie();
+ $olduser_cookie = wp_generate_auth_cookie( $old_user_id, $expiration, 'logged_in', $token );
if ( $secure_auth_cookie ) {
$auth_cookie_name = USER_SWITCHING_SECURE_COOKIE;
- $scheme = 'secure_auth';
+ $scheme = 'secure_auth';
} else {
$auth_cookie_name = USER_SWITCHING_COOKIE;
- $scheme = 'auth';
+ $scheme = 'auth';
}
if ( $pop ) {
@@ -1005,7 +1230,7 @@ function user_switching_set_olduser_cookie( $old_user_id, $pop = false, $token =
array_push( $auth_cookie, wp_generate_auth_cookie( $old_user_id, $expiration, $scheme, $token ) );
}
- $auth_cookie = json_encode( $auth_cookie );
+ $auth_cookie = wp_json_encode( $auth_cookie );
if ( false === $auth_cookie ) {
return;
@@ -1124,7 +1349,7 @@ function user_switching_get_olduser_cookie() {
/**
* Gets the value of the auth cookie containing the list of originating users.
*
- * @return string[] Array of originating user authentication cookie values. Empty array if there are none.
+ * @return array Array of originating user authentication cookie values. Empty array if there are none.
*/
function user_switching_get_auth_cookie() {
if ( user_switching::secure_auth_cookie() ) {
@@ -1159,10 +1384,10 @@ function switch_to_user( $user_id, $remember = false, $set_old_user = true ) {
return false;
}
- $old_user_id = ( is_user_logged_in() ) ? get_current_user_id() : false;
- $old_token = function_exists( 'wp_get_session_token' ) ? wp_get_session_token() : '';
+ $old_user_id = ( is_user_logged_in() ) ? get_current_user_id() : false;
+ $old_token = wp_get_session_token();
$auth_cookies = user_switching_get_auth_cookie();
- $auth_cookie = end( $auth_cookies );
+ $auth_cookie = end( $auth_cookies );
$cookie_parts = $auth_cookie ? wp_parse_auth_cookie( $auth_cookie ) : false;
if ( $set_old_user && $old_user_id ) {
@@ -1171,24 +1396,23 @@ function switch_to_user( $user_id, $remember = false, $set_old_user = true ) {
user_switching_set_olduser_cookie( $old_user_id, false, $old_token );
} else {
// Switching back, either after being switched off or after being switched to another user
- $new_token = ( $cookie_parts && isset( $cookie_parts['token'] ) ) ? $cookie_parts['token'] : '';
+ $new_token = $cookie_parts['token'] ?? '';
user_switching_clear_olduser_cookie( false );
}
/**
* Attaches the original user ID and session token to the new session when a user switches to another user.
*
- * @param array $session Array of extra data.
- * @param int $user_id User ID.
- * @return array Array of extra data.
+ * @param array $session Array of extra data.
+ * @return array Array of extra data.
*/
- $session_filter = function( array $session, $user_id ) use ( $old_user_id, $old_token ) {
- $session['switched_from_id'] = $old_user_id;
+ $session_filter = function ( array $session ) use ( $old_user_id, $old_token ) {
+ $session['switched_from_id'] = $old_user_id;
$session['switched_from_session'] = $old_token;
return $session;
};
- add_filter( 'attach_session_information', $session_filter, 99, 2 );
+ add_filter( 'attach_session_information', $session_filter, 99 );
wp_clear_auth_cookie();
wp_set_auth_cookie( $user_id, $remember, '', $new_token );
@@ -1196,7 +1420,7 @@ function switch_to_user( $user_id, $remember = false, $set_old_user = true ) {
remove_filter( 'attach_session_information', $session_filter, 99 );
- if ( $set_old_user ) {
+ if ( $set_old_user && $old_user_id ) {
/**
* Fires when a user switches to another user account.
*
@@ -1251,7 +1475,7 @@ function switch_off_user() {
return false;
}
- $old_token = function_exists( 'wp_get_session_token' ) ? wp_get_session_token() : '';
+ $old_token = wp_get_session_token();
user_switching_set_olduser_cookie( $old_user_id, false, $old_token );
wp_clear_auth_cookie();