-
Notifications
You must be signed in to change notification settings - Fork 1
/
client-admin-cleanup.php
184 lines (153 loc) · 4.51 KB
/
client-admin-cleanup.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Plugin Name: Client Admin Cleanup
* Description: This WordPress plugin hides and restricts access to some parts of the wp-admin for clients. It's like cleaning up the UI of things they don't need.
* Version: 1.1.0
* Author: Upperdog
* Author URI: https://upperdog.com
* Author Email: hello@upperdog.com
* License: GPLv2 or later
*
* @package client-admin-cleanup
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Client Admin Cleanup.
*/
class Client_Admin_Cleanup {
/**
* Construct.
*/
public function __construct() {
add_action( 'admin_init', array( $this, 'block_admin_pages' ) );
add_action( 'admin_menu', array( $this, 'remove_admin_menu_items' ) );
add_action( 'admin_bar_menu', array( $this, 'remove_admin_bar_items' ), 999 );
add_action( 'customize_register', array( $this, 'remove_customizer_sections' ), 15 );
add_action( 'wp_dashboard_setup', array( $this, 'remove_dashboard_widgets' ) );
}
/**
* Checks if current user is allowed.
*
* @return bool (true|false) If current user is allowed to see full UI or not.
*/
public function allow_current_user() {
// Get current user.
$current_user = wp_get_current_user();
// Define default main user id.
$default_user_id = 1;
// Define default allowed users.
if ( is_a( get_user_by( 'ID', $default_user_id ), 'WP_User' ) ) {
$default_user = get_user_by( 'ID', $default_user_id );
$default_allowed_users = array( $default_user->data->user_login );
} else {
$default_allowed_users = array();
}
// Apply filter to let theme/plugin authors define allowed users.
$allowed_users = apply_filters( 'client_admin_cleanup_allowed_users', $default_allowed_users );
// If current user is allowd return true, otherwise false.
if ( in_array( $current_user->user_login, $allowed_users ) ) {
return true;
} else {
return false;
}
}
/**
* Remove admin menu items.
*
* @global array $submenu WP admin submenu items.
*/
public function remove_admin_menu_items() {
global $submenu;
if ( ! $this->allow_current_user() ) {
// Dashboard -> Updates.
remove_submenu_page( 'index.php', 'update-core.php' );
// Jetpack.
remove_menu_page( 'jetpack' );
// Customize.
unset( $submenu['themes.php'][6] );
// Themes.
remove_submenu_page( 'themes.php', 'themes.php' );
// Plugins.
remove_menu_page( 'plugins.php' );
// Tools.
remove_menu_page( 'tools.php' );
// Options/settings.
remove_menu_page( 'options-general.php' );
}
}
/**
* Remove items from adminbar.
*
* @param object $wp_admin_bar WP admin bar.
*/
public function remove_admin_bar_items( $wp_admin_bar ) {
if ( ! $this->allow_current_user() ) {
$wp_admin_bar->remove_node( 'customize' );
$wp_admin_bar->remove_node( 'themes' );
}
}
/**
* Remove customizer sections.
*
* @param object $wp_customize WP customizer object.
*/
public function remove_customizer_sections( $wp_customize ) {
if ( ! $this->allow_current_user() ) {
$wp_customize->remove_section( 'custom_css' );
$wp_customize->remove_section( 'static_front_page' );
}
}
/**
* Block admin pages.
*
* @global array $pagenow WP admin current page.
*/
public function block_admin_pages() {
if ( ! $this->allow_current_user() ) {
global $pagenow;
$blocked_admin_pages = array(
'customize.php',
'plugins.php',
'plugin-install.php',
'plugin-editor.php',
'themes.php',
'update-core.php',
'tools.php',
'import.php',
'export.php',
'site-health.php',
'export-personal-data.php',
'erase-personal-data.php',
'tools.php?page=wp-migrate-db',
'tools.php?page=regenerate-thumbnails',
'tools.php?page=disable_comments_tools',
'options-general.php',
'options-writing.php',
'options-reading.php',
'options-media.php',
'options-permalink.php',
'options-privacy.php',
'options-general.php?page=disable_comments_settings',
);
if ( in_array( $pagenow, $blocked_admin_pages ) ) {
wp_safe_redirect( admin_url( '/' ) );
exit;
}
}
}
/**
* Remove dashboard widgets.
*/
public function remove_dashboard_widgets() {
if ( ! $this->allow_current_user() ) {
remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
remove_meta_box( 'dashboard_secondary', 'dashboard', 'side' );
remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_site_health', 'dashboard', 'normal' );
remove_action( 'welcome_panel', 'wp_welcome_panel' );
}
}
}
$client_admin_cleanup = new Client_Admin_Cleanup();