MySLP_Admin_UserSwitching
in package
Manage user switching.
Table of Contents
Constants
- REDIRECT_TYPE_COMMENT = 'comment'
- REDIRECT_TYPE_NONE = \null
- REDIRECT_TYPE_POST = 'post'
- REDIRECT_TYPE_TERM = 'term'
- REDIRECT_TYPE_URL = 'url'
- REDIRECT_TYPE_USER = 'user'
Properties
- $application : string
- The name used to identify the application during a WordPress redirect.
Methods
- action_admin_bar_menu() : void
- Adds a 'Switch back to {user}' link to the account menu, and a `Switch To` link to the user edit menu.
- action_init() : void
- Loads localisation files and routes actions depending on the 'action' query var.
- action_personal_options() : void
- Outputs the 'Switch To' link on the user editing screen if the current user has permission to switch to them.
- action_plugins_loaded() : void
- action_shutdown_for_wp_die() : void
- Ensures that any early exits (like `wp_die`) during the switching process are handled gracefully.
- action_wp_footer() : void
- Adds a 'Switch back to {user}' link to the WordPress footer if the admin toolbar isn't showing.
- action_wp_meta() : void
- Adds a 'Switch back to {user}' link to the Meta sidebar widget.
- authenticate_old_user() : bool
- Authenticates an old user by verifying the latest entry in the auth cookie.
- current_url() : string
- Returns the current URL.
- filter_login_message() : string
- Adds a 'Switch back to {user}' link to the WordPress login screen.
- filter_map_meta_cap() : array<string|int, string>
- Filters the required primitive capabilities for the given primitive or meta capability.
- filter_removable_query_args() : array<string|int, string>
- Filters the list of query arguments which get removed from admin area URLs in WordPress.
- filter_user_has_cap() : array<string|int, bool>
- Filters a user's capabilities so they can be altered at runtime.
- filter_user_row_actions() : array<string|int, string>
- Adds a 'Switch To' link to each list of user actions on the Users screen.
- get_admin_redirect_to() : array<string, int>|null
- Returns a context-aware redirect parameter for use when switching off in the admin area.
- get_duplicated_switch() : array<string|int, mixed>|null
- Detects if the target user has any sessions that originated from another user switching into their account.
- get_instance() : MySLP_Admin_UserSwitching
- Singleton instantiator.
- get_old_user() : false|WP_User
- Validates the old user cookie and returns its user data.
- init_hooks() : void
- Sets up all the filters and actions.
- maybe_switch_url() : string|false
- Returns the switch to or switch back URL for a given user.
- remember() : bool
- Returns whether the current logged-in user is being remembered in the form of a persistent browser cookie (ie. they checked the 'Remember Me' check box when they logged in). This is used to persist the 'remember me' value when the user switches to another user.
- remove_query_args() : string
- Removes a list of common confirmation-style query args from a URL.
- secure_auth_cookie() : bool
- Returns whether or not User Switching's equivalent of the 'auth' cookie should be secure.
- secure_olduser_cookie() : bool
- Returns whether or not User Switching's equivalent of the 'logged_in' cookie should be secure.
- switch_back_url() : string
- Returns the nonce-secured URL needed to switch back to the originating user.
- switch_off_url() : string
- Returns the nonce-secured URL needed to switch off the current user.
- switch_to_url() : string
- Returns the nonce-secured URL needed to switch to a given user ID.
- get_redirect() : string
- Fetches the URL to redirect to for a given user (used after switching).
- switch_back_message() : string
- Returns the 'Switch back to {user}' link.
- switched_back_message() : string
- Returns the 'Switched back to {user}' message.
- switched_to_message() : string
- Returns the 'Switched to {user}' message.
- __construct() : mixed
- Private class constructor. Use `get_instance()` to get the instance.
Constants
REDIRECT_TYPE_COMMENT
public
mixed
REDIRECT_TYPE_COMMENT
= 'comment'
REDIRECT_TYPE_NONE
public
mixed
REDIRECT_TYPE_NONE
= \null
REDIRECT_TYPE_POST
public
mixed
REDIRECT_TYPE_POST
= 'post'
REDIRECT_TYPE_TERM
public
mixed
REDIRECT_TYPE_TERM
= 'term'
REDIRECT_TYPE_URL
public
mixed
REDIRECT_TYPE_URL
= 'url'
REDIRECT_TYPE_USER
public
mixed
REDIRECT_TYPE_USER
= 'user'
Properties
$application
The name used to identify the application during a WordPress redirect.
public
static string
$application
= 'WordPress/User Switching'
Methods
action_admin_bar_menu()
Adds a 'Switch back to {user}' link to the account menu, and a `Switch To` link to the user edit menu.
public
action_admin_bar_menu(WP_Admin_Bar $wp_admin_bar) : void
Parameters
- $wp_admin_bar : WP_Admin_Bar
-
The admin bar object.
action_init()
Loads localisation files and routes actions depending on the 'action' query var.
public
action_init() : void
action_personal_options()
Outputs the 'Switch To' link on the user editing screen if the current user has permission to switch to them.
public
action_personal_options(WP_User $user) : void
Parameters
- $user : WP_User
-
User object for this screen.
action_plugins_loaded()
public
action_plugins_loaded() : void
action_shutdown_for_wp_die()
Ensures that any early exits (like `wp_die`) during the switching process are handled gracefully.
public
action_shutdown_for_wp_die() : void
action_wp_footer()
Adds a 'Switch back to {user}' link to the WordPress footer if the admin toolbar isn't showing.
public
action_wp_footer() : void
action_wp_meta()
Adds a 'Switch back to {user}' link to the Meta sidebar widget.
public
action_wp_meta() : void
authenticate_old_user()
Authenticates an old user by verifying the latest entry in the auth cookie.
public
static authenticate_old_user(WP_User $user) : bool
Parameters
- $user : WP_User
-
A WP_User object (usually from the logged_in cookie).
Return values
bool —Whether verification with the auth cookie passed.
current_url()
Returns the current URL.
public
static current_url() : string
Return values
string —The current URL.
filter_login_message()
Adds a 'Switch back to {user}' link to the WordPress login screen.
public
filter_login_message(string $message) : string
Parameters
- $message : string
-
The login screen message.
Return values
string —The login screen message.
filter_map_meta_cap()
Filters the required primitive capabilities for the given primitive or meta capability.
public
filter_map_meta_cap(array<string|int, string> $required_caps, string $cap, int $user_id, array<string|int, mixed> $args) : array<string|int, string>
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.
Parameters
- $required_caps : array<string|int, string>
-
Array of required primitive capabilities for the requested capability.
- $cap : string
-
Capability or meta capability being checked.
- $user_id : int
-
Concerned user ID.
- $args : array<string|int, mixed>
-
{ Arguments that go with the requested capability check.
Tags
Return values
array<string|int, string> —Array of required capabilities for the requested action.
filter_removable_query_args()
Filters the list of query arguments which get removed from admin area URLs in WordPress.
public
filter_removable_query_args(array<string|int, string> $args) : array<string|int, string>
Parameters
- $args : array<string|int, string>
-
Array of removable query arguments.
Tags
Return values
array<string|int, string> —Updated array of removable query arguments.
filter_user_has_cap()
Filters a user's capabilities so they can be altered at runtime.
public
filter_user_has_cap(array<string|int, bool> $user_caps, array<string|int, string> $required_caps, array<string|int, mixed> $args, WP_User $user) : array<string|int, bool>
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.
Parameters
- $user_caps : array<string|int, bool>
-
Array of key/value pairs where keys represent a capability name and boolean values represent whether the user has that capability.
- $required_caps : array<string|int, string>
-
Array of required primitive capabilities for the requested capability.
- $args : array<string|int, mixed>
-
{ Arguments that accompany the requested capability check.
- $user : WP_User
-
Concerned user object.
Tags
Return values
array<string|int, bool> —Array of concerned user's capabilities.
filter_user_row_actions()
Adds a 'Switch To' link to each list of user actions on the Users screen.
public
filter_user_row_actions(array<string|int, string> $actions, WP_User $user) : array<string|int, string>
Parameters
- $actions : array<string|int, string>
-
Array of actions to display for this user row.
- $user : WP_User
-
The user object displayed in this row.
Return values
array<string|int, string> —Array of actions to display for this user row.
get_admin_redirect_to()
Returns a context-aware redirect parameter for use when switching off in the admin area.
public
static get_admin_redirect_to() : array<string, int>|null
This is used to redirect the user to the URL of the item they're editing at the time.
Return values
array<string, int>|nullget_duplicated_switch()
Detects if the target user has any sessions that originated from another user switching into their account.
public
static get_duplicated_switch(WP_User $target, WP_User $ignore) : array<string|int, mixed>|null
Returns information about the first such session, if any.
Parameters
- $target : WP_User
-
Target user.
- $ignore : WP_User
-
User to ignore when checking for duplicate switches.
Tags
Return values
array<string|int, mixed>|null —{ Information about the duplicate, or null if there is none.
@type int $login The login time of the session that originated from another user.
@type WP_User $user The user who switched into the target user's account.
}
get_instance()
Singleton instantiator.
public
static get_instance() : MySLP_Admin_UserSwitching
Return values
MySLP_Admin_UserSwitching —User Switching instance.
get_old_user()
Validates the old user cookie and returns its user data.
public
static get_old_user() : false|WP_User
Return values
false|WP_User —False if there's no old user cookie or it's invalid, WP_User object if it's present and valid.
init_hooks()
Sets up all the filters and actions.
public
init_hooks() : void
maybe_switch_url()
Returns the switch to or switch back URL for a given user.
public
static maybe_switch_url(WP_User $user) : string|false
Parameters
- $user : WP_User
-
The user to be switched to.
Return values
string|false —The required URL, or false if there's no old user or the user doesn't have the required capability.
remember()
Returns whether the current logged-in user is being remembered in the form of a persistent browser cookie (ie. they checked the 'Remember Me' check box when they logged in). This is used to persist the 'remember me' value when the user switches to another user.
public
static remember() : bool
Return values
bool —Whether the current user is being 'remembered' or not.
remove_query_args()
Removes a list of common confirmation-style query args from a URL.
public
static remove_query_args(string $url) : string
Parameters
- $url : string
-
A URL.
Return values
string —The URL with query args removed.
secure_auth_cookie()
Returns whether or not User Switching's equivalent of the 'auth' cookie should be secure.
public
static secure_auth_cookie() : bool
This is used to determine whether to set a secure auth cookie or not.
Return values
bool —Should the auth cookie be secure?
secure_olduser_cookie()
Returns whether or not User Switching's equivalent of the 'logged_in' cookie should be secure.
public
static secure_olduser_cookie() : bool
This is used to set the 'secure' flag on the old user cookie, for enhanced security.
Tags
Return values
bool —Should the old user cookie be secure?
switch_back_url()
Returns the nonce-secured URL needed to switch back to the originating user.
public
static switch_back_url(WP_User $user) : string
Parameters
- $user : WP_User
-
The old user.
Return values
string —The required URL.
switch_off_url()
Returns the nonce-secured URL needed to switch off the current user.
public
static switch_off_url() : string
Return values
string —The required URL.
switch_to_url()
Returns the nonce-secured URL needed to switch to a given user ID.
public
static switch_to_url(WP_User $user) : string
Parameters
- $user : WP_User
-
The user to be switched to.
Return values
string —The required URL.
get_redirect()
Fetches the URL to redirect to for a given user (used after switching).
protected
static get_redirect([WP_User|null $new_user = null ][, WP_User|null $old_user = null ]) : string
Parameters
- $new_user : WP_User|null = null
-
Optional. The new user's WP_User object.
- $old_user : WP_User|null = null
-
Optional. The old user's WP_User object.
Return values
string —The URL to redirect to.
switch_back_message()
Returns the 'Switch back to {user}' link.
protected
switch_back_message(WP_User $user) : string
Parameters
- $user : WP_User
-
The user to be switched back to.
Return values
string —The 'Switch back to {user}' link.
switched_back_message()
Returns the 'Switched back to {user}' message.
protected
switched_back_message(WP_User $user) : string
Parameters
- $user : WP_User
-
The user who has been switched back to.
Return values
string —The 'Switched back to {user}' message.
switched_to_message()
Returns the 'Switched to {user}' message.
protected
switched_to_message(WP_User $user) : string
Parameters
- $user : WP_User
-
The user who has been switched to.
Return values
string —The 'Switched to {user}' message.
__construct()
Private class constructor. Use `get_instance()` to get the instance.
private
__construct() : mixed