Skip to content

Commit 470862b

Browse files
committed
Ensure only one downgrade request is sent per page load, and make the identification of a neccesary downgrade process front-end, not back-end
1 parent b664e9e commit 470862b

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

class-jquery-migrate-helper.php

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -236,27 +236,6 @@ public static function downgrade_jquery_version() {
236236
return;
237237
}
238238

239-
// An array of functions that may trigger a jQuery Migrate downgrade.
240-
$deprecated = array(
241-
'andSelf',
242-
'browser',
243-
'live',
244-
'boxModel',
245-
'support.boxModel',
246-
'size',
247-
'swap',
248-
'clean',
249-
'sub',
250-
);
251-
252-
preg_match( '/\)\.(?<function>.+?) is not a function/si', $_POST['msg'], $regex_match );
253-
$function = ( isset( $regex_match['function'] ) ? $regex_match['function'] : null );
254-
255-
// If no function was detected, or it was not an acknowledged deprecated feature, do not downgrade.
256-
if ( null === $function || ! in_array( $function, $deprecated ) ) {
257-
return;
258-
}
259-
260239
update_option( '_jquery_migrate_downgrade_version', 'yes' );
261240
update_option( '_jquery_migrate_has_auto_downgraded', 'yes' );
262241

@@ -282,9 +261,37 @@ public static function fatal_error_handler() {
282261
?>
283262

284263
<script type="text/javascript">
264+
var jQueryMigrateHelperHasSentDowngrade = false;
265+
285266
window.onerror = function( msg, url, line, col, error ) {
267+
// Break out early, do not processing if a downgrade reqeust was already sent.
268+
if ( jQueryMigrateHelperHasSentDowngrade ) {
269+
return true;
270+
}
271+
286272
var xhr = new XMLHttpRequest();
287273
var nonce = '<?php echo esc_js( wp_create_nonce( 'jquery-migrate-automatic-downgrade' ) ); ?>';
274+
var jQueryFunctions = [
275+
'andSelf',
276+
'browser',
277+
'live',
278+
'boxModel',
279+
'support.boxModel',
280+
'size',
281+
'swap',
282+
'clean',
283+
'sub',
284+
];
285+
var match_pattern = /\)\.(.+?) is not a function/;
286+
var erroredFunction = msg.match( match_pattern );
287+
288+
// If there was no matching functions, do not try to downgrade.
289+
if ( typeof erroredFunction !== 'object' || typeof erroredFunction[1] === "undefined" || -1 === jQueryFunctions.indexOf( erroredFunction[1] ) ) {
290+
return true;
291+
}
292+
293+
// Set that we've now attempted a downgrade request.
294+
jQueryMigrateHelperHasSentDowngrade = true;
288295

289296
xhr.open( 'POST', '<?php echo esc_js( admin_url( 'admin-ajax.php' ) ); ?>' );
290297
xhr.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
@@ -308,7 +315,7 @@ public static function fatal_error_handler() {
308315
}
309316
};
310317

311-
xhr.send( encodeURI( 'action=jquery-migrate-downgrade-version&_wpnonce=' + nonce + '&msg=' + msg ) );
318+
xhr.send( encodeURI( 'action=jquery-migrate-downgrade-version&_wpnonce=' + nonce ) );
312319

313320
// Suppress error alerts in older browsers
314321
return true;

0 commit comments

Comments
 (0)