From 0f83364811d6d0ca6a0c6cd2602c2c7224b1edc0 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Fri, 22 Aug 2014 00:24:56 +0000 Subject: [PATCH] Autosave: prevent setting multiple stale `wp-saving-post-*` cookies when the browser disregards "session cookies" expiration on quit: - Add expiration time of 24 hours for these cookies. - Rename them to `wp-saving-post` (no post_id) so there is never more than one cookie per domain. Fixes #29294. git-svn-id: https://develop.svn.wordpress.org/trunk@29572 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/post.php | 5 +++-- src/wp-includes/js/autosave.js | 24 ++++++++++-------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/wp-admin/post.php b/src/wp-admin/post.php index 19c21a745d..805c46e6ed 100644 --- a/src/wp-admin/post.php +++ b/src/wp-admin/post.php @@ -229,8 +229,9 @@ case 'editpost': $post_id = edit_post(); // Session cookie flag that the post was saved - if ( isset( $_COOKIE['wp-saving-post-' . $post_id] ) ) - setcookie( 'wp-saving-post-' . $post_id, 'saved' ); + if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) { + setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS ); + } redirect_post($post_id); // Send user on their way while we keep working diff --git a/src/wp-includes/js/autosave.js b/src/wp-includes/js/autosave.js index dd08cb4e35..b338e94982 100644 --- a/src/wp-includes/js/autosave.js +++ b/src/wp-includes/js/autosave.js @@ -286,7 +286,7 @@ window.autosave = function() { }); } - wpCookies.set( 'wp-saving-post-' + post_id, 'check' ); + wpCookies.set( 'wp-saving-post', post_id + '-check', 24 * 60 * 60 ); }); } @@ -309,20 +309,17 @@ window.autosave = function() { function checkPost() { var content, post_title, excerpt, $notice, postData = getSavedPostData(), - cookie = wpCookies.get( 'wp-saving-post-' + post_id ); + cookie = wpCookies.get( 'wp-saving-post' ); - if ( ! postData ) { + if ( cookie === post_id + '-saved' ) { + wpCookies.remove( 'wp-saving-post' ); + // The post was saved properly, remove old data and bail + setData( false ); return; } - if ( cookie ) { - wpCookies.remove( 'wp-saving-post-' + post_id ); - - if ( cookie === 'saved' ) { - // The post was saved properly, remove old data and bail - setData( false ); - return; - } + if ( ! postData ) { + return; } // There is a newer autosave. Don't show two "restore" notices at the same time. @@ -334,9 +331,8 @@ window.autosave = function() { post_title = $( '#title' ).val() || ''; excerpt = $( '#excerpt' ).val() || ''; - // cookie == 'check' means the post was not saved properly, always show #local-storage-notice - if ( cookie !== 'check' && compare( content, postData.content ) && - compare( post_title, postData.post_title ) && compare( excerpt, postData.excerpt ) ) { + if ( compare( content, postData.content ) && compare( post_title, postData.post_title ) && + compare( excerpt, postData.excerpt ) ) { return; }