<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[26995] trunk: Autosave: refactor autosave.js, use heartbeat for transport and move all "Add/Edit Post" related functionality to post.js.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/26995">26995</a></dd>
<dt>Author</dt> <dd>azaozz</dd>
<dt>Date</dt> <dd>2014-01-22 04:55:37 +0000 (Wed, 22 Jan 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>Autosave: refactor autosave.js, use heartbeat for transport and move all "Add/Edit Post" related functionality to post.js. See <a href="http://core.trac.wordpress.org/ticket/25272">#25272</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminadminajaxphp">trunk/src/wp-admin/admin-ajax.php</a></li>
<li><a href="#trunksrcwpadmineditformadvancedphp">trunk/src/wp-admin/edit-form-advanced.php</a></li>
<li><a href="#trunksrcwpadminincludesajaxactionsphp">trunk/src/wp-admin/includes/ajax-actions.php</a></li>
<li><a href="#trunksrcwpadminincludesmiscphp">trunk/src/wp-admin/includes/misc.php</a></li>
<li><a href="#trunksrcwpadminincludespostphp">trunk/src/wp-admin/includes/post.php</a></li>
<li><a href="#trunksrcwpadminjspostjs">trunk/src/wp-admin/js/post.js</a></li>
<li><a href="#trunksrcwpadminpostphp">trunk/src/wp-admin/post.php</a></li>
<li><a href="#trunksrcwpincludesjsautosavejs">trunk/src/wp-includes/js/autosave.js</a></li>
<li><a href="#trunksrcwpincludesrevisionphp">trunk/src/wp-includes/revision.php</a></li>
<li><a href="#trunktestsphpunitincludestestcaseajaxphp">trunk/tests/phpunit/includes/testcase-ajax.php</a></li>
<li><a href="#trunktestsphpunittestsajaxAutosavephp">trunk/tests/phpunit/tests/ajax/Autosave.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminadminajaxphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/admin-ajax.php (26994 => 26995)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/admin-ajax.php 2014-01-22 02:28:13 UTC (rev 26994)
+++ trunk/src/wp-admin/admin-ajax.php 2014-01-22 04:55:37 UTC (rev 26995)
</span><span class="lines">@@ -50,7 +50,7 @@
</span><span class="cx"> 'oembed-cache', 'image-editor', 'delete-comment', 'delete-tag', 'delete-link',
</span><span class="cx"> 'delete-meta', 'delete-post', 'trash-post', 'untrash-post', 'delete-page', 'dim-comment',
</span><span class="cx"> 'add-link-category', 'add-tag', 'get-tagcloud', 'get-comments', 'replyto-comment',
</span><del>- 'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'autosave', 'closed-postboxes',
</del><ins>+ 'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'closed-postboxes',
</ins><span class="cx"> 'hidden-columns', 'update-welcome-panel', 'menu-get-metabox', 'wp-link-ajax',
</span><span class="cx"> 'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',
</span><span class="cx"> 'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
</span></span></pre></div>
<a id="trunksrcwpadmineditformadvancedphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/edit-form-advanced.php (26994 => 26995)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/edit-form-advanced.php 2014-01-22 02:28:13 UTC (rev 26994)
+++ trunk/src/wp-admin/edit-form-advanced.php 2014-01-22 04:55:37 UTC (rev 26995)
</span><span class="lines">@@ -402,7 +402,6 @@
</span><span class="cx">
</span><span class="cx"> echo $form_extra;
</span><span class="cx">
</span><del>-wp_nonce_field( 'autosave', 'autosavenonce', false );
</del><span class="cx"> wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
</span><span class="cx"> wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
</span><span class="cx"> ?>
</span></span></pre></div>
<a id="trunksrcwpadminincludesajaxactionsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/includes/ajax-actions.php (26994 => 26995)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/includes/ajax-actions.php 2014-01-22 02:28:13 UTC (rev 26994)
+++ trunk/src/wp-admin/includes/ajax-actions.php 2014-01-22 04:55:37 UTC (rev 26995)
</span><span class="lines">@@ -1089,68 +1089,6 @@
</span><span class="cx"> $x->send();
</span><span class="cx"> }
</span><span class="cx">
</span><del>-function wp_ajax_autosave() {
- define( 'DOING_AUTOSAVE', true );
-
- check_ajax_referer( 'autosave', 'autosavenonce' );
-
- if ( ! empty( $_POST['catslist'] ) )
- $_POST['post_category'] = explode( ',', $_POST['catslist'] );
- if ( $_POST['post_type'] == 'page' || empty( $_POST['post_category'] ) )
- unset( $_POST['post_category'] );
-
- $data = '';
- $supplemental = array();
- $id = $revision_id = 0;
-
- $post_id = (int) $_POST['post_id'];
- $_POST['ID'] = $_POST['post_ID'] = $post_id;
- $post = get_post( $post_id );
- if ( empty( $post->ID ) || ! current_user_can( 'edit_post', $post->ID ) )
- wp_die( __( 'You are not allowed to edit this post.' ) );
-
- if ( 'page' == $post->post_type && ! current_user_can( 'edit_page', $post->ID ) )
- wp_die( __( 'You are not allowed to edit this page.' ) );
-
- if ( 'auto-draft' == $post->post_status )
- $_POST['post_status'] = 'draft';
-
- if ( ! empty( $_POST['autosave'] ) ) {
- if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) {
- // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked
- $id = edit_post();
- } else {
- // Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user.
- $revision_id = wp_create_post_autosave( $post->ID );
- if ( is_wp_error($revision_id) )
- $id = $revision_id;
- else
- $id = $post->ID;
- }
-
- if ( ! is_wp_error($id) ) {
- /* translators: draft saved date format, see http://php.net/date */
- $draft_saved_date_format = __('g:i:s a');
- /* translators: %s: date and time */
- $data = sprintf( __('Draft saved at %s.'), date_i18n( $draft_saved_date_format ) );
- }
- } else {
- if ( ! empty( $_POST['auto_draft'] ) )
- $id = 0; // This tells us it didn't actually save
- else
- $id = $post->ID;
- }
-
- // @todo Consider exposing any errors, rather than having 'Saving draft...'
- $x = new WP_Ajax_Response( array(
- 'what' => 'autosave',
- 'id' => $id,
- 'data' => $data,
- 'supplemental' => $supplemental
- ) );
- $x->send();
-}
-
</del><span class="cx"> function wp_ajax_closed_postboxes() {
</span><span class="cx"> check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
</span><span class="cx"> $closed = isset( $_POST['closed'] ) ? explode( ',', $_POST['closed']) : array();
</span></span></pre></div>
<a id="trunksrcwpadminincludesmiscphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/includes/misc.php (26994 => 26995)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/includes/misc.php 2014-01-22 02:28:13 UTC (rev 26994)
+++ trunk/src/wp-admin/includes/misc.php 2014-01-22 04:55:37 UTC (rev 26995)
</span><span class="lines">@@ -734,7 +734,6 @@
</span><span class="cx"> if ( 2 === wp_verify_nonce( $received['post_nonce'], 'update-post_' . $post_id ) ) {
</span><span class="cx"> $response['wp-refresh-post-nonces'] = array(
</span><span class="cx"> 'replace' => array(
</span><del>- 'autosavenonce' => wp_create_nonce('autosave'),
</del><span class="cx"> 'getpermalinknonce' => wp_create_nonce('getpermalink'),
</span><span class="cx"> 'samplepermalinknonce' => wp_create_nonce('samplepermalink'),
</span><span class="cx"> 'closedpostboxesnonce' => wp_create_nonce('closedpostboxes'),
</span><span class="lines">@@ -768,3 +767,29 @@
</span><span class="cx"> return $settings;
</span><span class="cx"> }
</span><span class="cx"> add_filter( 'heartbeat_settings', 'wp_heartbeat_set_suspension' );
</span><ins>+
+/**
+ * Autosave with heartbeat
+ *
+ * @since 3.9
+ */
+function heartbeat_autosave( $response, $data ) {
+ if ( ! empty( $data['wp_autosave'] ) ) {
+ $saved = wp_autosave( $data['wp_autosave'] );
+
+ if ( is_wp_error( $saved ) ) {
+ $response['wp_autosave'] = array( 'success' => false, 'message' => $saved->get_error_message() );
+ } elseif ( empty( $saved ) ) {
+ $response['wp_autosave'] = array( 'success' => false, 'message' => __( 'Error while saving.' ) );
+ } else {
+ /* translators: draft saved date format, see http://php.net/date */
+ $draft_saved_date_format = __( 'g:i:s a' );
+ /* translators: %s: date and time */
+ $response['wp_autosave'] = array( 'success' => true, 'message' => sprintf( __( 'Draft saved at %s.' ), date_i18n( $draft_saved_date_format ) ) );
+ }
+ }
+
+ return $response;
+}
+// Run later as we have to set DOING_AUTOSAVE for back-compat
+add_filter( 'heartbeat_received', 'heartbeat_autosave', 500, 2 );
</ins></span></pre></div>
<a id="trunksrcwpadminincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/includes/post.php (26994 => 26995)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/includes/post.php 2014-01-22 02:28:13 UTC (rev 26994)
+++ trunk/src/wp-admin/includes/post.php 2014-01-22 04:55:37 UTC (rev 26995)
</span><span class="lines">@@ -79,9 +79,14 @@
</span><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx">
</span><del>- if ( ! empty( $post_data['post_status'] ) )
</del><ins>+ if ( ! empty( $post_data['post_status'] ) ) {
</ins><span class="cx"> $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
</span><span class="cx">
</span><ins>+ // No longer an auto-draft
+ if ( 'auto-draft' == $post_data['post_status'] )
+ $post_data['post_status'] = 'draft';
+ }
+
</ins><span class="cx"> // What to do based on which button they pressed
</span><span class="cx"> if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
</span><span class="cx"> $post_data['post_status'] = 'draft';
</span><span class="lines">@@ -190,9 +195,6 @@
</span><span class="cx"> $post_data = _wp_translate_postdata( true, $post_data );
</span><span class="cx"> if ( is_wp_error($post_data) )
</span><span class="cx"> wp_die( $post_data->get_error_message() );
</span><del>- if ( ( empty( $post_data['action'] ) || 'autosave' != $post_data['action'] ) && 'auto-draft' == $post_data['post_status'] ) {
- $post_data['post_status'] = 'draft';
- }
</del><span class="cx">
</span><span class="cx"> if ( isset($post_data['visibility']) ) {
</span><span class="cx"> switch ( $post_data['visibility'] ) {
</span><span class="lines">@@ -1335,22 +1337,30 @@
</span><span class="cx"> * @uses _wp_translate_postdata()
</span><span class="cx"> * @uses _wp_post_revision_fields()
</span><span class="cx"> *
</span><del>- * @return unknown
</del><ins>+ * @param mixed $post_data Associative array containing the post data or int post ID.
+ * @return mixed The autosave revision ID. WP_Error or 0 on error.
</ins><span class="cx"> */
</span><del>-function wp_create_post_autosave( $post_id ) {
- $translated = _wp_translate_postdata( true );
- if ( is_wp_error( $translated ) )
- return $translated;
</del><ins>+function wp_create_post_autosave( $post_data ) {
+ if ( is_numeric( $post_data ) ) {
+ $post_id = $post_data;
+ $post_data = &$_POST;
+ } else {
+ $post_id = (int) $post_data['post_ID'];
+ }
</ins><span class="cx">
</span><ins>+ $post_data = _wp_translate_postdata( true, $post_data );
+ if ( is_wp_error( $post_data ) )
+ return $post_data;
+
</ins><span class="cx"> $post_author = get_current_user_id();
</span><span class="cx">
</span><span class="cx"> // Store one autosave per author. If there is already an autosave, overwrite it.
</span><span class="cx"> if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
</span><del>- $new_autosave = _wp_post_revision_fields( $_POST, true );
</del><ins>+ $new_autosave = _wp_post_revision_fields( $post_data, true );
</ins><span class="cx"> $new_autosave['ID'] = $old_autosave->ID;
</span><span class="cx"> $new_autosave['post_author'] = $post_author;
</span><span class="cx">
</span><del>- // If the new autosave is the same content as the post, delete the old autosave.
</del><ins>+ // If the new autosave has the same content as the post, delete the autosave.
</ins><span class="cx"> $post = get_post( $post_id );
</span><span class="cx"> $autosave_is_different = false;
</span><span class="cx"> foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
</span><span class="lines">@@ -1362,14 +1372,14 @@
</span><span class="cx">
</span><span class="cx"> if ( ! $autosave_is_different ) {
</span><span class="cx"> wp_delete_post_revision( $old_autosave->ID );
</span><del>- return;
</del><ins>+ return 0;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> return wp_update_post( $new_autosave );
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> // _wp_put_post_revision() expects unescaped.
</span><del>- $post_data = wp_unslash( $_POST );
</del><ins>+ $post_data = wp_unslash( $post_data );
</ins><span class="cx">
</span><span class="cx"> // Otherwise create the new autosave as a special post revision
</span><span class="cx"> return _wp_put_post_revision( $post_data, true );
</span><span class="lines">@@ -1395,58 +1405,82 @@
</span><span class="cx"> function post_preview() {
</span><span class="cx">
</span><span class="cx"> $post_ID = (int) $_POST['post_ID'];
</span><del>- $status = get_post_status( $post_ID );
- if ( 'auto-draft' == $status )
- wp_die( __('Preview not available. Please save as a draft first.') );
</del><ins>+ $_POST['ID'] = $post_ID;
</ins><span class="cx">
</span><del>- if ( isset($_POST['catslist']) )
- $_POST['post_category'] = explode(",", $_POST['catslist']);
</del><ins>+ if ( ! $post = get_post( $post_ID ) )
+ wp_die( __('You attempted to preview a non existing item.') );
</ins><span class="cx">
</span><del>- if ( isset($_POST['tags_input']) )
- $_POST['tags_input'] = explode(",", $_POST['tags_input']);
</del><ins>+ if ( ! current_user_can( 'edit_post', $post->ID ) )
+ wp_die( __('You are not allowed to preview this item.') );
</ins><span class="cx">
</span><del>- if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
- unset($_POST['post_category']);
</del><ins>+ $is_autosave = false;
</ins><span class="cx">
</span><del>- $_POST['ID'] = $post_ID;
- $post = get_post($post_ID);
-
- if ( 'page' == $post->post_type ) {
- if ( ! current_user_can('edit_page', $post_ID) )
- wp_die( __('You are not allowed to edit this page.') );
</del><ins>+ if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'draft' == $post->post_status || 'auto-draft' == $post->post_status ) ) {
+ $saved_post_id = edit_post();
</ins><span class="cx"> } else {
</span><del>- if ( ! current_user_can('edit_post', $post_ID) )
- wp_die( __('You are not allowed to edit this post.') );
- }
</del><ins>+ $is_autosave = true;
</ins><span class="cx">
</span><del>- $user_id = get_current_user_id();
- $locked = wp_check_post_lock( $post->ID );
- if ( ! $locked && 'draft' == $post->post_status && $user_id == $post->post_author ) {
- $id = edit_post();
- } else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
- $id = wp_create_post_autosave( $post->ID );
- if ( ! is_wp_error($id) )
- $id = $post->ID;
</del><ins>+ if ( 'auto-draft' == $_POST['post_status'] )
+ $_POST['post_status'] = 'draft';
+
+ $saved_post_id = wp_create_post_autosave( $post->ID );
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- if ( is_wp_error($id) )
- wp_die( $id->get_error_message() );
</del><ins>+ if ( is_wp_error( $saved_post_id ) )
+ wp_die( $saved_post_id->get_error_message() );
</ins><span class="cx">
</span><del>- if ( ! $locked && $_POST['post_status'] == 'draft' && $user_id == $post->post_author ) {
- $url = add_query_arg( 'preview', 'true', get_permalink($id) );
- } else {
- $nonce = wp_create_nonce('post_preview_' . $id);
- $args = array(
- 'preview' => 'true',
- 'preview_id' => $id,
- 'preview_nonce' => $nonce,
- );
</del><ins>+ $query_args = array( 'preview' => 'true' );
</ins><span class="cx">
</span><ins>+ if ( $is_autosave && $saved_post_id ) {
+ $query_args['preview_id'] = $post->ID;
+ $query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );
+
</ins><span class="cx"> if ( isset( $_POST['post_format'] ) )
</span><del>- $args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
-
- $url = add_query_arg( $args, get_permalink($id) );
</del><ins>+ $query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
</ins><span class="cx"> }
</span><span class="cx">
</span><ins>+ $url = add_query_arg( $query_args, get_permalink( $post->ID ) );
</ins><span class="cx"> return apply_filters( 'preview_post_link', $url );
</span><span class="cx"> }
</span><ins>+
+/**
+ * Save a post submitted with XHR
+ *
+ * Intended for use with heartbeat and autosave.js
+ *
+ * @since 3.9
+ *
+ * @param $post_data Associative array of the submitted post data.
+ * @return mixed The value 0 or WP_Error on failure. The saved post ID on success.
+ * Te ID can be the draft post_id or the autosave revision post_id.
+ */
+function wp_autosave( $post_data ) {
+ // Back-compat
+ if ( ! defined( 'DOING_AUTOSAVE' ) )
+ define( 'DOING_AUTOSAVE', true );
+
+ $post_id = (int) $post_data['post_id'];
+ $post_data['ID'] = $post_data['post_ID'] = $post_id;
+
+ if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) )
+ return new WP_Error( 'invalid_nonce', __('ERROR: invalid post data.') );
+
+ $post = get_post( $post_id );
+
+ if ( ! current_user_can( 'edit_post', $post->ID ) )
+ return new WP_Error( 'edit_post', __('You are not allowed to edit this item.') );
+
+ if ( 'auto-draft' == $post->post_status )
+ $post_data['post_status'] = 'draft';
+
+ if ( $post_data['post_type'] != 'page' && ! empty( $post_data['catslist'] ) )
+ $post_data['post_category'] = explode( ',', $post_data['catslist'] );
+
+ if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) {
+ // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked
+ return edit_post( $post_data );
+ } else {
+ // Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user.
+ return wp_create_post_autosave( $post_data );
+ }
+}
</ins></span></pre></div>
<a id="trunksrcwpadminjspostjs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/js/post.js (26994 => 26995)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/js/post.js 2014-01-22 02:28:13 UTC (rev 26994)
+++ trunk/src/wp-admin/js/post.js 2014-01-22 04:55:37 UTC (rev 26995)
</span><span class="lines">@@ -1,7 +1,9 @@
</span><span class="cx"> /* global postL10n, ajaxurl, wpAjax, setPostThumbnailL10n, postboxes, pagenow, tinymce, alert, deleteUserSetting, getUserSetting, setUserSetting */
</span><span class="cx"> /* global theList:true, theExtraList:true, autosave:true */
</span><span class="cx">
</span><del>-var tagBox, commentsBox, editPermalink, makeSlugeditClickable, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint;
</del><ins>+var tagBox, commentsBox, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint;
+// Back-compat: prevent fatal errors
+makeSlugeditClickable = editPermalink = function(){};
</ins><span class="cx">
</span><span class="cx"> // return an array with any duplicate, whitespace or values removed
</span><span class="cx"> function array_unique_noempty(a) {
</span><span class="lines">@@ -268,10 +270,9 @@
</span><span class="cx"> send.lock = lock;
</span><span class="cx">
</span><span class="cx"> data['wp-refresh-post-lock'] = send;
</span><del>-});
</del><span class="cx">
</span><del>-// Post locks: update the lock string or show the dialog if somebody has taken over editing
-$(document).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
</del><ins>+}).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
+ // Post locks: update the lock string or show the dialog if somebody has taken over editing
</ins><span class="cx"> var received, wrap, avatar;
</span><span class="cx">
</span><span class="cx"> if ( data['wp-refresh-post-lock'] ) {
</span><span class="lines">@@ -282,19 +283,16 @@
</span><span class="cx"> wrap = $('#post-lock-dialog');
</span><span class="cx">
</span><span class="cx"> if ( wrap.length && ! wrap.is(':visible') ) {
</span><del>- if ( typeof autosave == 'function' ) {
- $(document).on('autosave-disable-buttons.post-lock', function() {
- wrap.addClass('saving');
- }).on('autosave-enable-buttons.post-lock', function() {
</del><ins>+ if ( typeof wp != 'undefined' && wp.autosave ) {
+ // Save the latest changes and disable
+ $(document).one( 'heartbeat-tick', function() {
+ wp.autosave.server.disable();
</ins><span class="cx"> wrap.removeClass('saving').addClass('saved');
</span><del>- window.onbeforeunload = null;
</del><ins>+ $(window).off( 'beforeunload.edit-post' );
</ins><span class="cx"> });
</span><span class="cx">
</span><del>- // Save the latest changes and disable
- if ( ! autosave() )
- window.onbeforeunload = null;
-
- autosave = function(){};
</del><ins>+ wrap.addClass('saving');
+ wp.autosave.server.triggerSave();
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> if ( received.lock_error.avatar_src ) {
</span><span class="lines">@@ -309,6 +307,22 @@
</span><span class="cx"> $('#active_post_lock').val( received.new_lock );
</span><span class="cx"> }
</span><span class="cx"> }
</span><ins>+}).on( 'after-autosave.update-post-slug', function() {
+ // create slug area only if not already there
+ if ( ! $('#edit-slug-box > *').length ) {
+ $.post( ajaxurl, {
+ action: 'sample-permalink',
+ post_id: $('#post_ID').val(),
+ new_title: typeof fullscreen != 'undefined' && fullscreen.settings.visible ? $('#wp-fullscreen-title').val() : $('#title').val(),
+ samplepermalinknonce: $('#samplepermalinknonce').val()
+ },
+ function( data ) {
+ if ( data != '-1' ) {
+ $('#edit-slug-box').html(data);
+ }
+ }
+ );
+ }
</ins><span class="cx"> });
</span><span class="cx">
</span><span class="cx"> }(jQuery));
</span><span class="lines">@@ -354,8 +368,14 @@
</span><span class="cx"> }(jQuery));
</span><span class="cx">
</span><span class="cx"> jQuery(document).ready( function($) {
</span><del>- var stamp, visibility, updateVisibility, updateText,
- sticky = '', last = 0, co = $('#content');
</del><ins>+ var stamp, visibility, $submitButtons,
+ sticky = '',
+ last = 0,
+ co = $('#content'),
+ $editSlugWrap = $('#edit-slug-box'),
+ postId = $('#post_ID').val() || 0,
+ $submitpost = $('#submitpost'),
+ releaseLock = true;
</ins><span class="cx">
</span><span class="cx"> postboxes.add_postbox_toggles(pagenow);
</span><span class="cx">
</span><span class="lines">@@ -380,6 +400,142 @@
</span><span class="cx"> wp.heartbeat.interval( 15 );
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+ // The form is being submitted by the user
+ $submitButtons = $submitpost.find( ':button, :submit, a.submitdelete, #post-preview' ).on( 'click.autosave', function( event ) {
+ var $button = $(this);
+
+ if ( $button.prop('disabled') ) {
+ event.preventDefault();
+ return;
+ }
+
+ if ( $button.hasClass('submitdelete') ) {
+ return;
+ }
+
+ // The form submission can be blocked from JS or by using HTML 5.0 validation on some fields.
+ // Run this only on an actual 'submit'.
+ $('form#post').off( 'submit.edit-post' ).on( 'submit.edit-post', function( event ) {
+ if ( event.isDefaultPrevented() ) {
+ return;
+ }
+
+ wp.autosave.server.disable();
+ releaseLock = false;
+ $(window).off( 'beforeunload.edit-post' );
+
+ $submitButtons.prop( 'disabled', true ).addClass( 'button-disabled' );
+
+ if ( $button.attr('id') === 'publish' ) {
+ $submitpost.find('#major-publishing-actions .spinner').show();
+ } else {
+ $submitpost.find('#minor-publishing .spinner').show();
+ }
+ });
+ });
+
+ // Submit the form saving a draft or an autosave, and show a preview in a new tab
+ $('#post-preview').on( 'click.post-preview', function( event ) {
+ var $this = $(this),
+ $form = $('form#post'),
+ $previewField = $('input#wp-preview'),
+ target = $this.attr('target') || 'wp-preview',
+ ua = navigator.userAgent.toLowerCase();
+
+ event.preventDefault();
+
+ if ( $this.prop('disabled') ) {
+ return;
+ }
+
+ if ( typeof wp != 'undefined' && wp.autosave ) {
+ wp.autosave.server.tempBlockSave();
+ }
+
+ $previewField.val('dopreview');
+ $form.attr( 'target', target ).submit().attr( 'target', '' );
+
+ // Workaround for WebKit bug preventing a form submitting twice to the same action.
+ // https://bugs.webkit.org/show_bug.cgi?id=28633
+ if ( ua.indexOf('safari') !== -1 && ua.indexOf('chrome') === -1 ) {
+ $form.attr( 'action', function( index, value ) {
+ return value + '?t=' + ( new Date() ).getTime();
+ });
+ }
+
+ $previewField.val('');
+ });
+
+ // This code is meant to allow tabbing from Title to Post content.
+ $('#title').on( 'keydown.editor-focus', function( event ) {
+ var editor;
+
+ if ( event.keyCode === 9 && ! event.ctrlKey && ! event.altKey && ! event.shiftKey ) {
+ editor = typeof tinymce != 'undefined' && tinymce.get('content');
+
+ if ( editor && ! editor.isHidden() ) {
+ editor.focus();
+ } else {
+ $('#content').focus();
+ }
+
+ event.preventDefault();
+ }
+ });
+
+ // Autosave new posts after a title is typed
+ if ( $( '#auto_draft' ).val() ) {
+ $( '#title' ).blur( function() {
+ if ( ! this.value || $( '#auto_draft' ).val() !== '1' ) {
+ return;
+ }
+
+ if ( typeof wp != 'undefined' && wp.autosave ) {
+ wp.autosave.server.triggerSave();
+ }
+ });
+ }
+
+ $(document).on( 'autosave-disable-buttons.edit-post', function() {
+ $submitButtons.prop( 'disabled', true ).addClass( 'button-disabled' );
+ }).on( 'autosave-enable-buttons.edit-post', function() {
+ if ( ! window.wp || ! window.wp.heartbeat || ! window.wp.heartbeat.hasConnectionError() ) {
+ $submitButtons.prop( 'disabled', false ).removeClass( 'button-disabled' );
+ }
+ });
+
+ $(window).on( 'beforeunload.edit-post', function() {
+ var editor = typeof tinymce !== 'undefined' && tinymce.get('content');
+
+ if ( ( editor && ! editor.isHidden() && editor.isDirty() ) ||
+ ( typeof wp !== 'undefined' && wp.autosave && wp.autosave.server.postChanged() ) ) {
+
+ return autosaveL10n.saveAlert;
+ }
+ }).on( 'unload.edit-post', function( event ) {
+ if ( ! releaseLock ) {
+ return;
+ }
+
+ // Unload is triggered (by hand) on removing the Thickbox iframe.
+ // Make sure we process only the main document unload.
+ if ( event.target && event.target.nodeName != '#document' ) {
+ return;
+ }
+
+ $.ajax({
+ type: 'POST',
+ url: ajaxurl,
+ async: false,
+ data: {
+ action: 'wp-remove-post-lock',
+ _wpnonce: $('#_wpnonce').val(),
+ post_ID: $('#post_ID').val(),
+ active_post_lock: $('#active_post_lock').val()
+ }
+ });
+ });
+
</ins><span class="cx"> // multi-taxonomies
</span><span class="cx"> if ( $('#tagsdiv-post_tag').length ) {
</span><span class="cx"> tagBox.init();
</span><span class="lines">@@ -689,28 +845,22 @@
</span><span class="cx"> } // end submitdiv
</span><span class="cx">
</span><span class="cx"> // permalink
</span><del>- if ( $('#edit-slug-box').length ) {
- editPermalink = function(post_id) {
- var slug_value, i,
- c = 0,
- e = $( '#editable-post-name' ),
- revert_e = e.html(),
- real_slug = $( '#post_name' ),
- revert_slug = real_slug.val(),
- b = $( '#edit-slug-buttons' ),
- revert_b = b.html(),
- full = $( '#editable-post-name-full' ).html();
</del><ins>+ if ( $editSlugWrap.length ) {
+ function editPermalink() {
+ var i, c = 0, e = $('#editable-post-name'), revert_e = e.html(), real_slug = $('#post_name'),
+ revert_slug = real_slug.val(), b = $('#edit-slug-buttons'), revert_b = b.html(),
+ full = $('#editable-post-name-full').html();
</ins><span class="cx">
</span><span class="cx"> $('#view-post-btn').hide();
</span><span class="cx"> b.html('<a href="#" class="save button button-small">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
</span><span class="cx"> b.children('.save').click(function() {
</span><span class="cx"> var new_slug = e.children('input').val();
</span><span class="cx"> if ( new_slug == $('#editable-post-name-full').text() ) {
</span><del>- return $('.cancel', '#edit-slug-buttons').click();
</del><ins>+ return $('#edit-slug-buttons .cancel').click();
</ins><span class="cx"> }
</span><span class="cx"> $.post(ajaxurl, {
</span><span class="cx"> action: 'sample-permalink',
</span><del>- post_id: post_id,
</del><ins>+ post_id: postId,
</ins><span class="cx"> new_slug: new_slug,
</span><span class="cx"> new_title: $('#title').val(),
</span><span class="cx"> samplepermalinknonce: $('#samplepermalinknonce').val()
</span><span class="lines">@@ -724,13 +874,12 @@
</span><span class="cx"> }
</span><span class="cx"> b.html(revert_b);
</span><span class="cx"> real_slug.val(new_slug);
</span><del>- makeSlugeditClickable();
</del><span class="cx"> $('#view-post-btn').show();
</span><span class="cx"> });
</span><span class="cx"> return false;
</span><span class="cx"> });
</span><span class="cx">
</span><del>- $('.cancel', '#edit-slug-buttons').click(function() {
</del><ins>+ $('#edit-slug-buttons .cancel').click(function() {
</ins><span class="cx"> $('#view-post-btn').show();
</span><span class="cx"> e.html(revert_e);
</span><span class="cx"> b.html(revert_b);
</span><span class="lines">@@ -760,12 +909,13 @@
</span><span class="cx"> }).focus();
</span><span class="cx"> };
</span><span class="cx">
</span><del>- makeSlugeditClickable = function() {
- $('#editable-post-name').click(function() {
- $('#edit-slug-buttons').children('.edit-slug').click();
- });
- };
- makeSlugeditClickable();
</del><ins>+ $editSlugWrap.on( 'click', function( event ) {
+ var $target = $( event.target );
+
+ if ( $target.is('#editable-post-name') || $target.hasClass('edit-slug') ) {
+ editPermalink();
+ }
+ });
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> // word count
</span></span></pre></div>
<a id="trunksrcwpadminpostphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/post.php (26994 => 26995)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/post.php 2014-01-22 02:28:13 UTC (rev 26994)
+++ trunk/src/wp-admin/post.php 2014-01-22 04:55:37 UTC (rev 26995)
</span><span class="lines">@@ -307,7 +307,7 @@
</span><span class="cx"> break;
</span><span class="cx">
</span><span class="cx"> case 'preview':
</span><del>- check_admin_referer( 'autosave', 'autosavenonce' );
</del><ins>+ check_admin_referer( 'update-post_' . $post_id );
</ins><span class="cx">
</span><span class="cx"> $url = post_preview();
</span><span class="cx">
</span></span></pre></div>
<a id="trunksrcwpincludesjsautosavejs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/js/autosave.js (26994 => 26995)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/js/autosave.js 2014-01-22 02:28:13 UTC (rev 26994)
+++ trunk/src/wp-includes/js/autosave.js 2014-01-22 04:55:37 UTC (rev 26995)
</span><span class="lines">@@ -1,688 +1,597 @@
</span><del>-/* global switchEditors, autosaveL10n, tinymce, ajaxurl, wpAjax, makeSlugeditClickable, wpCookies */
-var autosave, autosavePeriodical, fullscreen, doPreview,
- autosaveLast = '',
- autosaveDelayPreview = false,
- notSaved = true,
- blockSave = false,
- autosaveLockRelease = true;
</del><ins>+/* global tinymce, wpCookies, autosaveL10n, switchEditors */
+// Back-compat: prevent fatal errors
+window.autosave = function(){};
</ins><span class="cx">
</span><del>-jQuery(document).ready( function($) {
</del><ins>+( function( $, window ) {
+ function autosave() {
+ var initialCompareString,
+ lastTriggerSave = 0,
+ isSuspended = false,
+ $document = $(document);
</ins><span class="cx">
</span><del>- if ( $('#wp-content-wrap').hasClass('tmce-active') && typeof switchEditors != 'undefined' ) {
- autosaveLast = wp.autosave.getCompareString({
- post_title : $('#title').val() || '',
- content : switchEditors.pre_wpautop( $('#content').val() ) || '',
- excerpt : $('#excerpt').val() || ''
- });
- } else {
- autosaveLast = wp.autosave.getCompareString();
- }
</del><ins>+ /**
+ * Returns the data saved in both local and remote autosave
+ *
+ * @return object Object containing the post data
+ */
+ function getPostData( type ) {
+ var post_name, parent_id, data,
+ time = ( new Date() ).getTime(),
+ cats = [],
+ editor = typeof tinymce !== 'undefined' && tinymce.get('content');
</ins><span class="cx">
</span><del>- autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true});
</del><ins>+ // Don't run editor.save() more often than every 3 sec.
+ // It is resource intensive and might slow down typing in long posts on slow devices.
+ if ( editor && ! editor.isHidden() && time - 3000 > lastTriggerSave ) {
+ editor.save();
+ lastTriggerSave = time;
+ }
</ins><span class="cx">
</span><del>- //Disable autosave after the form has been submitted
- $('#post').submit(function() {
- $.cancel(autosavePeriodical);
- autosaveLockRelease = false;
- });
</del><ins>+ data = {
+ post_id: $( '#post_ID' ).val() || 0,
+ post_type: $( '#post_type' ).val() || '',
+ post_author: $( '#post_author' ).val() || '',
+ post_title: $( '#title' ).val() || '',
+ content: $( '#content' ).val() || '',
+ excerpt: $( '#excerpt' ).val() || ''
+ };
</ins><span class="cx">
</span><del>- $('input[type="submit"], a.submitdelete', '#submitpost').click(function(){
- blockSave = true;
- window.onbeforeunload = null;
- $(':button, :submit', '#submitpost').each(function(){
- var t = $(this);
- if ( t.hasClass('button-primary') )
- t.addClass('button-primary-disabled');
- else
- t.addClass('button-disabled');
- });
- if ( $(this).attr('id') == 'publish' )
- $('#major-publishing-actions .spinner').show();
- else
- $('#minor-publishing .spinner').show();
- });
</del><ins>+ if ( type === 'local' ) {
+ return data;
+ }
</ins><span class="cx">
</span><del>- window.onbeforeunload = function(){
- var editor = typeof(tinymce) != 'undefined' ? tinymce.activeEditor : false;
</del><ins>+ $( 'input[id^="in-category-"]:checked' ).each( function() {
+ cats.push( this.value );
+ });
+ data.catslist = cats.join(',');
</ins><span class="cx">
</span><del>- if ( editor && ! editor.isHidden() ) {
- if ( editor.isDirty() )
- return autosaveL10n.saveAlert;
- } else {
- if ( wp.autosave.getCompareString() != autosaveLast )
- return autosaveL10n.saveAlert;
- }
- };
</del><ins>+ if ( post_name = $( '#post_name' ).val() ) {
+ data.post_name = post_name;
+ }
</ins><span class="cx">
</span><del>- $(window).unload( function(e) {
- if ( ! autosaveLockRelease )
- return;
</del><ins>+ if ( parent_id = $( '#parent_id' ).val() ) {
+ data.parent_id = parent_id;
+ }
</ins><span class="cx">
</span><del>- // unload fires (twice) on removing the Thickbox iframe. Make sure we process only the main document unload.
- if ( e.target && e.target.nodeName != '#document' )
- return;
</del><ins>+ if ( $( '#comment_status' ).prop( 'checked' ) ) {
+ data.comment_status = 'open';
+ }
</ins><span class="cx">
</span><del>- $.ajax({
- type: 'POST',
- url: ajaxurl,
- async: false,
- data: {
- action: 'wp-remove-post-lock',
- _wpnonce: $('#_wpnonce').val(),
- post_ID: $('#post_ID').val(),
- active_post_lock: $('#active_post_lock').val()
</del><ins>+ if ( $( '#ping_status' ).prop( 'checked' ) ) {
+ data.ping_status = 'open';
</ins><span class="cx"> }
</span><del>- });
- } );
</del><span class="cx">
</span><del>- // preview
- $('#post-preview').click(function(){
- if ( $('#auto_draft').val() == '1' && notSaved ) {
- autosaveDelayPreview = true;
- autosave();
- return false;
- }
- doPreview();
- return false;
- });
</del><ins>+ if ( $( '#auto_draft' ).val() === '1' ) {
+ data.auto_draft = '1';
+ }
</ins><span class="cx">
</span><del>- doPreview = function() {
- $('input#wp-preview').val('dopreview');
- $('form#post').attr('target', 'wp-preview').submit().attr('target', '');
-
- /*
- * Workaround for WebKit bug preventing a form submitting twice to the same action.
- * https://bugs.webkit.org/show_bug.cgi?id=28633
- */
- var ua = navigator.userAgent.toLowerCase();
- if ( ua.indexOf('safari') != -1 && ua.indexOf('chrome') == -1 ) {
- $('form#post').attr('action', function(index, value) {
- return value + '?t=' + new Date().getTime();
- });
</del><ins>+ return data;
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- $('input#wp-preview').val('');
- };
-
- // This code is meant to allow tabbing from Title to Post content.
- $('#title').on( 'keydown.editor-focus', function( event ) {
- var editor;
-
- if ( event.which === 9 && ! event.ctrlKey && ! event.altKey && ! event.shiftKey ) {
- if ( typeof tinymce !== 'undefined' ) {
- editor = tinymce.get('content');
</del><ins>+ // Concatenate title, content and excerpt. Used to track changes when auto-saving.
+ function getCompareString( postData ) {
+ if ( typeof postData === 'object' ) {
+ return ( postData.post_title || '' ) + '::' + ( postData.content || '' ) + '::' + ( postData.excerpt || '' );
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- if ( editor && ! editor.isHidden() ) {
- $(this).one( 'keyup', function() {
- editor.focus();
- });
- } else {
- $('#content').focus();
- }
</del><ins>+ return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' );
+ }
</ins><span class="cx">
</span><del>- event.preventDefault();
</del><ins>+ function disableButtons() {
+ $document.trigger('autosave-disable-buttons');
+ // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
+ setTimeout( enableButtons, 5000 );
</ins><span class="cx"> }
</span><del>- });
</del><span class="cx">
</span><del>- // autosave new posts after a title is typed but not if Publish or Save Draft is clicked
- if ( '1' == $('#auto_draft').val() ) {
- $('#title').blur( function() {
- if ( !this.value || $('#auto_draft').val() != '1' )
- return;
- delayed_autosave();
- });
- }
-
- // When connection is lost, keep user from submitting changes.
- $(document).on('heartbeat-connection-lost.autosave', function( e, error, status ) {
- if ( 'timeout' === error || 503 == status ) {
- var notice = $('#lost-connection-notice');
- if ( ! wp.autosave.local.hasStorage ) {
- notice.find('.hide-if-no-sessionstorage').hide();
- }
- notice.show();
- autosave_disable_buttons();
</del><ins>+ function enableButtons() {
+ $document.trigger( 'autosave-enable-buttons' );
</ins><span class="cx"> }
</span><del>- }).on('heartbeat-connection-restored.autosave', function() {
- $('#lost-connection-notice').hide();
- autosave_enable_buttons();
- });
-});
</del><span class="cx">
</span><del>-function autosave_parse_response( response ) {
- var res = wpAjax.parseAjaxResponse(response, 'autosave'), post_id, sup;
-
- if ( res && res.responses && res.responses.length ) {
- if ( res.responses[0].supplemental ) {
- sup = res.responses[0].supplemental;
-
- jQuery.each( sup, function( selector, value ) {
- if ( selector.match(/^replace-/) )
- jQuery( '#' + selector.replace('replace-', '') ).val( value );
- });
</del><ins>+ function suspend() {
+ isSuspended = true;
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- // if no errors: add slug UI and update autosave-message
- if ( !res.errors ) {
- if ( post_id = parseInt( res.responses[0].id, 10 ) )
- autosave_update_slug( post_id );
-
- if ( res.responses[0].data ) // update autosave message
- jQuery('.autosave-message').text( res.responses[0].data );
</del><ins>+ function resume() {
+ isSuspended = false;
</ins><span class="cx"> }
</span><del>- }
</del><span class="cx">
</span><del>- return res;
-}
</del><ins>+ // Autosave in localStorage
+ function autosaveLocal() {
+ var restorePostData, undoPostData, blog_id, post_id, hasStorage, intervalTimer,
+ lastCompareString;
</ins><span class="cx">
</span><del>-// called when autosaving pre-existing post
-function autosave_saved(response) {
- blockSave = false;
- autosave_parse_response(response); // parse the ajax response
- autosave_enable_buttons(); // re-enable disabled form buttons
-}
</del><ins>+ // Check if the browser supports sessionStorage and it's not disabled
+ function checkStorage() {
+ var test = Math.random().toString(),
+ result = false;
</ins><span class="cx">
</span><del>-// called when autosaving new post
-function autosave_saved_new(response) {
- blockSave = false;
- var res = autosave_parse_response(response), post_id;
</del><ins>+ try {
+ window.sessionStorage.setItem( 'wp-test', test );
+ result = window.sessionStorage.getItem( 'wp-test' ) === test;
+ window.sessionStorage.removeItem( 'wp-test' );
+ } catch(e) {}
</ins><span class="cx">
</span><del>- if ( res && res.responses.length && !res.errors ) {
- // An ID is sent only for real auto-saves, not for autosave=0 "keepalive" saves
- post_id = parseInt( res.responses[0].id, 10 );
</del><ins>+ hasStorage = result;
+ return result;
+ }
</ins><span class="cx">
</span><del>- if ( post_id ) {
- notSaved = false;
- jQuery('#auto_draft').val('0'); // No longer an auto-draft
- }
</del><ins>+ /**
+ * Initialize the local storage
+ *
+ * @return mixed False if no sessionStorage in the browser or an Object containing all postData for this blog
+ */
+ function getStorage() {
+ var stored_obj = false;
+ // Separate local storage containers for each blog_id
+ if ( hasStorage && blog_id ) {
+ stored_obj = sessionStorage.getItem( 'wp-autosave-' + blog_id );
</ins><span class="cx">
</span><del>- autosave_enable_buttons();
-
- if ( autosaveDelayPreview ) {
- autosaveDelayPreview = false;
- doPreview();
- }
- } else {
- autosave_enable_buttons(); // re-enable disabled form buttons
- }
-}
-
-function autosave_update_slug(post_id) {
- // create slug area only if not already there
- if ( 'undefined' != makeSlugeditClickable && jQuery.isFunction(makeSlugeditClickable) && !jQuery('#edit-slug-box > *').size() ) {
- jQuery.post( ajaxurl, {
- action: 'sample-permalink',
- post_id: post_id,
- new_title: fullscreen && fullscreen.settings.visible ? jQuery('#wp-fullscreen-title').val() : jQuery('#title').val(),
- samplepermalinknonce: jQuery('#samplepermalinknonce').val()
- },
- function(data) {
- if ( data !== '-1' ) {
- var box = jQuery('#edit-slug-box');
- box.html(data);
- if (box.hasClass('hidden')) {
- box.fadeIn('fast', function () {
- box.removeClass('hidden');
- });
</del><ins>+ if ( stored_obj ) {
+ stored_obj = JSON.parse( stored_obj );
+ } else {
+ stored_obj = {};
</ins><span class="cx"> }
</span><del>- makeSlugeditClickable();
</del><span class="cx"> }
</span><ins>+
+ return stored_obj;
</ins><span class="cx"> }
</span><del>- );
- }
-}
</del><span class="cx">
</span><del>-function autosave_loading() {
- jQuery('.autosave-message').html(autosaveL10n.savingText);
-}
</del><ins>+ /**
+ * Set the storage for this blog
+ *
+ * Confirms that the data was saved successfully.
+ *
+ * @return bool
+ */
+ function setStorage( stored_obj ) {
+ var key;
</ins><span class="cx">
</span><del>-function autosave_enable_buttons() {
- jQuery(document).trigger('autosave-enable-buttons');
- if ( ! wp.heartbeat || ! wp.heartbeat.hasConnectionError() ) {
- // delay that a bit to avoid some rare collisions while the DOM is being updated.
- setTimeout(function(){
- var parent = jQuery('#submitpost');
- parent.find(':button, :submit').removeAttr('disabled');
- parent.find('.spinner').hide();
- }, 500);
- }
-}
</del><ins>+ if ( hasStorage && blog_id ) {
+ key = 'wp-autosave-' + blog_id;
+ sessionStorage.setItem( key, JSON.stringify( stored_obj ) );
+ return sessionStorage.getItem( key ) !== null;
+ }
</ins><span class="cx">
</span><del>-function autosave_disable_buttons() {
- jQuery(document).trigger('autosave-disable-buttons');
- jQuery('#submitpost').find(':button, :submit').prop('disabled', true);
- // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
- setTimeout( autosave_enable_buttons, 5000 );
-}
</del><ins>+ return false;
+ }
</ins><span class="cx">
</span><del>-function delayed_autosave() {
- setTimeout(function(){
- if ( blockSave )
- return;
- autosave();
- }, 200);
-}
</del><ins>+ /**
+ * Get the saved post data for the current post
+ *
+ * @return mixed False if no storage or no data or the postData as an Object
+ */
+ function getSavedPostData() {
+ var stored = getStorage();
</ins><span class="cx">
</span><del>-autosave = function() {
- var post_data = wp.autosave.getPostData(),
- compareString,
- successCallback;
</del><ins>+ if ( ! stored || ! post_id ) {
+ return false;
+ }
</ins><span class="cx">
</span><del>- blockSave = true;
</del><ins>+ return stored[ 'post_' + post_id ] || false;
+ }
</ins><span class="cx">
</span><del>- // post_data.content cannot be retrieved at the moment
- if ( ! post_data.autosave )
- return false;
</del><ins>+ /**
+ * Set (save or delete) post data in the storage.
+ *
+ * If stored_data evaluates to 'false' the storage key for the current post will be removed
+ *
+ * $param stored_data The post data to store or null/false/empty to delete the key
+ * @return bool
+ */
+ function setData( stored_data ) {
+ var stored = getStorage();
</ins><span class="cx">
</span><del>- // No autosave while thickbox is open (media buttons)
- if ( jQuery('#TB_window').css('display') == 'block' )
- return false;
</del><ins>+ if ( ! stored || ! post_id ) {
+ return false;
+ }
</ins><span class="cx">
</span><del>- compareString = wp.autosave.getCompareString( post_data );
</del><ins>+ if ( stored_data ) {
+ stored[ 'post_' + post_id ] = stored_data;
+ } else if ( stored.hasOwnProperty( 'post_' + post_id ) ) {
+ delete stored[ 'post_' + post_id ];
+ } else {
+ return false;
+ }
</ins><span class="cx">
</span><del>- // Nothing to save or no change.
- if ( compareString == autosaveLast )
- return false;
</del><ins>+ return setStorage( stored );
+ }
</ins><span class="cx">
</span><del>- autosaveLast = compareString;
- jQuery(document).triggerHandler('wpcountwords', [ post_data.content ]);
</del><ins>+ /**
+ * Save post data for the current post
+ *
+ * Runs on a 15 sec. interval, saves when there are differences in the post title or content.
+ * When the optional data is provided, updates the last saved post data.
+ *
+ * $param data optional Object The post data for saving, minimum 'post_title' and 'content'
+ * @return bool
+ */
+ function save( data ) {
+ var postData, compareString,
+ result = false;
</ins><span class="cx">
</span><del>- // Disable buttons until we know the save completed.
- autosave_disable_buttons();
</del><ins>+ if ( isSuspended ) {
+ return false;
+ }
</ins><span class="cx">
</span><del>- if ( post_data.auto_draft == '1' ) {
- successCallback = autosave_saved_new; // new post
- } else {
- successCallback = autosave_saved; // pre-existing post
- }
</del><ins>+ if ( data ) {
+ postData = getSavedPostData() || {};
+ $.extend( postData, data );
+ } else {
+ postData = getPostData('local');
+ }
</ins><span class="cx">
</span><del>- jQuery.ajax({
- data: post_data,
- beforeSend: autosave_loading,
- type: 'POST',
- url: ajaxurl,
- success: successCallback
- });
</del><ins>+ compareString = getCompareString( postData );
</ins><span class="cx">
</span><del>- return true;
-};
</del><ins>+ if ( typeof lastCompareString === 'undefined' ) {
+ lastCompareString = initialCompareString;
+ }
</ins><span class="cx">
</span><del>-// Autosave in localStorage
-// set as simple object/mixin for now
-window.wp = window.wp || {};
-wp.autosave = wp.autosave || {};
</del><ins>+ // If the content, title and excerpt did not change since the last save, don't save again
+ if ( compareString === lastCompareString ) {
+ return false;
+ }
</ins><span class="cx">
</span><del>-(function($){
-// Returns the data for saving in both localStorage and autosaves to the server
-wp.autosave.getPostData = function() {
- var ed = typeof tinymce != 'undefined' ? tinymce.activeEditor : null, post_name, parent_id, cats = [],
- data = {
- action: 'autosave',
- autosave: true,
- post_id: $('#post_ID').val() || 0,
- autosavenonce: $('#autosavenonce').val() || '',
- post_type: $('#post_type').val() || '',
- post_author: $('#post_author').val() || '',
- excerpt: $('#excerpt').val() || ''
- };
</del><ins>+ postData.save_time = ( new Date() ).getTime();
+ postData.status = $( '#post_status' ).val() || '';
+ result = setData( postData );
</ins><span class="cx">
</span><del>- if ( ed && !ed.isHidden() ) {
- // Don't run while the tinymce spellcheck is on. It resets all found words.
- if ( ed.plugins.spellchecker && ed.plugins.spellchecker.active ) {
- data.autosave = false;
- return data;
- } else {
- tinymce.triggerSave();
- }
- }
</del><ins>+ if ( result ) {
+ lastCompareString = compareString;
+ }
</ins><span class="cx">
</span><del>- data.post_title = $('#title').val() || '';
- data.content = $('#content').val() || '';
</del><ins>+ return result;
+ }
</ins><span class="cx">
</span><del>- /*
- // We haven't been saving tags with autosave since 2.8... Start again?
- $('.the-tags').each( function() {
- data[this.name] = this.value;
- });
- */
</del><ins>+ // Run on DOM ready
+ function run() {
+ post_id = $('#post_ID').val() || 0;
</ins><span class="cx">
</span><del>- $('input[id^="in-category-"]:checked').each( function() {
- cats.push(this.value);
- });
- data.catslist = cats.join(',');
</del><ins>+ // Check if the local post data is different than the loaded post data.
+ if ( $( '#wp-content-wrap' ).hasClass( 'tmce-active' ) ) {
+ // If TinyMCE loads first, check the post 1.5 sec. after it is ready.
+ // By this time the content has been loaded in the editor and 'saved' to the textarea.
+ // This prevents false positives.
+ $document.on( 'tinymce-editor-init.autosave', function() {
+ window.setTimeout( function() {
+ checkPost();
+ }, 1500 );
+ });
+ } else {
+ checkPost();
+ }
</ins><span class="cx">
</span><del>- if ( post_name = $('#post_name').val() )
- data.post_name = post_name;
</del><ins>+ // Save every 15 sec.
+ intervalTimer = window.setInterval( save, 15000 );
</ins><span class="cx">
</span><del>- if ( parent_id = $('#parent_id').val() )
- data.parent_id = parent_id;
</del><ins>+ $( 'form#post' ).on( 'submit.autosave-local', function() {
+ var editor = typeof tinymce !== 'undefined' && tinymce.get('content'),
+ post_id = $('#post_ID').val() || 0;
</ins><span class="cx">
</span><del>- if ( $('#comment_status').prop('checked') )
- data.comment_status = 'open';
</del><ins>+ if ( editor && ! editor.isHidden() ) {
+ // Last onSubmit event in the editor, needs to run after the content has been moved to the textarea.
+ editor.on( 'submit', function() {
+ save({
+ post_title: $( '#title' ).val() || '',
+ content: $( '#content' ).val() || '',
+ excerpt: $( '#excerpt' ).val() || ''
+ });
+ });
+ } else {
+ save({
+ post_title: $( '#title' ).val() || '',
+ content: $( '#content' ).val() || '',
+ excerpt: $( '#excerpt' ).val() || ''
+ });
+ }
</ins><span class="cx">
</span><del>- if ( $('#ping_status').prop('checked') )
- data.ping_status = 'open';
</del><ins>+ wpCookies.set( 'wp-saving-post-' + post_id, 'check' );
+ });
+ }
</ins><span class="cx">
</span><del>- if ( $('#auto_draft').val() == '1' )
- data.auto_draft = '1';
</del><ins>+ // Strip whitespace and compare two strings
+ function compare( str1, str2 ) {
+ function removeSpaces( string ) {
+ return string.toString().replace(/[\x20\t\r\n\f]+/g, '');
+ }
</ins><span class="cx">
</span><del>- return data;
-};
</del><ins>+ return ( removeSpaces( str1 || '' ) === removeSpaces( str2 || '' ) );
+ }
</ins><span class="cx">
</span><del>-// Concatenate title, content and excerpt. Used to track changes when auto-saving.
-wp.autosave.getCompareString = function( post_data ) {
- if ( typeof post_data === 'object' ) {
- return ( post_data.post_title || '' ) + '::' + ( post_data.content || '' ) + '::' + ( post_data.excerpt || '' );
- }
</del><ins>+ /**
+ * Check if the saved data for the current post (if any) is different than the loaded post data on the screen
+ *
+ * Shows a standard message letting the user restore the post data if different.
+ *
+ * @return void
+ */
+ function checkPost() {
+ var content, post_title, excerpt, $notice,
+ postData = getSavedPostData(),
+ cookie = wpCookies.get( 'wp-saving-post-' + post_id );
</ins><span class="cx">
</span><del>- return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' );
-};
</del><ins>+ if ( ! postData ) {
+ return;
+ }
</ins><span class="cx">
</span><del>-wp.autosave.local = {
</del><ins>+ if ( cookie ) {
+ wpCookies.remove( 'wp-saving-post-' + post_id );
</ins><span class="cx">
</span><del>- lastSavedData: '',
- blog_id: 0,
- hasStorage: false,
</del><ins>+ if ( cookie === 'saved' ) {
+ // The post was saved properly, remove old data and bail
+ setData( false );
+ return;
+ }
+ }
</ins><span class="cx">
</span><del>- // Check if the browser supports sessionStorage and it's not disabled
- checkStorage: function() {
- var test = Math.random(), result = false;
</del><ins>+ // There is a newer autosave. Don't show two "restore" notices at the same time.
+ if ( $( '#has-newer-autosave' ).length ) {
+ return;
+ }
</ins><span class="cx">
</span><del>- try {
- sessionStorage.setItem('wp-test', test);
- result = sessionStorage.getItem('wp-test') == test;
- sessionStorage.removeItem('wp-test');
- } catch(e) {}
</del><ins>+ content = $( '#content' ).val() || '';
+ post_title = $( '#title' ).val() || '';
+ excerpt = $( '#excerpt' ).val() || '';
</ins><span class="cx">
</span><del>- this.hasStorage = result;
- return result;
- },
</del><ins>+ // 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 ) ) {
</ins><span class="cx">
</span><del>- /**
- * Initialize the local storage
- *
- * @return mixed False if no sessionStorage in the browser or an Object containing all post_data for this blog
- */
- getStorage: function() {
- var stored_obj = false;
- // Separate local storage containers for each blog_id
- if ( this.hasStorage && this.blog_id ) {
- stored_obj = sessionStorage.getItem( 'wp-autosave-' + this.blog_id );
</del><ins>+ return;
+ }
</ins><span class="cx">
</span><del>- if ( stored_obj )
- stored_obj = JSON.parse( stored_obj );
- else
- stored_obj = {};
- }
</del><ins>+ restorePostData = postData;
+ undoPostData = {
+ content: content,
+ post_title: post_title,
+ excerpt: excerpt
+ };
</ins><span class="cx">
</span><del>- return stored_obj;
- },
</del><ins>+ $notice = $( '#local-storage-notice' );
+ $('.wrap h2').first().after( $notice.addClass( 'updated' ).show() );
</ins><span class="cx">
</span><del>- /**
- * Set the storage for this blog
- *
- * Confirms that the data was saved successfully.
- *
- * @return bool
- */
- setStorage: function( stored_obj ) {
- var key;
</del><ins>+ $notice.on( 'click.autosae-local', function( event ) {
+ var $target = $( event.target );
</ins><span class="cx">
</span><del>- if ( this.hasStorage && this.blog_id ) {
- key = 'wp-autosave-' + this.blog_id;
- sessionStorage.setItem( key, JSON.stringify( stored_obj ) );
- return sessionStorage.getItem( key ) !== null;
- }
</del><ins>+ if ( $target.hasClass( 'restore-backup' ) ) {
+ restorePost( restorePostData );
+ $target.parent().hide();
+ $(this).find( 'p.undo-restore' ).show();
+ } else if ( $target.hasClass( 'undo-restore-backup' ) ) {
+ restorePost( undoPostData );
+ $target.parent().hide();
+ $(this).find( 'p.local-restore' ).show();
+ }
</ins><span class="cx">
</span><del>- return false;
- },
</del><ins>+ event.preventDefault();
+ });
+ }
</ins><span class="cx">
</span><del>- /**
- * Get the saved post data for the current post
- *
- * @return mixed False if no storage or no data or the post_data as an Object
- */
- getData: function() {
- var stored = this.getStorage(), post_id = $('#post_ID').val();
</del><ins>+ // Restore the current title, content and excerpt from postData.
+ function restorePost( postData ) {
+ var editor;
</ins><span class="cx">
</span><del>- if ( !stored || !post_id )
- return false;
</del><ins>+ if ( postData ) {
+ // Set the last saved data
+ lastCompareString = getCompareString( postData );
</ins><span class="cx">
</span><del>- return stored[ 'post_' + post_id ] || false;
- },
</del><ins>+ if ( $( '#title' ).val() !== postData.post_title ) {
+ $( '#title' ).focus().val( postData.post_title || '' );
+ }
</ins><span class="cx">
</span><del>- /**
- * Set (save or delete) post data in the storage.
- *
- * If stored_data evaluates to 'false' the storage key for the current post will be removed
- *
- * $param stored_data The post data to store or null/false/empty to delete the key
- * @return bool
- */
- setData: function( stored_data ) {
- var stored = this.getStorage(), post_id = $('#post_ID').val();
</del><ins>+ $( '#excerpt' ).val( postData.excerpt || '' );
+ editor = typeof tinymce !== 'undefined' && tinymce.get('content');
</ins><span class="cx">
</span><del>- if ( !stored || !post_id )
- return false;
</del><ins>+ if ( editor && ! editor.isHidden() && typeof switchEditors !== 'undefined' ) {
+ // Make sure there's an undo level in the editor
+ editor.undoManager.add();
+ editor.setContent( postData.content ? switchEditors.wpautop( postData.content ) : '' );
+ } else {
+ // Make sure the Text editor is selected
+ $( '#content-html' ).click();
+ $( '#content' ).val( postData.content );
+ }
</ins><span class="cx">
</span><del>- if ( stored_data )
- stored[ 'post_' + post_id ] = stored_data;
- else if ( stored.hasOwnProperty( 'post_' + post_id ) )
- delete stored[ 'post_' + post_id ];
- else
- return false;
</del><ins>+ return true;
+ }
</ins><span class="cx">
</span><del>- return this.setStorage(stored);
- },
</del><ins>+ return false;
+ }
</ins><span class="cx">
</span><del>- /**
- * Save post data for the current post
- *
- * Runs on a 15 sec. schedule, saves when there are differences in the post title or content.
- * When the optional data is provided, updates the last saved post data.
- *
- * $param data optional Object The post data for saving, minimum 'post_title' and 'content'
- * @return bool
- */
- save: function( data ) {
- var result = false, post_data, compareString;
</del><ins>+ // Initialize and run checkPost() on loading the script (before TinyMCE init)
+ blog_id = typeof window.autosaveL10n !== 'undefined' && window.autosaveL10n.blog_id;
</ins><span class="cx">
</span><del>- if ( ! data ) {
- post_data = wp.autosave.getPostData();
- } else {
- post_data = this.getData() || {};
- $.extend( post_data, data );
- post_data.autosave = true;
- }
</del><ins>+ // Check if the browser supports sessionStorage and it's not disabled
+ if ( ! checkStorage() ) {
+ return;
+ }
</ins><span class="cx">
</span><del>- // Cannot get the post data at the moment
- if ( ! post_data.autosave )
- return false;
</del><ins>+ // Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'.
+ if ( ! blog_id || ( ! $('#content').length && ! $('#excerpt').length ) ) {
+ return;
+ }
</ins><span class="cx">
</span><del>- compareString = wp.autosave.getCompareString( post_data );
</del><ins>+ $document.ready( run );
</ins><span class="cx">
</span><del>- // If the content, title and excerpt did not change since the last save, don't save again
- if ( compareString == this.lastSavedData )
- return false;
</del><ins>+ return {
+ hasStorage: hasStorage,
+ getSavedPostData: getSavedPostData,
+ save: save
+ };
+ }
</ins><span class="cx">
</span><del>- post_data.save_time = (new Date()).getTime();
- post_data.status = $('#post_status').val() || '';
- result = this.setData( post_data );
</del><ins>+ // Autosave on the server
+ function autosaveServer() {
+ var _disabled, _blockSave, _blockSaveTimer, previousCompareString, lastCompareString,
+ nextRun = 0;
</ins><span class="cx">
</span><del>- if ( result )
- this.lastSavedData = compareString;
</del><ins>+ // Block saving for the next 10 sec.
+ function tempBlockSave() {
+ _blockSave = true;
+ window.clearTimeout( _blockSaveTimer );
</ins><span class="cx">
</span><del>- return result;
- },
</del><ins>+ _blockSaveTimer = window.setTimeout( function() {
+ _blockSave = false;
+ }, 10000 );
+ }
</ins><span class="cx">
</span><del>- // Initialize and run checkPost() on loading the script (before TinyMCE init)
- init: function( settings ) {
- var self = this;
</del><ins>+ // Runs on heartbeat-response
+ function response( data ) {
+ _schedule();
+ _blockSave = false;
+ lastCompareString = previousCompareString;
+ previousCompareString = '';
</ins><span class="cx">
</span><del>- // Check if the browser supports sessionStorage and it's not disabled
- if ( ! this.checkStorage() )
- return;
</del><ins>+ $document.trigger( 'after-autosave', [data] );
+ $( '.autosave-message' ).text( data.message );
+ enableButtons();
</ins><span class="cx">
</span><del>- // Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'.
- if ( ! $('#content').length && ! $('#excerpt').length )
- return;
</del><ins>+ if ( data.success ) {
+ // No longer an auto-draft
+ $( '#auto_draft' ).val('');
+ }
+ }
</ins><span class="cx">
</span><del>- if ( settings )
- $.extend( this, settings );
</del><ins>+ /**
+ * Disable autosave
+ *
+ * Intended to run on form.submit
+ */
+ function disable() {
+ _disabled = true;
+ }
</ins><span class="cx">
</span><del>- if ( !this.blog_id )
- this.blog_id = typeof window.autosaveL10n != 'undefined' ? window.autosaveL10n.blog_id : 0;
</del><ins>+ /**
+ * Save immediately
+ *
+ * Resets the timing and tells heartbeat to connect now
+ *
+ * @return void
+ */
+ function triggerSave() {
+ nextRun = 0;
+ wp.heartbeat.connectNow();
+ }
</ins><span class="cx">
</span><del>- $(document).ready( function(){ self.run(); } );
- },
</del><ins>+ /**
+ * Checks if the post content in the textarea has changed since page load.
+ *
+ * This also happens when TinyMCE is active and editor.save() is triggered by
+ * wp.autosave.getPostData().
+ *
+ * @return bool
+ */
+ function postChanged() {
+ return getCompareString() !== initialCompareString;
+ }
</ins><span class="cx">
</span><del>- // Run on DOM ready
- run: function() {
- var self = this;
</del><ins>+ // Runs on 'heartbeat-send'
+ function save() {
+ var postData, compareString;
</ins><span class="cx">
</span><del>- // Check if the local post data is different than the loaded post data.
- this.checkPost();
</del><ins>+ if ( isSuspended || _disabled || _blockSave ) {
+ return false;
+ }
</ins><span class="cx">
</span><del>- // Set the schedule
- this.schedule = $.schedule({
- time: 15 * 1000,
- func: function() { wp.autosave.local.save(); },
- repeat: true,
- protect: true
- });
</del><ins>+ if ( ( new Date() ).getTime() < nextRun ) {
+ return false;
+ }
</ins><span class="cx">
</span><del>- $('form#post').on('submit.autosave-local', function() {
- var editor = typeof tinymce != 'undefined' && tinymce.get('content'), post_id = $('#post_ID').val() || 0;
</del><ins>+ postData = getPostData();
+ compareString = getCompareString( postData );
</ins><span class="cx">
</span><del>- if ( editor && ! editor.isHidden() ) {
- // Last onSubmit event in the editor, needs to run after the content has been moved to the textarea.
- editor.onSubmit.add( function() {
- wp.autosave.local.save({
- post_title: $('#title').val() || '',
- content: $('#content').val() || '',
- excerpt: $('#excerpt').val() || ''
- });
- });
- } else {
- self.save({
- post_title: $('#title').val() || '',
- content: $('#content').val() || '',
- excerpt: $('#excerpt').val() || ''
- });
- }
</del><ins>+ // First check
+ if ( typeof lastCompareString === 'undefined' ) {
+ lastCompareString = initialCompareString;
+ }
</ins><span class="cx">
</span><del>- wpCookies.set( 'wp-saving-post-' + post_id, 'check' );
- });
- },
</del><ins>+ // No change
+ if ( compareString === lastCompareString ) {
+ return false;
+ }
</ins><span class="cx">
</span><del>- // Strip whitespace and compare two strings
- compare: function( str1, str2 ) {
- function remove( string ) {
- return string.toString().replace(/[\x20\t\r\n\f]+/g, '');
- }
</del><ins>+ previousCompareString = compareString;
+ tempBlockSave();
+ disableButtons();
</ins><span class="cx">
</span><del>- return ( remove( str1 || '' ) == remove( str2 || '' ) );
- },
</del><ins>+ $document.trigger( 'wpcountwords', [ postData.content ] )
+ .trigger( 'before-autosave', [ postData ] );
</ins><span class="cx">
</span><del>- /**
- * Check if the saved data for the current post (if any) is different than the loaded post data on the screen
- *
- * Shows a standard message letting the user restore the post data if different.
- *
- * @return void
- */
- checkPost: function() {
- var self = this, post_data = this.getData(), content, post_title, excerpt, notice,
- post_id = $('#post_ID').val() || 0, cookie = wpCookies.get( 'wp-saving-post-' + post_id );
</del><ins>+ $( '.autosave-message' ).text( autosaveL10n.savingText );
+ postData._wpnonce = $( '#_wpnonce' ).val() || '';
</ins><span class="cx">
</span><del>- if ( ! post_data )
- return;
</del><ins>+ return postData;
+ }
</ins><span class="cx">
</span><del>- if ( cookie ) {
- wpCookies.remove( 'wp-saving-post-' + post_id );
-
- if ( cookie == 'saved' ) {
- // The post was saved properly, remove old data and bail
- this.setData( false );
- return;
</del><ins>+ function _schedule() {
+ nextRun = ( new Date() ).getTime() + ( autosaveL10n.autosaveInterval * 1000 ) || 60000;
</ins><span class="cx"> }
</span><del>- }
</del><span class="cx">
</span><del>- // There is a newer autosave. Don't show two "restore" notices at the same time.
- if ( $('#has-newer-autosave').length )
- return;
</del><ins>+ $document.on( 'heartbeat-send.autosave', function( event, data ) {
+ var autosaveData = save();
</ins><span class="cx">
</span><del>- content = $('#content').val() || '';
- post_title = $('#title').val() || '';
- excerpt = $('#excerpt').val() || '';
</del><ins>+ if ( autosaveData ) {
+ data.wp_autosave = autosaveData;
+ }
+ }).on( 'heartbeat-tick.autosave', function( event, data ) {
+ if ( data.wp_autosave ) {
+ response( data.wp_autosave );
+ }
+ }).on( 'heartbeat-connection-lost.autosave', function( event, error, status ) {
+ // When connection is lost, keep user from submitting changes.
+ if ( 'timeout' === error || 603 === status ) {
+ var $notice = $('#lost-connection-notice');
</ins><span class="cx">
</span><del>- if ( $('#wp-content-wrap').hasClass('tmce-active') && typeof switchEditors != 'undefined' )
- content = switchEditors.pre_wpautop( content );
</del><ins>+ if ( ! wp.autosave.local.hasStorage ) {
+ $notice.find('.hide-if-no-sessionstorage').hide();
+ }
</ins><span class="cx">
</span><del>- // cookie == 'check' means the post was not saved properly, always show #local-storage-notice
- if ( cookie != 'check' && this.compare( content, post_data.content ) && this.compare( post_title, post_data.post_title ) && this.compare( excerpt, post_data.excerpt ) ) {
- return;
</del><ins>+ $notice.show();
+ disableButtons();
+ }
+ }).on( 'heartbeat-connection-restored.autosave', function() {
+ $('#lost-connection-notice').hide();
+ enableButtons();
+ }).ready( function() {
+ _schedule();
+ });
+
+ return {
+ disable: disable,
+ tempBlockSave: tempBlockSave,
+ triggerSave: triggerSave,
+ postChanged: postChanged
+ };
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- this.restore_post_data = post_data;
- this.undo_post_data = {
- content: content,
- post_title: post_title,
- excerpt: excerpt
- };
-
- notice = $('#local-storage-notice');
- $('.wrap h2').first().after( notice.addClass('updated').show() );
-
- notice.on( 'click', function(e) {
- var target = $( e.target );
-
- if ( target.hasClass('restore-backup') ) {
- self.restorePost( self.restore_post_data );
- target.parent().hide();
- $(this).find('p.undo-restore').show();
- } else if ( target.hasClass('undo-restore-backup') ) {
- self.restorePost( self.undo_post_data );
- target.parent().hide();
- $(this).find('p.local-restore').show();
</del><ins>+ // Wait for TinyMCE to initialize plus 1 sec. for any external css to finish loading,
+ // then 'save' to the textarea before setting initialCompareString.
+ // This avoids any insignificant differences between the initial textarea content and the content
+ // extracted from the editor.
+ $document.on( 'tinymce-editor-init.autosave', function( event, editor ) {
+ if ( editor.id === 'content' ) {
+ window.setTimeout( function() {
+ editor.save();
+ initialCompareString = getCompareString();
+ }, 1000 );
</ins><span class="cx"> }
</span><del>-
- e.preventDefault();
</del><ins>+ }).ready( function() {
+ // Set the initial compare string in case TinyMCE is not used or not loaded first
+ initialCompareString = getCompareString();
</ins><span class="cx"> });
</span><del>- },
</del><span class="cx">
</span><del>- // Restore the current title, content and excerpt from post_data.
- restorePost: function( post_data ) {
- var editor;
-
- if ( post_data ) {
- // Set the last saved data
- this.lastSavedData = wp.autosave.getCompareString( post_data );
-
- if ( $('#title').val() != post_data.post_title )
- $('#title').focus().val( post_data.post_title || '' );
-
- $('#excerpt').val( post_data.excerpt || '' );
- editor = typeof tinymce != 'undefined' && tinymce.get('content');
-
- if ( editor && ! editor.isHidden() && typeof switchEditors != 'undefined' ) {
- // Make sure there's an undo level in the editor
- editor.undoManager.add();
- editor.setContent( post_data.content ? switchEditors.wpautop( post_data.content ) : '' );
- } else {
- // Make sure the Text editor is selected
- $('#content-html').click();
- $('#content').val( post_data.content );
- }
-
- return true;
- }
-
- return false;
</del><ins>+ return {
+ getPostData: getPostData,
+ getCompareString: getCompareString,
+ disableButtons: disableButtons,
+ enableButtons: enableButtons,
+ suspend: suspend,
+ resume: resume,
+ local: autosaveLocal(),
+ server: autosaveServer()
+ };
</ins><span class="cx"> }
</span><del>-};
</del><span class="cx">
</span><del>-wp.autosave.local.init();
</del><ins>+ window.wp = window.wp || {};
+ window.wp.autosave = autosave();
</ins><span class="cx">
</span><del>-}(jQuery));
</del><ins>+}( jQuery, window ));
</ins></span></pre></div>
<a id="trunksrcwpincludesrevisionphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/revision.php (26994 => 26995)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/revision.php 2014-01-22 02:28:13 UTC (rev 26994)
+++ trunk/src/wp-includes/revision.php 2014-01-22 04:55:37 UTC (rev 26995)
</span><span class="lines">@@ -215,7 +215,7 @@
</span><span class="cx"> *
</span><span class="cx"> * @param int|object|array $post Post ID, post object OR post array.
</span><span class="cx"> * @param bool $autosave Optional. Is the revision an autosave?
</span><del>- * @return mixed Null or 0 if error, new revision ID if success.
</del><ins>+ * @return mixed WP_Error or 0 if error, new revision ID if success.
</ins><span class="cx"> */
</span><span class="cx"> function _wp_put_post_revision( $post = null, $autosave = false ) {
</span><span class="cx"> if ( is_object($post) )
</span><span class="lines">@@ -223,8 +223,8 @@
</span><span class="cx"> elseif ( !is_array($post) )
</span><span class="cx"> $post = get_post($post, ARRAY_A);
</span><span class="cx">
</span><del>- if ( !$post || empty($post['ID']) )
- return;
</del><ins>+ if ( ! $post || empty($post['ID']) )
+ return new WP_Error( 'invalid_post', __( 'Invalid post ID' ) );
</ins><span class="cx">
</span><span class="cx"> if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
</span><span class="cx"> return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
</span></span></pre></div>
<a id="trunktestsphpunitincludestestcaseajaxphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/includes/testcase-ajax.php (26994 => 26995)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/includes/testcase-ajax.php 2014-01-22 02:28:13 UTC (rev 26994)
+++ trunk/tests/phpunit/includes/testcase-ajax.php 2014-01-22 04:55:37 UTC (rev 26995)
</span><span class="lines">@@ -42,12 +42,12 @@
</span><span class="cx"> 'oembed_cache', 'image-editor', 'delete-comment', 'delete-tag', 'delete-link',
</span><span class="cx"> 'delete-meta', 'delete-post', 'trash-post', 'untrash-post', 'delete-page', 'dim-comment',
</span><span class="cx"> 'add-link-category', 'add-tag', 'get-tagcloud', 'get-comments', 'replyto-comment',
</span><del>- 'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'autosave', 'closed-postboxes',
</del><ins>+ 'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'closed-postboxes',
</ins><span class="cx"> 'hidden-columns', 'update-welcome-panel', 'menu-get-metabox', 'wp-link-ajax',
</span><span class="cx"> 'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',
</span><span class="cx"> 'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
</span><span class="cx"> 'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
</span><del>- 'wp-remove-post-lock', 'dismiss-wp-pointer', 'nopriv_autosave'
</del><ins>+ 'wp-remove-post-lock', 'dismiss-wp-pointer', 'heartbeat', 'nopriv_heartbeat',
</ins><span class="cx"> );
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestsajaxAutosavephp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/ajax/Autosave.php (26994 => 26995)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/ajax/Autosave.php 2014-01-22 02:28:13 UTC (rev 26994)
+++ trunk/tests/phpunit/tests/ajax/Autosave.php 2014-01-22 04:55:37 UTC (rev 26995)
</span><span class="lines">@@ -23,92 +23,158 @@
</span><span class="cx"> protected $_post = null;
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * user_id
+ * @var int
+ */
+ protected $user_id = 0;
+
+ /**
</ins><span class="cx"> * Set up the test fixture
</span><span class="cx"> */
</span><span class="cx"> public function setUp() {
</span><span class="cx"> parent::setUp();
</span><ins>+ // Set a user so the $post has 'post_author'
+ $this->user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
+ wp_set_current_user( $this->user_id );
+
</ins><span class="cx"> $post_id = $this->factory->post->create( array( 'post_status' => 'draft' ) );
</span><span class="cx"> $this->_post = get_post( $post_id );
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Tear down the test fixture.
+ * Reset the current user
+ */
+ public function tearDown() {
+ parent::tearDown();
+ wp_set_current_user( 0 );
+ }
+
+ /**
</ins><span class="cx"> * Test autosaving a post
</span><span class="cx"> * @return void
</span><span class="cx"> */
</span><span class="cx"> public function test_autosave_post() {
</span><ins>+ // The original post_author
+ wp_set_current_user( $this->user_id );
</ins><span class="cx">
</span><del>- // Become an admin
- $this->_setRole( 'administrator' );
-
</del><span class="cx"> // Set up the $_POST request
</span><span class="cx"> $md5 = md5( uniqid() );
</span><span class="cx"> $_POST = array(
</span><del>- 'post_id' => $this->_post->ID,
- 'autosavenonce' => wp_create_nonce( 'autosave' ),
- 'post_content' => $this->_post->post_content . PHP_EOL . $md5,
- 'post_type' => 'post',
- 'autosave' => 1,
</del><ins>+ 'action' => 'heartbeat',
+ '_nonce' => wp_create_nonce( 'heartbeat-nonce' ),
+ 'data' => array(
+ 'wp_autosave' => array(
+ 'post_id' => $this->_post->ID,
+ '_wpnonce' => wp_create_nonce( 'update-post_' . $this->_post->ID ),
+ 'post_content' => $this->_post->post_content . PHP_EOL . $md5,
+ 'post_type' => 'post',
+ ),
+ ),
</ins><span class="cx"> );
</span><span class="cx">
</span><span class="cx"> // Make the request
</span><span class="cx"> try {
</span><del>- $this->_handleAjax( 'autosave' );
</del><ins>+ $this->_handleAjax( 'heartbeat' );
</ins><span class="cx"> } catch ( WPAjaxDieContinueException $e ) {
</span><span class="cx"> unset( $e );
</span><span class="cx"> }
</span><span class="cx">
</span><del>- // Get the response
- $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
</del><ins>+ // Get the response, it is in heartbeat's response
+ $response = json_decode( $this->_last_response, true );
</ins><span class="cx">
</span><span class="cx"> // Ensure everything is correct
</span><del>- $this->assertEquals( $this->_post->ID, (int) $xml->response[0]->autosave['id'] );
- $this->assertEquals( 'autosave_' . $this->_post->ID, (string) $xml->response['action']);
</del><ins>+ $this->assertNotEmpty( $response['wp_autosave'] );
+ $this->assertTrue( $response['wp_autosave']['success'] );
</ins><span class="cx">
</span><span class="cx"> // Check that the edit happened
</span><del>- $post = get_post( $this->_post->ID) ;
</del><ins>+ $post = get_post( $this->_post->ID );
</ins><span class="cx"> $this->assertGreaterThanOrEqual( 0, strpos( $post->post_content, $md5 ) );
</span><span class="cx"> }
</span><del>-
</del><ins>+
</ins><span class="cx"> /**
</span><del>- * Test with an invalid nonce
</del><ins>+ * Test autosaving a locked post
</ins><span class="cx"> * @return void
</span><span class="cx"> */
</span><del>- public function test_with_invalid_nonce( ) {
</del><ins>+ public function test_autosave_locked_post() {
+ // Lock the post to another user
+ $another_user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
+ wp_set_current_user( $another_user_id );
+ wp_set_post_lock( $this->_post->ID );
</ins><span class="cx">
</span><del>- // Become an administrator
- $this->_setRole( 'administrator' );
</del><ins>+ wp_set_current_user( $this->user_id );
</ins><span class="cx">
</span><ins>+ // Ensure post is locked
+ $this->assertEquals( $another_user_id, wp_check_post_lock( $this->_post->ID ) );
+
</ins><span class="cx"> // Set up the $_POST request
</span><ins>+ $md5 = md5( uniqid() );
</ins><span class="cx"> $_POST = array(
</span><del>- 'post_id' => $this->_post->ID,
- 'autosavenonce' => md5( uniqid() ),
- 'autosave' => 1
</del><ins>+ 'action' => 'heartbeat',
+ '_nonce' => wp_create_nonce( 'heartbeat-nonce' ),
+ 'data' => array(
+ 'wp_autosave' => array(
+ 'post_id' => $this->_post->ID,
+ '_wpnonce' => wp_create_nonce( 'update-post_' . $this->_post->ID ),
+ 'post_content' => $this->_post->post_content . PHP_EOL . $md5,
+ 'post_type' => 'post',
+ ),
+ ),
</ins><span class="cx"> );
</span><span class="cx">
</span><span class="cx"> // Make the request
</span><del>- $this->setExpectedException( 'WPAjaxDieStopException', '-1' );
- $this->_handleAjax( 'autosave' );
</del><ins>+ try {
+ $this->_handleAjax( 'heartbeat' );
+ } catch ( WPAjaxDieContinueException $e ) {
+ unset( $e );
+ }
+
+ $response = json_decode( $this->_last_response, true );
+
+ // Ensure everything is correct
+ $this->assertNotEmpty( $response['wp_autosave'] );
+ $this->assertTrue( $response['wp_autosave']['success'] );
+
+ // Check that the original post was NOT edited
+ $post = get_post( $this->_post->ID );
+ $this->assertFalse( strpos( $post->post_content, $md5 ) );
+
+ // Check if the autosave post was created
+ $autosave = wp_get_post_autosave( $this->_post->ID, get_current_user_id() );
+ $this->assertNotEmpty( $autosave );
+ $this->assertGreaterThanOrEqual( 0, strpos( $autosave->post_content, $md5 ) );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Test with a bad post id
</del><ins>+ * Test with an invalid nonce
</ins><span class="cx"> * @return void
</span><span class="cx"> */
</span><del>- public function test_with_invalid_post_id( ) {
</del><ins>+ public function test_with_invalid_nonce( ) {
</ins><span class="cx">
</span><del>- // Become an administrator
- $this->_setRole( 'administrator' );
</del><ins>+ wp_set_current_user( $this->user_id );
</ins><span class="cx">
</span><span class="cx"> // Set up the $_POST request
</span><span class="cx"> $_POST = array(
</span><del>- 'post_id' => 0,
- 'autosavenonce' => wp_create_nonce( 'autosave' ),
- 'autosave' => 1,
- 'post_type' => 'post'
</del><ins>+ 'action' => 'heartbeat',
+ '_nonce' => wp_create_nonce( 'heartbeat-nonce' ),
+ 'data' => array(
+ 'wp_autosave' => array(
+ 'post_id' => $this->_post->ID,
+ '_wpnonce' => substr( md5( uniqid() ), 0, 10 ),
+ ),
+ ),
</ins><span class="cx"> );
</span><span class="cx">
</span><span class="cx"> // Make the request
</span><del>- $this->setExpectedException( 'WPAjaxDieStopException', 'You are not allowed to edit this post.' );
- $this->_handleAjax( 'autosave' );
</del><ins>+ try {
+ $this->_handleAjax( 'heartbeat' );
+ } catch ( WPAjaxDieContinueException $e ) {
+ unset( $e );
+ }
+
+ $response = json_decode( $this->_last_response, true );
+
+ $this->assertNotEmpty( $response['wp_autosave'] );
+ $this->assertFalse( $response['wp_autosave']['success'] );
</ins><span class="cx"> }
</span><span class="cx"> }
</span></span></pre>
</div>
</div>
</body>
</html>