-
Notifications
You must be signed in to change notification settings - Fork 66
/
airplane-mode.php
1369 lines (1136 loc) · 51.6 KB
/
airplane-mode.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
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: Airplane Mode
* Plugin URI: https://github.com/norcross/airplane-mode
* Description: Control loading of external files when developing locally.
* Author: Andrew Norcross
* Author URI: https://andrewnorcross.com/
* Version: 0.2.8
* Text Domain: airplane-mode
* Requires WP: 4.4
* Domain Path: languages
* GitHub Plugin URI: https://github.com/norcross/airplane-mode
* @package airplane-mode
*/
/*
* 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.
*/
// Set our base if not already defined.
if ( ! defined( 'AIRMDE_BASE' ) ) {
define( 'AIRMDE_BASE', plugin_basename( __FILE__ ) );
}
// Set our directory if not already defined.
if ( ! defined( 'AIRMDE_DIR' ) ) {
define( 'AIRMDE_DIR', plugin_dir_path( __FILE__ ) );
}
// Set our version if not already defined.
if ( ! defined( 'AIRMDE_VER' ) ) {
define( 'AIRMDE_VER', '0.2.8' );
}
// Load our WP-CLI helper if that is defined and available.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once dirname( __FILE__ ) . '/inc/wp-cli.php';
}
// Ensure the class has not already been loaded.
if ( ! class_exists( 'Airplane_Mode_Core' ) ) {
/**
* Call our class.
*/
class Airplane_Mode_Core {
/**
* Static property to hold our singleton instance.
*
* @var $instance
*/
static $instance = false;
/**
* Set a var for the number of HTTP requests.
*
* @var $http_count
*/
private $http_count = 0;
/**
* This is our constructor. There are many like it, but this one is mine.
*/
private function __construct() {
add_action( 'plugins_loaded', array( $this, 'textdomain' ) );
add_action( 'style_loader_src', array( $this, 'block_style_load' ), 100 );
add_action( 'script_loader_src', array( $this, 'block_script_load' ), 100 );
add_action( 'admin_init', array( $this, 'remove_update_crons' ) );
add_action( 'admin_init', array( $this, 'remove_schedule_hook' ) );
add_filter( 'embed_oembed_html', array( $this, 'block_oembed_html' ), 1, 4 );
add_filter( 'get_avatar', array( $this, 'replace_gravatar' ), 1, 5 );
add_filter( 'map_meta_cap', array( $this, 'prevent_auto_updates' ), 10, 2 );
add_filter( 'default_avatar_select', array( $this, 'default_avatar' ) );
// Kill all the http requests.
add_filter( 'pre_http_request', array( $this, 'disable_http_reqs' ), 10, 3 );
// Check for our query string and handle accordingly.
add_action( 'init', array( $this, 'toggle_check' ) );
// Check for status change and purge transients as needed.
add_action( 'airplane_mode_status_change', array( $this, 'purge_transients' ) );
// Add our counter action.
add_action( 'airplane_mode_http_args', array( $this, 'count_http_requests' ), 0, 0 );
// CSS loader and top toggle.
add_action( 'admin_bar_menu', array( $this, 'admin_bar_toggle' ), 9999 );
add_action( 'wp_enqueue_scripts', array( $this, 'toggle_css' ), 9999 );
add_action( 'admin_enqueue_scripts', array( $this, 'toggle_css' ), 9999 );
// Body class on each location for the display.
add_filter( 'body_class', array( $this, 'body_class' ) );
add_filter( 'login_body_class', array( $this, 'body_class' ) );
add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
// Remove bulk action for updating themes/plugins.
add_filter( 'bulk_actions-plugins', array( $this, 'remove_bulk_actions' ) );
add_filter( 'bulk_actions-themes', array( $this, 'remove_bulk_actions' ) );
add_filter( 'bulk_actions-plugins-network', array( $this, 'remove_bulk_actions' ) );
add_filter( 'bulk_actions-themes-network', array( $this, 'remove_bulk_actions' ) );
// Admin UI items.
add_action( 'admin_menu', array( $this, 'admin_menu_items' ), 9999 );
add_action( 'network_admin_menu', array( $this, 'ms_admin_menu_items' ), 9999 );
add_filter( 'install_plugins_tabs', array( $this, 'plugin_add_tabs' ) );
// Theme update API for different calls.
add_filter( 'themes_api', array( $this, 'bypass_theme_api_call' ), 10, 3 );
add_filter( 'themes_api_result', array( $this, 'bypass_theme_api_result' ), 10, 3 );
// Time based transient checks.
add_filter( 'pre_site_transient_update_themes', array( $this, 'last_checked_themes' ) );
add_filter( 'pre_site_transient_update_plugins', array( $this, 'last_checked_plugins' ) );
add_filter( 'pre_site_transient_update_core', array( $this, 'last_checked_core' ) );
add_filter( 'site_transient_update_themes', array( $this, 'remove_update_array' ) );
add_filter( 'site_transient_update_plugins', array( $this, 'remove_update_array' ) );
// Disable fetching languages from online
add_filter( 'site_transient_available_translations', array( $this, 'available_translations' ), 9999, 1 );
// Use our own filters for scripts and stylesheets to allow local.
add_filter( 'airplane_mode_parse_style', array( $this, 'bypass_asset_block' ), 10, 2 );
add_filter( 'airplane_mode_parse_script', array( $this, 'bypass_asset_block' ), 10, 2 );
// Our activation / deactivation triggers.
register_activation_hook( __FILE__, array( $this, 'create_setting' ) );
register_deactivation_hook( __FILE__, array( $this, 'remove_setting' ) );
// All our various filter checks.
if ( $this->enabled() ) {
// Allows locally defined JETPACK_DEV_DEBUG constant to override filter.
if ( ! defined( 'JETPACK_DEV_DEBUG' ) ) {
if ( ! function_exists( 'get_plugin_data' ) || ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if ( is_plugin_active( 'jetpack/jetpack.php' ) ) {
$jetpack_plugin = get_plugin_data( trailingslashit( WP_PLUGIN_DIR ) . 'jetpack/jetpack.php' );
if ( version_compare( '8.8.0', $jetpack_plugin['Version'], '<=' ) ) {
// Keep jetpack 8.8.0+ from attempting external requests.
add_filter( 'jetpack_offline_mode', '__return_true', 9999 );
} else {
// Keep jetpack <8.8.0 from attempting external requests.
add_filter( 'jetpack_development_mode', '__return_true', 9999 );
}
}
}
// Prevent BuddyPress from falling back to Gravatar avatars.
add_filter( 'bp_core_fetch_avatar_no_grav', '__return_true' );
// Disable automatic updater updates.
add_filter( 'automatic_updater_disabled', '__return_true' );
// 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 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' );
// 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' );
remove_action( 'init', 'wp_schedule_update_checks' );
// 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 );
}
}
}
/**
* If an instance exists, this returns it. If not, it creates one and
* returns it.
*
* @return $instance
*/
public static function getInstance() {
if ( ! self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Load our textdomain for localization.
*
* @return void
*/
public function textdomain() {
load_plugin_textdomain( 'airplane-mode', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Set our initial airplane mode setting to 'on' on activation.
*/
public function create_setting() {
add_site_option( 'airplane-mode', 'on' );
set_transient( 'wporg_theme_feature_list', array(), 999999999999 );
}
/**
* Remove our setting on plugin deactivation.
*/
public function remove_setting() {
delete_option( 'airplane-mode' );
delete_site_option( 'airplane-mode' );
delete_transient( 'wporg_theme_feature_list' );
}
/**
* Helper function to check the current status.
*
* @return bool True if status is 'on'; false if not.
*/
public function enabled() {
// Bail if CLI.
if ( defined( 'WP_CLI' ) and WP_CLI ) {
return false;
}
// Pull our status from the options table.
$option = get_site_option( 'airplane-mode' );
// Backup check for regular options table.
if ( false === $option ) {
$option = get_option( 'airplane-mode' );
}
// Return the option flag.
return 'on' === $option;
}
/**
* Check the URL of a stylesheet and remove any that are not on the local URL.
*
* @param string|false $source The source URL of the CSS sheet, or false if there isn't one.
*
* @return string|false|Airplane_Mode_WP_Error $source The same URL, or an error object.
*/
public function block_style_load( $source ) {
// Bail if disabled.
if ( ! $this->enabled() ) {
return $source;
}
// Plugins can set this to a messed up value that we don't want to pass to `parse_url()`.
if ( empty( $source ) ) {
return $source;
}
// Parse the URL being passed to pull out the host.
$parsed = parse_url( $source, PHP_URL_HOST );
// First run the filter to allow a source host to get through.
if ( false === apply_filters( 'airplane_mode_parse_style', true, $parsed ) ) {
return $source;
}
// If we don't share the same URL as the site itself, return an error object. Otherwise return the URL.
return isset( $parsed ) && false === strpos( home_url(), $parsed )
? new Airplane_Mode_WP_Error( 'airplane_mode_enabled', __( 'Airplane Mode blocked style', 'airplane-mode' ), array(
'return' => '',
'src' => $source,
) )
: $source;
}
/**
* Check the URL of a JS file and remove any that are not on the local URL.
*
* @param string|false $source The source URL of the JS file, or false if there isn't one.
*
* @return string|false|Airplane_Mode_WP_Error $source The same URL, or an error object.
*/
public function block_script_load( $source ) {
// Bail if disabled.
if ( ! $this->enabled() ) {
return $source;
}
// Plugins can set this to a messed up value that we don't want to pass to `parse_url()`.
if ( empty( $source ) ) {
return $source;
}
// Parse the URL being passed to pull out the host.
$parsed = parse_url( $source, PHP_URL_HOST );
// First run the filter to allow a source host to get through.
if ( false === apply_filters( 'airplane_mode_parse_script', true, $parsed ) ) {
return $source;
}
// If we don't share the same URL as the site itself, return an error object. Otherwise return the URL.
return isset( $parsed ) && false === strpos( home_url(), $parsed )
? new Airplane_Mode_WP_Error( 'airplane_mode_enabled', __( 'Airplane Mode blocked script', 'airplane-mode' ), array(
'return' => '',
'src' => $source,
) )
: $source;
}
/**
* Use our existing filter to check for local assets.
*
* @param boolean $block Whether to block the specific asset. Defaults to 'true'.
* @param array $parsed The URL of the asset, parsed.
*
* @return boolean
*/
public function bypass_asset_block( $block, $parsed ) {
// Create an array of the approved local domains.
$local = apply_filters( 'airplane_mode_local_hosts', array( 'localhost', '127.0.0.1' ) );
// If our parsed URL host is in that array, return false. Otherwise, return our blocking choice.
return ! empty( $local ) && in_array( $parsed, $local ) ? false : $block;
}
/**
* Block oEmbeds from displaying.
*
* @param string $html The embed HTML.
* @param string $url The attempted embed URL.
* @param array $attr An array of shortcode attributes.
* @param int $post_ID Post ID.
*
* @return string
*/
public function block_oembed_html( $html, $url, $attr, $post_ID ) {
return $this->enabled() ? sprintf( '<div class="loading-placeholder airplane-mode-placeholder"><p>%s</p></div>', sprintf( __( 'Airplane Mode is enabled. oEmbed blocked for %1$s.', 'airplane-mode' ), esc_url( $url ) ) ) : $html;
}
/**
* Add body class to front-end pages and login based on plugin status.
*
* @param array $classes The existing array of body classes.
*
* @return array $classes The updated array of body classes.
*/
public function body_class( $classes ) {
// Add the class based on the current status.
$classes[] = $this->enabled() ? 'airplane-mode-enabled' : 'airplane-mode-disabled';
// Also add in the margin setup for Query Monitor because I'm a perfectionist.
if ( ! class_exists( 'QueryMonitor' ) || defined( 'QM_DISABLED' ) && QM_DISABLED ) {
$classes[] = 'airplane-mode-no-qm';
}
// Return our array of classes.
return $classes;
}
/**
* Add body class to admin pages based on plugin status.
*
* @param string $classes The existing space-separated list of CSS classes.
*
* @return string $classes The updated space-separated list of CSS classes.
*/
public function admin_body_class( $classes ) {
// First add the standard set of classes based on status.
$classes .= $this->enabled() ? ' airplane-mode-enabled' : ' airplane-mode-disabled';
// Also add in the margin setup for Query Monitor because I'm a perfectionist.
if ( ! class_exists( 'QueryMonitor' ) || defined( 'QM_DISABLED' ) && QM_DISABLED ) {
$classes .= ' airplane-mode-no-qm';
}
// Return our string of classes.
return $classes;
}
/**
* Remove menu items for updates from a standard WP install.
*
* @return null
*/
public function admin_menu_items() {
// Bail if disabled, or on a multisite.
if ( ! $this->enabled() || is_multisite() ) {
return;
}
// Remove our items.
remove_submenu_page( 'index.php', 'update-core.php' );
}
/**
* Remove menu items for updates from a multisite instance.
*
* @return null
*/
public function ms_admin_menu_items() {
// Bail if disabled or not on our network admin.
if ( ! $this->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.
*/
public function replace_gravatar( $avatar, $id_or_email, $size, $default, $alt ) {
// Bail if disabled.
if ( ! $this->enabled() || false === strpos( $avatar, 'gravatar.com' ) ) {
return $avatar;
}
// Swap out the file for a base64 encoded image generated based on the $id_or_email.
$image = $this->generate_color_avatar( $id_or_email );
// Build the image string.
$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.
*/
public function default_avatar( $avatar_list ) {
// Bail if disabled.
if ( ! $this->enabled() ) {
return $avatar_list;
}
// Remove images.
$avatar_list = preg_replace( '|<img([^>]+)> |i', '', $avatar_list );
// Send back the list.
return $avatar_list;
}
/**
* Disable all the HTTP requests being made with the action
* happening before the status check so others can allow certain
* items as desired.
*
* @param bool|array|WP_Error $status Whether to preempt an HTTP request return. Default false.
* @param array $args HTTP request arguments.
* @param string $url The request URL.
*
* @return bool|array|WP_Error A WP_Error object if Airplane Mode is enabled. Original $status if not.
*/
public function disable_http_reqs( $status = false, $args = array(), $url = '' ) {
// Pass our data to the action to allow a bypass.
do_action( 'airplane_mode_http_args', $status, $args, $url );
if ( ! $this->enabled() ) {
return $status;
}
$url_host = parse_url( $url, PHP_URL_HOST );
// Allow the request to pass through if the URL host matches the site's host.
if ( $url_host && parse_url( home_url(), PHP_URL_HOST ) === $url_host ) {
// But allow this to be disabled via a filter.
if ( apply_filters( 'airplane_mode_allow_local_bypass', true, $url, $args ) ) {
return $status;
}
}
// Allow certain HTTP API requests to pass through via a filter.
if ( apply_filters( 'airplane_mode_allow_http_api_request', false, $url, $args, $url_host ) ) {
return $status;
}
// Disable the http requests if enabled.
return new WP_Error( 'airplane_mode_enabled', __( 'Airplane Mode is enabled', 'airplane-mode' ) );
}
/**
* Load our small CSS file for the toggle switch.
*/
public function toggle_css() {
// Don't display CSS on the front-end if the admin bar is not loading.
if ( ! is_admin() && ! is_admin_bar_showing() ) {
return;
}
// Set a suffix for loading the minified or normal.
$file = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? 'airplane-mode.css' : 'airplane-mode.min.css';
// Set a version for browser caching.
$vers = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? time() : AIRMDE_VER;
// Load the CSS file itself.
wp_enqueue_style( 'airplane-mode', plugins_url( '/lib/css/' . $file, __FILE__ ), array(), $vers, 'all' );
}
/**
* Sets the mode.
*
* @param string $mode Desired mode ('on' or 'off').
*
* @return bool Whether the setting changed.
*/
public function set_mode( $mode = 'on' ) {
// Check what mode we're currently in, with "on" as a fallback.
if ( ! in_array( $mode, array( 'on', 'off' ) ) ) {
$mode = 'on';
}
// Update the setting.
$return = update_site_option( 'airplane-mode', $mode );
// Fire action to allow for functions to run on status change.
do_action( 'airplane_mode_status_change', $mode );
// And return the status we just set.
return $return;
}
/**
* Enables airplane mode.
*
* @return bool Whether the setting changed.
*/
public function enable() {
return self::set_mode( 'on' );
}
/**
* Disables airplane mode.
*
* @return bool Whether the setting changed.
*/
public function disable() {
return self::set_mode( 'off' );
}
/**
* Check the user action from the toggle switch to set the option
* to 'on' or 'off'.
*
* @return void If any of the sanity checks fail and we bail early.
*/
public function toggle_check() {
// Bail if current user doesn't have cap.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Set a sanitized variable of our potential nonce and request.
$nonce = isset( $_GET['airmde_nonce'] ) ? sanitize_key( $_GET['airmde_nonce'] ) : '';
$switch = isset( $_REQUEST['airplane-mode'] ) ? sanitize_key( $_REQUEST['airplane-mode'] ) : '';
// Check for our nonce.
if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'airmde_nonce' ) ) {
return;
}
// Now check for our query string.
if ( empty( $switch ) || ! in_array( $switch, array( 'on', 'off' ) ) ) {
return;
}
// Delete old per-site option.
delete_option( 'airplane-mode' );
// Set our mode based on the toggle action.
self::set_mode( $switch );
// And go about our business.
wp_redirect( self::get_redirect() );
exit;
}
/**
* 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(
'airplane-mode',
'airmde_nonce',
'user_switched',
'switched_off',
'switched_back',
'message',
'update',
'updated',
'settings-updated',
'saved',
'activated',
'activate',
'deactivate',
'enabled',
'disabled',
'locked',
'skipped',
'deleted',
'trashed',
'untrashed',
'force-check',
) );
// Redirect away from the update core page.
$redirect = str_replace( 'update-core.php', '', $redirect );
// And return the redirect.
return apply_filters( 'airplane_mode_redirect_url', $redirect );
}
/**
* Add our quick toggle to the admin bar to enable / disable.
*
* @param WP_Admin_Bar $wp_admin_bar The admin bar object.
*
* @return void if current user can't manage options and we bail early.
*/
public function admin_bar_toggle( WP_Admin_Bar $wp_admin_bar ) {
// Bail if current user doesn't have cap.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Get the current status.
$status = $this->enabled();
// Set a title message (translatable).
$title = $status ? __( 'Airplane Mode is enabled.', 'airplane-mode' ) : __( 'Airplane Mode is disabled.', 'airplane-mode' );
// Set our toggle variable parameter (in reverse since we want the opposite action).
$toggle = $status ? 'off' : 'on';
// Set my HTTP request count label to a blank string for now.
$label = '';
// Get and display the HTTP count when Query Monitor isn't active.
if ( ! class_exists( 'QueryMonitor' ) || defined( 'QM_DISABLED' ) && QM_DISABLED ) {
// Pull my HTTP count.
$count = ! empty( $this->http_count ) ? number_format_i18n( $this->http_count ) : 0;
$count_label = sprintf( _n( 'There was %s HTTP request.', 'There were %s HTTP requests.', $count, 'airplane-mode' ), $count );
// Build the markup for my label.
$label = '<span class="ab-label" aria-hidden="true">' . absint( $count ) . '</span>';
$label .= '<span class="screen-reader-text">' . esc_html( $count_label ) . '</span>';
// Amend the tooltip title with the count.
$title .= ' ' . $count_label;
}
// Get our link with the status parameter.
$link = wp_nonce_url( add_query_arg( 'airplane-mode', $toggle ), 'airmde_nonce', 'airmde_nonce' );
// Now add the admin bar link.
$wp_admin_bar->add_node(
array(
'id' => 'airplane-mode-toggle',
'title' => '<span class="ab-icon"></span>' . $label,
'href' => esc_url( $link ),
'position' => 0,
'meta' => array(
'title' => $title,
),
)
);
}
/**
* 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.
*/
public function prevent_auto_updates( $caps, $cap ) {
// Check for being enabled and look for specific cap requirements.
if ( $this->enabled() && in_array( $cap, array( 'update_plugins', 'update_themes', 'update_core' ) ) ) {
$caps[] = 'do_not_allow';
}
// Send back the data array.
return $caps;
}
/**
* Check the new status after airplane mode has been enabled or
* disabled and purge related transients.
*
* @param boolean $force Whether to force the purge.
*
* @return void
*/
public function purge_transients( $force = false ) {
// First check for the filter to avoid this action overall.
if ( empty( $force ) && false === $clear = apply_filters( 'airplane_mode_purge_transients', true ) ) {
return;
}
// Purge the transients related to updates when disabled or the force is called.
if ( ! $this->enabled() || ! empty( $force ) ) {
delete_site_transient( 'update_core' );
delete_site_transient( 'update_plugins' );
delete_site_transient( 'update_themes' );
delete_site_transient( 'wporg_theme_feature_list' );
}
}
/**
* Remove all the various places WP does the update checks. As you can see there are a lot of them.
*
* @return null
*/
public function remove_update_crons() {
// Bail if disabled.
if ( ! $this->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
// 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' );
// Don't forget when the language packs do it.
remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
remove_action( 'upgrader_process_complete', 'wp_version_check' );
remove_action( 'upgrader_process_complete', 'wp_update_plugins' );
remove_action( 'upgrader_process_complete', 'wp_update_themes' );
}
/**
* Remove all the various schedule hooks for themes, plugins, etc.
*
* @return null
*/
public function remove_schedule_hook() {
// Bail if disabled.
if ( ! $this->enabled() ) {
return;
}
// Clear all my hooks.
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' );
}
/**
* Override the API call made for pulling themes from the .org repo.
*
* @param false|object|array $override Whether to override the WordPress.org Themes API. Default false.
* @param string $action Requested action. Likely values are 'theme_information',
* 'feature_list', or 'query_themes'.
* @param object $args Arguments used to query for installer pages from the Themes API.
*
* @return bool True if enabled, otherwise the existng value.
*/
public function bypass_theme_api_call( $override, $action, $args ) {
// Bail if disabled.
if ( ! $this->enabled() ) {
return $override;
}
// Return false on feature list to avoid the API call.
return ! empty( $action ) && 'feature_list' === $action ? true : $override;
}
/**
* Hijack the expected themes API result.
*
* @param array|object|WP_Error $res WordPress.org Themes API response.
* @param string $action Requested action. Likely values are 'theme_information',
* 'feature_list', or 'query_themes'.
* @param object $args Arguments used to query for installer pages from the WordPress.org Themes API.
*
* @return bool An empty array if enabled, otherwise the existng result.
*/
public function bypass_theme_api_result( $res, $action, $args ) {
// Bail if disabled.
if ( ! $this->enabled() ) {
return $res;
}
// Return false on feature list to avoid the API call.
return ! empty( $action ) && in_array( $action, array( 'feature_list', 'query_themes' ) ) ? array() : $res;
}
/**
* Always send back that the latest version of WordPress is the one we're running
*
* @return object the modified output with our information
*/
public function last_checked_core() {
// Bail if disabled.
if ( ! $this->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
*/
public function last_checked_themes() {
// Bail if disabled.
if ( ! $this->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
*/
public function last_checked_plugins() {
// Bail if disabled.
if ( ! $this->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';
}