<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[41597] trunk/src: Customize: Extend changesets to support autosave revisions with restoration notifications, and introduce a new default linear history mode for saved changesets (with a filter for opt-in to changeset branching).</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/41597">41597</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/41597","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>westonruter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-09-26 07:37:02 +0000 (Tue, 26 Sep 2017)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Customize: Extend changesets to support autosave revisions with restoration notifications, and introduce a new default linear history mode for saved changesets (with a filter for opt-in to changeset branching).

* Autosaved changes made on top of `auto-draft` changesets get written on top of the `auto-draft` itself, similar to how autosaves for posts will overwrite post drafts.
* Autosaved changes made to saved changesets (e.g. `draft`, `future`) will be placed into an autosave revision for that changeset and that user.
* Opening the Customizer will now prompt the user to restore their most recent auto-draft changeset; if notification is dismissed or ignored then the auto-draft will be marked as dismissed and will not be prompted to user in a notification again.
* Customizer will no longer automatically supply the `changeset_uuid` param in the `customize.php` URL when branching changesets are not active.
* If user closes Customizer explicitly via clicking on X link, then autosave auto-draft/autosave will be dismissed so as to not be prompted again.
* If there is a changeset already saved as a `draft` or `future` (UI is forthcoming) then this changeset will now be autoloaded for the user to keep making additional changes. This is the linear model for changesets.
* To restore the previous behavior of the Customizer where each session started a new changeset, regardless of whether or not there was an existing changeset saved, there is now a `customize_changeset_branching` hook which can be filtered to return `true`.
* `wp.customize.requestChangesetUpdate()` now supports a second with options including `autosave`, `title`, and `date`.
* The window `blur` event for `customize.php` has been replaced with a `visibilitychange` event to reduce autosave requests when clicking into preview window.
* Adds `autosaved` and `branching` args to `WP_Customize_Manager`.
* The `changeset_uuid` param for `WP_Customize_Manager` is extended to recognize a `false` value which causes the Customizer to defer identifying the UUID until `after_setup_theme` in the new `WP_Customize_Manager::establish_loaded_changeset()` method.
* A new `customize_autosaved` query parameter can now be supplied which is passed into the `autosaved` arg in `WP_Customize_Manager`; this option is an opt-in to source data from the autosave revision, allowing a user to restore autosaved changes.

