Ticket #23665: 23665-3.patch
File 23665-3.patch, 12.9 KB (added by , 12 years ago) |
---|
-
wp-admin/includes/ajax-actions.php
1043 1043 unset($_POST['post_category']); 1044 1044 1045 1045 $do_autosave = (bool) $_POST['autosave']; 1046 $do_lock = true;1047 1046 $data = ''; 1048 1047 $supplemental = array(); 1049 1048 $id = $revision_id = 0; 1050 1049 1051 /* translators: draft saved date format, see http://php.net/date */ 1052 $draft_saved_date_format = __('g:i:s a'); 1053 /* translators: %s: date and time */ 1054 $message = sprintf( __('Draft saved at %s.'), date_i18n( $draft_saved_date_format ) ); 1050 if ( ! $user_id = get_current_user_id() ) 1051 wp_die('-1'); 1055 1052 1056 1053 $post_id = (int) $_POST['post_id']; 1057 1054 $_POST['ID'] = $_POST['post_ID'] = $post_id; … … 1059 1056 if ( 'auto-draft' == $post->post_status ) 1060 1057 $_POST['post_status'] = 'draft'; 1061 1058 1062 if ( $last = wp_check_post_lock( $post->ID ) ) {1063 // This will change after we have per-user autosaves1064 $do_autosave = $do_lock = false;1065 1066 $last_user = get_userdata( $last );1067 $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );1068 $data = __( 'Autosave disabled.' );1069 1070 $supplemental['disable_autosave'] = 'disable';1071 }1072 1073 1059 if ( 'page' == $post->post_type ) { 1074 1060 if ( !current_user_can('edit_page', $post->ID) ) 1075 1061 wp_die( __( 'You are not allowed to edit this page.' ) ); … … 1079 1065 } 1080 1066 1081 1067 if ( $do_autosave ) { 1082 // Drafts and auto-drafts are just overwritten by autosave 1083 if ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status) {1068 // Drafts and auto-drafts are just overwritten by autosave for the same user 1069 if ( $user_id == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) { 1084 1070 $id = edit_post(); 1085 } else { // Non drafts are not overwritten. The autosave is stored in a special post revision .1071 } else { // Non drafts are not overwritten. The autosave is stored in a special post revision for each user. 1086 1072 $revision_id = wp_create_post_autosave( $post->ID ); 1087 1073 if ( is_wp_error($revision_id) ) 1088 1074 $id = $revision_id; 1089 1075 else 1090 1076 $id = $post->ID; 1091 1077 } 1092 $data = $message; 1078 1079 if ( is_wp_error($id) ) { 1080 // is_wp_error($id) overwrites $data in WP_Ajax_Response but no point in doing wp_create_nonce('update-post_' . $id) below 1081 // todo: Needs review. The errors generated in WP_Ajax_Response and parsed with wpAjax.parseAjaxResponse() haven't been used for many years. 1082 $data = $id; 1083 $id = 0; 1084 } else { 1085 /* translators: draft saved date format, see http://php.net/date */ 1086 $draft_saved_date_format = __('g:i:s a'); 1087 /* translators: %s: date and time */ 1088 $data = sprintf( __('Draft saved at %s.'), date_i18n( $draft_saved_date_format ) ); 1089 } 1093 1090 } else { 1094 1091 if ( ! empty( $_POST['auto_draft'] ) ) 1095 1092 $id = 0; // This tells us it didn't actually save … … 1103 1100 $supplemental['replace-samplepermalinknonce'] = wp_create_nonce('samplepermalink'); 1104 1101 $supplemental['replace-closedpostboxesnonce'] = wp_create_nonce('closedpostboxes'); 1105 1102 $supplemental['replace-_ajax_linking_nonce'] = wp_create_nonce( 'internal-linking' ); 1106 if ( $id ) { 1107 if ( $_POST['post_type'] == 'post' ) 1108 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id); 1109 elseif ( $_POST['post_type'] == 'page' ) 1110 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-page_' . $id); 1111 } 1103 if ( $id ) 1104 $supplemental['replace-_wpnonce'] = wp_create_nonce('update-post_' . $id); 1112 1105 } 1113 1106 1114 1107 $x = new WP_Ajax_Response( array( -
wp-admin/includes/post.php
1266 1266 if ( is_wp_error( $translated ) ) 1267 1267 return $translated; 1268 1268 1269 // Only store one autosave. If there is already an autosave, overwrite it. 1270 if ( $old_autosave = wp_get_post_autosave( $post_id ) ) { 1269 $post_author = get_current_user_id(); 1270 1271 // Store one autosave per author. If there is already an autosave, overwrite it. 1272 if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) { 1271 1273 $new_autosave = _wp_post_revision_fields( $_POST, true ); 1272 1274 $new_autosave['ID'] = $old_autosave->ID; 1273 $new_autosave['post_author'] = get_current_user_id();1275 $new_autosave['post_author'] = $post_author; 1274 1276 return wp_update_post( $new_autosave ); 1275 1277 } 1276 1278 … … 1325 1327 wp_die(__('You are not allowed to edit this post.')); 1326 1328 } 1327 1329 1328 if ( 'draft' == $post->post_status ) { 1330 $user_id = get_current_user_id(); 1331 if ( 'draft' == $post->post_status && $user_id == $post->post_author ) { 1329 1332 $id = edit_post(); 1330 1333 } else { // Non drafts are not overwritten. The autosave is stored in a special post revision. 1331 1334 $id = wp_create_post_autosave( $post->ID ); … … 1336 1339 if ( is_wp_error($id) ) 1337 1340 wp_die( $id->get_error_message() ); 1338 1341 1339 if ( $_POST['post_status'] == 'draft' ) {1342 if ( $_POST['post_status'] == 'draft' && $user_id == $post->post_author ) { 1340 1343 $url = add_query_arg( 'preview', 'true', get_permalink($id) ); 1341 1344 } else { 1342 1345 $nonce = wp_create_nonce('post_preview_' . $id); -
wp-admin/post.php
173 173 174 174 if ( ! wp_check_post_lock( $post->ID ) ) { 175 175 $active_post_lock = wp_set_post_lock( $post->ID ); 176 177 if ( 'attachment' !== $post_type )178 wp_enqueue_script('autosave');179 176 } 180 177 181 178 add_action( 'admin_footer', '_admin_notice_post_locked' ); 182 179 180 if ( 'attachment' !== $post_type ) 181 wp_enqueue_script('autosave'); 182 183 183 $title = $post_type_object->labels->edit_item; 184 184 $post = get_post($post_id, OBJECT, 'edit'); 185 185 -
wp-includes/js/autosave.js
1 var autosave, autosaveLast = '', autosavePeriodical, autosave OldMessage = '', autosaveDelayPreview = false, notSaved = true, blockSave = false, fullscreen, autosaveLockRelease = true;1 var autosave, autosaveLast = '', autosavePeriodical, autosaveDelayPreview = false, notSaved = true, blockSave = false, fullscreen, autosaveLockRelease = true; 2 2 3 3 jQuery(document).ready( function($) { 4 4 … … 130 130 } 131 131 }); 132 132 133 function autosave_parse_response( response) {134 var res = wpAjax.parseAjaxResponse(response, 'autosave'), message = '', postID, sup;133 function autosave_parse_response( response ) { 134 var res = wpAjax.parseAjaxResponse(response, 'autosave'), post_id, sup; 135 135 136 136 if ( res && res.responses && res.responses.length ) { 137 message = res.responses[0].data; // The saved message or error.138 // someone else is editing: disable autosave, set errors139 137 if ( res.responses[0].supplemental ) { 140 138 sup = res.responses[0].supplemental; 141 if ( 'disable' == sup['disable_autosave'] ) {142 autosave = function() {};143 autosaveLockRelease = false;144 res = { errors: true };145 }146 139 147 if ( sup['active-post-lock'] ) { 148 jQuery('#active_post_lock').val( sup['active-post-lock'] ); 149 } 150 151 if ( sup['alert'] ) { 152 jQuery('#autosave-alert').remove(); 153 jQuery('#titlediv').after('<div id="autosave-alert" class="error below-h2"><p>' + sup['alert'] + '</p></div>'); 154 } 155 156 jQuery.each(sup, function(selector, value) { 157 if ( selector.match(/^replace-/) ) { 158 jQuery('#'+selector.replace('replace-', '')).val(value); 159 } 140 jQuery.each( sup, function( selector, value ) { 141 if ( selector.match(/^replace-/) ) 142 jQuery( '#' + selector.replace('replace-', '') ).val( value ); 160 143 }); 161 144 } 162 145 163 // if no errors: add slug UI 146 // if no errors: add slug UI and update autosave-message 164 147 if ( !res.errors ) { 165 postID = parseInt( res.responses[0].id, 10 ); 166 if ( !isNaN(postID) && postID > 0 ) { 167 autosave_update_slug(postID); 168 } 148 if ( post_id = parseInt( res.responses[0].id, 10 ) ) 149 autosave_update_slug( post_id ); 150 151 if ( res.responses[0].data ) // update autosave message 152 jQuery('.autosave-message').text( res.responses[0].data ); 169 153 } 170 154 } 171 if ( message ) { // update autosave message 172 jQuery('.autosave-message').html(message); 173 } else if ( autosaveOldMessage && res ) { 174 jQuery('.autosave-message').html( autosaveOldMessage ); 175 } 155 176 156 return res; 177 157 } 178 158 … … 186 166 // called when autosaving new post 187 167 function autosave_saved_new(response) { 188 168 blockSave = false; 189 var res = autosave_parse_response(response), post ID;169 var res = autosave_parse_response(response), post_id; 190 170 191 171 if ( res && res.responses.length && !res.errors ) { 192 172 // An ID is sent only for real auto-saves, not for autosave=0 "keepalive" saves 193 postID = parseInt( res.responses[0].id, 10 ); 194 if ( !isNaN(postID) && postID > 0 ) { 173 post_id = parseInt( res.responses[0].id, 10 ); 174 175 if ( post_id ) { 195 176 notSaved = false; 196 177 jQuery('#auto_draft').val('0'); // No longer an auto-draft 197 178 } 179 198 180 autosave_enable_buttons(); 181 199 182 if ( autosaveDelayPreview ) { 200 183 autosaveDelayPreview = false; 201 184 doPreview(); … … 286 269 successCallback = autosave_saved; // pre-existing post 287 270 } 288 271 289 autosaveOldMessage = jQuery('#autosave').html();290 272 jQuery.ajax({ 291 273 data: post_data, 292 274 beforeSend: doAutoSave ? autosave_loading : null, -
wp-includes/revision.php
135 135 * Retrieve the autosaved data of the specified post. 136 136 * 137 137 * Returns a post object containing the information that was autosaved for the 138 * specified post. 138 * specified post. If the optional $user_id is passed, returns the autosave for that user 139 * otherwise returns the latest autosave. 139 140 * 140 141 * @package WordPress 141 142 * @subpackage Post_Revisions 142 143 * @since 2.6.0 143 * 144 * @uses wp_get_post_revisions() 145 * 144 146 * @param int $post_id The post ID. 147 * @param int $user_id optional The post author ID. 145 148 * @return object|bool The autosaved data or false on failure or when no autosave exists. 146 149 */ 147 function wp_get_post_autosave( $post_id ) { 150 function wp_get_post_autosave( $post_id, $user_id = 0 ) { 151 $revisions = wp_get_post_revisions($post_id); 148 152 149 if ( !$post = get_post( $post_id ) ) 150 return false; 153 foreach ( $revisions as $revision ) { 154 if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) { 155 if ( $user_id && $user_id != $revision->post_author ) 156 continue; 151 157 152 $q = array( 153 'name' => "{$post->ID}-autosave", 154 'post_parent' => $post->ID, 155 'post_type' => 'revision', 156 'post_status' => 'inherit' 157 ); 158 return $revision; 159 break; 160 } 161 } 158 162 159 // Use WP_Query so that the result gets cached160 $autosave_query = new WP_Query;161 162 add_action( 'parse_query', '_wp_get_post_autosave_hack' );163 $autosave = $autosave_query->query( $q );164 remove_action( 'parse_query', '_wp_get_post_autosave_hack' );165 166 if ( $autosave && is_array($autosave) && is_object($autosave[0]) )167 return $autosave[0];168 169 163 return false; 170 164 } 171 165 172 166 /** 173 * Internally used to hack WP_Query into submission.174 *175 * @package WordPress176 * @subpackage Post_Revisions177 * @since 2.6.0178 *179 * @param object $query WP_Query object180 */181 function _wp_get_post_autosave_hack( $query ) {182 $query->is_single = false;183 }184 185 /**186 167 * Determines if the specified post is a revision. 187 168 * 188 169 * @package WordPress … … 195 176 function wp_is_post_revision( $post ) { 196 177 if ( !$post = wp_get_post_revision( $post ) ) 197 178 return false; 179 198 180 return (int) $post->post_parent; 199 181 } 200 182 … … 211 193 function wp_is_post_autosave( $post ) { 212 194 if ( !$post = wp_get_post_revision( $post ) ) 213 195 return false; 214 if ( "{$post->post_parent}-autosave" !== $post->post_name ) 215 return false; 216 return (int) $post->post_parent; 196 197 if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) ) 198 return (int) $post->post_parent; 199 200 return false; 217 201 } 218 202 219 203 /** … … 234 218 $post = get_object_vars( $post ); 235 219 elseif ( !is_array($post) ) 236 220 $post = get_post($post, ARRAY_A); 221 237 222 if ( !$post || empty($post['ID']) ) 238 223 return; 239 224 … … 249 234 250 235 if ( $revision_id ) 251 236 do_action( '_wp_put_post_revision', $revision_id ); 237 252 238 return $revision_id; 253 239 } 254 240 … … 312 298 $fields = array_keys( _wp_post_revision_fields() ); 313 299 314 300 $update = array(); 315 foreach( array_intersect( array_keys( $revision ), $fields ) as $field ) 301 foreach( array_intersect( array_keys( $revision ), $fields ) as $field ) { 316 302 $update[$field] = $revision[$field]; 303 } 317 304 318 305 if ( !$update ) 319 306 return false; … … 374 361 * @return array empty if no revisions 375 362 */ 376 363 function wp_get_post_revisions( $post_id = 0, $args = null ) { 377 if ( ! WP_POST_REVISIONS )378 return array();379 364 if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) ) 380 365 return array(); 381 366 … … 385 370 386 371 if ( !$revisions = get_children( $args ) ) 387 372 return array(); 373 388 374 return $revisions; 389 375 } 390 376