-
Notifications
You must be signed in to change notification settings - Fork 8
/
class-jquery-migrate-helper.php
762 lines (640 loc) · 29.4 KB
/
class-jquery-migrate-helper.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
<?php
/**
* Class jQuery_Migrate_Helper,
*
*/
if ( ! defined( 'ABSPATH' ) ) {
die( 'Invalid request.' );
}
class jQuery_Migrate_Helper {
private function __construct() {}
public static function init_actions() {
$public_deprecations = get_option( '_jquery_migrate_public_deprecation_logging', 'no' );
/*
* To be able to replace the src, scripts should not be concatenated.
*
* Concatenation is only forcefully disabled if the user is logged in, or public deprecation logging
* has been enabled, to reduce the performance impact of the plugin.
*/
if ( is_user_logged_in() || 'yes' === $public_deprecations ) {
if ( ! defined( 'CONCATENATE_SCRIPTS' ) ) {
define( 'CONCATENATE_SCRIPTS', false );
}
$GLOBALS['concatenate_scripts'] = false;
}
add_action( 'wp_default_scripts', array( __CLASS__, 'replace_scripts' ), -1 );
// We need our own script for displaying warnings to run as late as possible.
// Print it separately after the footer scripts.
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ) );
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register_scripts' ) );
add_action( 'admin_print_footer_scripts', array( __CLASS__, 'print_scripts' ), 100 );
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts_frontend' ), 100 );
add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) );
add_action( '_admin_menu', array( __CLASS__, 'append_tools_menu_counter' ) );
add_action( '_user_admin_menu', array( __CLASS__, 'append_tools_menu_counter' ) );
add_action( 'admin_bar_menu', array( __CLASS__, 'admin_bar_menu' ), 100 );
add_action( 'admin_init', array( __CLASS__, 'admin_settings' ) );
add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
add_action( 'wp_ajax_jquery-migrate-dismiss-notice', array( __CLASS__, 'admin_notices_dismiss' ) );
add_action( 'wp_ajax_jquery-migrate-log-notice', array( __CLASS__, 'log_migrate_notice' ) );
add_action( 'wp_ajax_nopriv_jquery-migrate-log-notice', array( __CLASS__, 'log_migrate_notice' ) );
add_action( 'wp_ajax_jquery-migrate-downgrade-version', array( __CLASS__, 'downgrade_jquery_version' ) );
add_action( 'wp_head', array( __CLASS__, 'fatal_error_handler' ) );
add_action( 'admin_head', array( __CLASS__, 'fatal_error_handler' ) );
add_filter( 'site_status_tests', array( __CLASS__, 'site_health_check' ) );
// Set up our scheduled weekly notification.
if ( ! wp_next_scheduled( 'enable_jquery_migrate_helper_notification' ) && ! wp_installing() ) {
wp_schedule_event( time() + DAY_IN_SECONDS, 'weekly', 'enable_jquery_migrate_helper_notification' );
}
add_action( 'enable_jquery_migrate_helper_notification', array( __CLASS__, 'scheduled_event_handler' ) );
}
/**
* Run the scheduled event ot send an email summary to the site admin.
*/
public static function scheduled_event_handler() {
self::send_message( 'weekly' );
}
/**
* Appends the count of migration notices to the `Tools` menu item.
*
* This is done to indicate that a submenu item has actionable content.
*
* @global array $menu
*/
public static function append_tools_menu_counter() {
global $menu;
$count = self::logged_migration_notice_count();
// Don't add extra markup if not needed.
if ( $count < 1 ) {
return;
}
// Menu position 75 is the Tools menu.
$menu[75][0] .= sprintf(
' <span class="update-plugins jqmh-deprecations count-%1$d"><span class="plugin-count">%1$d</span></span>',
self::logged_migration_notice_count()
);
}
/**
* Add the jQuery Migrate plugin to the Tools sub-menu.
*/
public static function admin_menu() {
add_submenu_page(
'tools.php',
__( 'jQuery Migrate', 'enable-jquery-migrate-helper' ),
sprintf(
'%1$s <span class="update-plugins jqmh-deprecations count-%2$d"><span class="plugin-count">%2$d</span></span>',
__( 'jQuery Migrate', 'enable-jquery-migrate-helper' ),
self::logged_migration_notice_count()
),
'manage_options',
'jqmh',
array( __CLASS__, 'admin_page' )
);
}
/**
* Render the plugin tools page.
*/
public static function admin_page() {
echo '<div class="wrap">';
include_once __DIR__ . '/admin/header.php';
echo '</div>';
}
/**
* Settings page save handler.
*/
public static function admin_settings() {
if ( ! isset( $_POST['jqmh-settings'] ) || ! current_user_can( 'manage_options' ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'jqmh-settings' ) ) {
return;
}
$previous_downgrade = get_option( '_jquery_migrate_downgrade_version', 'no' );
if ( isset( $_POST['live-deprecations'] ) ) {
delete_option( '_jquery_migrate_deprecations_dismissed_notice' );
} else {
update_option( '_jquery_migrate_deprecations_dismissed_notice', time() );
}
if ( ! empty( $_POST['jquery-version'] ) && 'yes' === $_POST['jquery-version'] ) {
update_option( '_jquery_migrate_downgrade_version', 'yes' );
} else {
update_option( '_jquery_migrate_downgrade_version', 'no' );
// Disable logging by default when enabling modern jQuery versions.
if ( 'yes' === $previous_downgrade ) {
update_option( '_jquery_migrate_modern_deprecations', 'no' );
update_option( '_jquery_migrate_deprecations_dismissed_notice', time() );
}
}
if ( isset( $_POST['public-deprecation-logging'] ) ) {
update_option( '_jquery_migrate_public_deprecation_logging', 'yes' );
} else {
update_option( '_jquery_migrate_public_deprecation_logging', 'no' );
}
if ( isset( $_POST['modern-deprecations'] ) ) {
update_option( '_jquery_migrate_modern_deprecations', 'yes' );
} else {
update_option( '_jquery_migrate_modern_deprecations', 'no' );
}
if ( isset( $_POST['automatic-downgrade'] ) ) {
update_option( '_jquery_migrate_has_auto_downgraded', 'no' );
} else {
update_option( '_jquery_migrate_has_auto_downgraded', 'yes' );
}
}
public static function site_health_check( $tests ) {
$tests['direct']['enable-jquery-migrate-helper'] = array(
'label' => __( 'WordPress jQuery Version', 'enable-jquery-migrate-helper' ),
'test' => array( __CLASS__, 'site_health_test' ),
);
return $tests;
}
public static function site_health_test() {
$result = array(
'label' => __( 'WordPress jQuery Version', 'enable-jquery-migrate-helper' ),
'status' => 'good',
'badge' => array(
'label' => __( 'Performance' ),
'color' => 'blue',
),
'description' => sprintf(
'<p>%s</p>',
__( 'Your site is using the latest jQuery version that comes with WordPress.', 'enable-jquery-migrate-helper' )
),
'actions' => '',
'test' => 'enable-jquery-migrate-helper',
);
$downgrade_state = get_option( '_jquery_migrate_downgrade_version', 'no' );
if ( 'no' !== $downgrade_state ) {
$result['label'] = __( 'You are using a legacy version of jQuery', 'enable-jquery-migrate-helper' );
$result['status'] = 'critical';
$result['description'] = sprintf(
'<p>%s</p>',
__( 'Your site is using a legacy version of jQuery, 1.12.4-wp, probably in order to fix problems that were experienced with your plugins or themes after an update of WordPress. Please make sure that your plugins and themes become compatible with WordPress 5.6 by contacting the authors.', 'enable-jquery-migrate-helper' )
);
$result['actions'] = sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( 'tools.php?page=jqmh' ) ),
__( 'Plugin settings', 'enable-jquery-migrate-helper' )
);
}
return $result;
}
/**
* Ajax handler for automatic downgrades.
*/
public static function downgrade_jquery_version() {
/*
* Only allow the downgrade to be triggered automatically by site visitors if an admin hasn't
* changed the option themselves; in that case only an admin should be able to set the version.
*/
$has_auto_downgraded = get_option( '_jquery_migrate_has_auto_downgraded', 'no' );
if ( 'no' !== $has_auto_downgraded ) {
return;
}
if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'jquery-migrate-automatic-downgrade' ) ) {
return;
}
update_option( '_jquery_migrate_downgrade_version', 'yes' );
update_option( '_jquery_migrate_has_auto_downgraded', 'yes' );
self::send_message( 'automatic-downgrade' );
wp_send_json_success( array( 'reload' => true ) );
}
/**
* Add a fatal error handler for uncaught errors.
*
* This will look for deprecated jQuery functions, and send an AJAX call letting the plugin
* know that it should serve future requests as a downgraded version of jQuery.
*
* Vanilla JavaScript is used here to remove all dependencies on libraries, even though they
* all look very pretty, this ensures that code can run no matter the circumstances.
*/
public static function fatal_error_handler() {
// If an auto-downgraded has already been performed, do not output the error handler.
if ( 'no' !== get_option( '_jquery_migrate_has_auto_downgraded', 'no' ) ) {
return;
}
?>
<script type="text/javascript">
var jQueryMigrateHelperHasSentDowngrade = false;
window.onerror = function( msg, url, line, col, error ) {
// Break out early, do not processing if a downgrade reqeust was already sent.
if ( jQueryMigrateHelperHasSentDowngrade ) {
return true;
}
var xhr = new XMLHttpRequest();
var nonce = '<?php echo esc_js( wp_create_nonce( 'jquery-migrate-automatic-downgrade' ) ); ?>';
var jQueryFunctions = [
'andSelf',
'browser',
'live',
'boxModel',
'support.boxModel',
'size',
'swap',
'clean',
'sub',
];
var match_pattern = /\)\.(.+?) is not a function/;
var erroredFunction = msg.match( match_pattern );
// If there was no matching functions, do not try to downgrade.
if ( null === erroredFunction || typeof erroredFunction !== 'object' || typeof erroredFunction[1] === "undefined" || -1 === jQueryFunctions.indexOf( erroredFunction[1] ) ) {
return true;
}
// Set that we've now attempted a downgrade request.
jQueryMigrateHelperHasSentDowngrade = true;
xhr.open( 'POST', '<?php echo esc_js( admin_url( 'admin-ajax.php' ) ); ?>' );
xhr.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
xhr.onload = function () {
var response,
reload = false;
if ( 200 === xhr.status ) {
try {
response = JSON.parse( xhr.response );
reload = response.data.reload;
} catch ( e ) {
reload = false;
}
}
// Automatically reload the page if a deprecation caused an automatic downgrade, ensure visitors get the best possible experience.
if ( reload ) {
location.reload();
}
};
xhr.send( encodeURI( 'action=jquery-migrate-downgrade-version&_wpnonce=' + nonce ) );
// Suppress error alerts in older browsers
return true;
}
</script>
<?php
}
// Pre-register scripts on 'wp_default_scripts' action, they won't be overwritten by $wp_scripts->add().
private static function set_script( $scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
$script = $scripts->query( $handle, 'registered' );
if ( $script ) {
// If already added
$script->src = $src;
$script->deps = $deps;
$script->ver = $ver;
$script->args = $in_footer;
unset( $script->extra['group'] );
if ( $in_footer ) {
$script->add_data( 'group', 1 );
}
} else {
// Add the script
if ( $in_footer ) {
$scripts->add( $handle, $src, $deps, $ver, 1 );
} else {
$scripts->add( $handle, $src, $deps, $ver );
}
}
}
/*
* Enqueue jQuery migrate, and force it to be the development version.
*
* This will ensure that console errors are generated that we can surface to the
* end user in a responsible manner so that they can update their plugins and theme,
* or make a decision to switch to other plugin/theme if no updates are available.
*/
public static function replace_scripts( $scripts ) {
$assets_url = plugins_url( 'js/', __FILE__ );
if ( version_compare( $GLOBALS['wp_version'], '5.6-alpha', '<' ) || 'yes' === get_option( '_jquery_migrate_downgrade_version', 'no' ) ) {
self::set_script( $scripts, 'jquery-migrate', $assets_url . 'jquery-migrate/jquery-migrate-1.4.1-wp.js', array(), '1.4.1-wp' );
self::set_script( $scripts, 'jquery-core', $assets_url . 'jquery/jquery-1.12.4-wp.js', array(), '1.12.4-wp' );
self::set_script( $scripts, 'jquery', false, array( 'jquery-core', 'jquery-migrate' ), '1.12.4-wp' );
// All the jQuery UI stuff comes here.
// self::set_script( $scripts, '', $assets_url . 'jquery-ui/', array(), '' );
self::set_script( $scripts, 'jquery-ui-core', $assets_url . 'jquery-ui/core.min.js', array( 'jquery' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-core', $assets_url . 'jquery-ui/effect.min.js', array( 'jquery' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-blind', $assets_url . 'jquery-ui/effect-blind.min.js', array( 'jquery-effects-core' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-bounce', $assets_url . 'jquery-ui/effect-bounce.min.js', array( 'jquery-effects-core' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-clip', $assets_url . 'jquery-ui/effect-clip.min.js', array( 'jquery-effects-core' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-drop', $assets_url . 'jquery-ui/effect-drop.min.js', array( 'jquery-effects-core' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-explode', $assets_url . 'jquery-ui/effect-explode.min.js', array( 'jquery-effects-core' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-fade', $assets_url . 'jquery-ui/effect-fade.min.js', array( 'jquery-effects-core' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-fold', $assets_url . 'jquery-ui/effect-fold.min.js', array( 'jquery-effects-core' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-highlight', $assets_url . 'jquery-ui/effect-highlight.min.js', array( 'jquery-effects-core' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-puff', $assets_url . 'jquery-ui/effect-puff.min.js', array( 'jquery-effects-core', 'jquery-effects-scale' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-pulsate', $assets_url . 'jquery-ui/effect-pulsate.min.js', array( 'jquery-effects-core' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-scale', $assets_url . 'jquery-ui/effect-scale.min.js', array( 'jquery-effects-core', 'jquery-effects-size' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-shake', $assets_url . 'jquery-ui/effect-shake.min.js', array( 'jquery-effects-core' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-size', $assets_url . 'jquery-ui/effect-size.min.js', array( 'jquery-effects-core' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-slide', $assets_url . 'jquery-ui/effect-slide.min.js', array( 'jquery-effects-core' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-effects-transfer', $assets_url . 'jquery-ui/effect-transfer.min.js', array( 'jquery-effects-core' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-accordion', $assets_url . 'jquery-ui/accordion.min.js', array( 'jquery-ui-core', 'jquery-ui-widget' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-autocomplete', $assets_url . 'jquery-ui/autocomplete.min.js', array( 'jquery-ui-menu', 'wp-a11y' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-button', $assets_url . 'jquery-ui/button.min.js', array( 'jquery-ui-core', 'jquery-ui-widget' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-datepicker', $assets_url . 'jquery-ui/datepicker.min.js', array( 'jquery-ui-core' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-dialog', $assets_url . 'jquery-ui/dialog.min.js', array( 'jquery-ui-resizable', 'jquery-ui-draggable', 'jquery-ui-button', 'jquery-ui-position' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-draggable', $assets_url . 'jquery-ui/draggable.min.js', array( 'jquery-ui-mouse' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-droppable', $assets_url . 'jquery-ui/droppable.min.js', array( 'jquery-ui-draggable' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-menu', $assets_url . 'jquery-ui/menu.min.js', array( 'jquery-ui-core', 'jquery-ui-widget', 'jquery-ui-position' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-mouse', $assets_url . 'jquery-ui/mouse.min.js', array( 'jquery-ui-core', 'jquery-ui-widget' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-position', $assets_url . 'jquery-ui/position.min.js', array( 'jquery' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-progressbar', $assets_url . 'jquery-ui/progressbar.min.js', array( 'jquery-ui-core', 'jquery-ui-widget' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-resizable', $assets_url . 'jquery-ui/resizable.min.js', array( 'jquery-ui-mouse' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-selectable', $assets_url . 'jquery-ui/selectable.min.js', array( 'jquery-ui-mouse' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-selectmenu', $assets_url . 'jquery-ui/selectmenu.min.js', array( 'jquery-ui-menu' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-slider', $assets_url . 'jquery-ui/slider.min.js', array( 'jquery-ui-mouse' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-sortable', $assets_url . 'jquery-ui/sortable.min.js', array( 'jquery-ui-mouse' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-spinner', $assets_url . 'jquery-ui/spinner.min.js', array( 'jquery-ui-button' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-tabs', $assets_url . 'jquery-ui/tabs.min.js', array( 'jquery-ui-core', 'jquery-ui-widget' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-tooltip', $assets_url . 'jquery-ui/tooltip.min.js', array( 'jquery-ui-core', 'jquery-ui-widget', 'jquery-ui-position' ), '1.11.4-wp', 1 );
self::set_script( $scripts, 'jquery-ui-widget', $assets_url . 'jquery-ui/widget.min.js', array( 'jquery' ), '1.11.4-wp', 1 );
// This just updates the dependency of `jquery-touch-punch`.
self::set_script( $scripts, 'jquery-touch-punch', false, array( 'jquery-ui-widget', 'jquery-ui-mouse' ), '0.2.2', 1 );
} else {
if ( 'yes' === get_option( '_jquery_migrate_modern_deprecations', 'no' ) ) {
self::set_script( $scripts, 'jquery-migrate', $assets_url . 'jquery-migrate/jquery-migrate-3.4.1-wp.js', array(), '3.4.1-wp' );
self::set_script( $scripts, 'jquery', false, array( 'jquery-core', 'jquery-migrate' ), '3.6.4-wp' );
}
}
}
/**
* Register the deprecation notice capture handler script.
*/
public static function register_scripts() {
wp_register_script( 'jquery-migrate-deprecation-notices', plugins_url( 'js/deprecation-notice.js', __FILE__ ), array( 'jquery', 'jquery-migrate' ), false, true );
wp_localize_script(
'jquery-migrate-deprecation-notices',
'JQMH',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'report_nonce' => wp_create_nonce( 'jquery-migrate-report-deprecation' ),
'backend' => is_admin(),
'plugin_slug' => dirname( plugin_basename( __FILE__ ) ),
'capture_deprecations' => ( 'yes' === get_option( '_jquery_migrate_downgrade_version', 'no' ) || 'yes' === get_option( '_jquery_migrate_modern_deprecations', 'no' ) ),
'single_instance_log' => ( 'no' === get_option( '_jquery_migrate_downgrade_version', 'no' ) ), // Only show one instance of deprecations in jQuery 3.5
)
);
}
/**
* Output/print the deprecation notice script. Needs to be last in the footer.
*/
public static function print_scripts() {
wp_print_scripts( 'jquery-migrate-deprecation-notices' );
}
public static function enqueue_scripts_frontend() {
// Only load the asset for users who can act on them.
if ( ! current_user_can( 'manage_options' ) && 'no' === get_option( '_jquery_migrate_public_deprecation_logging', 'no' ) ) {
return;
}
wp_enqueue_script( 'jquery-migrate-deprecation-notices' );
}
/**
* HTML for jQuery Migrate deprecated notices.
*
* This notice is only displayed if there are JS migration deprecation notices
* for scripts loaded on the current screen.
*
* @since 1.0.0
*/
public static function deprecated_scripts_notice() {
// If the message has been dismissed, and it has been less than two weeks since it was seen,
// then skip displaying the deprecation lister for now.
if ( ! self::show_deprecated_scripts_notice() ) {
return;
}
include_once __DIR__ . '/templates/admin-notices/deprecated-scripts.php';
}
public static function show_deprecated_scripts_notice() {
return false === get_option( '_jquery_migrate_deprecations_dismissed_notice', false );
}
/**
* HTML for the Dashboard notice.
*/
public static function dashboard_notice() {
if ( ! self::show_dashboard_notice() ) {
return;
}
include_once __DIR__ . '/templates/admin-notices/welcome.php';
}
public static function show_dashboard_notice() {
// Show again in two weeks if the user has dismissed this notice.
$is_dismissed = get_option( '_jquery_migrate_dismissed_notice', false );
$recurrence = 1 * WEEK_IN_SECONDS;
// Force show the admin notice if using a downgraded jQuery version.
if ( 'no' !== get_option( '_jquery_migrate_downgrade_version', 'no' ) ) {
// Do not add this message to the plugins own admin page since it already contains
// a permanent notice.
if ( 'tools_page_jqmh' !== get_current_screen()->id) {
return true;
}
}
// Normally only show this warning on the dashboard page.
if ( 'dashboard' !== get_current_screen()->id ) {
return false;
}
// If the message has been dismissed, and it has been less than two weeks since it was seen,
// then skip showing the admin notice for now.
if ( false !== $is_dismissed && $is_dismissed > ( time() - $recurrence ) ) {
return false;
}
return true;
}
/**
* Display a dashboard notice if no deprecations have been logged in a while.
*
* This encourages users to remove the plugin when no longer needed, this will help gauge the jQuery
* migrations, and also remove the potential false-positive reports of jQuery issues caused by
* an unrelated core bug fixed in WordPress 5.5.1 that inflates the plugin numbers.
*/
public static function plugin_obsolete_message() {
$last_log = get_option( 'jqmh_last_log_time', null );
$recurrence = 1 * WEEK_IN_SECONDS;
/*
* If no log time is recorded, this is likely a recently installed or udpated plugin, set value to now,
* to give a buffer, and avoid showing the notice when we have no data telling if it’s needed or not.
*/
if ( null === $last_log ) {
update_option( 'jqmh_last_log_time', time() );
return;
}
if ( $last_log > ( time() - $recurrence ) ) {
return;
}
include_once __DIR__ . '/templates/admin-notices/no-longer-needed.php';
}
public static function admin_notices() {
// Show only to admins.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
self::dashboard_notice();
self::plugin_obsolete_message();
self::deprecated_scripts_notice();
}
public static function log_migrate_notice() {
if ( ! current_user_can( 'manage_options' ) && 'no' === get_option( '_jquery_migrate_public_deprecation_logging', 'no' ) ) {
status_header( 403 );
die();
}
if ( ! wp_verify_nonce( $_POST['nonce'], 'jquery-migrate-report-deprecation' ) ) {
status_header( 406, 'Invalid nonce' );
die();
}
$required_fields = array(
'notice',
);
foreach ( $required_fields as $required_field ) {
if ( ! isset( $_POST[ $required_field ] ) ) {
status_header( 400, 'Missing required fields' );
die();
}
}
$logs = get_option( 'jqmh_logs', array() );
$deprecation_data = array(
'notice' => wp_kses( $_POST['notice'], array() ),
);
/*
* Creating a hash of the deprecation data lets us ensure it is only reported once, to avoid
* filling the database with duplicates on busy sites.
*/
$deprecation_hash = md5( wp_json_encode( $deprecation_data ) );
if ( ! isset( $logs[ $deprecation_hash ] ) ) {
$logs[ $deprecation_hash ] = array_merge( array(
'registered' => date_i18n( 'Y-m-d H:i:s' ),
'page' => ( isset( $_POST['url'] ) ? esc_url_raw( $_POST['url'] ) : '' ),
'backend' => isset( $_POST['backend'] ) && $_POST['backend'],
), $deprecation_data );
update_option( 'jqmh_logs', $logs );
update_option( 'jqmh_last_log_time', time() );
}
wp_send_json_success();
}
/**
* Check if any errors have been logged to the database.
*
* @return int|null
*/
public static function logged_migration_notice_count() {
$logs = get_option( 'jqmh_logs', array() );
return count( $logs );
}
/**
* Handle ajax requests to dismiss a notice, and remember the dismissal.
*
* @return void
*/
public static function admin_notices_dismiss() {
if ( empty( $_POST['dismiss-notice-nonce'] ) || ! current_user_can( 'manage_options' ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['dismiss-notice-nonce'], $_POST['notice'] ) ) {
return;
}
switch ( $_POST['notice'] ) {
case 'jquery-migrate-deprecation-list':
update_option( '_jquery_migrate_deprecations_dismissed_notice', time() );
break;
case 'jquery-migrate-previous-deprecations':
delete_option( 'jqmh_logs' );
break;
case 'jquery-migrate-notice':
update_option( '_jquery_migrate_dismissed_notice', time() );
break;
case 'jquery-migrate-no-deprecations-notice':
update_option( 'jqmh_last_log_time', time() );
break;
}
}
/**
* Add this plugin to the admin bar as a menu item.
*
* This entry allows users to re-surface previously hidden notices from the plugin,
* and also allows for alerting of issues detected in the frontend, where we avoid
* notices.
*
* @param $wp_menu
*/
public static function admin_bar_menu( $wp_menu ) {
// Show only to those with the right capabilities.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$deprecation_count = self::logged_migration_notice_count();
$wp_menu->add_menu(
array(
'id' => 'enable-jquery-migrate-helper',
'title' => sprintf(
// translators: %s: Parenthesis with issue count.
__( 'jQuery Migrate %s', 'enable-jquery-migrate-helper' ),
sprintf(
'<span class="count-wrapper jqmh-deprecations" style="' . ( $deprecation_count > 0 ? '' : 'display:none;' ) . '">%s</span>',
sprintf(
// translators: 1: Total number of existing issues. 2: Issues discovered on currently loaded page.
__( '(Previously known: %1$d Discovered on this page: %2$s)', 'enable-jquery-migrate-helper' ),
$deprecation_count,
sprintf(
'<span class="count">%d</span>',
0
)
)
)
),
'href' => '#',
)
);
$wp_menu->add_node(
array(
'id' => 'enable-jquery-migrate-helper-page',
'title' => _x( 'Settings Page', 'Admin bar menu link', 'enable-jquery-migrate-helper' ),
'parent' => 'enable-jquery-migrate-helper',
'href' => get_admin_url( null, 'tools.php?page=jqmh' ),
)
);
$wp_menu->add_node(
array(
'id' => 'enable-jquery-migrate-helper-deprecation-logs',
'title' => _x( 'Logged deprecations', 'Admin bar menu link', 'enable-jquery-migrate-helper' ),
'parent' => 'enable-jquery-migrate-helper',
'href' => get_admin_url( null, 'tools.php?page=jqmh&tab=logs' ),
)
);
}
/**
* Send a pre-defined email to the site admin.
*
* @param string $template The template of the email to be sent.
* @return bool If the email was sent or not.
*/
private static function send_message( $template ) {
$file = null;
switch ( $template ) {
case 'weekly':
$title = __( 'Weekly jQuery Migrate Status Update', 'enable-jquery-migrate-helper' );
$file = 'weekly.php';
break;
case 'automatic-downgrade':
$title = __( 'Automatic jQuery version change', 'enable-jquery-migrate-helper' );
$file = 'automatic-downgrade.php';
break;
}
$file_path = __DIR__ . '/templates/email/' . $file;
if ( ! $file || ! file_exists( $file_path ) ) {
return false;
}
$recipient = get_bloginfo( 'admin_email' );
ob_start();
include $file_path;
$message = ob_get_clean();
/**
* Filter the contents of the notification email.
*
* If an empty value is returned, the email notice will not be sent.
*
* @since 1.2.0
*
* @param string $message The message that will be sent ot the site admin.
* @param string $template The currently invoked email template.
*/
$message = apply_filters( 'jqmh_email_message', $message, $template );
if ( empty( $message ) ) {
return false;
}
add_filter( 'wp_mail_content_type', function() {
return 'text/html';
} );
return wp_mail( $recipient, $title, $message );
}
}