From b7a5871e3e789b4167c7a0a41ab86f4ce6580430 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 13 Feb 2020 19:23:36 -0500 Subject: [PATCH 1/4] Compat: Fix URL polyfill test --- lib/compat.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat.php b/lib/compat.php index 9fc297fbf73776..9940409e714964 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -46,7 +46,7 @@ function gutenberg_add_url_polyfill( $scripts ) { wp_get_script_polyfill( $scripts, array( - '\'URL\' in window' => 'wp-polyfill-url', + 'window.URL && window.URL.prototype && window.URLSearchParams' => 'wp-polyfill-url', ) ) ); From 42cbd80339ff331c5d0e295cf587414da6c0a50b Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 13 Feb 2020 19:24:32 -0500 Subject: [PATCH 2/4] Compat: Use core-js-url-browser for URL polyfill --- lib/compat.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compat.php b/lib/compat.php index 9940409e714964..1145a5df7fddd0 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -36,9 +36,9 @@ function gutenberg_add_url_polyfill( $scripts ) { gutenberg_register_vendor_script( $scripts, 'wp-polyfill-url', - 'https://unpkg.com/polyfill-library@3.42.0/polyfills/URL/polyfill.js', + 'https://unpkg.com/core-js-url-browser@3.6.4/url.min.js', array(), - '3.42.0' + '3.6.4' ); did_action( 'init' ) && $scripts->add_inline_script( From fb1a5a64d7094575f9602c7cfb24993f023cb6c4 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 13 Feb 2020 19:25:33 -0500 Subject: [PATCH 3/4] Compat: Respect SCRIPT_DEBUG for minified suffix --- lib/compat.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/compat.php b/lib/compat.php index 1145a5df7fddd0..85135c84d7655e 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -33,10 +33,12 @@ function gutenberg_add_url_polyfill( $scripts ) { return; } + $suffix = SCRIPT_DEBUG ? '' : '.min'; + gutenberg_register_vendor_script( $scripts, 'wp-polyfill-url', - 'https://unpkg.com/core-js-url-browser@3.6.4/url.min.js', + 'https://unpkg.com/core-js-url-browser@3.6.4/url' . $suffix . '.js', array(), '3.6.4' ); From 9fa86b9bba421fc8e09a24b4936048d06a3a98d6 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 6 Mar 2020 11:35:33 -0500 Subject: [PATCH 4/4] Compat: Move polyfill script registration to client-assets.php Only scripts registered in gutenberg_register_vendor_scripts are downloaded as part of the plugin build script. --- lib/client-assets.php | 41 ++++++++++++++++++++++++++++++----------- lib/compat.php | 42 ++++++++---------------------------------- 2 files changed, 38 insertions(+), 45 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 10f36ce3a1d8d1..7bd011170d094c 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -205,17 +205,36 @@ function gutenberg_override_style( &$styles, $handle, $src, $deps = array(), $ve * * @param WP_Scripts $scripts WP_Scripts instance (passed by reference). */ -function gutenberg_register_vendor_scripts( &$scripts ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable - // This function is intentionally left empty. - // - // Scripts such as react and react-dom are expected to be overridden soon, - // and it is preferred to keep this function in place so as not to disturb - // tooling related to the plugin build process. - // - // TODO: Remove phpcs exception in function signature once this function - // regains its use. - // - // See https://github.com/WordPress/gutenberg/pull/20628. +function gutenberg_register_vendor_scripts( &$scripts ) { + $suffix = SCRIPT_DEBUG ? '' : '.min'; + + /* + * This script registration and the corresponding function should be removed + * once the plugin is updated to support WordPress 5.4.0 and newer. + * + * See: `gutenberg_add_url_polyfill` + */ + gutenberg_register_vendor_script( + $scripts, + 'wp-polyfill-url', + 'https://unpkg.com/core-js-url-browser@3.6.4/url' . $suffix . '.js', + array(), + '3.6.4' + ); + + /* + * This script registration and the corresponding function should be removed + * removed once the plugin is updated to support WordPress 5.4.0 and newer. + * + * See: `gutenberg_add_dom_rect_polyfill` + */ + gutenberg_register_vendor_script( + $scripts, + 'wp-polyfill-dom-rect', + 'https://unpkg.com/polyfill-library@3.42.0/polyfills/DOMRect/polyfill.js', + array(), + '3.42.0' + ); } add_action( 'wp_default_scripts', 'gutenberg_register_vendor_scripts' ); diff --git a/lib/compat.php b/lib/compat.php index 85135c84d7655e..67fefa0b102546 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -16,6 +16,10 @@ * * This can be removed when plugin support requires WordPress 5.4.0+. * + * The script registration occurs in `gutenberg_register_vendor_scripts`, which + * should be removed in coordination with this function. + * + * @see gutenberg_register_vendor_scripts * @see https://core.trac.wordpress.org/ticket/49360 * @see https://developer.mozilla.org/en-US/docs/Web/API/URL/URL * @see https://developer.wordpress.org/reference/functions/wp_default_packages_vendor/ @@ -25,24 +29,6 @@ * @param WP_Scripts $scripts WP_Scripts object. */ function gutenberg_add_url_polyfill( $scripts ) { - // Only register polyfill if not already registered. This prevents handling - // in an environment where core has updated to manage the polyfill. This - // depends on the action being handled after default script registration. - $is_polyfill_script_registered = (bool) $scripts->query( 'wp-polyfill-url', 'registered' ); - if ( $is_polyfill_script_registered ) { - return; - } - - $suffix = SCRIPT_DEBUG ? '' : '.min'; - - gutenberg_register_vendor_script( - $scripts, - 'wp-polyfill-url', - 'https://unpkg.com/core-js-url-browser@3.6.4/url' . $suffix . '.js', - array(), - '3.6.4' - ); - did_action( 'init' ) && $scripts->add_inline_script( 'wp-polyfill', wp_get_script_polyfill( @@ -60,6 +46,10 @@ function gutenberg_add_url_polyfill( $scripts ) { * * This can be removed when plugin support requires WordPress 5.4.0+. * + * The script registration occurs in `gutenberg_register_vendor_scripts`, which + * should be removed in coordination with this function. + * + * @see gutenberg_register_vendor_scripts * @see gutenberg_add_url_polyfill * @see https://core.trac.wordpress.org/ticket/49360 * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMRect @@ -70,22 +60,6 @@ function gutenberg_add_url_polyfill( $scripts ) { * @param WP_Scripts $scripts WP_Scripts object. */ function gutenberg_add_dom_rect_polyfill( $scripts ) { - // Only register polyfill if not already registered. This prevents handling - // in an environment where core has updated to manage the polyfill. This - // depends on the action being handled after default script registration. - $is_polyfill_script_registered = (bool) $scripts->query( 'wp-polyfill-dom-rect', 'registered' ); - if ( $is_polyfill_script_registered ) { - return; - } - - gutenberg_register_vendor_script( - $scripts, - 'wp-polyfill-dom-rect', - 'https://unpkg.com/polyfill-library@3.42.0/polyfills/DOMRect/polyfill.js', - array(), - '3.42.0' - ); - did_action( 'init' ) && $scripts->add_inline_script( 'wp-polyfill', wp_get_script_polyfill(