forked from norcross/airplane-mode
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.php
More file actions
600 lines (492 loc) · 22.7 KB
/
plugin.php
File metadata and controls
600 lines (492 loc) · 22.7 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<?php
/**
* Plugin Name: WP Core Blocker
* Plugin URI: https://github.com/devgeniem/wp-core-blocker
* Description: Disables WP from contacting wp.org servers and disables users from installing anything in wp-admin.
* Author: Onni Hakala / Geniem Oy
* Author URI: http://github.com/onnimonni
* Version: 2.0.1
* Requires WP: 4.4
* GitHub Plugin URI: https://github.com/devgeniem/wp-core-blocker
*/
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Andrew Norcross
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace geniem\helper;
use WP_Error;
use stdClass;
// Ensure the class has not already been loaded.
if ( ! class_exists( __NAMESPACE__ . '\Core_Blocker' ) ) {
/**
* Call our class.
*/
class Core_Blocker {
/**
* Activate hooks
*/
static function init() {
// Stop wp-cron from looking out for new plugin versions
add_action( 'admin_init', array( __CLASS__, 'remove_update_crons' ) );
add_action( 'admin_init', array( __CLASS__, 'remove_schedule_hook' ) );
// Disable gravatars
if ( defined( 'WP_CORE_BLOCKER_DISABLE_GRAVATAR' ) and WP_CORE_BLOCKER_DISABLE_GRAVATAR ) {
add_filter( 'get_avatar', array( __CLASS__, 'replace_gravatar' ), 1, 5 );
add_filter( 'default_avatar_select', array( __CLASS__, 'default_avatar' ) );
}
// Prevent users from even trying to update plugins and themes
add_filter( 'map_meta_cap', array( __CLASS__, 'prevent_auto_updates' ), 10, 2 );
// Remove bulk action for updating themes/plugins.
add_filter( 'bulk_actions-plugins', array( __CLASS__, 'remove_bulk_actions' ) );
add_filter( 'bulk_actions-themes', array( __CLASS__, 'remove_bulk_actions' ) );
add_filter( 'bulk_actions-plugins-network', array( __CLASS__, 'remove_bulk_actions' ) );
add_filter( 'bulk_actions-themes-network', array( __CLASS__, 'remove_bulk_actions' ) );
// Admin UI items.
add_action( 'admin_menu', array( __CLASS__, 'admin_menu_items' ), 9999 );
add_action( 'network_admin_menu', array( __CLASS__, 'ms_admin_menu_items' ), 9999 );
add_filter( 'install_plugins_tabs', array( __CLASS__, 'plugin_add_tabs' ) );
// Theme update API for different calls.
add_filter( 'themes_api_args', array( __CLASS__, 'bypass_theme_api' ), 10, 2 );
add_filter( 'themes_api', '__return_false' , 10, 2 );
// Time based transient checks.
add_filter( 'pre_site_transient_update_themes', array( __CLASS__, 'last_checked_themes' ) );
add_filter( 'pre_site_transient_update_plugins', array( __CLASS__, 'last_checked_plugins' ) );
add_filter( 'pre_site_transient_update_core', array( __CLASS__, 'last_checked_core' ) );
add_filter( 'site_transient_update_themes', array( __CLASS__, 'remove_update_array' ) );
add_filter( 'site_transient_update_plugins', array( __CLASS__, 'remove_plugin_updates' ) );
// Remove admin news dashboard widget
add_action( 'admin_init', array( __CLASS__, 'remove_dashboards' ) );
// Removes update check wp-cron
remove_action( 'init', 'wp_schedule_update_checks' );
// Disable overall core updates.
add_filter( 'auto_update_core', '__return_false' );
add_filter( 'wp_auto_update_core', '__return_false' );
// Disable automatic plugin and theme updates (used by WP to force push security fixes).
add_filter( 'auto_update_plugin', '__return_false' );
add_filter( 'auto_update_theme', '__return_false' );
// Tell WordPress we are on a version control system to add additional blocks.
add_filter( 'automatic_updates_is_vcs_checkout', '__return_true' );
// Disable translation updates.
add_filter( 'auto_update_translation', '__return_false' );
// Disable minor core updates.
add_filter( 'allow_minor_auto_core_updates', '__return_false' );
// Disable major core updates.
add_filter( 'allow_major_auto_core_updates', '__return_false' );
// Disable dev core updates.
add_filter( 'allow_dev_auto_core_updates', '__return_false' );
// Disable automatic updater updates.
add_filter( 'automatic_updater_disabled', '__return_true' );
// Run various hooks if the plugin should be enabled
if ( self::enabled() ) {
// Prevent BuddyPress from falling back to Gravatar avatars.
add_filter( 'bp_core_fetch_avatar_no_grav', '__return_true' );
// Hijack the themes api setup to bypass the API call.
add_filter( 'themes_api', '__return_true' );
// Disable debug emails (used by core for rollback alerts in automatic update deployment).
add_filter( 'automatic_updates_send_debug_email', '__return_false' );
// Disable update emails (for when we push the new WordPress versions manually) as well
// as the notification there is a new version emails.
add_filter( 'auto_core_update_send_email', '__return_false' );
add_filter( 'send_core_update_notification_email', '__return_false' );
add_filter( 'automatic_updates_send_debug_email ', '__return_false', 1 );
// Get rid of the version number in the footer.
add_filter( 'update_footer', '__return_empty_string', 11 );
// Filter out the pre core option.
add_filter( 'pre_option_update_core', '__return_null' );
// Remove some actions.
remove_action( 'admin_init', 'wp_plugin_update_rows' );
remove_action( 'admin_init', 'wp_theme_update_rows' );
remove_action( 'admin_notices', 'maintenance_nag' );
// Add back the upload tab.
add_action( 'install_themes_upload', 'install_themes_upload', 10, 0 );
// Define core contants for more protection.
if ( ! defined( 'AUTOMATIC_UPDATER_DISABLED' ) ) {
define( 'AUTOMATIC_UPDATER_DISABLED', true );
}
if ( ! defined( 'WP_AUTO_UPDATE_CORE' ) ) {
define( 'WP_AUTO_UPDATE_CORE', false );
}
}
}
/**
* Checks when plugin should be enabled This offers nice compatibilty with wp-cli
*/
static public function enabled() {
// Bail if CLI.
if ( defined( 'WP_CLI' ) and WP_CLI ) {
return false;
}
return true;
}
/**
* Remove menu items for updates from a standard WP install.
*
* @return null
*/
static public function admin_menu_items() {
// Bail if disabled, or on a multisite.
if ( ! self::enabled() || is_multisite() ) {
return;
}
// Remove our items.
remove_submenu_page( 'index.php', 'update-core.php' );
}
/**
* Remove WordPress news dashboard widget
*/
static function remove_dashboards() {
remove_meta_box( 'dashboard_primary', 'dashboard', 'normal' );
}
/**
* Remove menu items for updates from a multisite instance.
*
* @return null
*/
static public function ms_admin_menu_items() {
// Bail if disabled or not on our network admin.
if ( ! self::enabled() || ! is_network_admin() ) {
return;
}
// Remove the items.
remove_submenu_page( 'index.php', 'upgrade.php' );
}
/**
* Replace all instances of gravatar with a local image file
* to remove the call to remote service.
*
* @param string $avatar Image tag for the user's avatar.
* @param int|object|string $id_or_email A user ID, email address, or comment object.
* @param int $size Square avatar width and height in pixels to retrieve.
* @param string $default URL to a default image to use if no avatar is available.
* @param string $alt Alternative text to use in the avatar image tag.
*
* @return string `<img>` tag for the user's avatar.
*/
static public function replace_gravatar( $avatar, $id_or_email, $size, $default, $alt ) {
// Bail if disabled.
if ( ! self::enabled() ) {
return $avatar;
}
// Swap out the file for a base64 encoded image.
$image = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
$avatar = "<img alt='{$alt}' src='{$image}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' style='background:#eee;' />";
// Return the avatar.
return $avatar;
}
/**
* Remove avatar images from the default avatar list
*
* @param string $avatar_list List of default avatars.
*
* @return string Updated list with images removed
*/
static public function default_avatar( $avatar_list ) {
// Bail if disabled.
if ( ! self::enabled() ) {
return $avatar_list;
}
// Remove images.
$avatar_list = preg_replace( '|<img([^>]+)> |i', '', $avatar_list );
// Send back the list.
return $avatar_list;
}
/**
* Fetch the URL to redirect to after toggling Airplane Mode.
*
* @return string The URL to redirect to.
*/
protected static function get_redirect() {
// Return the args for the actual redirect.
$redirect = remove_query_arg( array(
'user_switched',
'switched_off',
'switched_back',
'message',
'update',
'updated',
'settings-updated',
'saved',
'activated',
'activate',
'deactivate',
'enabled',
'disabled',
'locked',
'skipped',
'deleted',
'trashed',
'untrashed',
) );
// Redirect away from the update core page.
$redirect = str_replace( 'update-core.php', '', $redirect );
// And return the redirect.
return apply_filters( 'core_blocker_redirect_url', $redirect );
}
/**
* Filter a user's meta capabilities to prevent auto-updates from being attempted.
*
* @param array $caps Returns the user's actual capabilities.
* @param string $cap Capability name.
*
* @return array The user's filtered capabilities.
*/
static public function prevent_auto_updates( $caps, $cap ) {
// Check for being enabled and look for specific cap requirements.
if ( self::enabled() && in_array( $cap, array( 'install_plugins', 'install_themes', 'update_plugins', 'update_themes', 'update_core' ) ) ) {
$caps[] = 'do_not_allow';
}
// Send back the data array.
return $caps;
}
/**
* Remove all the various places WP does the update checks. As you can see there are a lot of them.
*
* @return null
*/
static public function remove_update_crons() {
// Bail if disabled.
if ( ! self::enabled() ) {
return;
}
// Do a quick check to make sure we can remove things.
if ( ! function_exists( 'remove_action' ) ) {
return;
}
// Disable Theme Updates.
remove_action( 'load-update-core.php', 'wp_update_themes' );
remove_action( 'load-themes.php', 'wp_update_themes' );
remove_action( 'load-update.php', 'wp_update_themes' );
remove_action( 'wp_update_themes', 'wp_update_themes' );
remove_action( 'admin_init', '_maybe_update_themes' );
// Disable Plugin Updates.
remove_action( 'load-update-core.php', 'wp_update_plugins' );
remove_action( 'load-plugins.php', 'wp_update_plugins' );
remove_action( 'load-update.php', 'wp_update_plugins' );
remove_action( 'wp_update_plugins', 'wp_update_plugins' );
remove_action( 'admin_init', '_maybe_update_plugins' );
// Disable Core updates
add_action( 'init', function(){ remove_action('init','wp_version_check'); }, 2 );
// Don't look for WordPress updates. Seriously!
remove_action( 'wp_version_check', 'wp_version_check' );
remove_action( 'admin_init', '_maybe_update_core' );
// Not even maybe.
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' );
}
/**
* Remove all the various schedule hooks for themes, plugins, etc.
*
* @return null
*/
static public function remove_schedule_hook() {
// Bail if disabled.
if ( ! self::enabled() ) {
return;
}
wp_clear_scheduled_hook( 'wp_update_themes' );
wp_clear_scheduled_hook( 'wp_update_plugins' );
wp_clear_scheduled_hook( 'wp_version_check' );
wp_clear_scheduled_hook( 'wp_maybe_auto_update' );
}
/**
* Hijack the themes api setup to bypass the API call.
*
* @param object $args Arguments used to query for installer pages from the Themes API.
* @param string $action Requested action. Likely values are 'theme_information',
* 'feature_list', or 'query_themes'.
*
* @return bool true or false depending on the type of query
*/
static public function bypass_theme_api( $args, $action ) {
// Bail if disabled.
if ( ! self::enabled() ) {
return $args;
}
// Return false on feature list to avoid the API call.
return ! empty( $action ) && 'feature_list' === $action ? false : $args;
}
/**
* Always send back that the latest version of WordPress is the one we're running
*
* @return object the modified output with our information
*/
static public function last_checked_core() {
// Bail if disabled.
if ( ! self::enabled() ) {
return false;
}
// Call the global WP version.
global $wp_version;
// Return our object.
return (object) array(
'last_checked' => time(),
'updates' => array(),
'version_checked' => $wp_version,
);
}
/**
* Always send back that the latest version of our theme is the one we're running
*
* @return object the modified output with our information
*/
static public function last_checked_themes() {
// Bail if disabled.
if ( ! self::enabled() ) {
return false;
}
// Call the global WP version.
global $wp_version;
// Set a blank data array.
$data = array();
// Build my theme data array.
foreach ( wp_get_themes() as $theme ) {
$data[ $theme->get_stylesheet() ] = $theme->get( 'Version' );
}
// Return our object.
return (object) array(
'last_checked' => time(),
'updates' => array(),
'version_checked' => $wp_version,
'checked' => $data,
);
}
/**
* Always send back that the latest version of our plugins are the one we're running
*
* @return object the modified output with our information
*/
static public function last_checked_plugins() {
// Bail if disabled.
if ( ! self::enabled() ) {
return false;
}
// Call the global WP version.
global $wp_version;
// Set a blank data array.
$data = array();
// Add our plugin file if we don't have it.
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// Build my plugin data array.
foreach ( get_plugins() as $file => $pl ) {
$data[ $file ] = $pl['Version'];
}
// Return our object.
return (object) array(
'last_checked' => time(),
'updates' => array(),
'version_checked' => $wp_version,
'checked' => $data,
);
}
/**
* Return an empty array of items requiring update for both themes and plugins
*
* @param array $items All the items being passed for update.
*
* @return array An empty array, or the original items if not enabled.
*/
static public function remove_update_array( $items ) {
return ! self::enabled() ? $items : array();
}
/**
* Returns list of plugins which tells that there's no updates
*
* @param array $current Empty array
*
* @return array Lookalike data which is stored in site transient 'update_plugins'
*/
static public function remove_plugin_updates( $current ) {
if ( ! $current ) {
$current = new stdClass;
$current->last_checked = time();
$current->translations = array();
$plugins = get_plugins();
foreach ( $plugins as $file => $p ) {
$current->checked[ $file ] = strval($p['Version']);
}
$current->response = array();
}
return $current;
}
/**
* Remove the ability to update plugins/themes from single
* site and multisite bulk actions
*
* @param array $actions All the bulk actions.
*
* @return array $actions The remaining actions
*/
static public function remove_bulk_actions( $actions ) {
// Bail if disabled.
if ( ! self::enabled() ) {
return $actions;
}
// Set an array of items to be removed with optional filter.
if ( false === $remove = apply_filters( 'core_blocker_bulk_items', array( 'update-selected', 'update', 'upgrade' ) ) ) {
return $actions;
}
// Loop the item array and unset each.
foreach ( $remove as $key ) {
unset( $actions[ $key ] );
}
// Return the remaining.
return $actions;
}
/**
* Remove the tabs on the plugin page to add new items
* since they require the WP connection and will fail.
*
* @param array $nonmenu_tabs All the tabs displayed.
* @return array $nonmenu_tabs The remaining tabs.
*/
static public function plugin_add_tabs( $nonmenu_tabs ) {
// Bail if disabled.
if ( ! self::enabled() ) {
return $nonmenu_tabs;
}
// Set an array of tabs to be removed with optional filter.
if ( false === $remove = apply_filters( 'core_blocker_bulk_items', array( 'featured', 'popular', 'recommended', 'favorites', 'beta' ) ) ) {
return $nonmenu_tabs;
}
// Loop the item array and unset each.
foreach ( $remove as $key ) {
unset( $nonmenu_tabs[ $key ] );
}
// Return the tabs.
return $nonmenu_tabs;
}
// End class.
}
} //end class_exists
if ( ! class_exists( __NAMESPACE__ . '\CoreBlocker_WP_Error' ) ) {
class Core_Blocker_WP_Error extends WP_Error {
public function __tostring() {
$data = $this->get_error_data();
return $data['return'];
}
}
}
// Instantiate our plugin.
Core_Blocker::init();