Props westonruter, dlh, sayedwp, JoshuaWold, melchoyce.
See <a href="https://core.trac.wordpress.org/ticket/39896">#39896</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminjscustomizecontrolsjs">trunk/src/wp-admin/js/customize-controls.js</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizemanagerphp">trunk/src/wp-includes/class-wp-customize-manager.php</a></li>
<li><a href="#trunksrcwpincludesjscustomizepreviewjs">trunk/src/wp-includes/js/customize-preview.js</a></li>
<li><a href="#trunksrcwpincludesscriptloaderphp">trunk/src/wp-includes/script-loader.php</a></li>
<li><a href="#trunksrcwpincludesthemephp">trunk/src/wp-includes/theme.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminjscustomizecontrolsjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/js/customize-controls.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/js/customize-controls.js       2017-09-26 00:11:56 UTC (rev 41596)
+++ trunk/src/wp-admin/js/customize-controls.js 2017-09-26 07:37:02 UTC (rev 41597)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,4 +1,4 @@
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/* global _wpCustomizeHeader, _wpCustomizeBackground, _wpMediaViewsL10n, MediaElementPlayer, console */
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/* global _wpCustomizeHeader, _wpCustomizeBackground, _wpMediaViewsL10n, MediaElementPlayer, console, confirm */
</ins><span class="cx" style="display: block; padding: 0 10px"> (function( exports, $ ){
</span><span class="cx" style="display: block; padding: 0 10px">        var Container, focus, normalizedTransitionendEventName, api = wp.customize;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -355,14 +355,24 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.7.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @access public
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @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.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @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.
</ins><span class="cx" style="display: block; padding: 0 10px">          * @returns {jQuery.Promise} Promise resolving with the response data.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        api.requestChangesetUpdate = function requestChangesetUpdate( changes ) {
-               var deferred, request, submittedChanges = {}, data;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ api.requestChangesetUpdate = function requestChangesetUpdate( changes, args ) {
+               var deferred, request, submittedChanges = {}, data, submittedArgs;
</ins><span class="cx" style="display: block; padding: 0 10px">                 deferred = new $.Deferred();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                submittedArgs = _.extend( {
+                       title: null,
+                       date: null,
+                       autosave: false
+               }, args );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( changes ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        _.extend( submittedChanges, changes );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -379,20 +389,30 @@
</span><span class="cx" style="display: block; padding: 0 10px">                } );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Short-circuit when there are no pending changes.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( _.isEmpty( submittedChanges ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( _.isEmpty( submittedChanges ) && null === submittedArgs.title && null === submittedArgs.date ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         deferred.resolve( {} );
</span><span class="cx" style="display: block; padding: 0 10px">                        return deferred.promise();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // 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();
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Make sure that publishing a changeset waits for all changeset update requests to complete.
</span><span class="cx" style="display: block; padding: 0 10px">                api.state( 'processing' ).set( api.state( 'processing' ).get() + 1 );
</span><span class="cx" style="display: block; padding: 0 10px">                deferred.always( function() {
</span><span class="cx" style="display: block; padding: 0 10px">                        api.state( 'processing' ).set( api.state( 'processing' ).get() - 1 );
</span><span class="cx" style="display: block; padding: 0 10px">                } );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Allow plugins to attach additional params to the settings.
-               api.trigger( 'changeset-save', submittedChanges );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 // Ensure that if any plugins add data to save requests by extending query() that they get included here.
</span><span class="cx" style="display: block; padding: 0 10px">                data = api.previewer.query( { excludeCustomizedSaved: true } );
</span><span class="cx" style="display: block; padding: 0 10px">                delete data.customized; // Being sent in customize_changeset_data instead.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -401,6 +421,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        customize_theme: api.settings.theme.stylesheet,
</span><span class="cx" style="display: block; padding: 0 10px">                        customize_changeset_data: JSON.stringify( submittedChanges )
</span><span class="cx" style="display: block; padding: 0 10px">                } );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                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';
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                request = wp.ajax.post( 'customize_save', data );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1705,9 +1734,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                api.state( 'processing' ).unbind( onceProcessingComplete );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                request = api.requestChangesetUpdate();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         request = api.requestChangesetUpdate( {}, { autosave: true } );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 request.done( function() {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $( window ).off( 'beforeunload.customize-confirm' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                                       // Include autosaved param to load autosave revision without prompting user to restore it.
+                                       if ( ! api.state( 'saved' ).get() ) {
+                                               urlParser.search += '&customize_autosaved=on';
+                                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                         top.location.href = urlParser.href;
</span><span class="cx" style="display: block; padding: 0 10px">                                        deferred.resolve();
</span><span class="cx" style="display: block; padding: 0 10px">                                } );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4024,6 +4059,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        customize_messenger_channel: previewFrame.query.customize_messenger_channel
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        if ( ! api.state( 'saved' ).get() ) {
+                               params.customize_autosaved = 'on';
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        urlParser.search = $.param( params );
</span><span class="cx" style="display: block; padding: 0 10px">                        previewFrame.iframe = $( '<iframe />', {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4260,6 +4298,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        delete queryParams.customize_changeset_uuid;
</span><span class="cx" style="display: block; padding: 0 10px">                                        delete queryParams.customize_theme;
</span><span class="cx" style="display: block; padding: 0 10px">                                        delete queryParams.customize_messenger_channel;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        delete queryParams.customize_autosaved;
</ins><span class="cx" style="display: block; padding: 0 10px">                                         if ( _.isEmpty( queryParams ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                urlParser.search = '';
</span><span class="cx" style="display: block; padding: 0 10px">                                        } else {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4884,6 +4923,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        nonce: this.nonce.preview,
</span><span class="cx" style="display: block; padding: 0 10px">                                        customize_changeset_uuid: api.settings.changeset.uuid
</span><span class="cx" style="display: block; padding: 0 10px">                                };
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                if ( ! api.state( 'saved' ).get() ) {
+                                       queryVars.customize_autosaved = 'on';
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                /*
</span><span class="cx" style="display: block; padding: 0 10px">                                 * Exclude customized data if requested especially for calls to requestChangesetUpdate.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5098,6 +5140,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                                        parent.send( 'changeset-uuid', api.settings.changeset.uuid );
</span><span class="cx" style="display: block; padding: 0 10px">                                                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                                // Prevent subsequent requestChangesetUpdate() calls from including the settings that have been saved.
+                                               api._lastSavedRevision = Math.max( latestRevision, api._lastSavedRevision );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 if ( response.setting_validities ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                        api._handleSettingValidities( {
</span><span class="cx" style="display: block; padding: 0 10px">                                                                settingValidities: response.setting_validities,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5315,10 +5360,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        api.bind( 'change', function() {
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( state( 'saved' ).get() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        state( 'saved' ).set( false );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        populateChangesetUuidParam( true );
</del><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><span class="cx" style="display: block; padding: 0 10px">                        });
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // Populate changeset UUID param when state becomes dirty.
+                       if ( api.settings.changeset.branching ) {
+                               saved.bind( function( isSaved ) {
+                                       if ( ! isSaved ) {
+                                               populateChangesetUuidParam( true );
+                                       }
+                               });
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         saving.bind( function( isSaving ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                body.toggleClass( 'saving', isSaving );
</span><span class="cx" style="display: block; padding: 0 10px">                        } );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5371,14 +5424,123 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                history.replaceState( {}, document.title, urlParser.href );
</span><span class="cx" style="display: block; padding: 0 10px">                        };
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        changesetStatus.bind( function( newStatus ) {
-                               populateChangesetUuidParam( '' !== newStatus && 'publish' !== newStatus );
-                       } );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Show changeset UUID in URL when in branching mode and there is a saved changeset.
+                       if ( api.settings.changeset.branching ) {
+                               changesetStatus.bind( function( newStatus ) {
+                                       populateChangesetUuidParam( '' !== newStatus && 'publish' !== newStatus );
+                               } );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Expose states to the API.
</span><span class="cx" style="display: block; padding: 0 10px">                        api.state = state;
</span><span class="cx" style="display: block; padding: 0 10px">                }());
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Set up autosave prompt.
+               (function() {
+
+                       /**
+                        * Obtain the URL to restore the autosave.
+                        *
+                        * @returns {string} Customizer URL.
+                        */
+                       function getAutosaveRestorationUrl() {
+                               var urlParser, queryParams;
+                               urlParser = document.createElement( 'a' );
+                               urlParser.href = location.href;
+                               queryParams = api.utils.parseQueryString( urlParser.search.substr( 1 ) );
+                               if ( api.settings.changeset.latestAutoDraftUuid ) {
+                                       queryParams.changeset_uuid = api.settings.changeset.latestAutoDraftUuid;
+                               } else {
+                                       queryParams.customize_autosaved = 'on';
+                               }
+                               urlParser.search = $.param( queryParams );
+                               return urlParser.href;
+                       }
+
+                       /**
+                        * Remove parameter from the URL.
+                        *
+                        * @param {Array} params - Parameter names to remove.
+                        * @returns {void}
+                        */
+                       function stripParamsFromLocation( params ) {
+                               var urlParser = document.createElement( 'a' ), queryParams, strippedParams = 0;
+                               urlParser.href = location.href;
+                               queryParams = api.utils.parseQueryString( urlParser.search.substr( 1 ) );
+                               _.each( params, function( param ) {
+                                       if ( 'undefined' !== typeof queryParams[ param ] ) {
+                                               strippedParams += 1;
+                                               delete queryParams[ param ];
+                                       }
+                               } );
+                               if ( 0 === strippedParams ) {
+                                       return;
+                               }
+
+                               urlParser.search = $.param( queryParams );
+                               history.replaceState( {}, document.title, urlParser.href );
+                       }
+
+                       /**
+                        * Add notification regarding the availability of an autosave to restore.
+                        *
+                        * @returns {void}
+                        */
+                       function addAutosaveRestoreNotification() {
+                               var code = 'autosave_available', onStateChange;
+
+                               // Since there is an autosave revision and the user hasn't loaded with autosaved, add notification to prompt to load autosaved version.
+                               api.notifications.add( code, new api.Notification( code, {
+                                       message: api.l10n.autosaveNotice,
+                                       type: 'warning',
+                                       dismissible: true,
+                                       render: function() {
+                                               var li = api.Notification.prototype.render.call( this ), link;
+
+                                               // Handle clicking on restoration link.
+                                               link = li.find( 'a' );
+                                               link.prop( 'href', getAutosaveRestorationUrl() );
+                                               link.on( 'click', function( event ) {
+                                                       event.preventDefault();
+                                                       location.replace( getAutosaveRestorationUrl() );
+                                               } );
+
+                                               // Handle dismissal of notice.
+                                               li.find( '.notice-dismiss' ).on( 'click', function() {
+                                                       wp.ajax.post( 'dismiss_customize_changeset_autosave', {
+                                                               wp_customize: 'on',
+                                                               customize_theme: api.settings.theme.stylesheet,
+                                                               customize_changeset_uuid: api.settings.changeset.latestAutoDraftUuid || api.settings.changeset.uuid,
+                                                               nonce: api.settings.nonce.dismiss_autosave
+                                                       } );
+                                               } );
+
+                                               return li;
+                                       }
+                               } ) );
+
+                               // Remove the notification once the user starts making changes.
+                               onStateChange = function() {
+                                       api.notifications.remove( code );
+                                       api.state( 'saved' ).unbind( onStateChange );
+                                       api.state( 'saving' ).unbind( onStateChange );
+                                       api.state( 'changesetStatus' ).unbind( onStateChange );
+                               };
+                               api.state( 'saved' ).bind( onStateChange );
+                               api.state( 'saving' ).bind( onStateChange );
+                               api.state( 'changesetStatus' ).bind( onStateChange );
+                       }
+
+                       if ( api.settings.changeset.autosaved ) {
+                               stripParamsFromLocation( [ 'customize_autosaved' ] ); // Remove param when restoring autosave revision.
+                       } else if ( ! api.settings.changeset.branching && 'auto-draft' === api.settings.changeset.status ) {
+                               stripParamsFromLocation( [ 'changeset_uuid' ] ); // Remove UUID when restoring autosave auto-draft.
+                       }
+                       if ( api.settings.changeset.latestAutoDraftUuid || api.settings.changeset.hasAutosaveRevision ) {
+                               addAutosaveRestoreNotification();
+                       }
+               })();
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Check if preview url is valid and load the preview frame.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( api.previewer.previewUrl() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        api.previewer.refresh();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5742,27 +5904,83 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        channel: 'loader'
</span><span class="cx" style="display: block; padding: 0 10px">                });
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                /*
-                * If we receive a 'back' event, we're inside an iframe.
-                * Send any clicks to the 'Return' link to the parent page.
-                */
-               parent.bind( 'back', function() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Handle exiting of Customizer.
+               (function() {
+                       var isInsideIframe = false;
+
+                       function isCleanState() {
+                               return api.state( 'saved' ).get() && 'auto-draft' !== api.state( 'changesetStatus' ).get();
+                       }
+
+                       /*
+                        * If we receive a 'back' event, we're inside an iframe.
+                        * Send any clicks to the 'Return' link to the parent page.
+                        */
+                       parent.bind( 'back', function() {
+                               isInsideIframe = true;
+                       });
+
+                       // Prompt user with AYS dialog if leaving the Customizer with unsaved changes
+                       $( window ).on( 'beforeunload.customize-confirm', function() {
+                               if ( ! isCleanState() ) {
+                                       setTimeout( function() {
+                                               overlay.removeClass( 'customize-loading' );
+                                       }, 1 );
+                                       return api.l10n.saveAlert;
+                               }
+                       });
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         closeBtn.on( 'click.customize-controls-close', function( event ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                var clearedToClose = $.Deferred();
</ins><span class="cx" style="display: block; padding: 0 10px">                                 event.preventDefault();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                parent.send( 'close' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               /*
+                                * The isInsideIframe condition is because Customizer is not able to use a confirm()
+                                * since customize-loader.js will also use one. So autosave restorations are disabled
+                                * when customize-loader.js is used.
+                                */
+                               if ( isInsideIframe && isCleanState() ) {
+                                       clearedToClose.resolve();
+                               } else if ( confirm( api.l10n.saveAlert ) ) {
+
+                                       // Mark all settings as clean to prevent another call to requestChangesetUpdate.
+                                       api.each( function( setting ) {
+                                               setting._dirty = false;
+                                       });
+                                       $( document ).off( 'visibilitychange.wp-customize-changeset-update' );
+                                       $( window ).off( 'beforeunload.wp-customize-changeset-update' );
+
+                                       closeBtn.css( 'cursor', 'progress' );
+                                       if ( '' === api.state( 'changesetStatus' ).get() ) {
+                                               clearedToClose.resolve();
+                                       } else {
+                                               wp.ajax.send( 'dismiss_customize_changeset_autosave', {
+                                                       timeout: 500, // Don't wait too long.
+                                                       data: {
+                                                               wp_customize: 'on',
+                                                               customize_theme: api.settings.theme.stylesheet,
+                                                               customize_changeset_uuid: api.settings.changeset.uuid,
+                                                               nonce: api.settings.nonce.dismiss_autosave
+                                                       }
+                                               } ).always( function() {
+                                                       clearedToClose.resolve();
+                                               } );
+                                       }
+                               } else {
+                                       clearedToClose.reject();
+                               }
+
+                               clearedToClose.done( function() {
+                                       $( window ).off( 'beforeunload.customize-confirm' );
+                                       if ( isInsideIframe ) {
+                                               parent.send( 'close' );
+                                       } else {
+                                               window.location.href = closeBtn.prop( 'href' );
+                                       }
+                               } );
</ins><span class="cx" style="display: block; padding: 0 10px">                         });
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                });
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         })();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Prompt user with AYS dialog if leaving the Customizer with unsaved changes
-               $( window ).on( 'beforeunload.customize-confirm', function () {
-                       if ( ! api.state( 'saved' )() ) {
-                               setTimeout( function() {
-                                       overlay.removeClass( 'customize-loading' );
-                               }, 1 );
-                               return api.l10n.saveAlert;
-                       }
-               } );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 // Pass events through to the parent.
</span><span class="cx" style="display: block; padding: 0 10px">                $.each( [ 'saved', 'change' ], function ( i, event ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        api.bind( event, function() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6084,6 +6302,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                ( function() {
</span><span class="cx" style="display: block; padding: 0 10px">                        var timeoutId, updateChangesetWithReschedule, scheduleChangesetUpdate, updatePending = false;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        api.state( 'saved' ).bind( function( isSaved ) {
+                               if ( ! isSaved && ! api.settings.changeset.autosaved ) {
+                                       api.settings.changeset.autosaved = true; // Once a change is made then autosaving kicks in.
+                                       api.previewer.send( 'autosaving' );
+                               }
+                       } );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         /**
</span><span class="cx" style="display: block; padding: 0 10px">                         * Request changeset update and then re-schedule the next changeset update time.
</span><span class="cx" style="display: block; padding: 0 10px">                         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6093,7 +6318,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        updateChangesetWithReschedule = function() {
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( ! updatePending ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        updatePending = true;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        api.requestChangesetUpdate().always( function() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 api.requestChangesetUpdate( {}, { autosave: true } ).always( function() {
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 updatePending = false;
</span><span class="cx" style="display: block; padding: 0 10px">                                        } );
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6117,8 +6342,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        scheduleChangesetUpdate();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Save changeset when focus removed from window.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $( window ).on( 'blur.wp-customize-changeset-update', function() {
-                               updateChangesetWithReschedule();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $( document ).on( 'visibilitychange.wp-customize-changeset-update', function() {
+                               if ( document.hidden ) {
+                                       updateChangesetWithReschedule();
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         } );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Save changeset before unloading window.
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-manager.php      2017-09-26 00:11:56 UTC (rev 41596)
+++ trunk/src/wp-includes/class-wp-customize-manager.php        2017-09-26 07:37:02 UTC (rev 41597)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -174,14 +174,38 @@
</span><span class="cx" style="display: block; padding: 0 10px">        protected $messenger_channel;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Whether the autosave revision of the changeset should should be loaded.
+        *
+        * @since 4.9.0
+        * @var bool
+        */
+       protected $autosaved = false;
+
+       /**
+        * Whether the changeset branching is allowed.
+        *
+        * @since 4.9.0
+        * @var bool
+        */
+       protected $branching = true;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Whether settings should be previewed.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.9.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @var bool
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected $settings_previewed;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected $settings_previewed = true;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Whether a starter content changeset was saved.
+        *
+        * @since 4.9.0
+        * @var bool
+        */
+       protected $saved_starter_content_changeset = false;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Unsanitized values for Customize Settings parsed from $_POST['customized'].
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @var array
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -221,16 +245,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array $args {
</span><span class="cx" style="display: block; padding: 0 10px">         *     Args.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *     @type string $changeset_uuid     Changeset UUID, the post_name for the customize_changeset post containing the customized state. Defaults to new UUID.
-        *     @type string $theme              Theme to be previewed (for theme switch). Defaults to customize_theme or theme query params.
-        *     @type string $messenger_channel  Messenger channel. Defaults to customize_messenger_channel query param.
-        *     @type bool   $settings_previewed If settings should be previewed. Defaults to true.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  *     @type null|string|false $changeset_uuid     Changeset UUID, the `post_name` for the customize_changeset post containing the customized state.
+        *                                                 Defaults to `null` resulting in a UUID to be immediately generated. If `false` is provided, then
+        *                                                 then the changeset UUID will be determined during `after_setup_theme`: when the
+        *                                                 `customize_changeset_branching` filter returns false, then the default UUID will be that
+        *                                                 of the most recent `customize_changeset` post that has a status other than 'auto-draft',
+        *                                                 'publish', or 'trash'. Otherwise, if changeset branching is enabled, then a random UUID will be used.
+        *     @type string            $theme              Theme to be previewed (for theme switch). Defaults to customize_theme or theme query params.
+        *     @type string            $messenger_channel  Messenger channel. Defaults to customize_messenger_channel query param.
+        *     @type bool              $settings_previewed If settings should be previewed. Defaults to true.
+        *     @type bool              $branching          If changeset branching is allowed; otherwise, changesets are linear. Defaults to true.
+        *     @type bool              $autosaved          If data from a changeset's autosaved revision should be loaded if it exists. Defaults to false.
</ins><span class="cx" style="display: block; padding: 0 10px">          * }
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function __construct( $args = array() ) {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $args = array_merge(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        array_fill_keys( array( 'changeset_uuid', 'theme', 'messenger_channel', 'settings_previewed' ), null ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 array_fill_keys( array( 'changeset_uuid', 'theme', 'messenger_channel', 'settings_previewed', 'autosaved', 'branching' ), null ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         $args
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -251,16 +282,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $args['messenger_channel'] = sanitize_key( wp_unslash( $_REQUEST['customize_messenger_channel'] ) );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! isset( $args['settings_previewed'] ) ) {
-                       $args['settings_previewed'] = true;
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 $this->original_stylesheet = get_stylesheet();
</span><span class="cx" style="display: block; padding: 0 10px">                $this->theme = wp_get_theme( 0 === validate_file( $args['theme'] ) ? $args['theme'] : null );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->messenger_channel = $args['messenger_channel'];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->settings_previewed = ! empty( $args['settings_previewed'] );
</del><span class="cx" style="display: block; padding: 0 10px">                 $this->_changeset_uuid = $args['changeset_uuid'];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                foreach ( array( 'settings_previewed', 'autosaved', 'branching' ) as $key ) {
+                       if ( isset( $args[ $key ] ) ) {
+                               $this->$key = (bool) $args[ $key ];
+                       }
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 require_once( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
</span><span class="cx" style="display: block; padding: 0 10px">                require_once( ABSPATH . WPINC . '/class-wp-customize-panel.php' );
</span><span class="cx" style="display: block; padding: 0 10px">                require_once( ABSPATH . WPINC . '/class-wp-customize-section.php' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -343,6 +375,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'wp_ajax_customize_save',           array( $this, 'save' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'wp_ajax_customize_refresh_nonces', array( $this, 'refresh_nonces' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_action( 'wp_ajax_dismiss_customize_changeset_autosave', array( $this, 'handle_dismiss_changeset_autosave_request' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'customize_register',                 array( $this, 'register_controls' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'customize_register',                 array( $this, 'register_dynamic_settings' ), 11 ); // allow code to create settings first
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -474,7 +507,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! wp_is_uuid( $this->_changeset_uuid ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // If a changeset was provided is invalid.
+               if ( isset( $this->_changeset_uuid ) && false !== $this->_changeset_uuid && ! wp_is_uuid( $this->_changeset_uuid ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $this->wp_die( -1, __( 'Invalid changeset UUID' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -535,6 +569,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Make sure changeset UUID is established immediately after the theme is loaded.
+               add_action( 'after_setup_theme', array( $this, 'establish_loaded_changeset' ), 5 );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 /*
</span><span class="cx" style="display: block; padding: 0 10px">                 * Import theme starter content for fresh installations when landing in the customizer.
</span><span class="cx" style="display: block; padding: 0 10px">                 * Import starter content at after_setup_theme:100 so that any
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -548,6 +585,72 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Establish the loaded changeset.
+        *
+        * This method runs right at after_setup_theme and applies the 'customize_changeset_branching' filter to determine
+        * whether concurrent changesets are allowed. Then if the Customizer is not initialized with a `changeset_uuid` param,
+        * this method will determine which UUID should be used. If changeset branching is disabled, then the most saved
+        * changeset will be loaded by default. Otherwise, if there are no existing saved changesets or if changeset branching is
+        * enabled, then a new UUID will be generated.
+        *
+        * @since 4.9.0
+        */
+       public function establish_loaded_changeset() {
+
+               /**
+                * Filters whether or not changeset branching is allowed.
+                *
+                * By default in core, when changeset branching is not allowed, changesets will operate
+                * linearly in that only one saved changeset will exist at a time (with a 'draft' or
+                * 'future' status). This makes the Customizer operate in a way that is similar to going to
+                * "edit" to one existing post: all users will be making changes to the same post, and autosave
+                * revisions will be made for that post.
+                *
+                * By contrast, when changeset branching is allowed, then the model is like users going
+                * to "add new" for a page and each user makes changes independently of each other since
+                * they are all operating on their own separate pages, each getting their own separate
+                * initial auto-drafts and then once initially saved, autosave revisions on top of that
+                * user's specific post.
+                *
+                * Since linear changesets are deemed to be more suitable for the majority of WordPress users,
+                * they are the default. For WordPress sites that have heavy site management in the Customizer
+                * by multiple users then branching changesets should be enabled by means of this filter.
+                *
+                * @since 4.9.0
+                *
+                * @param bool                 $allow_branching Whether branching is allowed. If `false`, the default,
+                *                                              then only one saved changeset exists at a time.
+                * @param WP_Customize_Manager $wp_customize    Manager instance.
+                */
+               $this->branching = apply_filters( 'customize_changeset_branching', $this->branching, $this );
+
+               if ( empty( $this->_changeset_uuid ) ) {
+                       $changeset_uuid = null;
+
+                       if ( ! $this->branching ) {
+                               $unpublished_changeset_posts = $this->get_changeset_posts( array(
+                                       'post_status' => array_diff( get_post_stati(), array( 'auto-draft', 'publish', 'trash', 'inherit', 'private' ) ),
+                                       'exclude_restore_dismissed' => false,
+                                       'posts_per_page' => 1,
+                                       'order' => 'DESC',
+                                       'orderby' => 'date',
+                               ) );
+                               $unpublished_changeset_post = array_shift( $unpublished_changeset_posts );
+                               if ( ! empty( $unpublished_changeset_post ) && wp_is_uuid( $unpublished_changeset_post->post_name ) ) {
+                                       $changeset_uuid = $unpublished_changeset_post->post_name;
+                               }
+                       }
+
+                       // If no changeset UUID has been set yet, then generate a new one.
+                       if ( empty( $changeset_uuid ) ) {
+                               $changeset_uuid = wp_generate_uuid4();
+                       }
+
+                       $this->_changeset_uuid = $changeset_uuid;
+               }
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Callback to validate a theme once it is loaded
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 3.4.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -652,10 +755,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Get the changeset UUID.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.7.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 4.9.0 An exception is thrown if the changeset UUID has not been established yet.
+        * @see WP_Customize_Manager::establish_loaded_changeset()
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @throws Exception When the UUID has not been set yet.
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return string UUID.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function changeset_uuid() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( empty( $this->_changeset_uuid ) ) {
+                       throw new Exception( 'Changeset UUID has not been set.' ); // @todo Replace this with a call to `WP_Customize_Manager::establish_loaded_changeset()` during 4.9-beta2.
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $this->_changeset_uuid;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -825,6 +934,53 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Get changeset posts.
+        *
+        * @since 4.9.0
+        *
+        * @param array $args {
+        *     Args to pass into `get_posts()` to query changesets.
+        *
+        *     @type int    $posts_per_page             Number of posts to return. Defaults to -1 (all posts).
+        *     @type int    $author                     Post author. Defaults to current user.
+        *     @type string $post_status                Status of changeset. Defaults to 'auto-draft'.
+        *     @type bool   $exclude_restore_dismissed  Whether to exclude changeset auto-drafts that have been dismissed. Defaults to true.
+        * }
+        * @return WP_Post[] Auto-draft changesets.
+        */
+       protected function get_changeset_posts( $args = array() ) {
+               $default_args = array(
+                       'exclude_restore_dismissed' => true,
+                       'posts_per_page' => -1,
+                       'post_type' => 'customize_changeset',
+                       'post_status' => 'auto-draft',
+                       'order' => 'DESC',
+                       'orderby' => 'date',
+                       'no_found_rows' => true,
+                       'cache_results' => true,
+                       'update_post_meta_cache' => false,
+                       'update_post_term_cache' => false,
+                       'lazy_load_term_meta' => false,
+               );
+               if ( get_current_user_id() ) {
+                       $default_args['author'] = get_current_user_id();
+               }
+               $args = array_merge( $default_args, $args );
+
+               if ( ! empty( $args['exclude_restore_dismissed'] ) ) {
+                       unset( $args['exclude_restore_dismissed'] );
+                       $args['meta_query'] = array(
+                               array(
+                                       'key' => '_customize_restore_dismissed',
+                                       'compare' => 'NOT EXISTS',
+                               ),
+                       );
+               }
+
+               return get_posts( $args );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Get the changeset post id for the loaded changeset.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.7.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -833,7 +989,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function changeset_post_id() {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! isset( $this->_changeset_post_id ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $post_id = $this->find_changeset_post_id( $this->_changeset_uuid );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $post_id = $this->find_changeset_post_id( $this->changeset_uuid() );
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( ! $post_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $post_id = false;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -861,7 +1017,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $changeset_post ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return new WP_Error( 'missing_post' );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( 'customize_changeset' !== $changeset_post->post_type ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( 'revision' === $changeset_post->post_type ) {
+                       if ( 'customize_changeset' !== get_post_type( $changeset_post->post_parent ) ) {
+                               return new WP_Error( 'wrong_post_type' );
+                       }
+               } elseif ( 'customize_changeset' !== $changeset_post->post_type ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return new WP_Error( 'wrong_post_type' );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">                $changeset_data = json_decode( $changeset_post->post_content, true );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -878,6 +1038,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Get changeset data.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.7.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 4.9.0 This will return the changeset's data with a user's autosave revision merged on top, if one exists and $autosaved is true.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array Changeset data.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -889,12 +1050,25 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $changeset_post_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->_changeset_data = array();
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $data = $this->get_changeset_post_data( $changeset_post_id );
-                       if ( ! is_wp_error( $data ) ) {
-                               $this->_changeset_data = $data;
-                       } else {
-                               $this->_changeset_data = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( $this->autosaved ) {
+                               $autosave_post = wp_get_post_autosave( $changeset_post_id );
+                               if ( $autosave_post ) {
+                                       $data = $this->get_changeset_post_data( $autosave_post->ID );
+                                       if ( ! is_wp_error( $data ) ) {
+                                               $this->_changeset_data = $data;
+                                       }
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Load data from the changeset if it was not loaded from an autosave.
+                       if ( ! isset( $this->_changeset_data ) ) {
+                               $data = $this->get_changeset_post_data( $changeset_post_id );
+                               if ( ! is_wp_error( $data ) ) {
+                                       $this->_changeset_data = $data;
+                               } else {
+                                       $this->_changeset_data = array();
+                               }
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">                return $this->_changeset_data;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1374,6 +1548,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'data' => array_fill_keys( $this->pending_starter_content_settings_ids, array( 'starter_content' => true ) ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'starter_content' => true,
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->saved_starter_content_changeset = true;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->pending_starter_content_settings_ids = array();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1796,7 +1971,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $settings = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'changeset' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'uuid' => $this->_changeset_uuid,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'uuid' => $this->changeset_uuid(),
+                               'autosaved' => $this->autosaved,
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'timeouts' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'selectiveRefresh' => 250,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2078,7 +2254,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $changeset_post_id = $this->changeset_post_id();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( empty( $changeset_post_id ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $is_new_changeset = empty( $changeset_post_id );
+               if ( $is_new_changeset ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->create_posts ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                wp_send_json_error( 'cannot_create_changeset_post' );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2144,11 +2321,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $autosave = ! empty( $_POST['customize_changeset_autosave'] );
+               if ( $autosave && ! defined( 'DOING_AUTOSAVE' ) ) { // Back-compat.
+                       define( 'DOING_AUTOSAVE', true );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $r = $this->save_changeset_post( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'status' => $changeset_status,
</span><span class="cx" style="display: block; padding: 0 10px">                        'title' => $changeset_title,
</span><span class="cx" style="display: block; padding: 0 10px">                        'date_gmt' => $changeset_date_gmt,
</span><span class="cx" style="display: block; padding: 0 10px">                        'data' => $input_changeset_data,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'autosave' => $autosave,
</ins><span class="cx" style="display: block; padding: 0 10px">                 ) );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_wp_error( $r ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $response = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2163,6 +2346,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><span class="cx" style="display: block; padding: 0 10px">                        $response = $r;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // Dismiss all other auto-draft changeset posts for this user (they serve like autosave revisions), as there should only be one.
+                       if ( $is_new_changeset ) {
+                               $changeset_autodraft_posts = $this->get_changeset_posts( array(
+                                       'post_status' => 'auto-draft',
+                                       'exclude_restore_dismissed' => true,
+                                       'posts_per_page' => -1,
+                               ) );
+                               foreach ( $changeset_autodraft_posts as $autosave_autodraft_post ) {
+                                       if ( $autosave_autodraft_post->ID !== $this->changeset_post_id() ) {
+                                               update_post_meta( $autosave_autodraft_post->ID, '_customize_restore_dismissed', true );
+                                       }
+                               }
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         // Note that if the changeset status was publish, then it will get set to trash if revisions are not supported.
</span><span class="cx" style="display: block; padding: 0 10px">                        $response['changeset_status'] = get_post_status( $this->changeset_post_id() );
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( $is_publish && 'trash' === $response['changeset_status'] ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2212,6 +2409,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type string $date_gmt        Date in GMT. Optional.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type int    $user_id         ID for user who is saving the changeset. Optional, defaults to the current user ID.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type bool   $starter_content Whether the data is starter content. If false (default), then $starter_content will be cleared for any $data being saved.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         *     @type bool   $autosave        Whether this is a request to create an autosave revision.
</ins><span class="cx" style="display: block; padding: 0 10px">          * }
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array|WP_Error Returns array on success and WP_Error with array data on error.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2226,6 +2424,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'date_gmt' => null,
</span><span class="cx" style="display: block; padding: 0 10px">                                'user_id' => get_current_user_id(),
</span><span class="cx" style="display: block; padding: 0 10px">                                'starter_content' => false,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                'autosave' => false,
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                        $args
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2277,6 +2476,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $args['status'] = 'future';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Validate autosave param. See _wp_post_revision_fields() for why these fields are disallowed.
+               if ( $args['autosave'] ) {
+                       if ( $args['date_gmt'] ) {
+                               return new WP_Error( 'illegal_autosave_with_date_gmt' );
+                       } elseif ( $args['status'] ) {
+                               return new WP_Error( 'illegal_autosave_with_status' );
+                       } elseif ( $args['user_id'] && get_current_user_id() !== $args['user_id'] ) {
+                               return new WP_Error( 'illegal_autosave_with_non_current_user' );
+                       }
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // The request was made via wp.customize.previewer.save().
</span><span class="cx" style="display: block; padding: 0 10px">                $update_transactionally = (bool) $args['status'];
</span><span class="cx" style="display: block; padding: 0 10px">                $allow_revision = (bool) $args['status'];
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2519,8 +2729,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Note that updating a post with publish status will trigger WP_Customize_Manager::publish_changeset_values().
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $changeset_post_id ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $post_array['edit_date'] = true; // Prevent date clearing.
-                       $r = wp_update_post( wp_slash( $post_array ), true );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( $args['autosave'] && 'auto-draft' !== get_post_status( $changeset_post_id ) ) {
+                               // See _wp_translate_postdata() for why this is required as it will use the edit_post meta capability.
+                               add_filter( 'map_meta_cap', array( $this, 'grant_edit_post_capability_for_changeset' ), 10, 4 );
+                               $post_array['post_ID'] = $post_array['ID'];
+                               $post_array['post_type'] = 'customize_changeset';
+                               $r = wp_create_post_autosave( wp_slash( $post_array ) );
+                               remove_filter( 'map_meta_cap', array( $this, 'grant_edit_post_capability_for_changeset' ), 10 );
+                       } else {
+                               $post_array['edit_date'] = true; // Prevent date clearing.
+                               $r = wp_update_post( wp_slash( $post_array ), true );
+
+                               // Delete autosave revision when the changeset is updated.
+                               $autosave_draft = wp_get_post_autosave( $changeset_post_id );
+                               if ( $autosave_draft ) {
+                                       wp_delete_post( $autosave_draft->ID, true );
+                               }
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 } else {
</span><span class="cx" style="display: block; padding: 0 10px">                        $r = wp_insert_post( wp_slash( $post_array ), true );
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! is_wp_error( $r ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2547,6 +2772,35 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Re-map 'edit_post' meta cap for a customize_changeset post to be the same as 'customize' maps.
+        *
+        * There is essentially a "meta meta" cap in play here, where 'edit_post' meta cap maps to
+        * the 'customize' meta cap which then maps to 'edit_theme_options'. This is currently
+        * required in core for `wp_create_post_autosave()` because it will call
+        * `_wp_translate_postdata()` which in turn will check if a user can 'edit_post', but the
+        * the caps for the customize_changeset post type are all mapping to the meta capability.
+        * This should be able to be removed once #40922 is addressed in core.
+        *
+        * @since 4.9.0
+        * @link https://core.trac.wordpress.org/ticket/40922
+        * @see WP_Customize_Manager::save_changeset_post()
+        * @see _wp_translate_postdata()
+        *
+        * @param array  $caps    Returns the user's actual capabilities.
+        * @param string $cap     Capability name.
+        * @param int    $user_id The user ID.
+        * @param array  $args    Adds the context to the cap. Typically the object ID.
+        * @return array Capabilities.
+        */
+       public function grant_edit_post_capability_for_changeset( $caps, $cap, $user_id, $args ) {
+               if ( 'edit_post' === $cap && ! empty( $args[0] ) && 'customize_changeset' === get_post_type( $args[0] ) ) {
+                       $post_type_obj = get_post_type_object( 'customize_changeset' );
+                       $caps = map_meta_cap( $post_type_obj->cap->$cap, $user_id );
+               }
+               return $caps;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Whether a changeset revision should be made.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.7.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2786,6 +3040,51 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Delete a given auto-draft changeset or the autosave revision for a given changeset.
+        *
+        * @since 4.9.0
+        */
+       public function handle_dismiss_changeset_autosave_request() {
+               if ( ! $this->is_preview() ) {
+                       wp_send_json_error( 'not_preview', 400 );
+               }
+
+               if ( ! check_ajax_referer( 'dismiss_customize_changeset_autosave', 'nonce', false ) ) {
+                       wp_send_json_error( 'invalid_nonce', 403 );
+               }
+
+               $changeset_post_id = $this->changeset_post_id();
+               if ( empty( $changeset_post_id ) ) {
+                       wp_send_json_error( 'missing_changeset', 404 );
+               }
+
+               if ( 'auto-draft' === get_post_status( $changeset_post_id ) ) {
+                       if ( ! update_post_meta( $changeset_post_id, '_customize_restore_dismissed', true ) ) {
+                               wp_send_json_error( 'auto_draft_dismissal_failure', 500 );
+                       } else {
+                               wp_send_json_success( 'auto_draft_dismissed' );
+                       }
+               } else {
+                       $revision = wp_get_post_autosave( $changeset_post_id );
+
+                       if ( $revision ) {
+                               if ( ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->delete_post, $changeset_post_id ) ) {
+                                       wp_send_json_error( 'cannot_delete_autosave_revision', 403 );
+                               }
+
+                               if ( ! wp_delete_post( $revision->ID, true ) ) {
+                                       wp_send_json_error( 'autosave_revision_deletion_failure', 500 );
+                               } else {
+                                       wp_send_json_success( 'autosave_revision_deleted' );
+                               }
+                       } else {
+                               wp_send_json_error( 'no_autosave_to_delete', 404 );
+                       }
+               }
+               wp_send_json_error( 'unknown_error', 500 );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Add a customize setting.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 3.4.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3527,6 +3826,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $nonces = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'dismiss_autosave' => wp_create_nonce( 'dismiss_customize_changeset_autosave' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3563,11 +3863,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $autosave_revision_post = null;
+               $autosave_autodraft_post = null;
+               $changeset_post_id = $this->changeset_post_id();
+               if ( ! $this->saved_starter_content_changeset && ! $this->autosaved ) {
+                       if ( $changeset_post_id ) {
+                               $autosave_revision_post = wp_get_post_autosave( $changeset_post_id );
+                       } else {
+                               $autosave_autodraft_posts = $this->get_changeset_posts( array(
+                                       'posts_per_page' => 1,
+                                       'post_status' => 'auto-draft',
+                                       'exclude_restore_dismissed' => true,
+                               ) );
+                               if ( ! empty( $autosave_autodraft_posts ) ) {
+                                       $autosave_autodraft_post = array_shift( $autosave_autodraft_posts );
+                               }
+                       }
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Prepare Customizer settings to pass to JavaScript.
</span><span class="cx" style="display: block; padding: 0 10px">                $settings = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'changeset' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'uuid' => $this->changeset_uuid(),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'status' => $this->changeset_post_id() ? get_post_status( $this->changeset_post_id() ) : '',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'branching' => $this->branching,
+                               'autosaved' => $this->autosaved,
+                               'hasAutosaveRevision' => ! empty( $autosave_revision_post ),
+                               'latestAutoDraftUuid' => $autosave_autodraft_post ? $autosave_autodraft_post->post_name : null,
+                               'status' => $changeset_post_id ? get_post_status( $changeset_post_id ) : '',
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'timeouts' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'windowRefresh' => 250,
</span></span></pre></div>
<a id="trunksrcwpincludesjscustomizepreviewjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/js/customize-preview.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/js/customize-preview.js     2017-09-26 00:11:56 UTC (rev 41596)
+++ trunk/src/wp-includes/js/customize-preview.js       2017-09-26 07:37:02 UTC (rev 41597)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -36,6 +36,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        newQueryParams = api.utils.parseQueryString( urlParser.search.substr( 1 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        newQueryParams.customize_changeset_uuid = oldQueryParams.customize_changeset_uuid;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        if ( api.settings.changeset.autosaved ) {
+                               newQueryParams.customize_autosaved = 'on';
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( oldQueryParams.customize_theme ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                newQueryParams.customize_theme = oldQueryParams.customize_theme;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -364,6 +367,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                queryParams = api.utils.parseQueryString( element.search.substring( 1 ) );
</span><span class="cx" style="display: block; padding: 0 10px">                queryParams.customize_changeset_uuid = api.settings.changeset.uuid;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( api.settings.changeset.autosaved ) {
+                       queryParams.customize_autosaved = 'on';
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! api.settings.theme.active ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        queryParams.customize_theme = api.settings.theme.stylesheet;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -439,6 +445,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Include customized state query params in URL.
</span><span class="cx" style="display: block; padding: 0 10px">                        queryParams.customize_changeset_uuid = api.settings.changeset.uuid;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        if ( api.settings.changeset.autosaved ) {
+                               queryParams.customize_autosaved = 'on';
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( ! api.settings.theme.active ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                queryParams.customize_theme = api.settings.theme.stylesheet;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -516,6 +525,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $( form ).removeClass( 'customize-unpreviewable' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                stateParams.customize_changeset_uuid = api.settings.changeset.uuid;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( api.settings.changeset.autosaved ) {
+                       stateParams.customize_autosaved = 'on';
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! api.settings.theme.active ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        stateParams.customize_theme = api.settings.theme.stylesheet;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -555,7 +567,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                var previousPathName = location.pathname,
</span><span class="cx" style="display: block; padding: 0 10px">                        previousQueryString = location.search.substr( 1 ),
</span><span class="cx" style="display: block; padding: 0 10px">                        previousQueryParams = null,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        stateQueryParams = [ 'customize_theme', 'customize_changeset_uuid', 'customize_messenger_channel' ];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 stateQueryParams = [ 'customize_theme', 'customize_changeset_uuid', 'customize_messenger_channel', 'customize_autosaved' ];
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                return function keepAliveCurrentUrl() {
</span><span class="cx" style="display: block; padding: 0 10px">                        var urlParser, currentQueryParams;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -754,7 +766,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                });
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                api.preview.bind( 'saved', function( response ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
</del><span class="cx" style="display: block; padding: 0 10px">                         if ( response.next_changeset_uuid ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                api.settings.changeset.uuid = response.next_changeset_uuid;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -779,6 +790,25 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        api.trigger( 'saved', response );
</span><span class="cx" style="display: block; padding: 0 10px">                } );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Update the URLs to reflect the fact we've started autosaving.
+               api.preview.bind( 'autosaving', function() {
+                       if ( api.settings.changeset.autosaved ) {
+                               return;
+                       }
+
+                       api.settings.changeset.autosaved = true; // Start deferring to any autosave once changeset is updated.
+
+                       $( document.body ).find( 'a[href], area' ).each( function() {
+                               api.prepareLinkPreview( this );
+                       } );
+                       $( document.body ).find( 'form' ).each( function() {
+                               api.prepareFormPreview( this );
+                       } );
+                       if ( history.replaceState ) {
+                               history.replaceState( currentHistoryState, '', location.href );
+                       }
+               } );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 /*
</span><span class="cx" style="display: block; padding: 0 10px">                 * Clear dirty flag for settings when saved to changeset so that they
</span><span class="cx" style="display: block; padding: 0 10px">                 * won't be needlessly included in selective refresh or ajax requests.
</span></span></pre></div>
<a id="trunksrcwpincludesscriptloaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/script-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/script-loader.php   2017-09-26 00:11:56 UTC (rev 41596)
+++ trunk/src/wp-includes/script-loader.php     2017-09-26 07:37:02 UTC (rev 41597)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -561,6 +561,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'expandSidebar'      => _x( 'Show Controls', 'label for hide controls button without length constraints' ),
</span><span class="cx" style="display: block; padding: 0 10px">                'untitledBlogName'   => __( '(Untitled)' ),
</span><span class="cx" style="display: block; padding: 0 10px">                'serverSaveError'    => __( 'Failed connecting to the server. Please try saving again.' ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /* translators: placeholder is URL to the Customizer to load the autosaved version */
+               'autosaveNotice'     => __( 'There is a more recent autosave of your changes than the one you are previewing. <a href="%s">Restore the autosave</a>' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                 'videoHeaderNotice'   => __( 'This theme doesn\'t support video headers on this page. Navigate to the front page or another page that supports video headers.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                // Used for overriding the file types allowed in plupload.
</span><span class="cx" style="display: block; padding: 0 10px">                'allowedFiles'       => __( 'Allowed Files' ),
</span></span></pre></div>
<a id="trunksrcwpincludesthemephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/theme.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/theme.php   2017-09-26 00:11:56 UTC (rev 41596)
+++ trunk/src/wp-includes/theme.php     2017-09-26 07:37:02 UTC (rev 41597)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2787,15 +2787,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * called before wp_magic_quotes() gets called. Besides this fact, none of
</span><span class="cx" style="display: block; padding: 0 10px">         * the values should contain any characters needing slashes anyway.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $keys = array( 'changeset_uuid', 'customize_changeset_uuid', 'customize_theme', 'theme', 'customize_messenger_channel' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $keys = array( 'changeset_uuid', 'customize_changeset_uuid', 'customize_theme', 'theme', 'customize_messenger_channel', 'customize_autosaved' );
</ins><span class="cx" style="display: block; padding: 0 10px">         $input_vars = array_merge(
</span><span class="cx" style="display: block; padding: 0 10px">                wp_array_slice_assoc( $_GET, $keys ),
</span><span class="cx" style="display: block; padding: 0 10px">                wp_array_slice_assoc( $_POST, $keys )
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $theme = null;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $changeset_uuid = null;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $changeset_uuid = false; // Value false indicates UUID should be determined after_setup_theme to either re-use existing saved changeset or else generate a new UUID if none exists.
</ins><span class="cx" style="display: block; padding: 0 10px">         $messenger_channel = null;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        $autosaved = null;
+       $branching = false; // Set initially fo false since defaults to true for back-compat; can be overridden via the customize_changeset_branching filter.
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( $is_customize_admin_page && isset( $input_vars['changeset_uuid'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $changeset_uuid = sanitize_key( $input_vars['changeset_uuid'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2809,6 +2811,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">        } elseif ( isset( $input_vars['customize_theme'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $theme = $input_vars['customize_theme'];
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       if ( ! empty( $input_vars['customize_autosaved'] ) ) {
+               $autosaved = true;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( isset( $input_vars['customize_messenger_channel'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $messenger_channel = sanitize_key( $input_vars['customize_messenger_channel'] );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2830,7 +2837,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $settings_previewed = ! $is_customize_save_action;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $GLOBALS['wp_customize'] = new WP_Customize_Manager( compact( 'changeset_uuid', 'theme', 'messenger_channel', 'settings_previewed' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $GLOBALS['wp_customize'] = new WP_Customize_Manager( compact( 'changeset_uuid', 'theme', 'messenger_channel', 'settings_previewed', 'autosaved', 'branching' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre>
</div>
</div>

</body>
</html>