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

Remove offline commenting feature and add post request handler to display offline/error templates. #728

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
2 changes: 1 addition & 1 deletion .jshintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wp-includes/js/service-worker.js
wp-includes/js/service-worker-navigation-routing.js
wp-includes/js/service-worker-precaching.js
wp-includes/js/service-worker-offline-commenting.js
wp-includes/js/service-worker-offline-post-request-handling.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ public function serve( WP_Service_Worker_Scripts $scripts ) {
}

$scripts->register(
'wp-offline-commenting',
'wp-offline-post-request-handling',
array(
'src' => array( $this, 'get_offline_commenting_script' ),
'src' => array( $this, 'get_offline_post_request_script' ),
'deps' => array( 'wp-base-config' ),
)
);
Expand All @@ -308,7 +308,7 @@ public function serve( WP_Service_Worker_Scripts $scripts ) {
'wp-navigation-routing',
array(
'src' => array( $this, 'get_script' ),
'deps' => array( 'wp-base-config', 'wp-offline-commenting' ),
'deps' => array( 'wp-base-config', 'wp-offline-post-request-handling' ),
)
);

Expand Down Expand Up @@ -466,12 +466,12 @@ public function get_script() {
}

/**
* Get script for offline commenting requests.
* Get script for offline post 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
public function get_offline_post_request_script() {
$script = file_get_contents( PWA_PLUGIN_DIR . '/wp-includes/js/service-worker-offline-post-request-handling.js' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$script = preg_replace( '#/\*\s*global.+?\*/#', '', $script );

return str_replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

// IIFE is used for lexical scoping instead of just a braces block due to bug with const in Safari.
(() => {
const queue = new wp.serviceWorker.backgroundSync.Queue(
'wpPendingComments'
);
const errorMessages = ERROR_MESSAGES;

const commentHandler = ({ event }) => {
const clone = event.request.clone();
const offlinePostRequestHandler = ({ event }) => {
return fetch(event.request)
.then((response) => {
if (response.status < 500) {
Expand All @@ -34,13 +30,12 @@
statusText: errorResponse.statusText,
headers: errorResponse.headers,
};

let body = text.replace(
'{{{WP_SERVICE_WORKER_ERROR_MESSAGE}}}',
errorMessages.error
`${errorMessages.error} <strong>${errorMessages.submissionFailure}</strong>`
);
body = body.replace(
/([<]!--WP_SERVICE_WORKER_ERROR_TEMPLATE_BEGIN-->)((?:.|\n)+?)([<]!--WP_SERVICE_WORKER_ERROR_TEMPLATE_END-->)/,
/({{{WP_SERVICE_WORKER_ERROR_TEMPLATE_BEGIN}}})((?:.|\n)+?)({{{WP_SERVICE_WORKER_ERROR_TEMPLATE_END}}})/,
(details) => {
if (!errorText) {
return ''; // Remove the details from the document entirely.
Expand Down Expand Up @@ -71,13 +66,11 @@

// Replace the comments.
details = details.replace(
'<' +
'!--WP_SERVICE_WORKER_ERROR_TEMPLATE_BEGIN-->',
'{{{WP_SERVICE_WORKER_ERROR_TEMPLATE_BEGIN}}}',
''
);
details = details.replace(
'<' +
'!--WP_SERVICE_WORKER_ERROR_TEMPLATE_END-->',
'{{{WP_SERVICE_WORKER_ERROR_TEMPLATE_END}}}',
''
);
return details;
Expand All @@ -90,25 +83,6 @@
});
})
.catch(() => {
const bodyPromise = clone.blob();
bodyPromise.then(function (body) {
const request = event.request;
const req = new Request(request.url, {
method: request.method,
headers: request.headers,
mode: 'same-origin',
credentials: request.credentials,
referrer: request.referrer,
redirect: 'manual',
body,
});

// Add request to queue.
queue.pushRequest({
request: req,
});
});

// @todo This is duplicated with code in service-worker-navigation-routing.js.
return caches
.match(
Expand All @@ -124,9 +98,13 @@
headers: response.headers,
};

const connectionMessage = navigator.onLine
? errorMessages.serverOffline
: errorMessages.clientOffline;

const body = text.replace(
'{{{WP_SERVICE_WORKER_ERROR_MESSAGE}}}',
errorMessages.comment
`${connectionMessage} <strong>${errorMessages.submissionFailure}</strong>`
);

return new Response(body, init);
Expand All @@ -136,8 +114,8 @@
};

wp.serviceWorker.routing.registerRoute(
/\/wp-comments-post\.php$/,
commentHandler,
/.*/,
offlinePostRequestHandler,
'POST'
);
})();
8 changes: 4 additions & 4 deletions wp-includes/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ function wp_service_worker_get_error_messages() {
return apply_filters(
'wp_service_worker_error_messages',
array(
'clientOffline' => __( 'It seems you are offline. Please check your internet connection and try again.', 'pwa' ),
'serverOffline' => __( 'The server appears to be down, or your connection isn\'t working as expected. Please try again later.', 'pwa' ),
'error' => __( 'Something prevented the page from being rendered. Please try again.', 'pwa' ),
'comment' => __( 'Your comment will be submitted once you are back online!', 'pwa' ),
'clientOffline' => __( 'It seems you are offline. Please check your internet connection and try again.', 'pwa' ),
'serverOffline' => __( 'The server appears to be down, or your connection isn\'t working as expected. Please try again later.', 'pwa' ),
'error' => __( 'Something prevented the page from being rendered. Please try again.', 'pwa' ),
'submissionFailure' => __( 'Your submission failed. Please go back and try again.', 'pwa' ),
)
);
}
Expand Down