Enable jQuery Migrate Helper - Version 1.1.0

Version Description

Download this release

Release Info

Developer Clorith
Plugin Icon wp plugin Enable jQuery Migrate Helper
Version 1.1.0
Comparing to
See all releases

Code changes from version 1.0.1 to 1.1.0

class-jquery-migrate-helper.php CHANGED
@@ -1,176 +1,520 @@
1
- <?php
2
- /**
3
- * Class jQuery_Migrate_Helper,
4
- *
5
- */
6
-
7
- if ( ! defined( 'ABSPATH' ) ) {
8
- die( 'Invalid request.' );
9
- }
10
-
11
- class jQuery_Migrate_Helper {
12
-
13
- private function __construct() {}
14
-
15
- public static function init_actions() {
16
- // To be able to replace the src, scripts should not be concatenated.
17
- if ( ! defined( 'CONCATENATE_SCRIPTS' ) ) {
18
- define( 'CONCATENATE_SCRIPTS', false );
19
- }
20
-
21
- $GLOBALS['concatenate_scripts'] = false;
22
-
23
- add_action( 'wp_default_scripts', array( __CLASS__, 'replace_scripts' ), -1 );
24
-
25
- // We need our own script for displaying warnings to run as late as possible.
26
- // Print it separately after the footer scripts.
27
- add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ) );
28
- add_action( 'admin_print_footer_scripts', array( __CLASS__, 'print_scripts' ), 100 );
29
-
30
- add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
31
- add_action( 'wp_ajax_jquery-migrate-dismiss-notice', array( __CLASS__, 'admin_notices_dismiss' ) );
32
- }
33
-
34
- // Pre-register scripts on 'wp_default_scripts' action, they won't be overwritten by $wp_scripts->add().
35
- private static function set_script( $scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
36
- $script = $scripts->query( $handle, 'registered' );
37
-
38
- if ( $script ) {
39
- // If already added
40
- $script->src = $src;
41
- $script->deps = $deps;
42
- $script->ver = $ver;
43
- $script->args = $in_footer;
44
-
45
- unset( $script->extra['group'] );
46
-
47
- if ( $in_footer ) {
48
- $script->add_data( 'group', 1 );
49
- }
50
- } else {
51
- // Add the script
52
- if ( $in_footer ) {
53
- $scripts->add( $handle, $src, $deps, $ver, 1 );
54
- } else {
55
- $scripts->add( $handle, $src, $deps, $ver );
56
- }
57
- }
58
- }
59
-
60
- /*
61
- * Enqueue jQuery migrate, and force it to be the development version.
62
- *
63
- * This will ensure that console errors are generated, and we can surface these to the
64
- * end user in a responsible manner so that they can update their plugins and theme,
65
- * or make a decision to switch to other plugin/theme if no updates are available.
66
- */
67
- public static function replace_scripts( $scripts ) {
68
- $assets_url = plugins_url( 'js/', __FILE__ );
69
-
70
- self::set_script( $scripts, 'jquery-migrate', $assets_url . 'jquery-migrate-1.4.1-wp.js', array(), '1.4.1-wp' );
71
- self::set_script( $scripts, 'jquery', false, array( 'jquery-core', 'jquery-migrate' ), '1.12.4-wp' );
72
- }
73
-
74
- /**
75
- * Register the deprecation notice capture handler script.
76
- */
77
- public static function register_scripts() {
78
- wp_register_script( 'jquery-migrate-deprecation-notices', plugins_url( 'js/deprecation-notice.js', __FILE__ ), array( 'jquery' ), false, true );
79
- }
80
-
81
- /**
82
- * Output/print the deprecation notice script. Needs to be last in the footer.
83
- */
84
- public static function print_scripts() {
85
- wp_print_scripts( 'jquery-migrate-deprecation-notices' );
86
- }
87
-
88
- /**
89
- * HTML for jQuery Migrate deprecated notices.
90
- *
91
- * This notice is only displayed if there are JS migration deprecation notices
92
- * for scripts loaded on the current screen.
93
- *
94
- * @since 1.0.0
95
- */
96
- public static function deprecated_scripts_notice() {
97
- ?>
98
-
99
- <div class="notice notice-error is-dismissible jquery-migrate-deprecation-notice hidden">
100
- <h2><?php _ex( 'jQuery Migrate Helper', 'Admin notice header', 'enable-jquery-migrate-helper' ); ?> &mdash; <?php _ex( 'Warnings encountered', 'enable-jquery-migrate-helper' ); ?></h2>
101
- <p><?php _e( 'This page generated the following warnings:', 'enable-jquery-migrate-helper' ); ?></p>
102
-
103
- <ol class="jquery-migrate-deprecation-list"></ol>
104
-
105
- <p>
106
- <?php _e( 'Please make sure you are using the latest version of all of your plugins, and your theme.', 'enable-jquery-migrate-helper' ); ?>
107
- <?php _e( 'If you are, you may want to ask the developers of the code mentioned in the warnings for an update.', 'enable-jquery-migrate-helper' ); ?>
108
- </p>
109
- </div>
110
-
111
- <?php
112
- }
113
-
114
- /**
115
- * HTML for the Dashboard notice.
116
- */
117
- public static function dashboard_notice() {
118
- // Show again in two seeks if the user has dismissed this notice.
119
- $is_dismissed = get_option( '_jquery_migrate_dismissed_notice', false );
120
- $recurrence = 2 * WEEK_IN_SECONDS;
121
-
122
- // If the message has been dismissed, and it has been less than two weeks since it was seen,
123
- // then skip showing the admin notice for now.
124
- if ( false !== $is_dismissed && $is_dismissed > ( time() - $recurrence ) ) {
125
- return;
126
- }
127
-
128
- ?>
129
-
130
- <div class="notice notice-warning is-dismissible jquery-migrate-dashboard-notice">
131
- <h2><?php _ex( 'jQuery Migrate Helper', 'Admin notice header', 'enable-jquery-migrate-helper' ); ?></h2>
132
- <p>
133
- <?php _e( 'Right now you are using the Enable jQuery Migrate Helper plugin to enable support for old JavaScript code that uses deprecated functions in the jQuery JavaScript library.', 'enable-jquery-migrate-helper' ); ?>
134
- <?php _e( 'You will see warnings about these functions while using WordPress administration, but they will not be shown on the front-end of your site.', 'enable-jquery-migrate-helper' ); ?>
135
- <?php _e( 'To check if there are any warnings generated by your theme please look in the browser console.', 'enable-jquery-migrate-helper' ); ?>
136
- </p>
137
-
138
- <p>
139
- <?php _e( 'If you get warnings, you should check the theme or plugin that generated them for an update. There will very likely be one you can install.', 'enable-jquery-migrate-helper' ); ?>
140
- <?php _e( 'When you have updated your plugins and themes, and there are no more warnings, please deactivate Enable jQuery Migrate Helper.', 'enable-jquery-migrate-helper' ); ?>
141
- </p>
142
-
143
- <p>
144
- <?php _e( '* A script, a file, or some other piece of code is deprecated when its developers are in the process of replacing it with more modern code or removing it entirely.', 'enable-jquery-migrate-helper' ); ?>
145
- </p>
146
- <?php wp_nonce_field( 'jquery-migrate-notice', 'jquery-migrate-notice-nonce', false ); ?>
147
- </div>
148
-
149
- <?php
150
- }
151
-
152
- public static function admin_notices() {
153
- // Show only to admins.
154
- if ( ! current_user_can( 'update_plugins' ) ) {
155
- return;
156
- }
157
-
158
- if ( get_current_screen()->id === 'dashboard' ) {
159
- self::dashboard_notice();
160
- }
161
-
162
- self::deprecated_scripts_notice();
163
- }
164
-
165
- public static function admin_notices_dismiss() {
166
- if ( empty( $_POST['dismiss-notice-nonce'] ) || ! current_user_can( 'manage_options' ) ) {
167
- return;
168
- }
169
-
170
- if ( ! wp_verify_nonce( $_POST['dismiss-notice-nonce'], 'jquery-migrate-notice' ) ) {
171
- return;
172
- }
173
-
174
- update_option( '_jquery_migrate_dismissed_notice', time() );
175
- }
176
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Class jQuery_Migrate_Helper,
4
+ *
5
+ */
6
+
7
+ if ( ! defined( 'ABSPATH' ) ) {
8
+ die( 'Invalid request.' );
9
+ }
10
+
11
+ class jQuery_Migrate_Helper {
12
+
13
+ private function __construct() {}
14
+
15
+ public static function init_actions() {
16
+ // To be able to replace the src, scripts should not be concatenated.
17
+ if ( ! defined( 'CONCATENATE_SCRIPTS' ) ) {
18
+ define( 'CONCATENATE_SCRIPTS', false );
19
+ }
20
+
21
+ $GLOBALS['concatenate_scripts'] = false;
22
+
23
+ add_action( 'wp_default_scripts', array( __CLASS__, 'replace_scripts' ), -1 );
24
+
25
+ // We need our own script for displaying warnings to run as late as possible.
26
+ // Print it separately after the footer scripts.
27
+ add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ) );
28
+ add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register_scripts' ) );
29
+ add_action( 'admin_print_footer_scripts', array( __CLASS__, 'print_scripts' ), 100 );
30
+ add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts_frontend' ), 100 );
31
+
32
+ add_action( 'admin_bar_menu', array( __CLASS__, 'admin_bar_menu' ), 100 );
33
+ add_action( 'init', array( __CLASS__, 'maybe_show_admin_notices' ) );
34
+
35
+ add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) );
36
+ add_action( 'wp_ajax_jquery-migrate-dismiss-notice', array( __CLASS__, 'admin_notices_dismiss' ) );
37
+ add_action( 'wp_ajax_jquery-migrate-log-notice', array( __CLASS__, 'log_migrate_notice' ) );
38
+ }
39
+
40
+ // Pre-register scripts on 'wp_default_scripts' action, they won't be overwritten by $wp_scripts->add().
41
+ private static function set_script( $scripts, $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
42
+ $script = $scripts->query( $handle, 'registered' );
43
+
44
+ if ( $script ) {
45
+ // If already added
46
+ $script->src = $src;
47
+ $script->deps = $deps;
48
+ $script->ver = $ver;
49
+ $script->args = $in_footer;
50
+
51
+ unset( $script->extra['group'] );
52
+
53
+ if ( $in_footer ) {
54
+ $script->add_data( 'group', 1 );
55
+ }
56
+ } else {
57
+ // Add the script
58
+ if ( $in_footer ) {
59
+ $scripts->add( $handle, $src, $deps, $ver, 1 );
60
+ } else {
61
+ $scripts->add( $handle, $src, $deps, $ver );
62
+ }
63
+ }
64
+ }
65
+
66
+ /*
67
+ * Enqueue jQuery migrate, and force it to be the development version.
68
+ *
69
+ * This will ensure that console errors are generated, and we can surface these to the
70
+ * end user in a responsible manner so that they can update their plugins and theme,
71
+ * or make a decision to switch to other plugin/theme if no updates are available.
72
+ */
73
+ public static function replace_scripts( $scripts ) {
74
+ $assets_url = plugins_url( 'js/', __FILE__ );
75
+
76
+ self::set_script( $scripts, 'jquery-migrate', $assets_url . 'jquery-migrate-1.4.1-wp.js', array(), '1.4.1-wp' );
77
+ self::set_script( $scripts, 'jquery', false, array( 'jquery-core', 'jquery-migrate' ), '1.12.4-wp' );
78
+ }
79
+
80
+ /**
81
+ * Register the deprecation notice capture handler script.
82
+ */
83
+ public static function register_scripts() {
84
+ wp_register_script( 'jquery-migrate-deprecation-notices', plugins_url( 'js/deprecation-notice.js', __FILE__ ), array( 'jquery' ), false, true );
85
+
86
+ wp_localize_script(
87
+ 'jquery-migrate-deprecation-notices',
88
+ 'JQMH',
89
+ array(
90
+ 'ajaxurl' => admin_url( 'admin-ajax.php' ),
91
+ 'report_nonce' => wp_create_nonce( 'jquery-migrate-report-deprecation' ),
92
+ 'backend' => is_admin(),
93
+ 'plugin_slug' => dirname( plugin_basename( __FILE__ ) ),
94
+ )
95
+ );
96
+ }
97
+
98
+ /**
99
+ * Output/print the deprecation notice script. Needs to be last in the footer.
100
+ */
101
+ public static function print_scripts() {
102
+ wp_print_scripts( 'jquery-migrate-deprecation-notices' );
103
+ }
104
+
105
+ public static function enqueue_scripts_frontend() {
106
+ // Only load the asset for users who can act on them.
107
+ if ( ! current_user_can( 'manage_options' ) ) {
108
+ return;
109
+ }
110
+
111
+ wp_enqueue_script( 'jquery-migrate-deprecation-notices' );
112
+ }
113
+
114
+ /**
115
+ * HTML for jQuery Migrate deprecated notices.
116
+ *
117
+ * This notice is only displayed if there are JS migration deprecation notices
118
+ * for scripts loaded on the current screen.
119
+ *
120
+ * @since 1.0.0
121
+ */
122
+ public static function deprecated_scripts_notice() {
123
+ // If the message has been dismissed, and it has been less than two weeks since it was seen,
124
+ // then skip displaying the deprecation lister for now.
125
+ if ( ! self::show_deprecated_scripts_notice() ) {
126
+ return;
127
+ }
128
+ ?>
129
+
130
+ <div class="notice notice-error is-dismissible jquery-migrate-dashboard-notice jquery-migrate-deprecation-notice <?php echo ( empty( $logs ) ? 'hidden' : '' ); ?>" data-notice-id="jquery-migrate-deprecation-list">
131
+ <h2><?php _ex( 'jQuery Migrate Helper', 'Admin notice header', 'enable-jquery-migrate-helper' ); ?> &mdash; <?php _ex( 'Warnings encountered', 'enable-jquery-migrate-helper' ); ?></h2>
132
+ <p><?php _e( 'This page generated the following warnings:', 'enable-jquery-migrate-helper' ); ?></p>
133
+
134
+ <ol class="jquery-migrate-deprecation-list"></ol>
135
+
136
+ <p>
137
+ <?php _e( 'Please make sure you are using the latest version of all of your plugins, and your theme.', 'enable-jquery-migrate-helper' ); ?>
138
+ <?php _e( 'If you are, you may want to ask the developers of the code mentioned in the warnings for an update.', 'enable-jquery-migrate-helper' ); ?>
139
+ </p>
140
+
141
+ <?php wp_nonce_field( 'jquery-migrate-deprecation-list', 'jquery-migrate-deprecation-list-nonce', false ); ?>
142
+ </div>
143
+
144
+ <?php
145
+ }
146
+
147
+ public static function show_deprecated_scripts_notice() {
148
+ return false === get_option( '_jquery_migrate_deprecations_dismissed_notice', false );
149
+ }
150
+
151
+ public static function previous_deprecation_notices() {
152
+ if ( ! isset( $_GET['show-jqmh-previous-notices'] ) ) {
153
+ return;
154
+ }
155
+
156
+ $logs = get_option( 'jqmh_logs', array() );
157
+ ?>
158
+ <div class="notice notice-error is-dismissible jquery-migrate-dashboard-notice jquery-migrate-previous-deprecations" data-notice-id="jquery-migrate-previous-deprecations">
159
+ <h2><?php _e( 'Previously logged deprecation notices', 'enable-jquery-migrate-helper' ); ?></h2>
160
+
161
+ <p>
162
+ <?php _e( 'The following are deprecations logged from the front-end of your site, or while the deprecation box was disabled.', 'enable-jquery-migrate-helper' ); ?>
163
+ </p>
164
+
165
+ <table class="widefat striped">
166
+ <thead>
167
+ <tr>
168
+ <th><?php _ex( 'Time', 'Admin deprecation notices', 'enable-jquery-migrate-helper' ); ?></th>
169
+ <th><?php _ex( 'Notice', 'Admin deprecation notices', 'enable-jquery-migrate-helper' ); ?></th>
170
+ <th><?php _ex( 'Page', 'Admin deprecation notices', 'enable-jquery-migrate-helper' ); ?></th>
171
+ </tr>
172
+ </thead>
173
+
174
+ <tbody>
175
+ <?php if ( empty( $logs ) ) : ?>
176
+ <tr>
177
+ <td colspan="3">
178
+ <?php _e( 'No deprecations have been logged', 'enable-jquery-migrate-helper' ); ?>
179
+ </td>
180
+ </tr>
181
+ <?php endif; ?>
182
+
183
+ <?php foreach ( $logs as $log ) : ?>
184
+
185
+ <tr>
186
+ <td><?php echo esc_html( $log['registered'] ); ?></td>
187
+ <td><?php echo esc_html( $log['notice'] ); ?></td>
188
+ <td><?php echo esc_html( $log['page'] ); ?></td>
189
+ </tr>
190
+
191
+ <?php endforeach; ?>
192
+ </tbody>
193
+
194
+ <tfoot>
195
+ <tr>
196
+ <th><?php _ex( 'Time', 'Admin deprecation notices', 'enable-jquery-migrate-helper' ); ?></th>
197
+ <th><?php _ex( 'Notice', 'Admin deprecation notices', 'enable-jquery-migrate-helper' ); ?></th>
198
+ <th><?php _ex( 'Page', 'Admin deprecation notices', 'enable-jquery-migrate-helper' ); ?></th>
199
+ </tr>
200
+ </tfoot>
201
+ </table>
202
+
203
+ <?php wp_nonce_field( 'jquery-migrate-previous-deprecations', 'jquery-migrate-previous-deprecations-nonce', false ); ?>
204
+
205
+ <p></p>
206
+ </div>
207
+
208
+ <?php
209
+ }
210
+
211
+ /**
212
+ * HTML for the Dashboard notice.
213
+ */
214
+ public static function dashboard_notice() {
215
+ if ( ! self::show_dashboard_notice() ) {
216
+ return;
217
+ }
218
+ ?>
219
+
220
+ <div class="notice notice-warning is-dismissible jquery-migrate-dashboard-notice" data-notice-id="jquery-migrate-notice">
221
+ <h2><?php _ex( 'jQuery Migrate Helper', 'Admin notice header', 'enable-jquery-migrate-helper' ); ?></h2>
222
+ <p>
223
+ <?php _e( 'Right now you are using the Enable jQuery Migrate Helper plugin to enable support for old JavaScript code that uses deprecated functions in the jQuery JavaScript library.', 'enable-jquery-migrate-helper' ); ?>
224
+ <br>
225
+ <strong>
226
+ <?php _e( 'Please note that this is a temporary solution which will only work between WordPress 5.5.0 and 5.6.0 is, and is not meant as a permanent fix for unsupported code.', 'enable-jquery-migrate-helper' ); ?>
227
+ </strong>
228
+ </p>
229
+
230
+ <p>
231
+ <?php _e( 'If you get warnings, you should check the theme or plugin that generated them for an update. There will very likely be one you can install.', 'enable-jquery-migrate-helper' ); ?>
232
+ <?php _e( 'When you have updated your plugins and themes, and there are no more warnings, please deactivate Enable jQuery Migrate Helper.', 'enable-jquery-migrate-helper' ); ?>
233
+ </p>
234
+
235
+ <p>
236
+ <?php _e( '* A script, a file, or some other piece of code is deprecated when its developers are in the process of replacing it with more modern code or removing it entirely.', 'enable-jquery-migrate-helper' ); ?>
237
+ </p>
238
+ <?php wp_nonce_field( 'jquery-migrate-notice', 'jquery-migrate-notice-nonce', false ); ?>
239
+ </div>
240
+
241
+ <?php
242
+ }
243
+
244
+ public static function show_dashboard_notice() {
245
+ // Show again in two weeks if the user has dismissed this notice.
246
+ $is_dismissed = get_option( '_jquery_migrate_dismissed_notice', false );
247
+ $recurrence = 1 * WEEK_IN_SECONDS;
248
+
249
+ // If the message has been dismissed, and it has been less than two weeks since it was seen,
250
+ // then skip showing the admin notice for now.
251
+ if ( false !== $is_dismissed && $is_dismissed > ( time() - $recurrence ) ) {
252
+ return false;
253
+ }
254
+
255
+ return true;
256
+ }
257
+
258
+ /**
259
+ * Display a dashboard notice if no deprecations have been logged in a while.
260
+ *
261
+ * This encourages users to remove the plugin when no longer needed, this will help gauge the jQuery
262
+ * migrations, and also remove the potential false-positive reports of jQuery issues caused by
263
+ * an unrelated core bug fixed in WordPress 5.5.1 that inflates the plugin numbers.
264
+ */
265
+ public static function plugin_obsolete_message() {
266
+ $last_log = get_option( 'jqmh_last_log_time', null );
267
+ $recurrence = 1 * WEEK_IN_SECONDS;
268
+
269
+ /*
270
+ * If no log time is recorded, this is likely a recently updated plugin, so set the value to now,
271
+ * to give a buffer, and avoid showing the notice when we have no data telling if its needed or not.
272
+ */
273
+ if ( null === $last_log || self::logged_migration_notice_count() > 0 ) {
274
+ update_option( 'jqmh_last_log_time', time() );
275
+ return;
276
+ }
277
+
278
+ if ( $last_log > ( time() - $recurrence ) ) {
279
+ return;
280
+ }
281
+ ?>
282
+
283
+ <div class="notice notice-warning is-dismissible jquery-migrate-dashboard-notice" data-notice-id="jquery-migrate-no-deprecations-notice">
284
+ <h2><?php _ex( 'jQuery Migrate Helper', 'Admin notice header', 'enable-jquery-migrate-helper' ); ?></h2>
285
+
286
+ <p>
287
+ <?php _e( 'No deprecations have been logged on this site in a while, you may no longer need this plugins.', 'enable-jquery-migrate-helper' ); ?>
288
+ </p>
289
+
290
+ <p>
291
+ <?php _e( 'Please keep in mind that only notices on the public facing part of yoru site, or if you have disabled the display on the back end, will be logged and accounted for.', 'enable-jquery-migrate-helper' ); ?>
292
+ </p>
293
+
294
+ <p>
295
+ <?php _e( 'This means you should still check that things work as expected after the plugin is disabled, and if you know there have been warnings in the admin pages, you may still need to reach out to the plugin or theme authors affected.', 'enable-jquery-migrate-helper' ); ?>
296
+ </p>
297
+
298
+ <?php if ( is_wp_version_compatible( '5.5.1' ) ) : ?>
299
+
300
+ <p>
301
+ <strong>
302
+ <?php _e( 'You are using a WordPress version prior to 5.5.1, this plugin also helps with a bug found in WordPress 5.5.0, you should update to version 5.5.1, or later, before the plugin is deactivated.', 'enable-jquery-migarte-helper' ); ?>
303
+ </strong>
304
+ </p>
305
+
306
+ <?php endif; ?>
307
+
308
+ <?php wp_nonce_field( 'jquery-migrate-no-deprecations-notice', 'jquery-migrate-no-deprecations-notice-nonce', false ); ?>
309
+ </div>
310
+
311
+ <?php
312
+ }
313
+
314
+ public static function admin_notices() {
315
+ // Show only to admins.
316
+ if ( ! current_user_can( 'update_plugins' ) ) {
317
+ return;
318
+ }
319
+
320
+ if ( get_current_screen()->id === 'dashboard' ) {
321
+ self::dashboard_notice();
322
+
323
+ self::previous_deprecation_notices();
324
+
325
+ self::plugin_obsolete_message();
326
+ }
327
+
328
+ self::deprecated_scripts_notice();
329
+ }
330
+
331
+ public static function log_migrate_notice() {
332
+ if ( ! current_user_can( 'manage_options' ) ) {
333
+ status_header( 403 );
334
+ die();
335
+ }
336
+
337
+ if ( ! wp_verify_nonce( $_POST['nonce'], 'jquery-migrate-report-deprecation' ) ) {
338
+ status_header( 406, 'Invalid nonce' );
339
+ die();
340
+ }
341
+
342
+ $required_fields = array(
343
+ 'notice',
344
+ );
345
+
346
+ foreach ( $required_fields as $required_field ) {
347
+ if ( ! isset( $_POST[ $required_field ] ) ) {
348
+ status_header( 400, 'Missing required fields' );
349
+ die();
350
+ }
351
+ }
352
+
353
+ $logs = get_option( 'jqmh_logs', array() );
354
+
355
+ $deprecation_data = array(
356
+ 'notice' => wp_kses( $_POST['notice'], array() ),
357
+ );
358
+
359
+ /*
360
+ * Creating a hash of the deprecation data lets us ensure it is only reported once, to avoid
361
+ * filling the database with duplicates on busy sites.
362
+ */
363
+ $deprecation_hash = md5( wp_json_encode( $deprecation_data ) );
364
+
365
+ if ( ! isset( $logs[ $deprecation_hash ] ) ) {
366
+ $logs[ $deprecation_hash ] = array_merge( array(
367
+ 'registered' => date_i18n( 'Y-m-d H:i:s' ),
368
+ 'page' => ( isset( $_POST['url'] ) ? esc_url_raw( $_POST['url'] ) : '' ),
369
+ 'backend' => isset( $_POST['backend'] ) && $_POST['backend'],
370
+ ), $deprecation_data );
371
+
372
+ update_option( 'jqmh_logs', $logs );
373
+ update_option( 'jqmh_last_log_time', time() );
374
+ }
375
+
376
+ wp_send_json_success();
377
+ }
378
+
379
+ /**
380
+ * Check if any errors have been logged to the database.
381
+ *
382
+ * @return int|null
383
+ */
384
+ public static function logged_migration_notice_count() {
385
+ if ( ! current_user_can( 'manage_options' ) ) {
386
+ return null;
387
+ }
388
+
389
+ $logs = get_option( 'jqmh_logs', array() );
390
+
391
+ return count( $logs );
392
+ }
393
+
394
+ /**
395
+ * Handle ajax requests to dismiss a notice, and remember the dismissal.
396
+ *
397
+ * @return void
398
+ */
399
+ public static function admin_notices_dismiss() {
400
+ if ( empty( $_POST['dismiss-notice-nonce'] ) || ! current_user_can( 'manage_options' ) ) {
401
+ return;
402
+ }
403
+
404
+ if ( ! wp_verify_nonce( $_POST['dismiss-notice-nonce'], $_POST['notice'] ) ) {
405
+ return;
406
+ }
407
+
408
+ switch( $_POST['notice'] ) {
409
+ case 'jquery-migrate-deprecation-list':
410
+ update_option( '_jquery_migrate_deprecations_dismissed_notice', time() );
411
+ break;
412
+
413
+ case 'jquery-migrate-previous-deprecations':
414
+ delete_option( 'jqmh_logs' );
415
+ break;
416
+
417
+ case 'jquery-migrate-notice':
418
+ update_option( '_jquery_migrate_dismissed_notice', time() );
419
+ break;
420
+
421
+ case 'jquery-migrate-no-deprecations-notice':
422
+ update_option( 'jqmh_last_log_time', time() );
423
+ break;
424
+ }
425
+ }
426
+
427
+ /**
428
+ * Add this plugin to the admin bar as a menu item.
429
+ *
430
+ * This entry allows users to re-surface previously hidden notices from the plugin,
431
+ * and also allows for alerting of issues detected in the frontend, where injecting
432
+ * any notice isn't as elegant.
433
+ *
434
+ * @param $wp_menu
435
+ */
436
+ public static function admin_bar_menu( $wp_menu ) {
437
+ // Show only to those with the right capabilities.
438
+ if ( ! current_user_can( 'manage_options' ) ) {
439
+ return;
440
+ }
441
+
442
+ $deprecation_count = self::logged_migration_notice_count();
443
+
444
+ $wp_menu->add_menu(
445
+ array(
446
+ 'id' => 'enable-jquery-migrate-helper',
447
+ 'title' => sprintf(
448
+ // translators: %s: Parenthesis with issue count.
449
+ __( 'jQuery Migrate %s', 'enable-jquery-migrate-helper' ),
450
+ sprintf(
451
+ '<span class="count-wrapper" style="' . ( $deprecation_count > 0 ? '' : 'display:none;' ) . '">%s</span>',
452
+ sprintf(
453
+ // translators: 1: The amount of existing issues. 2: Issues discovered on the currently loaded page.
454
+ __( '(Previously known: %1$d Discovered on this page: %2$s)', 'enable-jquery-migrate-helper' ),
455
+ $deprecation_count,
456
+ sprintf(
457
+ '<span class="count">%d</span>',
458
+ 0
459
+ )
460
+ )
461
+ )
462
+ ),
463
+ 'href' => '#',
464
+ )
465
+ );
466
+
467
+ if ( ! self::show_deprecated_scripts_notice() ) {
468
+ $wp_menu->add_node(
469
+ array(
470
+ 'id' => 'enable-jquery-migrate-helper-show-deprecations',
471
+ 'title' => __( 'Display live deprecation notices', 'enable-jquery-migrate-helper' ),
472
+ 'parent' => 'enable-jquery-migrate-helper',
473
+ 'href' => get_admin_url( null, '?show-jqmh-deprecations' ),
474
+ )
475
+ );
476
+ }
477
+
478
+ if ( ! self::show_dashboard_notice() ) {
479
+ $wp_menu->add_node(
480
+ array(
481
+ 'id' => 'enable-jquery-migrate-helper-show-notices',
482
+ 'title' => __( 'Display plugin information notice', 'enable-jquery-migrate-helper' ),
483
+ 'parent' => 'enable-jquery-migrate-helper',
484
+ 'href' => get_admin_url( null, '?show-jqmh-notice' ),
485
+ )
486
+ );
487
+ }
488
+
489
+ $wp_menu->add_node(
490
+ array(
491
+ 'id' => 'enable-jquery-migrate-helper-show-previous-deprecations',
492
+ 'title' => __( 'Show a list of logged deprecations', 'enable-jquery-migrate-helper' ),
493
+ 'parent' => 'enable-jquery-migrate-helper',
494
+ 'href' => get_admin_url( null, '?show-jqmh-previous-notices' ),
495
+ )
496
+ );
497
+ }
498
+
499
+ public static function maybe_show_admin_notices() {
500
+ if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
501
+ return;
502
+ }
503
+
504
+ if ( isset( $_GET['show-jqmh-deprecations'] ) ) {
505
+ delete_option( '_jquery_migrate_deprecations_dismissed_notice' );
506
+
507
+ add_action( 'admin_notices', function() {
508
+ ?>
509
+ <div class="notice notice-success is-dismissible">
510
+ <?php _e( 'Live deprecation notices for jQuery Migrate have been enabled, they will show up in the admin interface when a notice is discovered.', 'enable-jquery-migrate-helper' ); ?>
511
+ </div>
512
+ <?php
513
+ } );
514
+ }
515
+
516
+ if ( isset( $_GET['show-jqmh-notice'] ) ) {
517
+ delete_option( '_jquery_migrate_dismissed_notice' );
518
+ }
519
+ }
520
+ }
enable-jquery-migrate-helper.php CHANGED
@@ -1,25 +1,25 @@
1
- <?php
2
- /*
3
- Plugin Name: Enable jQuery Migrate Helper
4
- Plugin URI: https://wordpress.org/plugins/enable-jquery-migrate-helper
5
- Description: Enable support for old and outdated plugins and themes during a jQuery update transitional phase.
6
- Version: 1.0.1
7
- Requires at least: 5.4
8
- Tested up to: 5.5
9
- Requires PHP: 5.6
10
- Author: The WordPress Team
11
- Author URI: https://wordpress.org
12
- Contributors: wordpressdotorg, clorith, azaozz
13
- License: GPLv2
14
- License URI: http://www.gnu.org/licenses/gpl-2.0.html
15
- Text Domain: enable-jquery-migrate-helper
16
- */
17
-
18
- if ( ! defined( 'ABSPATH' ) ) {
19
- die( 'Invalid request.' );
20
- }
21
-
22
- if ( version_compare( $GLOBALS['wp_version'], '5.6-alpha', '<' ) && ! class_exists( 'jQuery_Migrate_Helper' ) ) {
23
- include_once __DIR__ . '/class-jquery-migrate-helper.php';
24
- add_action( 'plugins_loaded', array( 'jQuery_Migrate_Helper', 'init_actions' ) );
25
- }
1
+ <?php
2
+ /*
3
+ Plugin Name: Enable jQuery Migrate Helper
4
+ Plugin URI: https://wordpress.org/plugins/enable-jquery-migrate-helper
5
+ Description: Enable support for old and outdated plugins and themes during a jQuery update transitional phase.
6
+ Version: 1.1.0
7
+ Requires at least: 5.4
8
+ Tested up to: 5.5
9
+ Requires PHP: 5.6
10
+ Author: The WordPress Team
11
+ Author URI: https://wordpress.org
12
+ Contributors: wordpressdotorg, clorith, azaozz
13
+ License: GPLv2
14
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
15
+ Text Domain: enable-jquery-migrate-helper
16
+ */
17
+
18
+ if ( ! defined( 'ABSPATH' ) ) {
19
+ die( 'Invalid request.' );
20
+ }
21
+
22
+ if ( version_compare( $GLOBALS['wp_version'], '5.6-alpha', '<' ) && ! class_exists( 'jQuery_Migrate_Helper' ) ) {
23
+ include_once __DIR__ . '/class-jquery-migrate-helper.php';
24
+ add_action( 'plugins_loaded', array( 'jQuery_Migrate_Helper', 'init_actions' ) );
25
+ }
js/deprecation-notice.js CHANGED
@@ -1,64 +1,157 @@
1
- /**
2
- * Show all jQuery Migrate warnings in the UI.
3
- */
4
- jQuery( document ).ready( function( $ ) {
5
- const notice = $( '.notice.jquery-migrate-deprecation-notice' );
6
- const warnings = jQuery.migrateWarnings;
7
-
8
- /**
9
- * Filter the trace, return the first URI that is to a plugin or theme script.
10
- */
11
- function getPluginSlugFromTrace( trace ) {
12
- let traceLines = trace.split( '\n' ),
13
- match = null;
14
-
15
- // Loop over each line in the stack trace
16
- traceLines.forEach( function( line ) {
17
- if ( ! line ) {
18
- return;
19
- }
20
-
21
- // Remove cache-busting.
22
- line = line.split( '?' )[0];
23
-
24
- // The first few lines are going to be references to the jquery-migrate script.
25
- // The first instance that is not one of them is probably a valid plugin or theme.
26
- if (
27
- ! match &&
28
- line.indexOf( '/jquery-migrate-helper/js' ) === -1 &&
29
- line.indexOf( '/enable-jquery-migrate-helper/js' ) === -1 &&
30
- ( line.indexOf( '/plugins/' ) > -1 || line.indexOf( '/themes/' ) > -1 )
31
- ) {
32
- match = line.replace( /.*?http/, 'http' );
33
- }
34
- } );
35
-
36
- // If the stack trace did not contain a matching plugin or theme, just return a null value.
37
- return match;
38
- }
39
-
40
- if ( notice.length && warnings.length ) {
41
- const list = notice.find( '.jquery-migrate-deprecation-list' );
42
-
43
- notice.show();
44
-
45
- warnings.forEach( function( entry ) {
46
- const trace = getPluginSlugFromTrace( entry.trace );
47
- let message = trace ? trace + ': ' : '';
48
-
49
- message += entry.warning;
50
- list.append( $( '<li></li>' ).text( message ) );
51
- } );
52
- }
53
-
54
- // Add handler for dismissing of the dashboard notice.
55
- $( document ).on( 'click', '.jquery-migrate-dashboard-notice .notice-dismiss', function() {
56
- $.post( {
57
- url: window.ajaxurl,
58
- data: {
59
- action: 'jquery-migrate-dismiss-notice',
60
- 'dismiss-notice-nonce': $( '#jquery-migrate-notice-nonce' ).val(),
61
- },
62
- } );
63
- } );
64
- } );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * Show all jQuery Migrate warnings in the UI.
3
+ */
4
+ jQuery( document ).ready( function( $ ) {
5
+ const notice = $( '.notice.jquery-migrate-deprecation-notice' );
6
+ const warnings = jQuery.migrateWarnings;
7
+ const adminbar = $( '#wp-admin-bar-enable-jquery-migrate-helper' );
8
+ const countWrapper = $( '.count-wrapper', adminbar );
9
+
10
+ /**
11
+ * Filter the trace, return the first URI that is to a plugin or theme script.
12
+ */
13
+ function getPluginSlugFromTrace( trace ) {
14
+ let traceLines = trace.split( '\n' ),
15
+ match = null;
16
+
17
+ // Loop over each line in the stack trace
18
+ traceLines.forEach( function( line ) {
19
+ if ( ! line ) {
20
+ return;
21
+ }
22
+
23
+ // Remove cache-busting.
24
+ line = line.split( '?' )[0];
25
+
26
+ // The first few lines are going to be references to the jquery-migrate script.
27
+ // The first instance that is not one of them is probably a valid plugin or theme.
28
+ if (
29
+ ! match &&
30
+ line.indexOf( '/' + JQMH.plugin_slug + '/js' ) === -1 &&
31
+ ( line.indexOf( '/plugins/' ) > -1 || line.indexOf( '/themes/' ) > -1 )
32
+ ) {
33
+ match = line.replace( /.*?http/, 'http' );
34
+ }
35
+ } );
36
+
37
+ // If the stack trace did not contain a matching plugin or theme, just return a null value.
38
+ return match;
39
+ }
40
+
41
+ /**
42
+ * Update the count of deprecations found on this page.
43
+ *
44
+ * @param count
45
+ */
46
+ function setAdminBarCount( count ) {
47
+ if ( ! adminbar.length ) {
48
+ return;
49
+ }
50
+
51
+ if ( ! countWrapper.is( ':visible' ) ) {
52
+ countWrapper.show();
53
+
54
+ countWrapperVisibility();
55
+ }
56
+
57
+ $( '.count', adminbar ).text( count );
58
+ }
59
+
60
+ /**
61
+ * Set the admin bar visibility level based on the warning counters.
62
+ */
63
+ function countWrapperVisibility() {
64
+ if ( countWrapper.is( ':visible' ) ) {
65
+ adminbar
66
+ .css( 'background-color', '#be4400' )
67
+ .css( 'color', '#eeeeee' );
68
+ } else {
69
+ adminbar
70
+ .css( 'background-color', '' )
71
+ .css( 'color', '' );
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Append the deprecation to the admin dashbaord, if applicable.
77
+ *
78
+ * @param message
79
+ */
80
+ function appendNoticeDisplay( message ) {
81
+ const list = notice.find( '.jquery-migrate-deprecation-list' );
82
+
83
+ if ( ! notice.length ) {
84
+ return;
85
+ }
86
+
87
+ if ( ! notice.is( ':visible' ) ) {
88
+ notice.show();
89
+ }
90
+
91
+ list.append( $( '<li></li>' ).text( message ) );
92
+ }
93
+
94
+ /**
95
+ * Try to log the deprecation for the admin area.
96
+ *
97
+ * @param message
98
+ */
99
+ function reportDeprecation( message ) {
100
+ // Do not write to the logfile if this is the backend, and the notices are written to the screen.
101
+ if ( JQMH.backend && notice.length ) {
102
+ return;
103
+ }
104
+
105
+ let data = {
106
+ action: 'jquery-migrate-log-notice',
107
+ notice: message,
108
+ nonce: JQMH.report_nonce,
109
+ backend: JQMH.backend,
110
+ url: window.location.href,
111
+ };
112
+
113
+ $.post( {
114
+ url: JQMH.ajaxurl,
115
+ data
116
+ } );
117
+ }
118
+
119
+ if ( warnings.length ) {
120
+ warnings.forEach( function( entry ) {
121
+ const trace = getPluginSlugFromTrace( entry.trace );
122
+ let message = trace ? trace + ': ' : '';
123
+
124
+ message += entry.warning;
125
+
126
+ appendNoticeDisplay( message );
127
+
128
+ reportDeprecation( message );
129
+ } );
130
+
131
+ setAdminBarCount( warnings.length );
132
+ }
133
+
134
+ // Add handler for dismissing of the dashboard notice.
135
+ $( document ).on( 'click', '.jquery-migrate-dashboard-notice .notice-dismiss', function() {
136
+ const $notice = $( this ).closest( '.notice' );
137
+ const notice_id = $notice.data( 'notice-id' );
138
+
139
+ $.post( {
140
+ url: window.ajaxurl,
141
+ data: {
142
+ action: 'jquery-migrate-dismiss-notice',
143
+ 'notice': notice_id,
144
+ 'dismiss-notice-nonce': $( '#' + notice_id + '-nonce' ).val(),
145
+ },
146
+ } );
147
+ } );
148
+
149
+ // When the previous deprecations are dismissed, reset the admin bar log display.
150
+ $( document ).on( 'click', '.jquery-migrate-previous-deprecations .notice-dismiss', function() {
151
+ countWrapper.hide();
152
+ countWrapperVisibility();
153
+ } );
154
+
155
+ // Check if the counter is visible on page load.
156
+ countWrapperVisibility();
157
+ } );
readme.txt CHANGED
@@ -1,36 +1,44 @@
1
- === Enable jQuery Migrate Helper ===
2
- Contributors: wordpressdotorg, clorith, azaozz
3
- Requires at least: 5.4
4
- Tested up to: 5.5
5
- Stable tag: 1.0.1
6
- Requires PHP: 5.6
7
- License: GPLv2 or later
8
- License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
-
10
- == Description ==
11
-
12
- With the update to WordPress 5.5, a migration tool known as `jquery-migrate` will no longer be enabled by default. This may lead to unexpected behaviors in some themes or plugins who run older code.
13
-
14
- This plugin serves as a temporary solution, enabling the migration script for your site to give your plugin and theme authors some more time to update, and test, their code.
15
-
16
- == Frequently Asked Questions ==
17
-
18
- = What does it mean that something is "deprecated" =
19
- A script, a file, or some other piece of code is deprecated when its developers are in the process of replacing it with more modern code or removing it entirely.
20
-
21
- = How do I find and use the browser console =
22
- WordPress.org has an article about [using the browsers console log to diagnose JavaScript errors](https://wordpress.org/support/article/using-your-browser-to-diagnose-javascript-errors/).
23
-
24
- == Installation ==
25
-
26
- 1. Upload to your plugins folder, usually `wp-content/plugins/`.
27
- 2. Activate the plugin on the plugin screen.
28
- 3. That's it! The plugin handles the rest automatically for you.
29
-
30
- == Changelog ==
31
-
32
- = v 1.0.1 =
33
- * Fix one of the admin notices being non-dismissible.
34
-
35
- = v 1.0.0 =
36
- * Initial release.
 
 
 
 
 
 
 
 
1
+ === Enable jQuery Migrate Helper ===
2
+ Contributors: wordpressdotorg, clorith, azaozz
3
+ Requires at least: 5.4
4
+ Tested up to: 5.5
5
+ Stable tag: 1.1.0
6
+ Requires PHP: 5.6
7
+ License: GPLv2 or later
8
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
+
10
+ == Description ==
11
+
12
+ With the update to WordPress 5.5, a migration tool known as `jquery-migrate` will no longer be enabled by default. This may lead to unexpected behaviors in some themes or plugins who run older code.
13
+
14
+ This plugin serves as a temporary solution, enabling the migration script for your site to give your plugin and theme authors some more time to update, and test, their code.
15
+
16
+ == Frequently Asked Questions ==
17
+
18
+ = What does it mean that something is "deprecated" =
19
+ A script, a file, or some other piece of code is deprecated when its developers are in the process of replacing it with more modern code or removing it entirely.
20
+
21
+ = How do I find and use the browser console =
22
+ WordPress.org has an article about [using the browsers console log to diagnose JavaScript errors](https://wordpress.org/support/article/using-your-browser-to-diagnose-javascript-errors/).
23
+
24
+ == Installation ==
25
+
26
+ 1. Upload to your plugins folder, usually `wp-content/plugins/`.
27
+ 2. Activate the plugin on the plugin screen.
28
+ 3. That's it! The plugin handles the rest automatically for you.
29
+
30
+ == Changelog ==
31
+
32
+ = v 1.1.0 =
33
+ * Added option to dismiss deprecation notices in backend
34
+ * Added logging of deprecation notices in the front end
35
+ * Added admin bar entry to show when deprecations occur
36
+ * Added view of logged deprecations
37
+ * Added dashboard notice encouraging users to remove the plugin if no deprecations have been logged in a while (1 week).
38
+ * Changed the time interval between showing the dashboard nag from 2 weeks to 1 week, as WordPress 5.6 comes closer.
39
+
40
+ = v 1.0.1 =
41
+ * Fix one of the admin notices being non-dismissible.
42
+
43
+ = v 1.0.0 =
44
+ * Initial release.
uninstall.php ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * jQuery_Migrate_Helper uninstall methods
4
+ */
5
+
6
+ // if uninstall.php is not called by WordPress, die
7
+ if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
8
+ die;
9
+ }
10
+
11
+ delete_option( 'jqmh_logs' );
12
+ delete_option( 'jqmh_last_log_time' );
13
+ delete_option( '_jquery_migrate_dismissed_notice' );
14
+ delete_option( '_jquery_migrate_deprecations_dismissed_notice' );
15
+ delete_option( '_jquery_migrate_previous_deprecations_dismissed_notice' );