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

Feature: Allow publishing from preview #115

Merged
merged 25 commits into from
Aug 6, 2017
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0db0028
Add Publish Snapshot link.
miina Feb 9, 2017
be831ba
Add frontend JS.
miina Feb 9, 2017
e38959b
Add relevant scripts, actions.
miina Feb 9, 2017
7f214b9
Add publishing logic. Use wp.ajax.post.
miina Feb 10, 2017
24840c8
Remove todos.
miina Feb 14, 2017
0edf0de
Fix JSCS.
miina Feb 14, 2017
8270875
Fix phpcs. Fix ESLint.
miina Feb 14, 2017
10ca219
Permission fixes.
miina Feb 15, 2017
62bba49
Fix indent.
miina Feb 15, 2017
472182d
Fix phpcs.
miina Feb 15, 2017
f42969c
Add support for theme change.
miina Feb 15, 2017
c5aaa1f
Fix coveralls phpunit.
miina Feb 15, 2017
93c91e1
Change Snapshot to Changeset.
miina Feb 20, 2017
e786376
Merge develop.
miina Jul 20, 2017
2c1282f
Resolve merge conflicts. Merge two frontend js files into one.
miina Jul 27, 2017
a1ef155
Remove changeset from storage after publishing.
miina Jul 27, 2017
b6fb4e3
Rework publishing changeset to use wp_nonce_url instead of AJAX.
miina Jul 31, 2017
b2ba684
Phpcs fixes.
miina Jul 31, 2017
2aaf977
Fix logic for non-action cases.
miina Jul 31, 2017
4fd7832
Remove obsolete ajax code; ensure valid redirect; improve query param…
westonruter Aug 2, 2017
1cf34c1
Merge branch 'develop' into feature/allow_publishing_from_preview
westonruter Aug 2, 2017
8dbccab
Rename references from snapshots to changesets
westonruter Aug 6, 2017
9a424ca
Improve handling of error scenarios when publishing from frontend
westonruter Aug 6, 2017
a4d8ff9
Merge branch 'develop' of https://github.com/xwp/wp-customize-snapsho…
westonruter Aug 6, 2017
f9fc1bb
Limit admin bar links for unauthorized users
westonruter Aug 6, 2017
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
72 changes: 72 additions & 0 deletions js/customize-snapshots-front.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* global jQuery, wp */
/* exported CustomizeSnapshotsFront */
var CustomizeSnapshotsFront = (function( $ ) {
'use strict';

var component = {
data: {
confirmationMsg: '',
action: '',
snapshotsFrontendPublishNonce: '',
errorMsg: ''
}
};
/**
* Init.
*
* @param {object} args Args.
* @returns {void}
*/
component.init = function init( args ) {
_.extend( component.data, args );

$( document ).ready( function() {
component.setupPublishButton();
} );
};

/**
* Set up snapshot frontend publish button.
*
* @returns {void}
*/
component.setupPublishButton = function setupPublishButton() {
var publishBtn = $( '#wp-admin-bar-publish-customize-snapshot a' );

if ( ! publishBtn.length ) {
return;
}

publishBtn.click( function( e ) {
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;
}
if ( ! wp.customize.settings.theme.active ) {
Copy link
Contributor Author

@miina miina Jul 20, 2017

Choose a reason for hiding this comment

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

@westonruter After merging develop there is an issue with publishing changeset from preview:
screen shot 2017-07-20 at 5 53 33 pm

Looks like wp.customize is not defined at this moment. Do you know if anything changed meanwhile in the logic or order of enqueuing the JS scripts? Tried to make the script dependent on customize-base but in this case wp.customize.settings is undefined.

Would be great if you could point out what to investigate or what might have changed to cause this issue. Not sure what I'm missing here, maybe it's something very obvious.

Copy link
Contributor

Choose a reason for hiding this comment

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

This wp.customize.settings value is actually set in customize-preview (and customize-controls) not customize-base.

See: https://github.com/WordPress/wordpress-develop/blob/4.8.0/src/wp-includes/js/customize-preview.js#L657-L658

Note that it is set at jQuery.ready.

But since you'd be clicking the button after jQuery.ready anyway, I don't see why you it would be undefined. Is customize-preview itself not getting enqueued? It should be if you've loaded a changeset on the frontend.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like the issue was because I was using the param changeset_uuid=... instead of customize_changeset_uuid in frontend.

Note that customize-snapshots-frontend.js file was added to develop branch so I merged the frontend JS file from this branch (customize-snapshots-front.js) into the one in develop (customize-snapshots-frontend.js). Let me know if there are any concerns with that. Also added removing the UUID from storage after publishing.

There is another issue with publishing the changesets from frontend:
WP_Customize_Setting::_preview_filter is returning the dirty value as old value instead of the actual old value in DB meaning that the setting is not saved when publishing.

Although I can see that Post_Type::hooks() is adding this filter:
add_action( 'transition_post_status', array( $this, 'start_pretending_customize_save_ajax_action' ), $priority - 1, 3 );, this method is always returning true: WP_Customize_Setting::is_current_blog_previewed().

Looks like WP_Customize_Manager is initialized in _wp_customize_include() before the filters have any effect. Also, not sure if https://github.com/xwp/wordpress-develop/pull/231 would fix that since the $_REQUEST['action'] is not customize_save at that moment.

Am I missing something or is creating another workaround necessary, e.g. reinitializing WP_Customize_Manager?

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( resp ) {
if ( resp && resp.errorMsg ) {
window.alert( resp.errorMsg ); // eslint-disable-line no-alert
}
} );

return true;
} );
};

return component;
})( jQuery );
125 changes: 119 additions & 6 deletions php/class-customize-snapshot-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ class Customize_Snapshot_Manager {
*/
public $original_stylesheet;

/**
* New active theme.
*
* @access public
* @var string
*/
public $stylesheet;

/**
* Constructor.
*
Expand All @@ -97,6 +105,8 @@ function hooks() {
add_action( 'init', array( $this->post_type, 'init' ) );
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( '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 @@ -186,15 +196,20 @@ public function doing_customize_save_ajax() {
*/
public function ensure_customize_manager() {
global $wp_customize;

$args = array();
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;
}

if ( null !== $this->stylesheet ) {
$args['theme'] = $this->stylesheet;
}

$wp_customize = new \WP_Customize_Manager( $args ); // WPCS: override ok.
}

