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

Commit

Permalink
Merge eb003e3 into c7b9124
Browse files Browse the repository at this point in the history
  • Loading branch information
Sayed Taqui committed Mar 21, 2017
2 parents c7b9124 + eb003e3 commit ef1b528
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 47 deletions.
45 changes: 42 additions & 3 deletions js/compat/customize-snapshots.js
Expand Up @@ -20,7 +20,6 @@

api.bind( 'ready', function() {
api.state.create( 'snapshot-exists', snapshot.data.snapshotExists );
snapshot.extendPreviewerQuery();

if ( api.state( 'snapshot-exists' ).get() ) {
api.state( 'saved' ).set( false );
Expand Down Expand Up @@ -248,7 +247,9 @@
addButtons: function addButtons() {
var snapshot = this,
header = $( '#customize-header-actions' ),
templateData = {}, setPreviewLinkHref;
disableButton = true,
templateData = {}, setPreviewLinkHref, currentTheme,
savedPreviewingTheme, themeNotActiveOrSaved;

snapshot.publishButton = header.find( '#save' );
snapshot.spinner = header.find( '.spinner' );
Expand All @@ -270,7 +271,17 @@
if ( ! snapshot.data.currentUserCanPublish ) {
snapshot.snapshotButton.attr( 'title', api.state( 'snapshot-exists' ).get() ? snapshot.data.i18n.permsMsg.update : snapshot.data.i18n.permsMsg.save );
}
snapshot.snapshotButton.prop( 'disabled', true );

currentTheme = api.settings.theme.stylesheet; // Or previewing theme.
savedPreviewingTheme = snapshot.data.previewingTheme;
themeNotActiveOrSaved = ! api.state( 'activated' ).get() && ! savedPreviewingTheme;
snapshot.isNotSavedPreviewingTheme = savedPreviewingTheme && savedPreviewingTheme !== currentTheme;

if ( themeNotActiveOrSaved || snapshot.isNotSavedPreviewingTheme ) {
disableButton = false;
}

snapshot.snapshotButton.prop( 'disabled', disableButton );

snapshot.snapshotButton.on( 'click', function( event ) {
var status;
Expand Down Expand Up @@ -489,6 +500,34 @@

snapshot.updateCountdown();
snapshot.editContainer.find( '.reset-time' ).toggle( scheduled );
},

/**
* Parse query string.
*
* Polyfill for function was introduced into core in 4.7 as wp.customize.utils.parseQueryString.
*
* @param {string} queryString Query string.
* @returns {object} Parsed query string.
*/
parseQueryString: function parseQueryString( queryString ) {
var queryParams = {};
_.each( queryString.split( '&' ), function( pair ) {
var parts, key, value;
parts = pair.split( '=', 2 );
if ( ! parts[0] ) {
return;
}
key = decodeURIComponent( parts[0].replace( /\+/g, ' ' ) );
key = key.replace( / /g, '_' ); // What PHP does.
if ( _.isUndefined( parts[1] ) ) {
value = null;
} else {
value = decodeURIComponent( parts[1].replace( /\+/g, ' ' ) );
}
queryParams[ key ] = value;
} );
return queryParams;
}
} );

Expand Down
85 changes: 59 additions & 26 deletions js/customize-snapshots.js
Expand Up @@ -19,6 +19,7 @@
initialServerDate: '',
initialServerTimestamp: 0,
initialClientTimestamp: 0,
previewingTheme: '',
i18n: {},
dirty: false
},
Expand Down Expand Up @@ -51,6 +52,8 @@
api.state( 'snapshot-exists' ).set( true );
}

snapshot.extendPreviewerQuery();

snapshot.editControlSettings = new api.Values();
snapshot.editControlSettings.create( 'title', snapshot.data.title );
snapshot.editControlSettings.create( 'date', snapshot.data.publishDate );
Expand All @@ -61,6 +64,7 @@

snapshot.frontendPreviewUrl = new api.Value( api.previewer.previewUrl.get() );
snapshot.frontendPreviewUrl.link( api.previewer.previewUrl );
snapshot.isNotSavedPreviewingTheme = false;

snapshot.addButtons();
snapshot.editSnapshotUI();
Expand Down Expand Up @@ -104,15 +108,22 @@
* @return {{}} Query vars for scroll, device, url, and autofocus.
*/
getStateQueryVars: function() {
var queryVars = {
var snapshot = this, queryVars;

queryVars = {
'autofocus[control]': null,
'autofocus[section]': null,
'autofocus[panel]': null
};

queryVars.scroll = parseInt( api.previewer.scroll, 10 ) || 0;
queryVars.device = api.previewedDevice.get();
queryVars.url = api.previewer.previewUrl.get();

if ( ! api.state( 'activated' ).get() || snapshot.isNotSavedPreviewingTheme ) {
queryVars.previewing_theme = true;
}

_.find( [ 'control', 'section', 'panel' ], function( constructType ) {
var found = false;
api[ constructType ].each( function( construct ) { // @todo Core needs to support more Backbone methods on wp.customize.Values().
Expand Down Expand Up @@ -225,7 +236,7 @@
}
}, savedDelay );

api.state( 'snapshot-exists' ).set( true );
api.state( 'snapshot-exists' ).set( ! isPublishStatus );

snapshot.statusButton.disableSelect.set( isPublishStatus );
snapshot.statusButton.disbleButton.set( true );
Expand Down Expand Up @@ -255,6 +266,7 @@
dialogElement;

snapshot.statusButton.disableSelect.set( false );
snapshot.statusButton.disbleButton.set( false );

if ( response.setting_validities ) {
invalidityCount = _.size( response.setting_validities, function( validity ) {
Expand Down Expand Up @@ -303,35 +315,55 @@
* @return {void}
*/
addButtons: function addButtons() {
var snapshot = this, setPreviewLinkHref;
var snapshot = this, disableButton = true, disableSelectButton = true,
setPreviewLinkHref, currentTheme, savedPreviewingTheme, themeNotActiveOrSaved;

snapshot.spinner = $( '#customize-header-actions' ).find( '.spinner' );
snapshot.publishButton = $( '#save' );

snapshot.publishButton.addClass( 'hidden' );
snapshot.statusButton = snapshot.addStatusButton();
snapshot.statusButton.disbleButton.set( true );

if ( api.state( 'changesetStatus' ).get() ) {
disableSelectButton = false;
if ( 'auto-draft' === api.state( 'changesetStatus' ).get() ) {
snapshot.statusButton.disable( false );
disableButton = false;
} else {
snapshot.statusButton.updateButtonText( 'alt-text' );
}
} else {
snapshot.statusButton.disable( true );
}

currentTheme = api.settings.theme.stylesheet; // Or previewing theme.
savedPreviewingTheme = snapshot.data.previewingTheme;
themeNotActiveOrSaved = ! api.state( 'activated' ).get() && ! savedPreviewingTheme;
snapshot.isNotSavedPreviewingTheme = savedPreviewingTheme && savedPreviewingTheme !== currentTheme;

if ( themeNotActiveOrSaved || snapshot.isNotSavedPreviewingTheme ) {
disableButton = false;
disableSelectButton = false;
}

snapshot.statusButton.disbleButton.set( disableButton );
snapshot.statusButton.disableSelect.set( disableSelectButton );

// Preview link.
snapshot.previewLink = $( $.trim( wp.template( 'snapshot-preview-link' )() ) );
snapshot.previewLink.toggle( api.state( 'snapshot-saved' ).get() );
snapshot.previewLink.attr( 'target', snapshot.data.uuid );
setPreviewLinkHref = _.debounce( function() {
var queryVars;
if ( api.state( 'snapshot-exists' ).get() ) {
snapshot.previewLink.attr( 'href', snapshot.getSnapshotFrontendPreviewUrl() );
} else {
snapshot.previewLink.attr( 'href', snapshot.frontendPreviewUrl.get() );
}

// Add the customize_theme param to the frontend URL if the theme is not active.
if ( ! api.state( 'activated' ).get() ) {
queryVars = snapshot.parseQueryString( snapshot.previewLink.prop( 'search' ).substr( 1 ) );
queryVars.customize_theme = api.settings.theme.stylesheet;
snapshot.previewLink.prop( 'search', $.param( queryVars ) );
}
} );
snapshot.frontendPreviewUrl.bind( setPreviewLinkHref );
setPreviewLinkHref();
Expand Down Expand Up @@ -632,7 +664,7 @@
*/
autoSaveEditBox: function() {
var snapshot = this, update,
delay = 2000, status, isValidChangesetStatus, isFutureDateAndStatus;
delay = 2000, status, isFutureDateAndStatus;

snapshot.updatePending = false;
snapshot.dirtyEditControlValues = false;
Expand Down Expand Up @@ -684,16 +716,7 @@
return undefined;
} );

isValidChangesetStatus = function() {
return _.contains( [ 'future', 'pending', 'draft' ], api.state( 'changesetStatus' ).get() );
};

// @todo Show loader and disable button while auto saving.
api.bind( 'changeset-save', function() {
if ( isValidChangesetStatus() ) {
snapshot.extendPreviewerQuery();
}
} );

api.bind( 'changeset-saved', function() {
if ( 'auto-draft' !== api.state( 'changesetStatus' ).get() ) {
Expand Down Expand Up @@ -1000,6 +1023,10 @@
api.previewer.query = function() {
var retval = originalQuery.apply( this, arguments );

if ( ! _.contains( [ 'future', 'pending', 'draft' ], snapshot.statusButton.value.get() ) ) {
return retval;
}

retval.customizer_state_query_vars = JSON.stringify( snapshot.getStateQueryVars() );

if ( snapshot.editControlSettings( 'title' ).get() ) {
Expand Down Expand Up @@ -1159,23 +1186,29 @@
* @return {void}.
*/
removeParamFromClose: function removeParamFromClose( targetParam ) {
var closeButton, queryString, updatedParams;
var snapshot = this, closeButton, queryString, updatedParams;
closeButton = $( '.customize-controls-close' );
queryString = closeButton.prop( 'search' ).substr( 1 );

if ( ! queryString.length ) {
return;
}

updatedParams = _.filter(
queryString.split( '&' ),
function( paramPair ) {
return 0 !== paramPair.indexOf( targetParam + '=' );
}
);
updatedParams = snapshot.parseQueryString( queryString );
delete updatedParams[ targetParam ];
closeButton.prop( 'search', $.param( updatedParams ) );
},

closeButton.prop( 'search', updatedParams.join( '&' ) );
}
/**
* Parse query string.
*
* @since 4.7.0
* @access public
*
* @param {string} queryString Query string.
* @returns {object} Parsed query string.
*/
parseQueryString: api.utils.parseQueryString
} );

if ( 'undefined' !== typeof _customizeSnapshotsSettings ) {
Expand Down
15 changes: 9 additions & 6 deletions php/class-customize-snapshot-manager-back-compat.php
Expand Up @@ -79,18 +79,15 @@ public function filter_customize_refresh_nonces( $nonces ) {
* @global \WP_Customize_Manager $wp_customize
*/
public function enqueue_controls_scripts() {

// Prevent loading the Snapshot interface if the theme is not active.
if ( ! $this->is_theme_active() ) {
return;
}
$this->ensure_customize_manager();

wp_enqueue_style( 'customize-snapshots' );
wp_enqueue_script( 'customize-snapshots-compat' );

if ( $this->snapshot ) {
$post = $this->snapshot->post();
$this->override_post_date_default_data( $post );
$preview_url_query_vars = $this->post_type->get_customizer_state_query_vars( $post->ID );
}

// Script data array.
Expand All @@ -104,6 +101,8 @@ public function enqueue_controls_scripts() {
'currentUserCanPublish' => current_user_can( 'customize_publish' ),
'initialServerDate' => current_time( 'mysql', false ),
'initialServerTimestamp' => floor( microtime( true ) * 1000 ),
'theme' => $this->original_stylesheet,
'previewingTheme' => isset( $preview_url_query_vars['theme'] ) ? $preview_url_query_vars['theme'] : '',
'i18n' => array(
'saveButton' => __( 'Save', 'customize-snapshots' ),
'updateButton' => __( 'Update', 'customize-snapshots' ),
Expand All @@ -124,7 +123,11 @@ public function enqueue_controls_scripts() {
'snapshotExists' => ( $this->snapshot && $this->snapshot->saved() ),
) );

wp_localize_script( 'customize-snapshots-compat', '_customizeSnapshotsCompatSettings', $exports );
wp_scripts()->add_inline_script(
'customize-snapshots-compat',
sprintf( 'var _customizeSnapshotsCompatSettings = %s;', wp_json_encode( $exports ) ),
'before'
);
}

/**
Expand Down
17 changes: 10 additions & 7 deletions php/class-customize-snapshot-manager.php
Expand Up @@ -203,6 +203,7 @@ public function ensure_customize_manager() {
*
* @return bool Whether theme is active.
*
* @deprecated in favor of WP_Customize_Manager::is_theme_active()
* @todo move to back compat?
*/
public function is_theme_active() {
Expand All @@ -225,7 +226,7 @@ public function add_snapshot_uuid_to_return_url() {
&&
$this->current_snapshot_uuid
&&
$this->is_theme_active()
$this->customize_manager->is_theme_active()
&&
false === strpos( $this->customize_manager->get_return_url(), '/wp-admin/' )
);
Expand Down Expand Up @@ -263,11 +264,7 @@ static public function encode_json( $value ) {
* @global \WP_Customize_Manager $wp_customize
*/
public function enqueue_controls_scripts() {

// Prevent loading the Snapshot interface if the theme is not active.
if ( ! $this->is_theme_active() ) {
return;
}
$this->ensure_customize_manager();

wp_enqueue_style( 'customize-snapshots' );
wp_enqueue_script( 'customize-snapshots' );
Expand All @@ -277,6 +274,7 @@ public function enqueue_controls_scripts() {
if ( $this->snapshot ) {
$post_id = $this->customize_manager->changeset_post_id();
$post = get_post( $post_id );
$preview_url_query_vars = $this->post_type->get_customizer_state_query_vars( $post->ID );
if ( $post instanceof \WP_Post ) {
$this->override_post_date_default_data( $post );
$edit_link = $this->snapshot->get_edit_link( $post );
Expand All @@ -292,6 +290,7 @@ public function enqueue_controls_scripts() {
'currentUserCanPublish' => current_user_can( 'customize_publish' ),
'initialServerDate' => current_time( 'mysql', false ),
'initialServerTimestamp' => floor( microtime( true ) * 1000 ),
'previewingTheme' => isset( $preview_url_query_vars['theme'] ) ? $preview_url_query_vars['theme'] : '',
'i18n' => array(
'saveButton' => __( 'Save', 'customize-snapshots' ),
'updateButton' => __( 'Update', 'customize-snapshots' ),
Expand All @@ -309,7 +308,11 @@ public function enqueue_controls_scripts() {
),
) );

wp_localize_script( 'customize-snapshots', '_customizeSnapshotsSettings', $exports );
wp_scripts()->add_inline_script(
'customize-snapshots',
sprintf( 'var _customizeSnapshotsSettings = %s;', wp_json_encode( $exports ) ),
'before'
);
}

/**
Expand Down

0 comments on commit ef1b528

Please sign in to comment.