-
Notifications
You must be signed in to change notification settings - Fork 3
/
hide-updates.php
122 lines (103 loc) · 3.28 KB
/
hide-updates.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
<?php
/**
* Plugin Name: Hide Updates
* Description: This plugin hides update notifications for core, plugin, and theme updates in the WordPress admin.
* Version: 1.1.6
* Author: Upperdog
* Author URI: https://upperdog.com
* Author Email: [email protected]
* License: GPLv3
*
* @package hide-updates
* @link https://github.com/upperdog/hide-updates
* @author Upperdog <[email protected]>
* @copyright Upperdog
* @license GPL-3.0-or-later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class HideUpdates {
/**
* Class constructor.
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'hide_updates_submenu_page' ) );
add_action( 'admin_init', array( $this, 'block_admin_pages' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_plugin_styles' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_plugin_styles' ) );
}
/**
* Check if current user is allowed to see updates.
*
* By default, only the user who installed the site is allowed to see updates.
* Developers can override this by utilizing the hide_updates_allowed_users
* filter which takes an array of usernames as argument.
*/
public function allow_current_user() {
$current_user = wp_get_current_user();
$default_user_id = 1;
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();
}
$allowed_users = apply_filters( 'hide_updates_allowed_users', $default_allowed_users );
return in_array( $current_user->user_login, $allowed_users );
}
/**
* Remove submenu pages for users not allowed to see WordPress updates.
*/
public function hide_updates_submenu_page() {
if ( ! $this->allow_current_user() ) {
remove_submenu_page( 'index.php', 'update-core.php' );
}
}
/**
* Block access to certain admin pages for users not allowed to see updates.
*/
public function block_admin_pages() {
if ( ! $this->allow_current_user() ) {
global $pagenow;
$blocked_admin_pages = array(
'update-core.php',
'plugins.php?plugin_status=upgrade',
);
$block_current_page = false;
foreach ( $blocked_admin_pages as $block_admin_page ) {
$block_admin_page = explode( '?', $block_admin_page );
if ( $pagenow == $block_admin_page[0] ) {
$block_current_page = true;
}
if ( isset( $block_admin_page[1] ) ) {
parse_str( $block_admin_page[1], $query_params );
foreach ( $query_params as $key => $value ) {
if ( isset( $_GET[ $key ] ) && $_GET[ $key ] == $value ) {
$block_current_page = true;
break;
} else {
$block_current_page = false;
}
}
}
if ( $block_current_page ) {
wp_safe_redirect( admin_url( '/' ) );
exit;
}
}
}
}
/**
* Enqueue plugin stylesheet.
*
* Updates are hidden using CSS to allow third-party site maintenance
* services to still access the updates.
*/
public function enqueue_plugin_styles() {
if ( is_user_logged_in() && ! $this->allow_current_user() ) {
wp_enqueue_style( 'hide_updates_css', plugins_url( 'hide-updates.css', __FILE__ ) );
}
}
}
$hide_updates = new HideUpdates();