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 7 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 65 additions & 0 deletions js/customize-snapshots-front.js
@@ -0,0 +1,65 @@
/* 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;
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
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this also include theme? Would not a theme switch also be needing to be accounted for here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

One thought: maybe it would be useful to include the theme param into the preview link in Customizer in case a non-active theme is being customized? Otherwise I change the theme, customize it, click on preview icon, and end up previewing the active theme. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, okay, just saw that this will basically be part of #101. Will pick that up.

Copy link
Contributor

Choose a reason for hiding this comment

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

The only hesitation to including the theme in the preview link is that if a user is not authenticated they won't be able to access the preview, since previewing a theme switch requires the switch_themes capability.

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

return true;
} );
};

return component;
})( jQuery );
91 changes: 90 additions & 1 deletion php/class-customize-snapshot-manager.php
Expand Up @@ -97,6 +97,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 @@ -333,6 +335,30 @@ public function enqueue_admin_scripts( $hook ) {
}
}

/**
* Enqueue frontend scripts.
*
* These files control the behavior frontend.
*/
public function enqueue_frontend_scripts() {
if ( ! $this->snapshot ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This also needs to short-circuit if the current user cannot publish changesets. In other words:

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 Snapshot?', '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 @@ -452,6 +478,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 @@ -472,6 +499,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 @@ -557,6 +588,29 @@ public function add_post_edit_screen_link( $wp_admin_bar ) {
) );
}

/**
* Adds a "Publish Snapshot" 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 Snapshot', 'customize-snapshots' ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename to “Publish Changeset”

'href' => remove_query_arg( $this->get_front_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 @@ -585,7 +639,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 @@ -914,4 +973,34 @@ public function get_front_uuid_param() {
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 ( ! isset( $_POST['uuid'] ) ) {
return;
}

$this->current_snapshot_uuid = esc_attr( $_POST['uuid'] );
Copy link
Contributor

Choose a reason for hiding this comment

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

Replace esc_attr( $_POST['uuid'] ) with sanitize_key( wp_unslash( $_POST['uuid'] ) ).

$this->ensure_customize_manager();
$r = $this->customize_manager->save_changeset_post( array(
'status' => 'publish',
) );

if ( is_wp_error( $r ) ) {
$msg = __( 'Publishing failed: ', 'customize-snapshots' );
foreach ( $r->errors as $name => $value ) {
$msg .= $name . '; ';
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be replaced with: $msg .= join( "; ", array_keys( $r->errors ) ). But is the it the key or the values that have the error messages to share?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The keys seem to include the error messages.

wp_send_json_error( array( 'errorMsg' => $msg ) );
} else {
wp_send_json_success( array( 'success' => true ) );
}
}
}
5 changes: 5 additions & 0 deletions php/class-plugin.php
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