$this->customize_manager = $wp_customize;
Expand Down Expand Up @@ -342,6 +357,31 @@ public function enqueue_admin_scripts( $hook ) {
}
}

/**
* Enqueue frontend scripts.
*
* These files control the behavior frontend.
*/
public function enqueue_frontend_scripts() {
if ( ! $this->snapshot || ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->publish_posts ) ) {
return;
}

$handle = 'customize-snapshots-front';
wp_enqueue_script( $handle );
$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',
'uuid' => $this->snapshot->uuid(),
);
wp_add_inline_script(
$handle,
sprintf( 'CustomizeSnapshotsFront.init( %s )', wp_json_encode( $exports ) ),
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

'after'
);
}

/**
* Get the Customize_Snapshot instance.
*
Expand Down Expand Up @@ -476,6 +516,7 @@ public function customize_menu( $wp_admin_bar ) {
$this->replace_customize_link( $wp_admin_bar );
$this->add_resume_snapshot_link( $wp_admin_bar );
$this->add_post_edit_screen_link( $wp_admin_bar );
$this->add_publish_snapshot_link( $wp_admin_bar );
$this->add_snapshot_exit_link( $wp_admin_bar );
}

Expand All @@ -497,6 +538,10 @@ public function print_admin_bar_styles() {
content: "\f179";
top: 2px;
}
#wpadminbar #wp-admin-bar-publish-customize-snapshot > .ab-item:before {
content: "\f147";
top: 2px;
}
#wpadminbar #wp-admin-bar-exit-customize-snapshot > .ab-item:before {
content: "\f158";
top: 2px;
Expand Down Expand Up @@ -592,6 +637,33 @@ public function add_post_edit_screen_link( $wp_admin_bar ) {
) );
}

/**
* Adds a "Publish Changeset" link to the Toolbar when in Snapshot mode.
*
* @param \WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance.
*/
public function add_publish_snapshot_link( $wp_admin_bar ) {
if ( ! $this->snapshot ) {
return;
}
$post = $this->snapshot->post();
if ( ! $post ) {
return;
}
$wp_admin_bar->add_menu( array(
'id' => 'publish-customize-snapshot',
'title' => __( 'Publish Changeset', 'customize-snapshots' ),
'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',
),
) );
}

/**
* Adds an "Exit Snapshot" link to the Toolbar when in Snapshot mode.
*
Expand Down Expand Up @@ -620,7 +692,12 @@ public function remove_all_non_snapshot_admin_bar_links( $wp_admin_bar ) {
if ( empty( $this->snapshot ) ) {
return;
}
$snapshot_admin_bar_node_ids = array( 'customize', 'exit-customize-snapshot', 'inspect-customize-snapshot' );
$snapshot_admin_bar_node_ids = array(
'customize',
'exit-customize-snapshot',
'inspect-customize-snapshot',
'publish-customize-snapshot',
);
foreach ( $wp_admin_bar->get_nodes() as $node ) {
if ( in_array( $node->id, $snapshot_admin_bar_node_ids, true ) || '#' === substr( $node->href, 0, 1 ) ) {
continue;
Expand Down Expand Up @@ -952,6 +1029,42 @@ public function get_customize_uuid_param() {
return constant( get_class( $this->post_type ) . '::CUSTOMIZE_UUID_PARAM_NAME' );
}

/**
* Publishes changeset from frontend.
*/
public function ajax_snapshot_frontend_publish() {
if ( ! check_ajax_referer( 'customize-snapshots-frontend-publish', 'nonce' ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Also important to check whether current_user_can( get_post_type_object( 'customize_changeset' )->cap->publish_posts )

status_header( 400 );
wp_send_json_error( 'bad_nonce' );
}

if ( ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->publish_posts ) ) {
status_header( 403 );
wp_send_json_error( 'insufficient_post_permissions' );
}

if ( ! isset( $_POST['uuid'] ) ) {
wp_send_json_error();
}

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

if ( is_wp_error( $r ) ) {
$msg = __( 'Publishing failed: ', 'customize-snapshots' );
$msg .= join( '; ', array_keys( $r->errors ) );
wp_send_json_error( array( 'errorMsg' => $msg ) );
} else {
wp_send_json_success( array( 'success' => true ) );
}
}

/**
* Save the preview url query vars in changeset meta.
*
Expand Down
5 changes: 5 additions & 0 deletions php/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ public function register_scripts( \WP_Scripts $wp_scripts ) {
$src = $this->dir_url . 'js/customize-snapshots-admin' . $min . '.js';
$deps = array( 'jquery', 'underscore' );
$wp_scripts->add( $handle, $src, $deps );

$handle = 'customize-snapshots-front';
$src = $this->dir_url . 'js/customize-snapshots-front' . $min . '.js';
$deps = array( 'jquery', 'wp-backbone', 'underscore' );
$wp_scripts->add( $handle, $src, $deps );
}

/**
Expand Down