-
Notifications
You must be signed in to change notification settings - Fork 1
/
show-hide-updates-exclusive.php
254 lines (217 loc) · 6.58 KB
/
show-hide-updates-exclusive.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/*
Plugin Name: Show/Hide Updates Exclusive
Description: This plugin hides all update notifications for Wordpress core, plugin and theme updates in Wordpress admin for all users except users whom administrators select as updater.
Plugin URI:
Version: 1.0.0
Author: PRESSMAN
Author URI: https://www.pressman.ne.jp/
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* The Show_Hide_Updates_Exclusive class
*
* @package WordPress_Plugins
* @subpackage Show_Hide_Updates_Exclusive
* @author sekikawa(satoshi_sekikawa@pressman.ne.jp)
* @copyright Pressman inc.
*/
class Show_Hide_Updates_Exclusive {
private static $instance;
/**
* Get instance
*
* @return Show_Hide_Updates_Exclusive
*/
public static function get_instance() {
if ( !isset( self::$instance ) ) {
self::$instance = new Show_Hide_Updates_Exclusive();
}
return self::$instance;
}
/**
* Show_Hide_Updates_Exclusive constructor.
*/
private function __construct() {
add_filter( 'init', [ $this, 'is_update_user' ] );
add_action( 'show_user_profile', [ $this, 'updater_field' ] );
add_action( 'edit_user_profile', [ $this, 'updater_field' ] );
add_action( 'personal_options_update', [ $this, 'save_custom_user_profile_fields' ] );
add_action( 'edit_user_profile_update', [ $this, 'save_custom_user_profile_fields' ] );
}
/**
* Check whether updater or not
*/
public function is_update_user() {
global $current_user;
$updater = get_user_meta( $current_user->ID, 'updater', true );
if ( '1' != $updater ) {
$this->construct();
}
}
/**
* Disable all updates
*/
public function construct() {
add_action( 'admin_init', [ $this, 'admin_init' ] );
/*
* Disable Theme Updates
*/
add_filter( 'pre_site_transient_update_themes', [ $this, 'last_checked_atm' ] );
/*
* Disable Plugin Updates
*/
add_filter( 'pre_site_transient_update_plugins', [ $this, 'last_checked_atm' ] );
/*
* Disable Core Updates
*/
add_filter( 'pre_site_transient_update_core', [ $this, 'last_checked_atm' ] );
/*
* Filter schedule checks
*/
add_action( 'schedule_event', [ $this, 'filter_cron_events' ] );
/*
* Disable All Automatic Updates
*/
add_filter( 'auto_update_translation', '__return_false' );
add_filter( 'automatic_updater_disabled', '__return_true' );
add_filter( 'allow_minor_auto_core_updates', '__return_false' );
add_filter( 'allow_major_auto_core_updates', '__return_false' );
add_filter( 'allow_dev_auto_core_updates', '__return_false' );
add_filter( 'auto_update_core', '__return_false' );
add_filter( 'wp_auto_update_core', '__return_false' );
add_filter( 'auto_core_update_send_email', '__return_false' );
add_filter( 'send_core_update_notification_email', '__return_false' );
add_filter( 'auto_update_plugin', '__return_false' );
add_filter( 'auto_update_theme', '__return_false' );
add_filter( 'automatic_updates_send_debug_email', '__return_false' );
add_filter( 'automatic_updates_is_vcs_checkout', '__return_true' );
add_filter( 'automatic_updates_send_debug_email ', '__return_false', 1 );
if ( !defined( 'AUTOMATIC_UPDATER_DISABLED' ) ) {
define( 'AUTOMATIC_UPDATER_DISABLED', true );
}
if ( !defined( 'WP_AUTO_UPDATE_CORE' ) ) {
define( 'WP_AUTO_UPDATE_CORE', false );
}
add_filter( 'pre_http_request', [ $this, 'block_request' ], 10, 3 );
}
/**
* Initialize and load the plugin stuff
*/
public function admin_init() {
if ( !function_exists( "remove_action" ) ) {
return;
}
/*
* Remove 'update plugins' option from bulk operations select list
*/
global $current_user;
$current_user->allcaps['update_plugins'] = 0;
/*
* Hide maintenance and update nag
*/
remove_action( 'admin_notices', 'update_nag', 3 );
remove_action( 'network_admin_notices', 'update_nag', 3 );
remove_action( 'admin_notices', 'maintenance_nag' );
remove_action( 'network_admin_notices', 'maintenance_nag' );
/*
* Disable Theme Updates
*/
remove_action( 'load-update-core.php', 'wp_update_themes' );
wp_clear_scheduled_hook( 'wp_update_themes' );
/*
* Disable Plugin Updates
*/
remove_action( 'load-update-core.php', 'wp_update_plugins' );
wp_clear_scheduled_hook( 'wp_update_plugins' );
/*
* Disable Core Updates
*/
wp_clear_scheduled_hook( 'wp_version_check' );
remove_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
remove_action( 'admin_init', 'wp_maybe_auto_update' );
remove_action( 'admin_init', 'wp_auto_update_core' );
wp_clear_scheduled_hook( 'wp_maybe_auto_update' );
}
/**
* Check the outgoing request
*/
public function block_request( $pre, $args, $url ) {
/* Empty url */
if ( empty( $url ) ) {
return $pre;
}
/* Invalid host */
if ( !$host = parse_url( $url, PHP_URL_HOST ) ) {
return $pre;
}
$url_data = parse_url( $url );
/* block request */
if ( false !== stripos( $host, 'api.wordpress.org' ) && ( false !== stripos( $url_data['path'], 'update-check' ) || false !== stripos( $url_data['path'], 'browse-happy' ) ) ) {
return true;
}
return $pre;
}
/**
* Filter cron events
*/
public function filter_cron_events( $event ) {
switch ( $event->hook ) {
case 'wp_version_check':
case 'wp_update_plugins':
case 'wp_update_themes':
case 'wp_maybe_auto_update':
$event = false;
break;
}
return $event;
}
/**
* Override version check info
*/
public function last_checked_atm( $t ) {
include( ABSPATH . WPINC . '/version.php' );
$current = new stdClass;
$current->updates = [];
$current->version_checked = $wp_version;
$current->last_checked = time();
return $current;
}
/**
* Display the checkbox
*/
public function updater_field( $user ) {
$updater = get_user_meta( $user->ID, 'updater', true );
$user = get_userdata( $user->ID );
if ( current_user_can( 'administrator' ) && ( isset( $user->caps['administrator'] ) ) ) {
?>
<table class="form-table">
<tr>
<th>
<label for="updater"><?php _e( 'Show Update' ); ?></label>
</th>
<td>
<label><input type="checkbox" name="updater" <?php if ( $updater == '1' ){ ?> checked="checked" <?php } ?>value="1">Show</label>
</td>
</tr>
</table>
<?php
}
}
/**
* Save updater info
*/
public function save_custom_user_profile_fields( $user_id ) {
if ( !current_user_can( 'administrator', $user_id ) ) {
return;
}
update_user_meta( $user_id, 'updater', $_POST['updater'] );
}
}
if ( class_exists( 'Show_Hide_Updates_Exclusive' ) ) {
Show_Hide_Updates_Exclusive::get_instance();
}