diff --git a/wp-admin/admin-ajax.php b/wp-admin/admin-ajax.php index 8847cda8b9..d57d56b1d4 100644 --- a/wp-admin/admin-ajax.php +++ b/wp-admin/admin-ajax.php @@ -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', diff --git a/wp-admin/edit-form-advanced.php b/wp-admin/edit-form-advanced.php index 91bf275324..64a160cf6b 100644 --- a/wp-admin/edit-form-advanced.php +++ b/wp-admin/edit-form-advanced.php @@ -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 ); ?> diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index 99fc973828..dca18a12a1 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -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(); diff --git a/wp-admin/includes/misc.php b/wp-admin/includes/misc.php index be8e2b218e..10d72d5745 100644 --- a/wp-admin/includes/misc.php +++ b/wp-admin/includes/misc.php @@ -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 ); diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index 983e4e7023..b39d87578e 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -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 ); + } +} diff --git a/wp-admin/js/post.js b/wp-admin/js/post.js index 5e25a7a252..20a796fd87 100644 --- a/wp-admin/js/post.js +++ b/wp-admin/js/post.js @@ -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(''+postL10n.ok+' '+postL10n.cancel+''); 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 diff --git a/wp-admin/js/post.min.js b/wp-admin/js/post.min.js index f99b7fed6d..cd43c8b344 100644 --- a/wp-admin/js/post.min.js +++ b/wp-admin/js/post.min.js @@ -1 +1 @@ -function array_unique_noempty(a){var b=[];return jQuery.each(a,function(a,c){c=jQuery.trim(c),c&&-1==jQuery.inArray(c,b)&&b.push(c)}),b}var tagBox,commentsBox,editPermalink,makeSlugeditClickable,WPSetThumbnailHTML,WPSetThumbnailID,WPRemoveThumbnail,wptitlehint;!function(a){tagBox={clean:function(a){var b=postL10n.comma;return","!==b&&(a=a.replace(new RegExp(b,"g"),",")),a=a.replace(/\s*,\s*/g,",").replace(/,+/g,",").replace(/[,\s]+$/,"").replace(/^[,\s]+/,""),","!==b&&(a=a.replace(/,/g,b)),a},parseTags:function(b){var c=b.id,d=c.split("-check-num-")[1],e=a(b).closest(".tagsdiv"),f=e.find(".the-tags"),g=postL10n.comma,h=f.val().split(g),i=[];return delete h[d],a.each(h,function(b,c){c=a.trim(c),c&&i.push(c)}),f.val(this.clean(i.join(g))),this.quickClicks(e),!1},quickClicks:function(b){var c,d,e=a(".the-tags",b),f=a(".tagchecklist",b),g=a(b).attr("id");e.length&&(d=e.prop("disabled"),c=e.val().split(postL10n.comma),f.empty(),a.each(c,function(b,c){var e,h;c=a.trim(c),c&&(e=a("").text(c),d||(h=a('X'),h.click(function(){tagBox.parseTags(this)}),e.prepend(" ").prepend(h)),f.append(e))}))},flushTags:function(b,c,d){var e,f,g,h=a(".the-tags",b),i=a("input.newtag",b),j=postL10n.comma;return c=c||!1,g=c?a(c).text():i.val(),e=h.val(),f=e?e+j+g:g,f=this.clean(f),f=array_unique_noempty(f.split(j)).join(j),h.val(f),this.quickClicks(b),c||i.val(""),"undefined"==typeof d&&i.focus(),!1},get:function(b){var c=b.substr(b.indexOf("-")+1);a.post(ajaxurl,{action:"get-tagcloud",tax:c},function(d,e){(0===d||"success"!=e)&&(d=wpAjax.broken),d=a('

'+d+"

