diff --git a/wp-admin/js/customize-controls.js b/wp-admin/js/customize-controls.js index a12ebe6e13..17ce623f6c 100644 --- a/wp-admin/js/customize-controls.js +++ b/wp-admin/js/customize-controls.js @@ -1,4 +1,4 @@ -/* global _wpCustomizeHeader, _wpCustomizeBackground, _wpMediaViewsL10n, MediaElementPlayer, console */ +/* global _wpCustomizeHeader, _wpCustomizeBackground, _wpMediaViewsL10n, MediaElementPlayer, console, confirm */ (function( exports, $ ){ var Container, focus, normalizedTransitionendEventName, api = wp.customize; @@ -355,14 +355,24 @@ * @since 4.7.0 * @access public * - * @param {object} [changes] Mapping of setting IDs to setting params each normally including a value property, or mapping to null. - * If not provided, then the changes will still be obtained from unsaved dirty settings. + * @param {object} [changes] - Mapping of setting IDs to setting params each normally including a value property, or mapping to null. + * If not provided, then the changes will still be obtained from unsaved dirty settings. + * @param {object} [args] - Additional options for the save request. + * @param {boolean} [args.autosave=false] - Whether changes will be stored in autosave revision if the changeset has been promoted from an auto-draft. + * @param {string} [args.title] - Title to update in the changeset. Optional. + * @param {string} [args.date] - Date to update in the changeset. Optional. * @returns {jQuery.Promise} Promise resolving with the response data. */ - api.requestChangesetUpdate = function requestChangesetUpdate( changes ) { - var deferred, request, submittedChanges = {}, data; + api.requestChangesetUpdate = function requestChangesetUpdate( changes, args ) { + var deferred, request, submittedChanges = {}, data, submittedArgs; deferred = new $.Deferred(); + submittedArgs = _.extend( { + title: null, + date: null, + autosave: false + }, args ); + if ( changes ) { _.extend( submittedChanges, changes ); } @@ -379,20 +389,30 @@ } ); // Short-circuit when there are no pending changes. - if ( _.isEmpty( submittedChanges ) ) { + if ( _.isEmpty( submittedChanges ) && null === submittedArgs.title && null === submittedArgs.date ) { deferred.resolve( {} ); return deferred.promise(); } + // Allow plugins to attach additional params to the settings. + api.trigger( 'changeset-save', submittedChanges, submittedArgs ); + + // A status would cause a revision to be made, and for this wp.customize.previewer.save() should be used. Status is also disallowed for revisions regardless. + if ( submittedArgs.status ) { + return deferred.reject( { code: 'illegal_status_in_changeset_update' } ).promise(); + } + + // Dates not beung allowed for revisions are is a technical limitation of post revisions. + if ( submittedArgs.date && submittedArgs.autosave ) { + return deferred.reject( { code: 'illegal_autosave_with_date_gmt' } ).promise(); + } + // Make sure that publishing a changeset waits for all changeset update requests to complete. api.state( 'processing' ).set( api.state( 'processing' ).get() + 1 ); deferred.always( function() { api.state( 'processing' ).set( api.state( 'processing' ).get() - 1 ); } ); - // Allow plugins to attach additional params to the settings. - api.trigger( 'changeset-save', submittedChanges ); - // Ensure that if any plugins add data to save requests by extending query() that they get included here. data = api.previewer.query( { excludeCustomizedSaved: true } ); delete data.customized; // Being sent in customize_changeset_data instead. @@ -401,6 +421,15 @@ customize_theme: api.settings.theme.stylesheet, customize_changeset_data: JSON.stringify( submittedChanges ) } ); + if ( null !== submittedArgs.title ) { + data.customize_changeset_title = submittedArgs.title; + } + if ( null !== submittedArgs.date ) { + data.customize_changeset_date = submittedArgs.date; + } + if ( false !== submittedArgs.autosave ) { + data.customize_changeset_autosave = 'true'; + } request = wp.ajax.post( 'customize_save', data ); @@ -1705,9 +1734,15 @@ api.state( 'processing' ).unbind( onceProcessingComplete ); - request = api.requestChangesetUpdate(); + request = api.requestChangesetUpdate( {}, { autosave: true } ); request.done( function() { $( window ).off( 'beforeunload.customize-confirm' ); + + // Include autosaved param to load autosave revision without prompting user to restore it. + if ( ! api.state( 'saved' ).get() ) { + urlParser.search += '&customize_autosaved=on'; + } + top.location.href = urlParser.href; deferred.resolve(); } ); @@ -4024,6 +4059,9 @@ customize_messenger_channel: previewFrame.query.customize_messenger_channel } ); + if ( ! api.state( 'saved' ).get() ) { + params.customize_autosaved = 'on'; + } urlParser.search = $.param( params ); previewFrame.iframe = $( '