Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compat: Use core-js-url-browser for URL polyfill #20225

Merged
merged 4 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

Expand Down
42 changes: 9 additions & 33 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -25,28 +29,12 @@
* @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;
}

gutenberg_register_vendor_script(
$scripts,
'wp-polyfill-url',
'https://unpkg.com/polyfill-library@3.42.0/polyfills/URL/polyfill.js',
array(),
'3.42.0'
);

did_action( 'init' ) && $scripts->add_inline_script(
'wp-polyfill',
wp_get_script_polyfill(
$scripts,
array(
'\'URL\' in window' => 'wp-polyfill-url',
'window.URL && window.URL.prototype && window.URLSearchParams' => 'wp-polyfill-url',
)
)
);
Expand All @@ -58,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
Expand All @@ -68,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(
Expand Down