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

Commit

Permalink
Add support for theme change.
Browse files Browse the repository at this point in the history
  • Loading branch information
miina committed Feb 15, 2017
1 parent 472182d commit f42969c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
25 changes: 15 additions & 10 deletions js/customize-snapshots-front.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,29 @@ var CustomizeSnapshotsFront = (function( $ ) {
}

publishBtn.click( function( e ) {
var request;
var request,
data = {
nonce: component.data.snapshotsFrontendPublishNonce,
uuid: component.data.uuid
};
e.preventDefault();

if ( ! window.confirm( component.data.confirmationMsg ) ) { // eslint-disable-line no-alert
return false;
}
request = wp.ajax.post( component.data.action, {
nonce: component.data.snapshotsFrontendPublishNonce,
uuid: component.data.uuid
} );
request.done( function( data ) {
if ( data && data.success ) {
if ( ! wp.customize.settings.theme.active ) {
data.stylesheet = wp.customize.settings.theme.stylesheet;
}
request = wp.ajax.post( component.data.action, data );

request.done( function( resp ) {
if ( resp && resp.success ) {
window.location = e.target.href;
}
} );
request.fail( function( data ) {
if ( data && data.errorMsg ) {
window.alert( data.errorMsg ); // eslint-disable-line no-alert
request.fail( function( resp ) {
if ( resp && resp.errorMsg ) {
window.alert( resp.errorMsg ); // eslint-disable-line no-alert
}
} );

Expand Down
25 changes: 17 additions & 8 deletions php/class-customize-snapshot-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function hooks() {
add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_controls_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
add_action( 'wp_ajax_customize-snapshots-frontend-publish', array( $this, 'ajax_snapshot_frontend_publish' ) );
add_action( 'wp_ajax_customize_snapshots_frontend_publish', array( $this, 'ajax_snapshot_frontend_publish' ) );

add_action( 'customize_controls_init', array( $this, 'add_snapshot_uuid_to_return_url' ) );
add_action( 'customize_controls_print_footer_scripts', array( $this, 'render_templates' ) );
Expand Down Expand Up @@ -183,16 +183,16 @@ public function doing_customize_save_ajax() {
* Ensure Customizer manager is instantiated.
*
* @global \WP_Customize_Manager $wp_customize
* @param array $args Array of params.
*/
public function ensure_customize_manager() {
public function ensure_customize_manager( $args = array() ) {
global $wp_customize;
if ( empty( $wp_customize ) || ! ( $wp_customize instanceof \WP_Customize_Manager ) ) {
require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
if ( null !== $this->current_snapshot_uuid ) {
$wp_customize = new \WP_Customize_Manager( array( 'changeset_uuid' => $this->current_snapshot_uuid ) ); // WPCS: override ok.
} else {
$wp_customize = new \WP_Customize_Manager(); // WPCS: override ok.
$args['changeset_uuid'] = $this->current_snapshot_uuid;
}
$wp_customize = new \WP_Customize_Manager( $args ); // WPCS: override ok.
}

$this->customize_manager = $wp_customize;
Expand Down Expand Up @@ -350,7 +350,7 @@ public function enqueue_frontend_scripts() {
$exports = array(
'confirmationMsg' => __( 'Are you sure that you want to publish the Changeset?', 'customize-snapshots' ),
'snapshotsFrontendPublishNonce' => wp_create_nonce( 'customize-snapshots-frontend-publish' ),
'action' => 'customize-snapshots-frontend-publish',
'action' => 'customize_snapshots_frontend_publish',
'uuid' => $this->snapshot->uuid(),
);
wp_add_inline_script(
Expand Down Expand Up @@ -605,7 +605,11 @@ public function add_publish_snapshot_link( $wp_admin_bar ) {
$wp_admin_bar->add_menu( array(
'id' => 'publish-customize-snapshot',
'title' => __( 'Publish Changeset', 'customize-snapshots' ),
'href' => remove_query_arg( $this->get_front_uuid_param() ),
'href' => remove_query_arg( array(
$this->get_front_uuid_param(),
'theme',
$this->get_customize_uuid_param(),
) ),
'meta' => array(
'class' => 'ab-item ab-customize-snapshots-item',
),
Expand Down Expand Up @@ -994,7 +998,12 @@ public function ajax_snapshot_frontend_publish() {
}

$this->current_snapshot_uuid = sanitize_key( wp_unslash( $_POST['uuid'] ) );
$this->ensure_customize_manager();
$args = array();
if ( isset( $_POST['stylesheet'] ) ) {
$stylesheet = sanitize_text_field( wp_unslash( $_POST['stylesheet'] ) );
$args['theme'] = $stylesheet;
}
$this->ensure_customize_manager( $args );
$r = $this->customize_manager->save_changeset_post( array(
'status' => 'publish',
) );
Expand Down

0 comments on commit f42969c

Please sign in to comment.