mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-22 17:18:32 +01:00
Autosave: refactor autosave.js, use heartbeat for transport and move all "Add/Edit Post" related functionality to post.js. See #25272.
Built from https://develop.svn.wordpress.org/trunk@26995 git-svn-id: http://core.svn.wordpress.org/trunk@26872 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
b4d76115cd
commit
335add2573
@ -50,7 +50,7 @@ $core_actions_post = array(
|
||||
'oembed-cache', 'image-editor', 'delete-comment', 'delete-tag', 'delete-link',
|
||||
'delete-meta', 'delete-post', 'trash-post', 'untrash-post', 'delete-page', 'dim-comment',
|
||||
'add-link-category', 'add-tag', 'get-tagcloud', 'get-comments', 'replyto-comment',
|
||||
'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'autosave', 'closed-postboxes',
|
||||
'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'closed-postboxes',
|
||||
'hidden-columns', 'update-welcome-panel', 'menu-get-metabox', 'wp-link-ajax',
|
||||
'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',
|
||||
'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
|
||||
|
@ -402,7 +402,6 @@ if ( 'draft' != get_post_status( $post ) )
|
||||
|
||||
echo $form_extra;
|
||||
|
||||
wp_nonce_field( 'autosave', 'autosavenonce', false );
|
||||
wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
|
||||
wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
|
||||
?>
|
||||
|
@ -1089,68 +1089,6 @@ function wp_ajax_add_user( $action ) {
|
||||
$x->send();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
function wp_ajax_closed_postboxes() {
|
||||
check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
|
||||
$closed = isset( $_POST['closed'] ) ? explode( ',', $_POST['closed']) : array();
|
||||
|
@ -734,7 +734,6 @@ function wp_refresh_post_nonces( $response, $data, $screen_id ) {
|
||||
if ( 2 === wp_verify_nonce( $received['post_nonce'], 'update-post_' . $post_id ) ) {
|
||||
$response['wp-refresh-post-nonces'] = array(
|
||||
'replace' => array(
|
||||
'autosavenonce' => wp_create_nonce('autosave'),
|
||||
'getpermalinknonce' => wp_create_nonce('getpermalink'),
|
||||
'samplepermalinknonce' => wp_create_nonce('samplepermalink'),
|
||||
'closedpostboxesnonce' => wp_create_nonce('closedpostboxes'),
|
||||
@ -768,3 +767,29 @@ function wp_heartbeat_set_suspension( $settings ) {
|
||||
return $settings;
|
||||
}
|
||||
add_filter( 'heartbeat_settings', 'wp_heartbeat_set_suspension' );
|
||||
|
||||
/**
|
||||
* 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 );
|
||||
|
@ -79,9 +79,14 @@ function _wp_translate_postdata( $update = false, $post_data = null ) {
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $post_data['post_status'] ) )
|
||||
if ( ! empty( $post_data['post_status'] ) ) {
|
||||
$post_data['post_status'] = sanitize_key( $post_data['post_status'] );
|
||||
|
||||
// No longer an auto-draft
|
||||
if ( 'auto-draft' == $post_data['post_status'] )
|
||||
$post_data['post_status'] = 'draft';
|
||||
}
|
||||
|
||||
// What to do based on which button they pressed
|
||||
if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
|
||||
$post_data['post_status'] = 'draft';
|
||||
@ -190,9 +195,6 @@ function edit_post( $post_data = null ) {
|
||||
$post_data = _wp_translate_postdata( true, $post_data );
|
||||
if ( is_wp_error($post_data) )
|
||||
wp_die( $post_data->get_error_message() );
|
||||
if ( ( empty( $post_data['action'] ) || 'autosave' != $post_data['action'] ) && 'auto-draft' == $post_data['post_status'] ) {
|
||||
$post_data['post_status'] = 'draft';
|
||||
}
|
||||
|
||||
if ( isset($post_data['visibility']) ) {
|
||||
switch ( $post_data['visibility'] ) {
|
||||
@ -1335,22 +1337,30 @@ function _admin_notice_post_locked() {
|
||||
* @uses _wp_translate_postdata()
|
||||
* @uses _wp_post_revision_fields()
|
||||
*
|
||||
* @return unknown
|
||||
* @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.
|
||||
*/
|
||||
function wp_create_post_autosave( $post_id ) {
|
||||
$translated = _wp_translate_postdata( true );
|
||||
if ( is_wp_error( $translated ) )
|
||||
return $translated;
|
||||
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'];
|
||||
}
|
||||
|
||||
$post_data = _wp_translate_postdata( true, $post_data );
|
||||
if ( is_wp_error( $post_data ) )
|
||||
return $post_data;
|
||||
|
||||
$post_author = get_current_user_id();
|
||||
|
||||
// Store one autosave per author. If there is already an autosave, overwrite it.
|
||||
if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
|
||||
$new_autosave = _wp_post_revision_fields( $_POST, true );
|
||||
$new_autosave = _wp_post_revision_fields( $post_data, true );
|
||||
$new_autosave['ID'] = $old_autosave->ID;
|
||||
$new_autosave['post_author'] = $post_author;
|
||||
|
||||
// If the new autosave is the same content as the post, delete the old autosave.
|
||||
// If the new autosave has the same content as the post, delete the autosave.
|
||||
$post = get_post( $post_id );
|
||||
$autosave_is_different = false;
|
||||
foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
|
||||
@ -1362,14 +1372,14 @@ function wp_create_post_autosave( $post_id ) {
|
||||
|
||||
if ( ! $autosave_is_different ) {
|
||||
wp_delete_post_revision( $old_autosave->ID );
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return wp_update_post( $new_autosave );
|
||||
}
|
||||
|
||||
// _wp_put_post_revision() expects unescaped.
|
||||
$post_data = wp_unslash( $_POST );
|
||||
$post_data = wp_unslash( $post_data );
|
||||
|
||||
// Otherwise create the new autosave as a special post revision
|
||||
return _wp_put_post_revision( $post_data, true );
|
||||
@ -1395,58 +1405,82 @@ function wp_create_post_autosave( $post_id ) {
|
||||
function post_preview() {
|
||||
|
||||
$post_ID = (int) $_POST['post_ID'];
|
||||
$status = get_post_status( $post_ID );
|
||||
if ( 'auto-draft' == $status )
|
||||
wp_die( __('Preview not available. Please save as a draft first.') );
|
||||
|
||||
if ( isset($_POST['catslist']) )
|
||||
$_POST['post_category'] = explode(",", $_POST['catslist']);
|
||||
|
||||
if ( isset($_POST['tags_input']) )
|
||||
$_POST['tags_input'] = explode(",", $_POST['tags_input']);
|
||||
|
||||
if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
|
||||
unset($_POST['post_category']);
|
||||
|
||||
$_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.') );
|
||||
if ( ! $post = get_post( $post_ID ) )
|
||||
wp_die( __('You attempted to preview a non existing item.') );
|
||||
|
||||
if ( ! current_user_can( 'edit_post', $post->ID ) )
|
||||
wp_die( __('You are not allowed to preview this item.') );
|
||||
|
||||
$is_autosave = false;
|
||||
|
||||
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();
|
||||
} else {
|
||||
if ( ! current_user_can('edit_post', $post_ID) )
|
||||
wp_die( __('You are not allowed to edit this post.') );
|
||||
$is_autosave = true;
|
||||
|
||||
if ( 'auto-draft' == $_POST['post_status'] )
|
||||
$_POST['post_status'] = 'draft';
|
||||
|
||||
$saved_post_id = wp_create_post_autosave( $post->ID );
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
if ( is_wp_error( $saved_post_id ) )
|
||||
wp_die( $saved_post_id->get_error_message() );
|
||||
|
||||
if ( is_wp_error($id) )
|
||||
wp_die( $id->get_error_message() );
|
||||
$query_args = array( 'preview' => 'true' );
|
||||
|
||||
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,
|
||||
);
|
||||
if ( $is_autosave && $saved_post_id ) {
|
||||
$query_args['preview_id'] = $post->ID;
|
||||
$query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );
|
||||
|
||||
if ( isset( $_POST['post_format'] ) )
|
||||
$args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
|
||||
|
||||
$url = add_query_arg( $args, get_permalink($id) );
|
||||
$query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
|
||||
}
|
||||
|
||||
$url = add_query_arg( $query_args, get_permalink( $post->ID ) );
|
||||
return apply_filters( 'preview_post_link', $url );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 );
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
/* global postL10n, ajaxurl, wpAjax, setPostThumbnailL10n, postboxes, pagenow, tinymce, alert, deleteUserSetting, getUserSetting, setUserSetting */
|
||||
/* global theList:true, theExtraList:true, autosave:true */
|
||||
|
||||
var tagBox, commentsBox, editPermalink, makeSlugeditClickable, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint;
|
||||
var tagBox, commentsBox, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint;
|
||||
// Back-compat: prevent fatal errors
|
||||
makeSlugeditClickable = editPermalink = function(){};
|
||||
|
||||
// return an array with any duplicate, whitespace or values removed
|
||||
function array_unique_noempty(a) {
|
||||
@ -268,10 +270,9 @@ $(document).on( 'heartbeat-send.refresh-lock', function( e, data ) {
|
||||
send.lock = lock;
|
||||
|
||||
data['wp-refresh-post-lock'] = send;
|
||||
});
|
||||
|
||||
// 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 ) {
|
||||
}).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
|
||||
// Post locks: update the lock string or show the dialog if somebody has taken over editing
|
||||
var received, wrap, avatar;
|
||||
|
||||
if ( data['wp-refresh-post-lock'] ) {
|
||||
@ -282,19 +283,16 @@ $(document).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
|
||||
wrap = $('#post-lock-dialog');
|
||||
|
||||
if ( wrap.length && ! wrap.is(':visible') ) {
|
||||
if ( typeof autosave == 'function' ) {
|
||||
$(document).on('autosave-disable-buttons.post-lock', function() {
|
||||
wrap.addClass('saving');
|
||||
}).on('autosave-enable-buttons.post-lock', function() {
|
||||
if ( typeof wp != 'undefined' && wp.autosave ) {
|
||||
// Save the latest changes and disable
|
||||
$(document).one( 'heartbeat-tick', function() {
|
||||
wp.autosave.server.disable();
|
||||
wrap.removeClass('saving').addClass('saved');
|
||||
window.onbeforeunload = null;
|
||||
$(window).off( 'beforeunload.edit-post' );
|
||||
});
|
||||
|
||||
// Save the latest changes and disable
|
||||
if ( ! autosave() )
|
||||
window.onbeforeunload = null;
|
||||
|
||||
autosave = function(){};
|
||||
wrap.addClass('saving');
|
||||
wp.autosave.server.triggerSave();
|
||||
}
|
||||
|
||||
if ( received.lock_error.avatar_src ) {
|
||||
@ -309,6 +307,22 @@ $(document).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
|
||||
$('#active_post_lock').val( received.new_lock );
|
||||
}
|
||||
}
|
||||
}).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);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
}(jQuery));
|
||||
@ -354,8 +368,14 @@ $(document).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
|
||||
}(jQuery));
|
||||
|
||||
jQuery(document).ready( function($) {
|
||||
var stamp, visibility, updateVisibility, updateText,
|
||||
sticky = '', last = 0, co = $('#content');
|
||||
var stamp, visibility, $submitButtons,
|
||||
sticky = '',
|
||||
last = 0,
|
||||
co = $('#content'),
|
||||
$editSlugWrap = $('#edit-slug-box'),
|
||||
postId = $('#post_ID').val() || 0,
|
||||
$submitpost = $('#submitpost'),
|
||||
releaseLock = true;
|
||||
|
||||
postboxes.add_postbox_toggles(pagenow);
|
||||
|
||||
@ -380,6 +400,142 @@ jQuery(document).ready( function($) {
|
||||
wp.heartbeat.interval( 15 );
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// multi-taxonomies
|
||||
if ( $('#tagsdiv-post_tag').length ) {
|
||||
tagBox.init();
|
||||
@ -689,28 +845,22 @@ jQuery(document).ready( function($) {
|
||||
} // end submitdiv
|
||||
|
||||
// permalink
|
||||
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();
|
||||
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();
|
||||
|
||||
$('#view-post-btn').hide();
|
||||
b.html('<a href="#" class="save button button-small">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
|
||||
b.children('.save').click(function() {
|
||||
var new_slug = e.children('input').val();
|
||||
if ( new_slug == $('#editable-post-name-full').text() ) {
|
||||
return $('.cancel', '#edit-slug-buttons').click();
|
||||
return $('#edit-slug-buttons .cancel').click();
|
||||
}
|
||||
$.post(ajaxurl, {
|
||||
action: 'sample-permalink',
|
||||
post_id: post_id,
|
||||
post_id: postId,
|
||||
new_slug: new_slug,
|
||||
new_title: $('#title').val(),
|
||||
samplepermalinknonce: $('#samplepermalinknonce').val()
|
||||
@ -724,13 +874,12 @@ jQuery(document).ready( function($) {
|
||||
}
|
||||
b.html(revert_b);
|
||||
real_slug.val(new_slug);
|
||||
makeSlugeditClickable();
|
||||
$('#view-post-btn').show();
|
||||
});
|
||||
return false;
|
||||
});
|
||||
|
||||
$('.cancel', '#edit-slug-buttons').click(function() {
|
||||
$('#edit-slug-buttons .cancel').click(function() {
|
||||
$('#view-post-btn').show();
|
||||
e.html(revert_e);
|
||||
b.html(revert_b);
|
||||
@ -760,12 +909,13 @@ jQuery(document).ready( function($) {
|
||||
}).focus();
|
||||
};
|
||||
|
||||
makeSlugeditClickable = function() {
|
||||
$('#editable-post-name').click(function() {
|
||||
$('#edit-slug-buttons').children('.edit-slug').click();
|
||||
});
|
||||
};
|
||||
makeSlugeditClickable();
|
||||
$editSlugWrap.on( 'click', function( event ) {
|
||||
var $target = $( event.target );
|
||||
|
||||
if ( $target.is('#editable-post-name') || $target.hasClass('edit-slug') ) {
|
||||
editPermalink();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// word count
|
||||
|
2
wp-admin/js/post.min.js
vendored
2
wp-admin/js/post.min.js
vendored
File diff suppressed because one or more lines are too long
@ -307,7 +307,7 @@ case 'delete':
|
||||
break;
|
||||
|
||||
case 'preview':
|
||||
check_admin_referer( 'autosave', 'autosavenonce' );
|
||||
check_admin_referer( 'update-post_' . $post_id );
|
||||
|
||||
$url = post_preview();
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
2
wp-includes/js/autosave.min.js
vendored
2
wp-includes/js/autosave.min.js
vendored
File diff suppressed because one or more lines are too long
@ -215,7 +215,7 @@ function wp_is_post_autosave( $post ) {
|
||||
*
|
||||
* @param int|object|array $post Post ID, post object OR post array.
|
||||
* @param bool $autosave Optional. Is the revision an autosave?
|
||||
* @return mixed Null or 0 if error, new revision ID if success.
|
||||
* @return mixed WP_Error or 0 if error, new revision ID if success.
|
||||
*/
|
||||
function _wp_put_post_revision( $post = null, $autosave = false ) {
|
||||
if ( is_object($post) )
|
||||
@ -223,8 +223,8 @@ function _wp_put_post_revision( $post = null, $autosave = false ) {
|
||||
elseif ( !is_array($post) )
|
||||
$post = get_post($post, ARRAY_A);
|
||||
|
||||
if ( !$post || empty($post['ID']) )
|
||||
return;
|
||||
if ( ! $post || empty($post['ID']) )
|
||||
return new WP_Error( 'invalid_post', __( 'Invalid post ID' ) );
|
||||
|
||||
if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
|
||||
return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
|
||||
|
Loading…
Reference in New Issue
Block a user