Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Commit 9ca3ff2

Browse files
committed
Ensure frontend preview link gets customize_theme param in theme switches
The 'customize_theme' param is used on the frontend, whereas just 'theme' is used in the admin.
1 parent 2559457 commit 9ca3ff2

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

js/compat/customize-snapshots.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,34 @@
500500

501501
snapshot.updateCountdown();
502502
snapshot.editContainer.find( '.reset-time' ).toggle( scheduled );
503+
},
504+
505+
/**
506+
* Parse query string.
507+
*
508+
* Polyfill for function was introduced into core in 4.7 as wp.customize.utils.parseQueryString.
509+
*
510+
* @param {string} queryString Query string.
511+
* @returns {object} Parsed query string.
512+
*/
513+
parseQueryString: function parseQueryString( queryString ) {
514+
var queryParams = {};
515+
_.each( queryString.split( '&' ), function( pair ) {
516+
var parts, key, value;
517+
parts = pair.split( '=', 2 );
518+
if ( ! parts[0] ) {
519+
return;
520+
}
521+
key = decodeURIComponent( parts[0].replace( /\+/g, ' ' ) );
522+
key = key.replace( / /g, '_' ); // What PHP does.
523+
if ( _.isUndefined( parts[1] ) ) {
524+
value = null;
525+
} else {
526+
value = decodeURIComponent( parts[1].replace( /\+/g, ' ' ) );
527+
}
528+
queryParams[ key ] = value;
529+
} );
530+
return queryParams;
503531
}
504532
} );
505533

js/customize-snapshots.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,19 @@
351351
snapshot.previewLink.toggle( api.state( 'snapshot-saved' ).get() );
352352
snapshot.previewLink.attr( 'target', snapshot.data.uuid );
353353
setPreviewLinkHref = _.debounce( function() {
354+
var queryVars;
354355
if ( api.state( 'snapshot-exists' ).get() ) {
355356
snapshot.previewLink.attr( 'href', snapshot.getSnapshotFrontendPreviewUrl() );
356357
} else {
357358
snapshot.previewLink.attr( 'href', snapshot.frontendPreviewUrl.get() );
358359
}
360+
361+
// Add the customize_theme param to the frontend URL if the theme is not active.
362+
if ( ! api.state( 'activated' ).get() ) {
363+
queryVars = snapshot.parseQueryString( snapshot.previewLink.prop( 'search' ).substr( 1 ) );
364+
queryVars.customize_theme = api.settings.theme.stylesheet;
365+
snapshot.previewLink.prop( 'search', $.param( queryVars ) );
366+
}
359367
} );
360368
snapshot.frontendPreviewUrl.bind( setPreviewLinkHref );
361369
setPreviewLinkHref();
@@ -1178,23 +1186,29 @@
11781186
* @return {void}.
11791187
*/
11801188
removeParamFromClose: function removeParamFromClose( targetParam ) {
1181-
var closeButton, queryString, updatedParams;
1189+
var snapshot = this, closeButton, queryString, updatedParams;
11821190
closeButton = $( '.customize-controls-close' );
11831191
queryString = closeButton.prop( 'search' ).substr( 1 );
11841192

11851193
if ( ! queryString.length ) {
11861194
return;
11871195
}
11881196

1189-
updatedParams = _.filter(
1190-
queryString.split( '&' ),
1191-
function( paramPair ) {
1192-
return 0 !== paramPair.indexOf( targetParam + '=' );
1193-
}
1194-
);
1197+
updatedParams = snapshot.parseQueryString( queryString );
1198+
delete updatedParams[ targetParam ];
1199+
closeButton.prop( 'search', $.param( updatedParams ) );
1200+
},
11951201

1196-
closeButton.prop( 'search', updatedParams.join( '&' ) );
1197-
}
1202+
/**
1203+
* Parse query string.
1204+
*
1205+
* @since 4.7.0
1206+
* @access public
1207+
*
1208+
* @param {string} queryString Query string.
1209+
* @returns {object} Parsed query string.
1210+
*/
1211+
parseQueryString: api.utils.parseQueryString
11981212
} );
11991213

12001214
if ( 'undefined' !== typeof _customizeSnapshotsSettings ) {

php/class-post-type.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ public function set_customizer_state_query_vars( $post_id, $query_vars ) {
900900
}
901901
if ( isset( $query_vars['previewing_theme'] ) ) {
902902
$theme = $this->snapshot_manager->customize_manager->get_stylesheet();
903-
$stored_query_vars['theme'] = $query_vars['previewing_theme'] ? $theme: '';
903+
$stored_query_vars['theme'] = $query_vars['previewing_theme'] ? $theme : '';
904904
}
905905
update_post_meta( $post_id, '_preview_url_query_vars', $stored_query_vars );
906906
return $stored_query_vars;
@@ -928,7 +928,7 @@ public function get_frontend_view_link( $post ) {
928928

929929
if ( isset( $preview_url_query_vars['theme'] ) && $current_theme !== $preview_url_query_vars['theme'] ) {
930930
$args = array_merge( $args, array(
931-
'theme' => $preview_url_query_vars['theme'],
931+
'customize_theme' => $preview_url_query_vars['theme'],
932932
) );
933933
}
934934

0 commit comments

Comments
 (0)