diff --git a/pwa.php b/pwa.php index c377c1654..e74aa8dd6 100644 --- a/pwa.php +++ b/pwa.php @@ -52,7 +52,6 @@ /** WP_Service_Worker_Component Implementation Classes */ require_once PWA_PLUGIN_DIR . '/wp-includes/components/class-wp-service-worker-configuration-component.php'; -require_once PWA_PLUGIN_DIR . '/wp-includes/components/class-wp-service-worker-offline-commenting-component.php'; require_once PWA_PLUGIN_DIR . '/wp-includes/components/class-wp-service-worker-navigation-routing-component.php'; require_once PWA_PLUGIN_DIR . '/wp-includes/components/class-wp-service-worker-precaching-routes-component.php'; require_once PWA_PLUGIN_DIR . '/wp-includes/components/class-wp-service-worker-precaching-routes.php'; diff --git a/wp-includes/class-wp-service-workers.php b/wp-includes/class-wp-service-workers.php index 4fa1e441e..850c8cf08 100644 --- a/wp-includes/class-wp-service-workers.php +++ b/wp-includes/class-wp-service-workers.php @@ -61,7 +61,6 @@ public function __construct() { 'navigation_routing' => new WP_Service_Worker_Navigation_Routing_Component(), 'precaching_routes' => new WP_Service_Worker_Precaching_Routes_Component(), 'caching_routes' => new WP_Service_Worker_Caching_Routes_Component(), - 'offline_commenting' => new WP_Service_Worker_Offline_Commenting_Component(), ); $this->scripts = new WP_Service_Worker_Scripts( $components ); diff --git a/wp-includes/components/class-wp-service-worker-navigation-routing-component.php b/wp-includes/components/class-wp-service-worker-navigation-routing-component.php index 5192f4126..97e68bb46 100644 --- a/wp-includes/components/class-wp-service-worker-navigation-routing-component.php +++ b/wp-includes/components/class-wp-service-worker-navigation-routing-component.php @@ -358,11 +358,19 @@ public function serve( WP_Service_Worker_Scripts $scripts ) { ); } + $scripts->register( + 'wp-offline-commenting', + array( + 'src' => array( $this, 'get_offline_commenting_script' ), + 'deps' => array( 'wp-base-config' ), + ) + ); + $scripts->register( 'wp-navigation-routing', array( 'src' => array( $this, 'get_script' ), - 'deps' => array( 'wp-base-config' ), + 'deps' => array( 'wp-base-config', 'wp-offline-commenting' ), ) ); @@ -485,4 +493,20 @@ public function get_script() { $script ); } + + /** + * Get script for offline commenting requests. + * + * @return string Script. + */ + public function get_offline_commenting_script() { + $script = file_get_contents( PWA_PLUGIN_DIR . '/wp-includes/js/service-worker-offline-commenting.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents + $script = preg_replace( '#/\*\s*global.+?\*/#', '', $script ); + + return str_replace( + array_keys( $this->replacements ), + array_values( $this->replacements ), + $script + ); + } } diff --git a/wp-includes/components/class-wp-service-worker-offline-commenting-component.php b/wp-includes/components/class-wp-service-worker-offline-commenting-component.php deleted file mode 100644 index c3a3cfa1d..000000000 --- a/wp-includes/components/class-wp-service-worker-offline-commenting-component.php +++ /dev/null @@ -1,153 +0,0 @@ -Version ); - if ( $template !== $stylesheet ) { - $revision .= sprintf( ';%s-v%s', $stylesheet, wp_get_theme( $stylesheet )->Version ); - } - - // Ensure the user-specific offline/500 pages are precached, and thet they update when user logs out or switches to another user. - $revision .= sprintf( ';user-%d', get_current_user_id() ); - - if ( ! is_admin() ) { - $offline_error_template_file = pwa_locate_template( array( 'offline.php', 'error.php' ) ); - $offline_error_precache_entry = array( - 'url' => add_query_arg( 'wp_error_template', 'offline', home_url( '/' ) ), - 'revision' => $revision . ';' . md5( $offline_error_template_file . file_get_contents( $offline_error_template_file ) ), // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents - ); - $server_error_template_file = pwa_locate_template( array( '500.php', 'error.php' ) ); - $server_error_precache_entry = array( - 'url' => add_query_arg( 'wp_error_template', '500', home_url( '/' ) ), - 'revision' => $revision . ';' . md5( $server_error_template_file . file_get_contents( $server_error_template_file ) ), // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents - ); - - /** - * Filters what is precached to serve as the offline error response on the frontend. - * - * The URL returned in this array will be precached by the service worker and served as the response when - * the client is offline or their connection fails. To prevent this behavior, this value can be filtered - * to return false. When a theme or plugin makes a change to the response, the revision value in the array - * must be incremented to ensure the URL is re-fetched to store in the precache. - * - * @since 0.2 - * - * @param array|false $entry { - * Offline error precache entry. - * - * @type string $url URL to page that shows the offline error template. - * @type string $revision Revision for the template. This defaults to the template and stylesheet names, with their respective theme versions. - * } - */ - $offline_error_precache_entry = apply_filters( 'wp_offline_error_precache_entry', $offline_error_precache_entry ); - - /** - * Filters what is precached to serve as the internal server error response on the frontend. - * - * The URL returned in this array will be precached by the service worker and served as the response when - * the server returns a 500 internal server error . To prevent this behavior, this value can be filtered - * to return false. When a theme or plugin makes a change to the response, the revision value in the array - * must be incremented to ensure the URL is re-fetched to store in the precache. - * - * @since 0.2 - * - * @param array $entry { - * Server error precache entry. - * - * @type string $url URL to page that shows the server error template. - * @type string $revision Revision for the template. This defaults to the template and stylesheet names, with their respective theme versions. - * } - */ - $server_error_precache_entry = apply_filters( 'wp_server_error_precache_entry', $server_error_precache_entry ); - - } else { - $offline_error_precache_entry = array( - 'url' => add_query_arg( 'code', 'offline', admin_url( 'admin-ajax.php?action=wp_error_template' ) ), // Upon core merge, this would use admin_url( 'error.php' ). - 'revision' => PWA_VERSION, // Upon core merge, this should be the core version. - ); - $server_error_precache_entry = array( - 'url' => add_query_arg( 'code', '500', admin_url( 'admin-ajax.php?action=wp_error_template' ) ), // Upon core merge, this would use admin_url( 'error.php' ). - 'revision' => PWA_VERSION, // Upon core merge, this should be the core version. - ); - } - - $scripts->register( - 'wp-offline-commenting', - array( - 'src' => array( $this, 'get_script' ), - 'deps' => array( 'wp-base-config' ), - ) - ); - - if ( $offline_error_precache_entry ) { - $scripts->precaching_routes()->register( $offline_error_precache_entry['url'], isset( $offline_error_precache_entry['revision'] ) ? $offline_error_precache_entry['revision'] : null ); - } - if ( $server_error_precache_entry ) { - $scripts->precaching_routes()->register( $server_error_precache_entry['url'], isset( $server_error_precache_entry['revision'] ) ? $server_error_precache_entry['revision'] : null ); - } - - $this->replacements = array( - 'ERROR_OFFLINE_URL' => wp_service_worker_json_encode( isset( $offline_error_precache_entry['url'] ) ? $offline_error_precache_entry['url'] : null ), - 'ERROR_500_URL' => wp_service_worker_json_encode( isset( $server_error_precache_entry['url'] ) ? $server_error_precache_entry['url'] : null ), - 'ERROR_MESSAGES' => wp_service_worker_json_encode( wp_service_worker_get_error_messages() ), - ); - } - - /** - * Gets the priority this component should be hooked into the service worker action with. - * - * @since 0.2 - * - * @return int Hook priority. A higher number means a lower priority. - */ - public function get_priority() { - return 10; - } - - /** - * Get script for routing navigation requests. - * - * @return string Script. - */ - public function get_script() { - $script = file_get_contents( PWA_PLUGIN_DIR . '/wp-includes/js/service-worker-offline-commenting.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents - $script = preg_replace( '#/\*\s*global.+?\*/#', '', $script ); - - return str_replace( - array_keys( $this->replacements ), - array_values( $this->replacements ), - $script - ); - } -}