-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuper-admin-switch-to-admin.php
144 lines (125 loc) · 3.55 KB
/
super-admin-switch-to-admin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Super Admin Switch to Admin
*
* @package Namespace
* @author Per Soderlind
* @copyright 2021 Per Soderlind
* @license GPL-2.0+
*
* @wordpress-plugin
* Plugin Name: Super Admin Switch to Admin
* Plugin URI: https://github.com/soderlind/super-admin-switch-to-admin
* GitHub Plugin URI: https://github.com/soderlind/super-admin-switch-to-admin
* Description: If you are logged in as a super admin, allows you to switch to a regular admin account on the current site.
* Version: 1.0.3
* Author: Per Soderlind
* Author URI: https://soderlind.no
* Network: true
* Text Domain: super-admin-switch-to-admin
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
declare( strict_types = 1 );
namespace Soderlind\Plugin\SuperAdminSwitchToAdmin;
if ( ! defined( 'ABSPATH' ) ) {
wp_die();
}
/**
* Class SuperAdminSwitchToAdmin
*/
class SuperAdminSwitchToAdmin {
/**
* Admin list.
*
* @var array
*/
private $admin_list = [];
/**
* Class constructor.
*/
public function __construct() {
add_action( 'admin_init', [ $this, 'switch_to_admin' ] );
}
/**
* Switch to admin.
*
* @return void
*/
public function switch_to_admin() {
// Bail if current site is not a multisite.
if ( ! \is_multisite() ) {
return;
}
// Bail if the current user is not a super admin.
if ( ! \is_super_admin() ) {
return;
}
// Check if User Switching plugin is active.
if ( function_exists( 'current_user_switched' ) ) {
\load_plugin_textdomain( 'super-admin-switch-to-admin', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
$switched_user = \current_user_switched();
// Bail if the user has already switched to another admin.
if ( $switched_user ) {
return;
}
// List all admins for the current site, except for the super admins.
$admins = get_users(
[
'blog_id' => \get_current_blog_id(),
'role' => 'administrator',
'exclude' => $this->get_super_admins_ids(),
]
);
// Collect all the admins for the current site.
foreach ( $admins as $admin ) {
$this->admin_list[ $admin->ID ] = $admin->display_name;
}
// If there is more than one admin, show the dropdown in the admin notices.
if ( count( $this->admin_list ) ) {
add_action( 'admin_notices', [ $this, 'admin_notice' ] );
}
}
}
/**
* Admin notice.
*
* @return void
*/
public function admin_notice() {
?>
<div class="updated notice notice-success is-dismissible">
<p>
<span class="dashicons dashicons-admin-users" style="color:#56c234" aria-hidden="true"></span>
<?php esc_html_e( 'You are logged in as a super admin. You can switch to a regular admin account on this site:', 'super-admin-switch-to-admin' ); ?>
<?php
// list admins in wp notice as a comma separated list. do not add a comma after the last admin.
$last_admin = end( $this->admin_list );
foreach ( $this->admin_list as $admin_id => $admin_name ) {
printf(
'<a href="%1$s">%2$s</a>%3$s',
esc_url( \user_switching::maybe_switch_url( \get_user_by( 'id', $admin_id ) ) ),
esc_html( $admin_name ),
$admin_name === $last_admin ? '' : ', '
);
}
?>
</p>
</div>
<?php
}
/**
* Get super admins ids.
*
* @return array
*/
private function get_super_admins_ids() {
$super_admins = \get_super_admins();
$super_admins_ids = [];
foreach ( $super_admins as $super_admin ) {
$super_admins_ids[] = \get_user_by( 'login', $super_admin )->ID;
}
return $super_admins_ids;
}
}
new SuperAdminSwitchToAdmin();