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

Enable status button if current theme is not active. #132

Merged
merged 15 commits into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions js/compat/customize-snapshots.js
Original file line number Diff line number Diff line change
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
51 changes: 35 additions & 16 deletions js/customize-snapshots.js
Original file line number Diff line number Diff line change
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 @@ -255,6 +266,7 @@
dialogElement;

snapshot.statusButton.disableSelect.set( false );
snapshot.statusButton.disbleButton.set( false );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm glad you caught this. It's something I was noticing in the case of setting validation errors, where I'd get a validation error message with a control but the Publish button would remain disabled.


if ( response.setting_validities ) {
invalidityCount = _.size( response.setting_validities, function( validity ) {
Expand Down Expand Up @@ -303,25 +315,37 @@
* @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() );
Expand Down Expand Up @@ -632,7 +656,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 +708,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 +1015,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
3 changes: 3 additions & 0 deletions php/class-customize-snapshot-manager-back-compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function enqueue_controls_scripts() {
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 +105,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 Down
2 changes: 2 additions & 0 deletions php/class-customize-snapshot-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ public function enqueue_controls_scripts() {
$this->override_post_date_default_data( $post );
$edit_link = $this->snapshot->get_edit_link( $post );
}
$preview_url_query_vars = $this->post_type->get_customizer_state_query_vars( $post_id );
}

// Script data array.
Expand All @@ -292,6 +293,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 Down
20 changes: 15 additions & 5 deletions php/class-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,10 @@ public function set_customizer_state_query_vars( $post_id, $query_vars ) {
if ( isset( $query_vars['scroll'] ) && is_int( $query_vars['scroll'] ) ) {
$stored_query_vars['scroll'] = $query_vars['scroll'];
}
if ( isset( $query_vars['previewing_theme'] ) ) {
$theme = $this->snapshot_manager->customize_manager->get_stylesheet();
$stored_query_vars['theme'] = $query_vars['previewing_theme'] ? $theme: '';
}
update_post_meta( $post_id, '_preview_url_query_vars', $stored_query_vars );
return $stored_query_vars;
}
Expand All @@ -917,11 +921,17 @@ public function get_frontend_view_link( $post ) {
$post = get_post( $post );
$preview_url_query_vars = $this->get_customizer_state_query_vars( $post->ID );
$base_url = isset( $preview_url_query_vars['url'] ) ? $preview_url_query_vars['url'] : home_url( '/' );
return add_query_arg(
array(
static::FRONT_UUID_PARAM_NAME => $post->post_name,
),
$base_url
$current_theme = get_stylesheet();
$args = array(
static::FRONT_UUID_PARAM_NAME => $post->post_name,
);

if ( isset( $preview_url_query_vars['theme'] ) && $current_theme !== $preview_url_query_vars['theme'] ) {
$args = array_merge( $args, array(
'theme' => $preview_url_query_vars['theme'],
) );
}

return add_query_arg( $args, $base_url );
}
}