"),a("a",d).click(function(){return tagBox.flushTags(a(this).closest(".inside").children(".tagsdiv"),this),!1}),a("#"+b).after(d)})},init:function(){var b=this,c=a("div.ajaxtag");a(".tagsdiv").each(function(){tagBox.quickClicks(this)}),a("input.tagadd",c).click(function(){b.flushTags(a(this).closest(".tagsdiv"))}),a("div.taghint",c).click(function(){a(this).css("visibility","hidden").parent().siblings(".newtag").focus()}),a("input.newtag",c).blur(function(){""===this.value&&a(this).parent().siblings(".taghint").css("visibility","")}).focus(function(){a(this).parent().siblings(".taghint").css("visibility","hidden")}).keyup(function(b){return 13==b.which?(tagBox.flushTags(a(this).closest(".tagsdiv")),!1):void 0}).keypress(function(a){return 13==a.which?(a.preventDefault(),!1):void 0}).each(function(){var b=a(this).closest("div.tagsdiv").attr("id");a(this).suggest(ajaxurl+"?action=ajax-tag-search&tax="+b,{delay:500,minchars:2,multiple:!0,multipleSep:postL10n.comma+" "})}),a("#post").submit(function(){a("div.tagsdiv").each(function(){tagBox.flushTags(this,!1,1)})}),a("a.tagcloud-link").click(function(){return tagBox.get(a(this).attr("id")),a(this).unbind().click(function(){return a(this).siblings(".the-tagcloud").toggle(),!1}),!1})}},commentsBox={st:0,get:function(b,c){var d,e=this.st;return c||(c=20),this.st+=c,this.total=b,a("#commentsdiv .spinner").show(),d={action:"get-comments",mode:"single",_ajax_nonce:a("#add_comment_nonce").val(),p:a("#post_ID").val(),start:e,number:c},a.post(ajaxurl,d,function(b){return b=wpAjax.parseAjaxResponse(b),a("#commentsdiv .widefat").show(),a("#commentsdiv .spinner").hide(),"object"==typeof b&&b.responses[0]?(a("#the-comment-list").append(b.responses[0].data),theList=theExtraList=null,a("a[className*=':']").unbind(),commentsBox.st>commentsBox.total?a("#show-comments").hide():a("#show-comments").show().children("a").html(postL10n.showcomm),void 0):1==b?(a("#show-comments").html(postL10n.endcomm),void 0):(a("#the-comment-list").append(''+wpAjax.broken+""),void 0)}),!1}},WPSetThumbnailHTML=function(b){a(".inside","#postimagediv").html(b)},WPSetThumbnailID=function(b){var c=a('input[value="_thumbnail_id"]',"#list-table");c.size()>0&&a("#meta\\["+c.attr("id").match(/[0-9]+/)+"\\]\\[value\\]").text(b)},WPRemoveThumbnail=function(b){a.post(ajaxurl,{action:"set-post-thumbnail",post_id:a("#post_ID").val(),thumbnail_id:-1,_ajax_nonce:b,cookie:encodeURIComponent(document.cookie)},function(a){"0"==a?alert(setPostThumbnailL10n.error):WPSetThumbnailHTML(a)})},a(document).on("heartbeat-send.refresh-lock",function(b,c){var d=a("#active_post_lock").val(),e=a("#post_ID").val(),f={};e&&a("#post-lock-dialog").length&&(f.post_id=e,d&&(f.lock=d),c["wp-refresh-post-lock"]=f)}),a(document).on("heartbeat-tick.refresh-lock",function(b,c){var d,e,f;c["wp-refresh-post-lock"]&&(d=c["wp-refresh-post-lock"],d.lock_error?(e=a("#post-lock-dialog"),e.length&&!e.is(":visible")&&("function"==typeof autosave&&(a(document).on("autosave-disable-buttons.post-lock",function(){e.addClass("saving")}).on("autosave-enable-buttons.post-lock",function(){e.removeClass("saving").addClass("saved"),window.onbeforeunload=null}),autosave()||(window.onbeforeunload=null),autosave=function(){}),d.lock_error.avatar_src&&(f=a('').attr("src",d.lock_error.avatar_src.replace(/&/g,"&")),e.find("div.post-locked-avatar").empty().append(f)),e.show().find(".currently-editing").text(d.lock_error.text),e.find(".wp-tab-first").focus())):d.new_lock&&a("#active_post_lock").val(d.new_lock))})}(jQuery),function(a){function b(){c=!1,window.clearTimeout(d),d=window.setTimeout(function(){c=!0},3e5)}var c,d;a(document).on("heartbeat-send.wp-refresh-nonces",function(b,d){var e,f;c&&(f=a("#post_ID").val())&&(e=a("#_wpnonce").val())&&(d["wp-refresh-post-nonces"]={post_id:f,post_nonce:e})}).on("heartbeat-tick.wp-refresh-nonces",function(c,d){var e=d["wp-refresh-post-nonces"];e&&(b(),e.replace&&a.each(e.replace,function(b,c){a("#"+b).val(c)}),e.heartbeatNonce&&(window.heartbeatSettings.nonce=e.heartbeatNonce))}).ready(function(){b()})}(jQuery),jQuery(document).ready(function(a){var b,c,d,e,f="",g=0,h=a("#content");postboxes.add_postbox_toggles(pagenow),a("#post-lock-dialog .notification-dialog").on("keydown",function(b){if(9==b.which){var c=a(b.target);c.hasClass("wp-tab-first")&&b.shiftKey?(a(this).find(".wp-tab-last").focus(),b.preventDefault()):c.hasClass("wp-tab-last")&&!b.shiftKey&&(a(this).find(".wp-tab-first").focus(),b.preventDefault())}}).filter(":visible").find(".wp-tab-first").focus(),"undefined"!=typeof wp&&wp.heartbeat&&a("#post-lock-dialog").length&&wp.heartbeat.interval(15),a("#tagsdiv-post_tag").length?tagBox.init():a("#side-sortables, #normal-sortables, #advanced-sortables").children("div.postbox").each(function(){return 0===this.id.indexOf("tagsdiv-")?(tagBox.init(),!1):void 0}),a(".categorydiv").each(function(){var b,c,d,e,f,g=a(this).attr("id");d=g.split("-"),d.shift(),e=d.join("-"),f=e+"_tab","category"==e&&(f="cats"),a("a","#"+e+"-tabs").click(function(){var b=a(this).attr("href");return a(this).parent().addClass("tabs").siblings("li").removeClass("tabs"),a("#"+e+"-tabs").siblings(".tabs-panel").hide(),a(b).show(),"#"+e+"-all"==b?deleteUserSetting(f):setUserSetting(f,"pop"),!1}),getUserSetting(f)&&a('a[href="#'+e+'-pop"]',"#"+e+"-tabs").click(),a("#new"+e).one("focus",function(){a(this).val("").removeClass("form-input-tip")}),a("#new"+e).keypress(function(b){13===b.keyCode&&(b.preventDefault(),a("#"+e+"-add-submit").click())}),a("#"+e+"-add-submit").click(function(){a("#new"+e).focus()}),b=function(b){return a("#new"+e).val()?(b.data+="&"+a(":checked","#"+e+"checklist").serialize(),a("#"+e+"-add-submit").prop("disabled",!0),b):!1},c=function(b,c){var d,f=a("#new"+e+"_parent");a("#"+e+"-add-submit").prop("disabled",!1),"undefined"!=c.parsed.responses[0]&&(d=c.parsed.responses[0].supplemental.newcat_parent)&&(f.before(d),f.remove())},a("#"+e+"checklist").wpList({alt:"",response:e+"-ajax-response",addBefore:b,addAfter:c}),a("#"+e+"-add-toggle").click(function(){return a("#"+e+"-adder").toggleClass("wp-hidden-children"),a('a[href="#'+e+'-all"]',"#"+e+"-tabs").click(),a("#new"+e).focus(),!1}),a("#"+e+"checklist, #"+e+"checklist-pop").on("click",'li.popular-category > label input[type="checkbox"]',function(){var b=a(this),c=b.is(":checked"),d=b.val();d&&b.parents("#taxonomy-"+e).length&&a("#in-"+e+"-"+d+", #in-popular-"+e+"-"+d).prop("checked",c)})}),a("#postcustom").length&&a("#the-list").wpList({addAfter:function(){a("table#list-table").show()},addBefore:function(b){return b.data+="&post_id="+a("#post_ID").val(),b}}),a("#submitdiv").length&&(b=a("#timestamp").html(),c=a("#post-visibility-display").html(),d=function(){var b=a("#post-visibility-select");"public"!=a("input:radio:checked",b).val()?(a("#sticky").prop("checked",!1),a("#sticky-span").hide()):a("#sticky-span").show(),"password"!=a("input:radio:checked",b).val()?a("#password-span").hide():a("#password-span").show()},e=function(){if(!a("#timestampdiv").length)return!0;var c,d,e,f,g=a("#post_status"),h=a('option[value="publish"]',g),i=a("#aa").val(),j=a("#mm").val(),k=a("#jj").val(),l=a("#hh").val(),m=a("#mn").val();return c=new Date(i,j-1,k,l,m),d=new Date(a("#hidden_aa").val(),a("#hidden_mm").val()-1,a("#hidden_jj").val(),a("#hidden_hh").val(),a("#hidden_mn").val()),e=new Date(a("#cur_aa").val(),a("#cur_mm").val()-1,a("#cur_jj").val(),a("#cur_hh").val(),a("#cur_mn").val()),c.getFullYear()!=i||1+c.getMonth()!=j||c.getDate()!=k||c.getMinutes()!=m?(a(".timestamp-wrap","#timestampdiv").addClass("form-invalid"),!1):(a(".timestamp-wrap","#timestampdiv").removeClass("form-invalid"),c>e&&"future"!=a("#original_post_status").val()?(f=postL10n.publishOnFuture,a("#publish").val(postL10n.schedule)):e>=c&&"publish"!=a("#original_post_status").val()?(f=postL10n.publishOn,a("#publish").val(postL10n.publish)):(f=postL10n.publishOnPast,a("#publish").val(postL10n.update)),d.toUTCString()==c.toUTCString()?a("#timestamp").html(b):a("#timestamp").html(f+" "+postL10n.dateFormat.replace("%1$s",a('option[value="'+a("#mm").val()+'"]',"#mm").text()).replace("%2$s",k).replace("%3$s",i).replace("%4$s",l).replace("%5$s",m)+" "),"private"==a("input:radio:checked","#post-visibility-select").val()?(a("#publish").val(postL10n.update),0===h.length?g.append('"):h.html(postL10n.privatelyPublished),a('option[value="publish"]',g).prop("selected",!0),a(".edit-post-status","#misc-publishing-actions").hide()):("future"==a("#original_post_status").val()||"draft"==a("#original_post_status").val()?h.length&&(h.remove(),g.val(a("#hidden_post_status").val())):h.html(postL10n.published),g.is(":hidden")&&a(".edit-post-status","#misc-publishing-actions").show()),a("#post-status-display").html(a("option:selected",g).text()),"private"==a("option:selected",g).val()||"publish"==a("option:selected",g).val()?a("#save-post").hide():(a("#save-post").show(),"pending"==a("option:selected",g).val()?a("#save-post").show().val(postL10n.savePending):a("#save-post").show().val(postL10n.saveDraft)),!0)},a(".edit-visibility","#visibility").click(function(){return a("#post-visibility-select").is(":hidden")&&(d(),a("#post-visibility-select").slideDown("fast"),a(this).hide()),!1}),a(".cancel-post-visibility","#post-visibility-select").click(function(){return a("#post-visibility-select").slideUp("fast"),a("#visibility-radio-"+a("#hidden-post-visibility").val()).prop("checked",!0),a("#post_password").val(a("#hidden-post-password").val()),a("#sticky").prop("checked",a("#hidden-post-sticky").prop("checked")),a("#post-visibility-display").html(c),a(".edit-visibility","#visibility").show(),e(),!1}),a(".save-post-visibility","#post-visibility-select").click(function(){var b=a("#post-visibility-select");return b.slideUp("fast"),a(".edit-visibility","#visibility").show(),e(),"public"!=a("input:radio:checked",b).val()&&a("#sticky").prop("checked",!1),f=a("#sticky").prop("checked")?"Sticky":"",a("#post-visibility-display").html(postL10n[a("input:radio:checked",b).val()+f]),!1}),a("input:radio","#post-visibility-select").change(function(){d()}),a("#timestampdiv").siblings("a.edit-timestamp").click(function(){return a("#timestampdiv").is(":hidden")&&(a("#timestampdiv").slideDown("fast"),a("#mm").focus(),a(this).hide()),!1}),a(".cancel-timestamp","#timestampdiv").click(function(){return a("#timestampdiv").slideUp("fast"),a("#mm").val(a("#hidden_mm").val()),a("#jj").val(a("#hidden_jj").val()),a("#aa").val(a("#hidden_aa").val()),a("#hh").val(a("#hidden_hh").val()),a("#mn").val(a("#hidden_mn").val()),a("#timestampdiv").siblings("a.edit-timestamp").show(),e(),!1}),a(".save-timestamp","#timestampdiv").click(function(){return e()&&(a("#timestampdiv").slideUp("fast"),a("#timestampdiv").siblings("a.edit-timestamp").show()),!1}),a("#post").on("submit",function(b){return e()?void 0:(b.preventDefault(),a("#timestampdiv").show(),a("#publishing-action .spinner").hide(),a("#publish").prop("disabled",!1).removeClass("button-primary-disabled"),!1)}),a("#post-status-select").siblings("a.edit-post-status").click(function(){return a("#post-status-select").is(":hidden")&&(a("#post-status-select").slideDown("fast"),a(this).hide()),!1}),a(".save-post-status","#post-status-select").click(function(){return a("#post-status-select").slideUp("fast"),a("#post-status-select").siblings("a.edit-post-status").show(),e(),!1}),a(".cancel-post-status","#post-status-select").click(function(){return a("#post-status-select").slideUp("fast"),a("#post_status").val(a("#hidden_post_status").val()),a("#post-status-select").siblings("a.edit-post-status").show(),e(),!1})),a("#edit-slug-box").length&&(editPermalink=function(b){var c,d,e=0,f=a("#editable-post-name"),g=f.html(),h=a("#post_name"),i=h.val(),j=a("#edit-slug-buttons"),k=j.html(),l=a("#editable-post-name-full").html();for(a("#view-post-btn").hide(),j.html(''+postL10n.ok+' '+postL10n.cancel+""),j.children(".save").click(function(){var c=f.children("input").val();return c==a("#editable-post-name-full").text()?a(".cancel","#edit-slug-buttons").click():(a.post(ajaxurl,{action:"sample-permalink",post_id:b,new_slug:c,new_title:a("#title").val(),samplepermalinknonce:a("#samplepermalinknonce").val()},function(b){var d=a("#edit-slug-box");d.html(b),d.hasClass("hidden")&&d.fadeIn("fast",function(){d.removeClass("hidden")}),j.html(k),h.val(c),makeSlugeditClickable(),a("#view-post-btn").show()}),!1)}),a(".cancel","#edit-slug-buttons").click(function(){return a("#view-post-btn").show(),f.html(g),j.html(k),h.val(i),!1}),d=0;dl.length/4?"":l,f.html('').children("input").keypress(function(a){var b=a.keyCode||0;return 13==b?(j.children(".save").click(),!1):27==b?(j.children(".cancel").click(),!1):void 0}).keyup(function(){h.val(this.value)}).focus()},makeSlugeditClickable=function(){a("#editable-post-name").click(function(){a("#edit-slug-buttons").children(".edit-slug").click()})},makeSlugeditClickable()),"undefined"!=typeof wpWordCount&&(a(document).triggerHandler("wpcountwords",[h.val()]),h.keyup(function(b){var c=b.keyCode||b.charCode;return c==g?!0:((13==c||8==g||46==g)&&a(document).triggerHandler("wpcountwords",[h.val()]),g=c,!0)})),wptitlehint=function(b){b=b||"title";var c=a("#"+b),d=a("#"+b+"-prompt-text");""===c.val()&&d.removeClass("screen-reader-text"),d.click(function(){a(this).addClass("screen-reader-text"),c.focus()}),c.blur(function(){""===this.value&&d.removeClass("screen-reader-text")}).focus(function(){d.addClass("screen-reader-text")}).keydown(function(b){d.addClass("screen-reader-text"),a(this).unbind(b)})},wptitlehint(),function(){function b(a){f?d.theme.resizeTo(null,e+a.pageY):h.height(Math.max(50,e+a.pageY)),a.preventDefault()}function c(){var b,c;f?(d.focus(),c=a("#wp-content-editor-container .mce-toolbar-grp").height(),b=parseInt(a("#content_ifr").css("height"),10)+c-28):(h.focus(),b=parseInt(h.css("height"),10)),g.off("mousemove.wp-editor-resize mouseup.wp-editor-resize"),b&&b>50&&5e3>b&&setUserSetting("ed_size",b)}var d,e,f,g=a(document),h=a("textarea#content"),i=a("#post-status-info");!h.length||"ontouchstart"in window||(h.css("resize","none"),i.on("mousedown.wp-editor-resize",function(i){"undefined"!=typeof tinymce&&(d=tinymce.get("content")),d&&!d.isHidden()?(f=!0,e=a("#content_ifr").height()-i.pageY):(f=!1,e=h.height()-i.pageY,h.blur()),g.on("mousemove.wp-editor-resize",b).on("mouseup.wp-editor-resize",c),i.preventDefault()}))}(),"undefined"!=typeof tinymce&&a("#post-formats-select input.post-format").on("change.set-editor-class",function(){var b,c,d=this.id;d&&a(this).prop("checked")&&(b=tinymce.get("content"),b&&(c=b.getBody(),c.className=c.className.replace(/\bpost-format-[^ ]+/,""),b.dom.addClass(c,"post-format-0"==d?"post-format-standard":d)))})}); \ No newline at end of file +function array_unique_noempty(a){var b=[];return jQuery.each(a,function(a,c){c=jQuery.trim(c),c&&-1==jQuery.inArray(c,b)&&b.push(c)}),b}var tagBox,commentsBox,WPSetThumbnailHTML,WPSetThumbnailID,WPRemoveThumbnail,wptitlehint;makeSlugeditClickable=editPermalink=function(){},function(a){tagBox={clean:function(a){var b=postL10n.comma;return","!==b&&(a=a.replace(new RegExp(b,"g"),",")),a=a.replace(/\s*,\s*/g,",").replace(/,+/g,",").replace(/[,\s]+$/,"").replace(/^[,\s]+/,""),","!==b&&(a=a.replace(/,/g,b)),a},parseTags:function(b){var c=b.id,d=c.split("-check-num-")[1],e=a(b).closest(".tagsdiv"),f=e.find(".the-tags"),g=postL10n.comma,h=f.val().split(g),i=[];return delete h[d],a.each(h,function(b,c){c=a.trim(c),c&&i.push(c)}),f.val(this.clean(i.join(g))),this.quickClicks(e),!1},quickClicks:function(b){var c,d,e=a(".the-tags",b),f=a(".tagchecklist",b),g=a(b).attr("id");e.length&&(d=e.prop("disabled"),c=e.val().split(postL10n.comma),f.empty(),a.each(c,function(b,c){var e,h;c=a.trim(c),c&&(e=a("").text(c),d||(h=a('X'),h.click(function(){tagBox.parseTags(this)}),e.prepend(" ").prepend(h)),f.append(e))}))},flushTags:function(b,c,d){var e,f,g,h=a(".the-tags",b),i=a("input.newtag",b),j=postL10n.comma;return c=c||!1,g=c?a(c).text():i.val(),e=h.val(),f=e?e+j+g:g,f=this.clean(f),f=array_unique_noempty(f.split(j)).join(j),h.val(f),this.quickClicks(b),c||i.val(""),"undefined"==typeof d&&i.focus(),!1},get:function(b){var c=b.substr(b.indexOf("-")+1);a.post(ajaxurl,{action:"get-tagcloud",tax:c},function(d,e){(0===d||"success"!=e)&&(d=wpAjax.broken),d=a('

