Customize: Ensure state query params persist in preview through calls to `history.pushState()` & `history.replaceState()`.

Allow history to be manipulated before DOM ready by sourcing state params from the current URL instead of from the `wp.customize.settings` object, since they will be the same anyway. This fixes a JS error since `wp.customize.settings` is not defined before DOM ready.

Amends [38810].
See #30937.
Fixes #38592.


git-svn-id: https://develop.svn.wordpress.org/trunk@39060 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter 2016-10-31 16:41:06 +00:00
parent f34e252334
commit abc7c676a4
1 changed files with 10 additions and 9 deletions

View File

@ -29,20 +29,21 @@
* @returns {string} URL with customized state.
*/
injectUrlWithState = function( url ) {
var urlParser, queryParams;
var urlParser, oldQueryParams, newQueryParams;
urlParser = document.createElement( 'a' );
urlParser.href = url;
queryParams = api.utils.parseQueryString( urlParser.search.substr( 1 ) );
oldQueryParams = api.utils.parseQueryString( location.search.substr( 1 ) );
newQueryParams = api.utils.parseQueryString( urlParser.search.substr( 1 ) );
queryParams.customize_changeset_uuid = api.settings.changeset.uuid;
if ( ! api.settings.theme.active ) {
queryParams.customize_theme = api.settings.theme.stylesheet;
newQueryParams.customize_changeset_uuid = oldQueryParams.customize_changeset_uuid;
if ( oldQueryParams.customize_theme ) {
newQueryParams.customize_theme = oldQueryParams.customize_theme;
}
if ( api.settings.theme.channel ) {
queryParams.customize_messenger_channel = api.settings.channel;
if ( oldQueryParams.customize_messenger_channel ) {
newQueryParams.customize_messenger_channel = oldQueryParams.customize_messenger_channel;
}
urlParser.search = $.param( queryParams );
return url;
urlParser.search = $.param( newQueryParams );
return urlParser.href;
};
history.replaceState = ( function( nativeReplaceState ) {