From 8f87703578694a2d953b4b8f9a620a677c4b64a5 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 22 Aug 2021 23:15:48 -0700 Subject: [PATCH] Use plugins_url() to allow Workbox script URLs to be filtered --- ...ass-wp-service-worker-configuration-component.php | 11 ++++++----- wp-includes/service-workers.php | 12 +++++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/wp-includes/components/class-wp-service-worker-configuration-component.php b/wp-includes/components/class-wp-service-worker-configuration-component.php index 16c1dc81e..c36550f94 100644 --- a/wp-includes/components/class-wp-service-worker-configuration-component.php +++ b/wp-includes/components/class-wp-service-worker-configuration-component.php @@ -51,8 +51,9 @@ public function get_priority() { * @return string Script. */ public function get_script() { - $current_scope = wp_service_workers()->get_current_scope(); - $workbox_dir = sprintf( 'wp-includes/js/workbox-v%s/', PWA_WORKBOX_VERSION ); + $current_scope = wp_service_workers()->get_current_scope(); + $workbox_dir_path = sprintf( 'wp-includes/js/workbox-v%s/', PWA_WORKBOX_VERSION ); + $workbox_dir_url = plugins_url( $workbox_dir_path, PWA_PLUGIN_FILE ); $script = ''; if ( SCRIPT_DEBUG ) { @@ -64,17 +65,17 @@ public function get_script() { // Load with importScripts() so that source map is available. $script .= sprintf( "importScripts( %s );\n", - wp_json_encode( PWA_PLUGIN_URL . $workbox_dir . 'workbox-sw.js' ) + wp_json_encode( $workbox_dir_url . 'workbox-sw.js' ) ); } else { // Inline the workbox-sw.js to avoid an additional HTTP request. - $wbjs = file_get_contents( PWA_PLUGIN_DIR . '/' . $workbox_dir . 'workbox-sw.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + $wbjs = file_get_contents( PWA_PLUGIN_DIR . '/' . $workbox_dir_path . 'workbox-sw.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $script .= preg_replace( '://# sourceMappingURL=.+?\.map\s*$:s', '', $wbjs ); } $options = array( 'debug' => SCRIPT_DEBUG, // When true, the dev builds are loaded. Otherwise, the prod builds are used. - 'modulePathPrefix' => PWA_PLUGIN_URL . $workbox_dir, + 'modulePathPrefix' => $workbox_dir_url, ); $script .= sprintf( "workbox.setConfig( %s );\n", wp_json_encode( $options ) ); diff --git a/wp-includes/service-workers.php b/wp-includes/service-workers.php index 31a53ff76..04d4dddfb 100644 --- a/wp-includes/service-workers.php +++ b/wp-includes/service-workers.php @@ -167,11 +167,13 @@ function wp_print_service_workers() { return; } - $workbox_window_src = sprintf( - '%s/wp-includes/js/workbox-v%s/workbox-window.%s.js', - PWA_PLUGIN_URL, - PWA_WORKBOX_VERSION, - SCRIPT_DEBUG ? 'dev' : 'prod' + $workbox_window_src = plugins_url( + sprintf( + 'wp-includes/js/workbox-v%s/workbox-window.%s.js', + PWA_WORKBOX_VERSION, + SCRIPT_DEBUG ? 'dev' : 'prod' + ), + PWA_PLUGIN_FILE ); $register_options = array( 'scope' => $scope,