'+d+"

"),a("a",d).click(function(){return tagBox.flushTags(a(this).closest(".inside").children(".tagsdiv"),this),!1}),a("#"+b).after(d)})},init:function(){var b=this,c=a("div.ajaxtag");a(".tagsdiv").each(function(){tagBox.quickClicks(this)}),a("input.tagadd",c).click(function(){b.flushTags(a(this).closest(".tagsdiv"))}),a("div.taghint",c).click(function(){a(this).css("visibility","hidden").parent().siblings(".newtag").focus()}),a("input.newtag",c).blur(function(){""===this.value&&a(this).parent().siblings(".taghint").css("visibility","")}).focus(function(){a(this).parent().siblings(".taghint").css("visibility","hidden")}).keyup(function(b){return 13==b.which?(tagBox.flushTags(a(this).closest(".tagsdiv")),!1):void 0}).keypress(function(a){return 13==a.which?(a.preventDefault(),!1):void 0}).each(function(){var b=a(this).closest("div.tagsdiv").attr("id");a(this).suggest(ajaxurl+"?action=ajax-tag-search&tax="+b,{delay:500,minchars:2,multiple:!0,multipleSep:postL10n.comma+" "})}),a("#post").submit(function(){a("div.tagsdiv").each(function(){tagBox.flushTags(this,!1,1)})}),a("a.tagcloud-link").click(function(){return tagBox.get(a(this).attr("id")),a(this).unbind().click(function(){return a(this).siblings(".the-tagcloud").toggle(),!1}),!1})}},commentsBox={st:0,get:function(b,c){var d,e=this.st;return c||(c=20),this.st+=c,this.total=b,a("#commentsdiv .spinner").show(),d={action:"get-comments",mode:"single",_ajax_nonce:a("#add_comment_nonce").val(),p:a("#post_ID").val(),start:e,number:c},a.post(ajaxurl,d,function(b){return b=wpAjax.parseAjaxResponse(b),a("#commentsdiv .widefat").show(),a("#commentsdiv .spinner").hide(),"object"==typeof b&&b.responses[0]?(a("#the-comment-list").append(b.responses[0].data),theList=theExtraList=null,a("a[className*=':']").unbind(),commentsBox.st>commentsBox.total?a("#show-comments").hide():a("#show-comments").show().children("a").html(postL10n.showcomm),void 0):1==b?(a("#show-comments").html(postL10n.endcomm),void 0):(a("#the-comment-list").append(''+wpAjax.broken+""),void 0)}),!1}},WPSetThumbnailHTML=function(b){a(".inside","#postimagediv").html(b)},WPSetThumbnailID=function(b){var c=a('input[value="_thumbnail_id"]',"#list-table");c.size()>0&&a("#meta\\["+c.attr("id").match(/[0-9]+/)+"\\]\\[value\\]").text(b)},WPRemoveThumbnail=function(b){a.post(ajaxurl,{action:"set-post-thumbnail",post_id:a("#post_ID").val(),thumbnail_id:-1,_ajax_nonce:b,cookie:encodeURIComponent(document.cookie)},function(a){"0"==a?alert(setPostThumbnailL10n.error):WPSetThumbnailHTML(a)})},a(document).on("heartbeat-send.refresh-lock",function(b,c){var d=a("#active_post_lock").val(),e=a("#post_ID").val(),f={};e&&a("#post-lock-dialog").length&&(f.post_id=e,d&&(f.lock=d),c["wp-refresh-post-lock"]=f)}).on("heartbeat-tick.refresh-lock",function(b,c){var d,e,f;c["wp-refresh-post-lock"]&&(d=c["wp-refresh-post-lock"],d.lock_error?(e=a("#post-lock-dialog"),e.length&&!e.is(":visible")&&("undefined"!=typeof wp&&wp.autosave&&(a(document).one("heartbeat-tick",function(){wp.autosave.server.disable(),e.removeClass("saving").addClass("saved"),a(window).off("beforeunload.edit-post")}),e.addClass("saving"),wp.autosave.server.triggerSave()),d.lock_error.avatar_src&&(f=a('').attr("src",d.lock_error.avatar_src.replace(/&/g,"&")),e.find("div.post-locked-avatar").empty().append(f)),e.show().find(".currently-editing").text(d.lock_error.text),e.find(".wp-tab-first").focus())):d.new_lock&&a("#active_post_lock").val(d.new_lock))}).on("after-autosave.update-post-slug",function(){a("#edit-slug-box > *").length||a.post(ajaxurl,{action:"sample-permalink",post_id:a("#post_ID").val(),new_title:"undefined"!=typeof fullscreen&&fullscreen.settings.visible?a("#wp-fullscreen-title").val():a("#title").val(),samplepermalinknonce:a("#samplepermalinknonce").val()},function(b){"-1"!=b&&a("#edit-slug-box").html(b)})})}(jQuery),function(a){function b(){c=!1,window.clearTimeout(d),d=window.setTimeout(function(){c=!0},3e5)}var c,d;a(document).on("heartbeat-send.wp-refresh-nonces",function(b,d){var e,f;c&&(f=a("#post_ID").val())&&(e=a("#_wpnonce").val())&&(d["wp-refresh-post-nonces"]={post_id:f,post_nonce:e})}).on("heartbeat-tick.wp-refresh-nonces",function(c,d){var e=d["wp-refresh-post-nonces"];e&&(b(),e.replace&&a.each(e.replace,function(b,c){a("#"+b).val(c)}),e.heartbeatNonce&&(window.heartbeatSettings.nonce=e.heartbeatNonce))}).ready(function(){b()})}(jQuery),jQuery(document).ready(function(a){function b(){var b,c=0,d=a("#editable-post-name"),e=d.html(),f=a("#post_name"),g=f.val(),h=a("#edit-slug-buttons"),i=h.html(),k=a("#editable-post-name-full").html();for(a("#view-post-btn").hide(),h.html(''+postL10n.ok+' '+postL10n.cancel+""),h.children(".save").click(function(){var b=d.children("input").val();return b==a("#editable-post-name-full").text()?a("#edit-slug-buttons .cancel").click():(a.post(ajaxurl,{action:"sample-permalink",post_id:j,new_slug:b,new_title:a("#title").val(),samplepermalinknonce:a("#samplepermalinknonce").val()},function(c){var d=a("#edit-slug-box");d.html(c),d.hasClass("hidden")&&d.fadeIn("fast",function(){d.removeClass("hidden")}),h.html(i),f.val(b),a("#view-post-btn").show()}),!1)}),a("#edit-slug-buttons .cancel").click(function(){return a("#view-post-btn").show(),d.html(e),h.html(i),f.val(g),!1}),b=0;bk.length/4?"":k,d.html('').children("input").keypress(function(a){var b=a.keyCode||0;return 13==b?(h.children(".save").click(),!1):27==b?(h.children(".cancel").click(),!1):void 0}).keyup(function(){f.val(this.value)}).focus()}var c,d,e,f="",g=0,h=a("#content"),i=a("#edit-slug-box"),j=a("#post_ID").val()||0,k=a("#submitpost"),l=!0;postboxes.add_postbox_toggles(pagenow),a("#post-lock-dialog .notification-dialog").on("keydown",function(b){if(9==b.which){var c=a(b.target);c.hasClass("wp-tab-first")&&b.shiftKey?(a(this).find(".wp-tab-last").focus(),b.preventDefault()):c.hasClass("wp-tab-last")&&!b.shiftKey&&(a(this).find(".wp-tab-first").focus(),b.preventDefault())}}).filter(":visible").find(".wp-tab-first").focus(),"undefined"!=typeof wp&&wp.heartbeat&&a("#post-lock-dialog").length&&wp.heartbeat.interval(15),e=k.find(":button, :submit, a.submitdelete, #post-preview").on("click.autosave",function(b){var c=a(this);return c.prop("disabled")?(b.preventDefault(),void 0):(c.hasClass("submitdelete")||a("form#post").off("submit.edit-post").on("submit.edit-post",function(b){b.isDefaultPrevented()||(wp.autosave.server.disable(),l=!1,a(window).off("beforeunload.edit-post"),e.prop("disabled",!0).addClass("button-disabled"),"publish"===c.attr("id")?k.find("#major-publishing-actions .spinner").show():k.find("#minor-publishing .spinner").show())}),void 0)}),a("#post-preview").on("click.post-preview",function(b){var c=a(this),d=a("form#post"),e=a("input#wp-preview"),f=c.attr("target")||"wp-preview",g=navigator.userAgent.toLowerCase();b.preventDefault(),c.prop("disabled")||("undefined"!=typeof wp&&wp.autosave&&wp.autosave.server.tempBlockSave(),e.val("dopreview"),d.attr("target",f).submit().attr("target",""),-1!==g.indexOf("safari")&&-1===g.indexOf("chrome")&&d.attr("action",function(a,b){return b+"?t="+(new Date).getTime()}),e.val(""))}),a("#title").on("keydown.editor-focus",function(b){var c;9!==b.keyCode||b.ctrlKey||b.altKey||b.shiftKey||(c="undefined"!=typeof tinymce&&tinymce.get("content"),c&&!c.isHidden()?c.focus():a("#content").focus(),b.preventDefault())}),a("#auto_draft").val()&&a("#title").blur(function(){this.value&&"1"===a("#auto_draft").val()&&"undefined"!=typeof wp&&wp.autosave&&wp.autosave.server.triggerSave()}),a(document).on("autosave-disable-buttons.edit-post",function(){e.prop("disabled",!0).addClass("button-disabled")}).on("autosave-enable-buttons.edit-post",function(){window.wp&&window.wp.heartbeat&&window.wp.heartbeat.hasConnectionError()||e.prop("disabled",!1).removeClass("button-disabled")}),a(window).on("beforeunload.edit-post",function(){var a="undefined"!=typeof tinymce&&tinymce.get("content");return a&&!a.isHidden()&&a.isDirty()||"undefined"!=typeof wp&&wp.autosave&&wp.autosave.server.postChanged()?autosaveL10n.saveAlert:void 0}).on("unload.edit-post",function(b){l&&(b.target&&"#document"!=b.target.nodeName||a.ajax({type:"POST",url:ajaxurl,async:!1,data:{action:"wp-remove-post-lock",_wpnonce:a("#_wpnonce").val(),post_ID:a("#post_ID").val(),active_post_lock:a("#active_post_lock").val()}}))}),a("#tagsdiv-post_tag").length?tagBox.init():a("#side-sortables, #normal-sortables, #advanced-sortables").children("div.postbox").each(function(){return 0===this.id.indexOf("tagsdiv-")?(tagBox.init(),!1):void 0}),a(".categorydiv").each(function(){var b,c,d,e,f,g=a(this).attr("id");d=g.split("-"),d.shift(),e=d.join("-"),f=e+"_tab","category"==e&&(f="cats"),a("a","#"+e+"-tabs").click(function(){var b=a(this).attr("href");return a(this).parent().addClass("tabs").siblings("li").removeClass("tabs"),a("#"+e+"-tabs").siblings(".tabs-panel").hide(),a(b).show(),"#"+e+"-all"==b?deleteUserSetting(f):setUserSetting(f,"pop"),!1}),getUserSetting(f)&&a('a[href="#'+e+'-pop"]',"#"+e+"-tabs").click(),a("#new"+e).one("focus",function(){a(this).val("").removeClass("form-input-tip")}),a("#new"+e).keypress(function(b){13===b.keyCode&&(b.preventDefault(),a("#"+e+"-add-submit").click())}),a("#"+e+"-add-submit").click(function(){a("#new"+e).focus()}),b=function(b){return a("#new"+e).val()?(b.data+="&"+a(":checked","#"+e+"checklist").serialize(),a("#"+e+"-add-submit").prop("disabled",!0),b):!1},c=function(b,c){var d,f=a("#new"+e+"_parent");a("#"+e+"-add-submit").prop("disabled",!1),"undefined"!=c.parsed.responses[0]&&(d=c.parsed.responses[0].supplemental.newcat_parent)&&(f.before(d),f.remove())},a("#"+e+"checklist").wpList({alt:"",response:e+"-ajax-response",addBefore:b,addAfter:c}),a("#"+e+"-add-toggle").click(function(){return a("#"+e+"-adder").toggleClass("wp-hidden-children"),a('a[href="#'+e+'-all"]',"#"+e+"-tabs").click(),a("#new"+e).focus(),!1}),a("#"+e+"checklist, #"+e+"checklist-pop").on("click",'li.popular-category > label input[type="checkbox"]',function(){var b=a(this),c=b.is(":checked"),d=b.val();d&&b.parents("#taxonomy-"+e).length&&a("#in-"+e+"-"+d+", #in-popular-"+e+"-"+d).prop("checked",c)})}),a("#postcustom").length&&a("#the-list").wpList({addAfter:function(){a("table#list-table").show()},addBefore:function(b){return b.data+="&post_id="+a("#post_ID").val(),b}}),a("#submitdiv").length&&(c=a("#timestamp").html(),d=a("#post-visibility-display").html(),updateVisibility=function(){var b=a("#post-visibility-select");"public"!=a("input:radio:checked",b).val()?(a("#sticky").prop("checked",!1),a("#sticky-span").hide()):a("#sticky-span").show(),"password"!=a("input:radio:checked",b).val()?a("#password-span").hide():a("#password-span").show()},updateText=function(){if(!a("#timestampdiv").length)return!0;var b,d,e,f,g=a("#post_status"),h=a('option[value="publish"]',g),i=a("#aa").val(),j=a("#mm").val(),k=a("#jj").val(),l=a("#hh").val(),m=a("#mn").val();return b=new Date(i,j-1,k,l,m),d=new Date(a("#hidden_aa").val(),a("#hidden_mm").val()-1,a("#hidden_jj").val(),a("#hidden_hh").val(),a("#hidden_mn").val()),e=new Date(a("#cur_aa").val(),a("#cur_mm").val()-1,a("#cur_jj").val(),a("#cur_hh").val(),a("#cur_mn").val()),b.getFullYear()!=i||1+b.getMonth()!=j||b.getDate()!=k||b.getMinutes()!=m?(a(".timestamp-wrap","#timestampdiv").addClass("form-invalid"),!1):(a(".timestamp-wrap","#timestampdiv").removeClass("form-invalid"),b>e&&"future"!=a("#original_post_status").val()?(f=postL10n.publishOnFuture,a("#publish").val(postL10n.schedule)):e>=b&&"publish"!=a("#original_post_status").val()?(f=postL10n.publishOn,a("#publish").val(postL10n.publish)):(f=postL10n.publishOnPast,a("#publish").val(postL10n.update)),d.toUTCString()==b.toUTCString()?a("#timestamp").html(c):a("#timestamp").html(f+" "+postL10n.dateFormat.replace("%1$s",a('option[value="'+a("#mm").val()+'"]',"#mm").text()).replace("%2$s",k).replace("%3$s",i).replace("%4$s",l).replace("%5$s",m)+" "),"private"==a("input:radio:checked","#post-visibility-select").val()?(a("#publish").val(postL10n.update),0===h.length?g.append('"):h.html(postL10n.privatelyPublished),a('option[value="publish"]',g).prop("selected",!0),a(".edit-post-status","#misc-publishing-actions").hide()):("future"==a("#original_post_status").val()||"draft"==a("#original_post_status").val()?h.length&&(h.remove(),g.val(a("#hidden_post_status").val())):h.html(postL10n.published),g.is(":hidden")&&a(".edit-post-status","#misc-publishing-actions").show()),a("#post-status-display").html(a("option:selected",g).text()),"private"==a("option:selected",g).val()||"publish"==a("option:selected",g).val()?a("#save-post").hide():(a("#save-post").show(),"pending"==a("option:selected",g).val()?a("#save-post").show().val(postL10n.savePending):a("#save-post").show().val(postL10n.saveDraft)),!0)},a(".edit-visibility","#visibility").click(function(){return a("#post-visibility-select").is(":hidden")&&(updateVisibility(),a("#post-visibility-select").slideDown("fast"),a(this).hide()),!1}),a(".cancel-post-visibility","#post-visibility-select").click(function(){return a("#post-visibility-select").slideUp("fast"),a("#visibility-radio-"+a("#hidden-post-visibility").val()).prop("checked",!0),a("#post_password").val(a("#hidden-post-password").val()),a("#sticky").prop("checked",a("#hidden-post-sticky").prop("checked")),a("#post-visibility-display").html(d),a(".edit-visibility","#visibility").show(),updateText(),!1}),a(".save-post-visibility","#post-visibility-select").click(function(){var b=a("#post-visibility-select");return b.slideUp("fast"),a(".edit-visibility","#visibility").show(),updateText(),"public"!=a("input:radio:checked",b).val()&&a("#sticky").prop("checked",!1),f=a("#sticky").prop("checked")?"Sticky":"",a("#post-visibility-display").html(postL10n[a("input:radio:checked",b).val()+f]),!1}),a("input:radio","#post-visibility-select").change(function(){updateVisibility()}),a("#timestampdiv").siblings("a.edit-timestamp").click(function(){return a("#timestampdiv").is(":hidden")&&(a("#timestampdiv").slideDown("fast"),a("#mm").focus(),a(this).hide()),!1}),a(".cancel-timestamp","#timestampdiv").click(function(){return a("#timestampdiv").slideUp("fast"),a("#mm").val(a("#hidden_mm").val()),a("#jj").val(a("#hidden_jj").val()),a("#aa").val(a("#hidden_aa").val()),a("#hh").val(a("#hidden_hh").val()),a("#mn").val(a("#hidden_mn").val()),a("#timestampdiv").siblings("a.edit-timestamp").show(),updateText(),!1}),a(".save-timestamp","#timestampdiv").click(function(){return updateText()&&(a("#timestampdiv").slideUp("fast"),a("#timestampdiv").siblings("a.edit-timestamp").show()),!1}),a("#post").on("submit",function(b){return updateText()?void 0:(b.preventDefault(),a("#timestampdiv").show(),a("#publishing-action .spinner").hide(),a("#publish").prop("disabled",!1).removeClass("button-primary-disabled"),!1)}),a("#post-status-select").siblings("a.edit-post-status").click(function(){return a("#post-status-select").is(":hidden")&&(a("#post-status-select").slideDown("fast"),a(this).hide()),!1}),a(".save-post-status","#post-status-select").click(function(){return a("#post-status-select").slideUp("fast"),a("#post-status-select").siblings("a.edit-post-status").show(),updateText(),!1}),a(".cancel-post-status","#post-status-select").click(function(){return a("#post-status-select").slideUp("fast"),a("#post_status").val(a("#hidden_post_status").val()),a("#post-status-select").siblings("a.edit-post-status").show(),updateText(),!1})),i.length&&i.on("click",function(c){var d=a(c.target);(d.is("#editable-post-name")||d.hasClass("edit-slug"))&&b()}),"undefined"!=typeof wpWordCount&&(a(document).triggerHandler("wpcountwords",[h.val()]),h.keyup(function(b){var c=b.keyCode||b.charCode;return c==g?!0:((13==c||8==g||46==g)&&a(document).triggerHandler("wpcountwords",[h.val()]),g=c,!0)})),wptitlehint=function(b){b=b||"title";var c=a("#"+b),d=a("#"+b+"-prompt-text");""===c.val()&&d.removeClass("screen-reader-text"),d.click(function(){a(this).addClass("screen-reader-text"),c.focus()}),c.blur(function(){""===this.value&&d.removeClass("screen-reader-text")}).focus(function(){d.addClass("screen-reader-text")}).keydown(function(b){d.addClass("screen-reader-text"),a(this).unbind(b)})},wptitlehint(),function(){function b(a){f?d.theme.resizeTo(null,e+a.pageY):h.height(Math.max(50,e+a.pageY)),a.preventDefault()}function c(){var b,c;f?(d.focus(),c=a("#wp-content-editor-container .mce-toolbar-grp").height(),b=parseInt(a("#content_ifr").css("height"),10)+c-28):(h.focus(),b=parseInt(h.css("height"),10)),g.off("mousemove.wp-editor-resize mouseup.wp-editor-resize"),b&&b>50&&5e3>b&&setUserSetting("ed_size",b)}var d,e,f,g=a(document),h=a("textarea#content"),i=a("#post-status-info");!h.length||"ontouchstart"in window||(h.css("resize","none"),i.on("mousedown.wp-editor-resize",function(i){"undefined"!=typeof tinymce&&(d=tinymce.get("content")),d&&!d.isHidden()?(f=!0,e=a("#content_ifr").height()-i.pageY):(f=!1,e=h.height()-i.pageY,h.blur()),g.on("mousemove.wp-editor-resize",b).on("mouseup.wp-editor-resize",c),i.preventDefault()}))}(),"undefined"!=typeof tinymce&&a("#post-formats-select input.post-format").on("change.set-editor-class",function(){var b,c,d=this.id;d&&a(this).prop("checked")&&(b=tinymce.get("content"),b&&(c=b.getBody(),c.className=c.className.replace(/\bpost-format-[^ ]+/,""),b.dom.addClass(c,"post-format-0"==d?"post-format-standard":d)))})}); \ No newline at end of file diff --git a/wp-admin/post.php b/wp-admin/post.php index a4f5cc7408..3df1c5fc6e 100644 --- a/wp-admin/post.php +++ b/wp-admin/post.php @@ -307,7 +307,7 @@ case 'delete': break; case 'preview': - check_admin_referer( 'autosave', 'autosavenonce' ); + check_admin_referer( 'update-post_' . $post_id ); $url = post_preview(); diff --git a/wp-includes/js/autosave.js b/wp-includes/js/autosave.js index 4c780a4a8a..ab6952b08f 100644 --- a/wp-includes/js/autosave.js +++ b/wp-includes/js/autosave.js @@ -1,688 +1,597 @@ -/* global switchEditors, autosaveL10n, tinymce, ajaxurl, wpAjax, makeSlugeditClickable, wpCookies */ -var autosave, autosavePeriodical, fullscreen, doPreview, - autosaveLast = '', - autosaveDelayPreview = false, - notSaved = true, - blockSave = false, - autosaveLockRelease = true; +/* global tinymce, wpCookies, autosaveL10n, switchEditors */ +// Back-compat: prevent fatal errors +window.autosave = function(){}; -jQuery(document).ready( function($) { +( function( $, window ) { + function autosave() { + var initialCompareString, + lastTriggerSave = 0, + isSuspended = false, + $document = $(document); - 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(); - } - - autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true}); - - //Disable autosave after the form has been submitted - $('#post').submit(function() { - $.cancel(autosavePeriodical); - autosaveLockRelease = false; - }); - - $('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(); - }); - - window.onbeforeunload = function(){ - var editor = typeof(tinymce) != 'undefined' ? tinymce.activeEditor : false; - - if ( editor && ! editor.isHidden() ) { - if ( editor.isDirty() ) - return autosaveL10n.saveAlert; - } else { - if ( wp.autosave.getCompareString() != autosaveLast ) - return autosaveL10n.saveAlert; - } - }; - - $(window).unload( function(e) { - if ( ! autosaveLockRelease ) - return; - - // 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; - - $.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() - } - }); - } ); - - // preview - $('#post-preview').click(function(){ - if ( $('#auto_draft').val() == '1' && notSaved ) { - autosaveDelayPreview = true; - autosave(); - return false; - } - doPreview(); - return false; - }); - - 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 + /** + * Returns the data saved in both local and remote autosave + * + * @return object Object containing the post data */ - 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(); + function getPostData( type ) { + var post_name, parent_id, data, + time = ( new Date() ).getTime(), + cats = [], + editor = typeof tinymce !== 'undefined' && tinymce.get('content'); + + // 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; + } + + 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() || '' + }; + + if ( type === 'local' ) { + return data; + } + + $( 'input[id^="in-category-"]:checked' ).each( function() { + cats.push( this.value ); }); - } + data.catslist = cats.join(','); - $('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'); + if ( post_name = $( '#post_name' ).val() ) { + data.post_name = post_name; } - if ( editor && ! editor.isHidden() ) { - $(this).one( 'keyup', function() { - editor.focus(); - }); - } else { - $('#content').focus(); + if ( parent_id = $( '#parent_id' ).val() ) { + data.parent_id = parent_id; } - event.preventDefault(); - } - }); - - // 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(); + if ( $( '#comment_status' ).prop( 'checked' ) ) { + data.comment_status = 'open'; } - notice.show(); - autosave_disable_buttons(); - } - }).on('heartbeat-connection-restored.autosave', function() { - $('#lost-connection-notice').hide(); - autosave_enable_buttons(); - }); -}); -function autosave_parse_response( response ) { - var res = wpAjax.parseAjaxResponse(response, 'autosave'), post_id, sup; + if ( $( '#ping_status' ).prop( 'checked' ) ) { + data.ping_status = 'open'; + } - if ( res && res.responses && res.responses.length ) { - if ( res.responses[0].supplemental ) { - sup = res.responses[0].supplemental; + if ( $( '#auto_draft' ).val() === '1' ) { + data.auto_draft = '1'; + } - jQuery.each( sup, function( selector, value ) { - if ( selector.match(/^replace-/) ) - jQuery( '#' + selector.replace('replace-', '') ).val( value ); - }); + return data; } - // 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 ); + // 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 || '' ); + } - if ( res.responses[0].data ) // update autosave message - jQuery('.autosave-message').text( res.responses[0].data ); - } - } - - return res; -} - -// 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 -} - -// called when autosaving new post -function autosave_saved_new(response) { - blockSave = false; - var res = autosave_parse_response(response), post_id; - - 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 ); - - if ( post_id ) { - notSaved = false; - jQuery('#auto_draft').val('0'); // No longer an auto-draft + return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' ); } - autosave_enable_buttons(); - - if ( autosaveDelayPreview ) { - autosaveDelayPreview = false; - doPreview(); + function disableButtons() { + $document.trigger('autosave-disable-buttons'); + // Re-enable 5 sec later. Just gives autosave a head start to avoid collisions. + setTimeout( enableButtons, 5000 ); } - } 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'); + function enableButtons() { + $document.trigger( 'autosave-enable-buttons' ); + } + + function suspend() { + isSuspended = true; + } + + function resume() { + isSuspended = false; + } + + // Autosave in localStorage + function autosaveLocal() { + var restorePostData, undoPostData, blog_id, post_id, hasStorage, intervalTimer, + lastCompareString; + + // Check if the browser supports sessionStorage and it's not disabled + function checkStorage() { + var test = Math.random().toString(), + result = false; + + try { + window.sessionStorage.setItem( 'wp-test', test ); + result = window.sessionStorage.getItem( 'wp-test' ) === test; + window.sessionStorage.removeItem( 'wp-test' ); + } catch(e) {} + + hasStorage = result; + return result; + } + + /** + * 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 ); + + if ( stored_obj ) { + stored_obj = JSON.parse( stored_obj ); + } else { + stored_obj = {}; + } + } + + return stored_obj; + } + + /** + * Set the storage for this blog + * + * Confirms that the data was saved successfully. + * + * @return bool + */ + function setStorage( stored_obj ) { + var key; + + if ( hasStorage && blog_id ) { + key = 'wp-autosave-' + blog_id; + sessionStorage.setItem( key, JSON.stringify( stored_obj ) ); + return sessionStorage.getItem( key ) !== null; + } + + return false; + } + + /** + * 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(); + + if ( ! stored || ! post_id ) { + return false; + } + + return stored[ 'post_' + post_id ] || false; + } + + /** + * 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(); + + if ( ! stored || ! post_id ) { + return false; + } + + if ( stored_data ) { + stored[ 'post_' + post_id ] = stored_data; + } else if ( stored.hasOwnProperty( 'post_' + post_id ) ) { + delete stored[ 'post_' + post_id ]; + } else { + return false; + } + + return setStorage( stored ); + } + + /** + * 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; + + if ( isSuspended ) { + return false; + } + + if ( data ) { + postData = getSavedPostData() || {}; + $.extend( postData, data ); + } else { + postData = getPostData('local'); + } + + compareString = getCompareString( postData ); + + if ( typeof lastCompareString === 'undefined' ) { + lastCompareString = initialCompareString; + } + + // If the content, title and excerpt did not change since the last save, don't save again + if ( compareString === lastCompareString ) { + return false; + } + + postData.save_time = ( new Date() ).getTime(); + postData.status = $( '#post_status' ).val() || ''; + result = setData( postData ); + + if ( result ) { + lastCompareString = compareString; + } + + return result; + } + + // Run on DOM ready + function run() { + post_id = $('#post_ID').val() || 0; + + // 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(); + } + + // Save every 15 sec. + intervalTimer = window.setInterval( save, 15000 ); + + $( 'form#post' ).on( 'submit.autosave-local', function() { + var editor = typeof tinymce !== 'undefined' && tinymce.get('content'), + post_id = $('#post_ID').val() || 0; + + 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() || '' }); } - makeSlugeditClickable(); + + wpCookies.set( 'wp-saving-post-' + post_id, 'check' ); + }); + } + + // Strip whitespace and compare two strings + function compare( str1, str2 ) { + function removeSpaces( string ) { + return string.toString().replace(/[\x20\t\r\n\f]+/g, ''); } + + return ( removeSpaces( str1 || '' ) === removeSpaces( str2 || '' ) ); } - ); - } -} -function autosave_loading() { - jQuery('.autosave-message').html(autosaveL10n.savingText); -} - -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); - } -} - -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 ); -} - -function delayed_autosave() { - setTimeout(function(){ - if ( blockSave ) - return; - autosave(); - }, 200); -} - -autosave = function() { - var post_data = wp.autosave.getPostData(), - compareString, - successCallback; - - blockSave = true; - - // post_data.content cannot be retrieved at the moment - if ( ! post_data.autosave ) - return false; - - // No autosave while thickbox is open (media buttons) - if ( jQuery('#TB_window').css('display') == 'block' ) - return false; - - compareString = wp.autosave.getCompareString( post_data ); - - // Nothing to save or no change. - if ( compareString == autosaveLast ) - return false; - - autosaveLast = compareString; - jQuery(document).triggerHandler('wpcountwords', [ post_data.content ]); - - // Disable buttons until we know the save completed. - autosave_disable_buttons(); - - if ( post_data.auto_draft == '1' ) { - successCallback = autosave_saved_new; // new post - } else { - successCallback = autosave_saved; // pre-existing post - } - - jQuery.ajax({ - data: post_data, - beforeSend: autosave_loading, - type: 'POST', - url: ajaxurl, - success: successCallback - }); - - return true; -}; - -// Autosave in localStorage -// set as simple object/mixin for now -window.wp = window.wp || {}; -wp.autosave = wp.autosave || {}; - -(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() || '' - }; - - 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(); - } - } - - data.post_title = $('#title').val() || ''; - data.content = $('#content').val() || ''; - - /* - // We haven't been saving tags with autosave since 2.8... Start again? - $('.the-tags').each( function() { - data[this.name] = this.value; - }); - */ - - $('input[id^="in-category-"]:checked').each( function() { - cats.push(this.value); - }); - data.catslist = cats.join(','); - - if ( post_name = $('#post_name').val() ) - data.post_name = post_name; - - if ( parent_id = $('#parent_id').val() ) - data.parent_id = parent_id; - - if ( $('#comment_status').prop('checked') ) - data.comment_status = 'open'; - - if ( $('#ping_status').prop('checked') ) - data.ping_status = 'open'; - - if ( $('#auto_draft').val() == '1' ) - data.auto_draft = '1'; - - return data; -}; - -// 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 || '' ); - } - - return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' ); -}; - -wp.autosave.local = { - - lastSavedData: '', - blog_id: 0, - hasStorage: false, - - // Check if the browser supports sessionStorage and it's not disabled - checkStorage: function() { - var test = Math.random(), result = false; - - try { - sessionStorage.setItem('wp-test', test); - result = sessionStorage.getItem('wp-test') == test; - sessionStorage.removeItem('wp-test'); - } catch(e) {} - - this.hasStorage = result; - return result; - }, - - /** - * 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 ); - - if ( stored_obj ) - stored_obj = JSON.parse( stored_obj ); - else - stored_obj = {}; - } - - return stored_obj; - }, - - /** - * Set the storage for this blog - * - * Confirms that the data was saved successfully. - * - * @return bool - */ - setStorage: function( stored_obj ) { - var key; - - if ( this.hasStorage && this.blog_id ) { - key = 'wp-autosave-' + this.blog_id; - sessionStorage.setItem( key, JSON.stringify( stored_obj ) ); - return sessionStorage.getItem( key ) !== null; - } - - return false; - }, - - /** - * 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(); - - if ( !stored || !post_id ) - return false; - - return stored[ 'post_' + post_id ] || false; - }, - - /** - * 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(); - - if ( !stored || !post_id ) - return false; - - if ( stored_data ) - stored[ 'post_' + post_id ] = stored_data; - else if ( stored.hasOwnProperty( 'post_' + post_id ) ) - delete stored[ 'post_' + post_id ]; - else - return false; - - return this.setStorage(stored); - }, - - /** - * 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; - - if ( ! data ) { - post_data = wp.autosave.getPostData(); - } else { - post_data = this.getData() || {}; - $.extend( post_data, data ); - post_data.autosave = true; - } - - // Cannot get the post data at the moment - if ( ! post_data.autosave ) - return false; - - compareString = wp.autosave.getCompareString( post_data ); - - // If the content, title and excerpt did not change since the last save, don't save again - if ( compareString == this.lastSavedData ) - return false; - - post_data.save_time = (new Date()).getTime(); - post_data.status = $('#post_status').val() || ''; - result = this.setData( post_data ); - - if ( result ) - this.lastSavedData = compareString; - - return result; - }, - - // Initialize and run checkPost() on loading the script (before TinyMCE init) - init: function( settings ) { - var self = this; - - // Check if the browser supports sessionStorage and it's not disabled - if ( ! this.checkStorage() ) - return; - - // Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'. - if ( ! $('#content').length && ! $('#excerpt').length ) - return; - - if ( settings ) - $.extend( this, settings ); - - if ( !this.blog_id ) - this.blog_id = typeof window.autosaveL10n != 'undefined' ? window.autosaveL10n.blog_id : 0; - - $(document).ready( function(){ self.run(); } ); - }, - - // Run on DOM ready - run: function() { - var self = this; - - // Check if the local post data is different than the loaded post data. - this.checkPost(); - - // Set the schedule - this.schedule = $.schedule({ - time: 15 * 1000, - func: function() { wp.autosave.local.save(); }, - repeat: true, - protect: true - }); - - $('form#post').on('submit.autosave-local', function() { - var editor = typeof tinymce != 'undefined' && tinymce.get('content'), post_id = $('#post_ID').val() || 0; - - 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() || '' + /** + * 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 ); + + if ( ! postData ) { + return; + } + + if ( cookie ) { + wpCookies.remove( 'wp-saving-post-' + post_id ); + + if ( cookie === 'saved' ) { + // The post was saved properly, remove old data and bail + setData( false ); + return; + } + } + + // There is a newer autosave. Don't show two "restore" notices at the same time. + if ( $( '#has-newer-autosave' ).length ) { + return; + } + + content = $( '#content' ).val() || ''; + post_title = $( '#title' ).val() || ''; + excerpt = $( '#excerpt' ).val() || ''; + + // cookie == 'check' means the post was not saved properly, always show #local-storage-notice + if ( cookie !== 'check' && compare( content, postData.content ) && + compare( post_title, postData.post_title ) && compare( excerpt, postData.excerpt ) ) { + + return; + } + + restorePostData = postData; + undoPostData = { + content: content, + post_title: post_title, + excerpt: excerpt + }; + + $notice = $( '#local-storage-notice' ); + $('.wrap h2').first().after( $notice.addClass( 'updated' ).show() ); + + $notice.on( 'click.autosae-local', function( event ) { + var $target = $( event.target ); + + 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(); + } + + event.preventDefault(); }); } - wpCookies.set( 'wp-saving-post-' + post_id, 'check' ); - }); - }, + // Restore the current title, content and excerpt from postData. + function restorePost( postData ) { + var editor; - // Strip whitespace and compare two strings - compare: function( str1, str2 ) { - function remove( string ) { - return string.toString().replace(/[\x20\t\r\n\f]+/g, ''); - } + if ( postData ) { + // Set the last saved data + lastCompareString = getCompareString( postData ); - return ( remove( str1 || '' ) == remove( str2 || '' ) ); - }, + if ( $( '#title' ).val() !== postData.post_title ) { + $( '#title' ).focus().val( postData.post_title || '' ); + } - /** - * 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 ); + $( '#excerpt' ).val( postData.excerpt || '' ); + editor = typeof tinymce !== 'undefined' && tinymce.get('content'); - if ( ! post_data ) - return; + 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 ); + } - if ( cookie ) { - wpCookies.remove( 'wp-saving-post-' + post_id ); + return true; + } - if ( cookie == 'saved' ) { - // The post was saved properly, remove old data and bail - this.setData( false ); + return false; + } + + // Initialize and run checkPost() on loading the script (before TinyMCE init) + blog_id = typeof window.autosaveL10n !== 'undefined' && window.autosaveL10n.blog_id; + + // Check if the browser supports sessionStorage and it's not disabled + if ( ! checkStorage() ) { return; } - } - // There is a newer autosave. Don't show two "restore" notices at the same time. - if ( $('#has-newer-autosave').length ) - return; - - content = $('#content').val() || ''; - post_title = $('#title').val() || ''; - excerpt = $('#excerpt').val() || ''; - - if ( $('#wp-content-wrap').hasClass('tmce-active') && typeof switchEditors != 'undefined' ) - content = switchEditors.pre_wpautop( content ); - - // 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; - } - - 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(); + // Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'. + if ( ! blog_id || ( ! $('#content').length && ! $('#excerpt').length ) ) { + return; } - e.preventDefault(); + $document.ready( run ); + + return { + hasStorage: hasStorage, + getSavedPostData: getSavedPostData, + save: save + }; + } + + // Autosave on the server + function autosaveServer() { + var _disabled, _blockSave, _blockSaveTimer, previousCompareString, lastCompareString, + nextRun = 0; + + // Block saving for the next 10 sec. + function tempBlockSave() { + _blockSave = true; + window.clearTimeout( _blockSaveTimer ); + + _blockSaveTimer = window.setTimeout( function() { + _blockSave = false; + }, 10000 ); + } + + // Runs on heartbeat-response + function response( data ) { + _schedule(); + _blockSave = false; + lastCompareString = previousCompareString; + previousCompareString = ''; + + $document.trigger( 'after-autosave', [data] ); + $( '.autosave-message' ).text( data.message ); + enableButtons(); + + if ( data.success ) { + // No longer an auto-draft + $( '#auto_draft' ).val(''); + } + } + + /** + * Disable autosave + * + * Intended to run on form.submit + */ + function disable() { + _disabled = true; + } + + /** + * Save immediately + * + * Resets the timing and tells heartbeat to connect now + * + * @return void + */ + function triggerSave() { + nextRun = 0; + wp.heartbeat.connectNow(); + } + + /** + * 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; + } + + // Runs on 'heartbeat-send' + function save() { + var postData, compareString; + + if ( isSuspended || _disabled || _blockSave ) { + return false; + } + + if ( ( new Date() ).getTime() < nextRun ) { + return false; + } + + postData = getPostData(); + compareString = getCompareString( postData ); + + // First check + if ( typeof lastCompareString === 'undefined' ) { + lastCompareString = initialCompareString; + } + + // No change + if ( compareString === lastCompareString ) { + return false; + } + + previousCompareString = compareString; + tempBlockSave(); + disableButtons(); + + $document.trigger( 'wpcountwords', [ postData.content ] ) + .trigger( 'before-autosave', [ postData ] ); + + $( '.autosave-message' ).text( autosaveL10n.savingText ); + postData._wpnonce = $( '#_wpnonce' ).val() || ''; + + return postData; + } + + function _schedule() { + nextRun = ( new Date() ).getTime() + ( autosaveL10n.autosaveInterval * 1000 ) || 60000; + } + + $document.on( 'heartbeat-send.autosave', function( event, data ) { + var autosaveData = save(); + + 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'); + + if ( ! wp.autosave.local.hasStorage ) { + $notice.find('.hide-if-no-sessionstorage').hide(); + } + + $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 + }; + } + + // 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 ); + } + }).ready( function() { + // Set the initial compare string in case TinyMCE is not used or not loaded first + initialCompareString = getCompareString(); }); - }, - // 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; + return { + getPostData: getPostData, + getCompareString: getCompareString, + disableButtons: disableButtons, + enableButtons: enableButtons, + suspend: suspend, + resume: resume, + local: autosaveLocal(), + server: autosaveServer() + }; } -}; -wp.autosave.local.init(); + window.wp = window.wp || {}; + window.wp.autosave = autosave(); -}(jQuery)); +}( jQuery, window )); diff --git a/wp-includes/js/autosave.min.js b/wp-includes/js/autosave.min.js index 3babb15790..e19af65811 100644 --- a/wp-includes/js/autosave.min.js +++ b/wp-includes/js/autosave.min.js @@ -1 +1 @@ -function autosave_parse_response(a){var b,c,d=wpAjax.parseAjaxResponse(a,"autosave");return d&&d.responses&&d.responses.length&&(d.responses[0].supplemental&&(c=d.responses[0].supplemental,jQuery.each(c,function(a,b){a.match(/^replace-/)&&jQuery("#"+a.replace("replace-","")).val(b)})),d.errors||((b=parseInt(d.responses[0].id,10))&&autosave_update_slug(b),d.responses[0].data&&jQuery(".autosave-message").text(d.responses[0].data))),d}function autosave_saved(a){blockSave=!1,autosave_parse_response(a),autosave_enable_buttons()}function autosave_saved_new(a){blockSave=!1;var b,c=autosave_parse_response(a);c&&c.responses.length&&!c.errors?(b=parseInt(c.responses[0].id,10),b&&(notSaved=!1,jQuery("#auto_draft").val("0")),autosave_enable_buttons(),autosaveDelayPreview&&(autosaveDelayPreview=!1,doPreview())):autosave_enable_buttons()}function autosave_update_slug(a){"undefined"!=makeSlugeditClickable&&jQuery.isFunction(makeSlugeditClickable)&&!jQuery("#edit-slug-box > *").size()&&jQuery.post(ajaxurl,{action:"sample-permalink",post_id:a,new_title:fullscreen&&fullscreen.settings.visible?jQuery("#wp-fullscreen-title").val():jQuery("#title").val(),samplepermalinknonce:jQuery("#samplepermalinknonce").val()},function(a){if("-1"!==a){var b=jQuery("#edit-slug-box");b.html(a),b.hasClass("hidden")&&b.fadeIn("fast",function(){b.removeClass("hidden")}),makeSlugeditClickable()}})}function autosave_loading(){jQuery(".autosave-message").html(autosaveL10n.savingText)}function autosave_enable_buttons(){jQuery(document).trigger("autosave-enable-buttons"),wp.heartbeat&&wp.heartbeat.hasConnectionError()||setTimeout(function(){var a=jQuery("#submitpost");a.find(":button, :submit").removeAttr("disabled"),a.find(".spinner").hide()},500)}function autosave_disable_buttons(){jQuery(document).trigger("autosave-disable-buttons"),jQuery("#submitpost").find(":button, :submit").prop("disabled",!0),setTimeout(autosave_enable_buttons,5e3)}function delayed_autosave(){setTimeout(function(){blockSave||autosave()},200)}var autosave,autosavePeriodical,fullscreen,doPreview,autosaveLast="",autosaveDelayPreview=!1,notSaved=!0,blockSave=!1,autosaveLockRelease=!0;jQuery(document).ready(function(a){autosaveLast=a("#wp-content-wrap").hasClass("tmce-active")&&"undefined"!=typeof switchEditors?wp.autosave.getCompareString({post_title:a("#title").val()||"",content:switchEditors.pre_wpautop(a("#content").val())||"",excerpt:a("#excerpt").val()||""}):wp.autosave.getCompareString(),autosavePeriodical=a.schedule({time:1e3*autosaveL10n.autosaveInterval,func:function(){autosave()},repeat:!0,protect:!0}),a("#post").submit(function(){a.cancel(autosavePeriodical),autosaveLockRelease=!1}),a('input[type="submit"], a.submitdelete',"#submitpost").click(function(){blockSave=!0,window.onbeforeunload=null,a(":button, :submit","#submitpost").each(function(){var b=a(this);b.hasClass("button-primary")?b.addClass("button-primary-disabled"):b.addClass("button-disabled")}),"publish"==a(this).attr("id")?a("#major-publishing-actions .spinner").show():a("#minor-publishing .spinner").show()}),window.onbeforeunload=function(){var a="undefined"!=typeof tinymce?tinymce.activeEditor:!1;if(a&&!a.isHidden()){if(a.isDirty())return autosaveL10n.saveAlert}else if(wp.autosave.getCompareString()!=autosaveLast)return autosaveL10n.saveAlert},a(window).unload(function(b){autosaveLockRelease&&(b.target&&"#document"!=b.target.nodeName||a.ajax({type:"POST",url:ajaxurl,async:!1,data:{action:"wp-remove-post-lock",_wpnonce:a("#_wpnonce").val(),post_ID:a("#post_ID").val(),active_post_lock:a("#active_post_lock").val()}}))}),a("#post-preview").click(function(){return"1"==a("#auto_draft").val()&¬Saved?(autosaveDelayPreview=!0,autosave(),!1):(doPreview(),!1)}),doPreview=function(){a("input#wp-preview").val("dopreview"),a("form#post").attr("target","wp-preview").submit().attr("target","");var b=navigator.userAgent.toLowerCase();-1!=b.indexOf("safari")&&-1==b.indexOf("chrome")&&a("form#post").attr("action",function(a,b){return b+"?t="+(new Date).getTime()}),a("input#wp-preview").val("")},a("#title").on("keydown.editor-focus",function(b){var c;9!==b.which||b.ctrlKey||b.altKey||b.shiftKey||("undefined"!=typeof tinymce&&(c=tinymce.get("content")),c&&!c.isHidden()?a(this).one("keyup",function(){c.focus()}):a("#content").focus(),b.preventDefault())}),"1"==a("#auto_draft").val()&&a("#title").blur(function(){this.value&&"1"==a("#auto_draft").val()&&delayed_autosave()}),a(document).on("heartbeat-connection-lost.autosave",function(b,c,d){if("timeout"===c||503==d){var e=a("#lost-connection-notice");wp.autosave.local.hasStorage||e.find(".hide-if-no-sessionstorage").hide(),e.show(),autosave_disable_buttons()}}).on("heartbeat-connection-restored.autosave",function(){a("#lost-connection-notice").hide(),autosave_enable_buttons()})}),autosave=function(){var a,b,c=wp.autosave.getPostData();return blockSave=!0,c.autosave?"block"==jQuery("#TB_window").css("display")?!1:(a=wp.autosave.getCompareString(c),a==autosaveLast?!1:(autosaveLast=a,jQuery(document).triggerHandler("wpcountwords",[c.content]),autosave_disable_buttons(),b="1"==c.auto_draft?autosave_saved_new:autosave_saved,jQuery.ajax({data:c,beforeSend:autosave_loading,type:"POST",url:ajaxurl,success:b}),!0)):!1},window.wp=window.wp||{},wp.autosave=wp.autosave||{},function(a){wp.autosave.getPostData=function(){var b,c,d="undefined"!=typeof tinymce?tinymce.activeEditor:null,e=[],f={action:"autosave",autosave:!0,post_id:a("#post_ID").val()||0,autosavenonce:a("#autosavenonce").val()||"",post_type:a("#post_type").val()||"",post_author:a("#post_author").val()||"",excerpt:a("#excerpt").val()||""};if(d&&!d.isHidden()){if(d.plugins.spellchecker&&d.plugins.spellchecker.active)return f.autosave=!1,f;tinymce.triggerSave()}return f.post_title=a("#title").val()||"",f.content=a("#content").val()||"",a('input[id^="in-category-"]:checked').each(function(){e.push(this.value)}),f.catslist=e.join(","),(b=a("#post_name").val())&&(f.post_name=b),(c=a("#parent_id").val())&&(f.parent_id=c),a("#comment_status").prop("checked")&&(f.comment_status="open"),a("#ping_status").prop("checked")&&(f.ping_status="open"),"1"==a("#auto_draft").val()&&(f.auto_draft="1"),f},wp.autosave.getCompareString=function(b){return"object"==typeof b?(b.post_title||"")+"::"+(b.content||"")+"::"+(b.excerpt||""):(a("#title").val()||"")+"::"+(a("#content").val()||"")+"::"+(a("#excerpt").val()||"")},wp.autosave.local={lastSavedData:"",blog_id:0,hasStorage:!1,checkStorage:function(){var a=Math.random(),b=!1;try{sessionStorage.setItem("wp-test",a),b=sessionStorage.getItem("wp-test")==a,sessionStorage.removeItem("wp-test")}catch(c){}return this.hasStorage=b,b},getStorage:function(){var a=!1;return this.hasStorage&&this.blog_id&&(a=sessionStorage.getItem("wp-autosave-"+this.blog_id),a=a?JSON.parse(a):{}),a},setStorage:function(a){var b;return this.hasStorage&&this.blog_id?(b="wp-autosave-"+this.blog_id,sessionStorage.setItem(b,JSON.stringify(a)),null!==sessionStorage.getItem(b)):!1},getData:function(){var b=this.getStorage(),c=a("#post_ID").val();return b&&c?b["post_"+c]||!1:!1},setData:function(b){var c=this.getStorage(),d=a("#post_ID").val();if(!c||!d)return!1;if(b)c["post_"+d]=b;else{if(!c.hasOwnProperty("post_"+d))return!1;delete c["post_"+d]}return this.setStorage(c)},save:function(b){var c,d,e=!1;return b?(c=this.getData()||{},a.extend(c,b),c.autosave=!0):c=wp.autosave.getPostData(),c.autosave?(d=wp.autosave.getCompareString(c),d==this.lastSavedData?!1:(c.save_time=(new Date).getTime(),c.status=a("#post_status").val()||"",e=this.setData(c),e&&(this.lastSavedData=d),e)):!1},init:function(b){var c=this;this.checkStorage()&&(a("#content").length||a("#excerpt").length)&&(b&&a.extend(this,b),this.blog_id||(this.blog_id="undefined"!=typeof window.autosaveL10n?window.autosaveL10n.blog_id:0),a(document).ready(function(){c.run()}))},run:function(){var b=this;this.checkPost(),this.schedule=a.schedule({time:15e3,func:function(){wp.autosave.local.save()},repeat:!0,protect:!0}),a("form#post").on("submit.autosave-local",function(){var c="undefined"!=typeof tinymce&&tinymce.get("content"),d=a("#post_ID").val()||0;c&&!c.isHidden()?c.onSubmit.add(function(){wp.autosave.local.save({post_title:a("#title").val()||"",content:a("#content").val()||"",excerpt:a("#excerpt").val()||""})}):b.save({post_title:a("#title").val()||"",content:a("#content").val()||"",excerpt:a("#excerpt").val()||""}),wpCookies.set("wp-saving-post-"+d,"check")})},compare:function(a,b){function c(a){return a.toString().replace(/[\x20\t\r\n\f]+/g,"")}return c(a||"")==c(b||"")},checkPost:function(){var b,c,d,e,f=this,g=this.getData(),h=a("#post_ID").val()||0,i=wpCookies.get("wp-saving-post-"+h);if(g)return i&&(wpCookies.remove("wp-saving-post-"+h),"saved"==i)?(this.setData(!1),void 0):(a("#has-newer-autosave").length||(b=a("#content").val()||"",c=a("#title").val()||"",d=a("#excerpt").val()||"",a("#wp-content-wrap").hasClass("tmce-active")&&"undefined"!=typeof switchEditors&&(b=switchEditors.pre_wpautop(b)),"check"!=i&&this.compare(b,g.content)&&this.compare(c,g.post_title)&&this.compare(d,g.excerpt)||(this.restore_post_data=g,this.undo_post_data={content:b,post_title:c,excerpt:d},e=a("#local-storage-notice"),a(".wrap h2").first().after(e.addClass("updated").show()),e.on("click",function(b){var c=a(b.target);c.hasClass("restore-backup")?(f.restorePost(f.restore_post_data),c.parent().hide(),a(this).find("p.undo-restore").show()):c.hasClass("undo-restore-backup")&&(f.restorePost(f.undo_post_data),c.parent().hide(),a(this).find("p.local-restore").show()),b.preventDefault()}))),void 0)},restorePost:function(b){var c;return b?(this.lastSavedData=wp.autosave.getCompareString(b),a("#title").val()!=b.post_title&&a("#title").focus().val(b.post_title||""),a("#excerpt").val(b.excerpt||""),c="undefined"!=typeof tinymce&&tinymce.get("content"),c&&!c.isHidden()&&"undefined"!=typeof switchEditors?(c.undoManager.add(),c.setContent(b.content?switchEditors.wpautop(b.content):"")):(a("#content-html").click(),a("#content").val(b.content)),!0):!1}},wp.autosave.local.init()}(jQuery); \ No newline at end of file +window.autosave=function(){},function(a,b){function c(){function c(b){var c,d,e,f=(new Date).getTime(),g=[],h="undefined"!=typeof tinymce&&tinymce.get("content");return h&&!h.isHidden()&&f-3e3>l&&(h.save(),l=f),e={post_id:a("#post_ID").val()||0,post_type:a("#post_type").val()||"",post_author:a("#post_author").val()||"",post_title:a("#title").val()||"",content:a("#content").val()||"",excerpt:a("#excerpt").val()||""},"local"===b?e:(a('input[id^="in-category-"]:checked').each(function(){g.push(this.value)}),e.catslist=g.join(","),(c=a("#post_name").val())&&(e.post_name=c),(d=a("#parent_id").val())&&(e.parent_id=d),a("#comment_status").prop("checked")&&(e.comment_status="open"),a("#ping_status").prop("checked")&&(e.ping_status="open"),"1"===a("#auto_draft").val()&&(e.auto_draft="1"),e)}function d(b){return"object"==typeof b?(b.post_title||"")+"::"+(b.content||"")+"::"+(b.excerpt||""):(a("#title").val()||"")+"::"+(a("#content").val()||"")+"::"+(a("#excerpt").val()||"")}function e(){n.trigger("autosave-disable-buttons"),setTimeout(f,5e3)}function f(){n.trigger("autosave-enable-buttons")}function g(){m=!0}function h(){m=!1}function i(){function e(){var a=Math.random().toString(),c=!1;try{b.sessionStorage.setItem("wp-test",a),c=b.sessionStorage.getItem("wp-test")===a,b.sessionStorage.removeItem("wp-test")}catch(d){}return v=c,c}function f(){var a=!1;return v&&t&&(a=sessionStorage.getItem("wp-autosave-"+t),a=a?JSON.parse(a):{}),a}function g(a){var b;return v&&t?(b="wp-autosave-"+t,sessionStorage.setItem(b,JSON.stringify(a)),null!==sessionStorage.getItem(b)):!1}function h(){var a=f();return a&&u?a["post_"+u]||!1:!1}function i(a){var b=f();if(!b||!u)return!1;if(a)b["post_"+u]=a;else{if(!b.hasOwnProperty("post_"+u))return!1;delete b["post_"+u]}return g(b)}function j(b){var e,f,g=!1;return m?!1:(b?(e=h()||{},a.extend(e,b)):e=c("local"),f=d(e),"undefined"==typeof x&&(x=k),f===x?!1:(e.save_time=(new Date).getTime(),e.status=a("#post_status").val()||"",g=i(e),g&&(x=f),g))}function l(){u=a("#post_ID").val()||0,a("#wp-content-wrap").hasClass("tmce-active")?n.on("tinymce-editor-init.autosave",function(){b.setTimeout(function(){p()},1500)}):p(),w=b.setInterval(j,15e3),a("form#post").on("submit.autosave-local",function(){var b="undefined"!=typeof tinymce&&tinymce.get("content"),c=a("#post_ID").val()||0;b&&!b.isHidden()?b.on("submit",function(){j({post_title:a("#title").val()||"",content:a("#content").val()||"",excerpt:a("#excerpt").val()||""})}):j({post_title:a("#title").val()||"",content:a("#content").val()||"",excerpt:a("#excerpt").val()||""}),wpCookies.set("wp-saving-post-"+c,"check")})}function o(a,b){function c(a){return a.toString().replace(/[\x20\t\r\n\f]+/g,"")}return c(a||"")===c(b||"")}function p(){var b,c,d,e,f=h(),g=wpCookies.get("wp-saving-post-"+u);if(f)return g&&(wpCookies.remove("wp-saving-post-"+u),"saved"===g)?(i(!1),void 0):(a("#has-newer-autosave").length||(b=a("#content").val()||"",c=a("#title").val()||"",d=a("#excerpt").val()||"","check"!==g&&o(b,f.content)&&o(c,f.post_title)&&o(d,f.excerpt)||(r=f,s={content:b,post_title:c,excerpt:d},e=a("#local-storage-notice"),a(".wrap h2").first().after(e.addClass("updated").show()),e.on("click.autosae-local",function(b){var c=a(b.target);c.hasClass("restore-backup")?(q(r),c.parent().hide(),a(this).find("p.undo-restore").show()):c.hasClass("undo-restore-backup")&&(q(s),c.parent().hide(),a(this).find("p.local-restore").show()),b.preventDefault()}))),void 0)}function q(b){var c;return b?(x=d(b),a("#title").val()!==b.post_title&&a("#title").focus().val(b.post_title||""),a("#excerpt").val(b.excerpt||""),c="undefined"!=typeof tinymce&&tinymce.get("content"),c&&!c.isHidden()&&"undefined"!=typeof switchEditors?(c.undoManager.add(),c.setContent(b.content?switchEditors.wpautop(b.content):"")):(a("#content-html").click(),a("#content").val(b.content)),!0):!1}var r,s,t,u,v,w,x;return t="undefined"!=typeof b.autosaveL10n&&b.autosaveL10n.blog_id,e()&&t&&(a("#content").length||a("#excerpt").length)?(n.ready(l),{hasStorage:v,getSavedPostData:h,save:j}):void 0}function j(){function g(){r=!0,b.clearTimeout(s),s=b.setTimeout(function(){r=!1},1e4)}function h(b){p(),r=!1,u=t,t="",n.trigger("after-autosave",[b]),a(".autosave-message").text(b.message),f(),b.success&&a("#auto_draft").val("")}function i(){q=!0}function j(){v=0,wp.heartbeat.connectNow()}function l(){return d()!==k}function o(){var b,f;return m||q||r?!1:(new Date).getTime()