Feature: Allow publishing from preview #115
Changes from 7 commits
0db0028
be831ba
e38959b
7f214b9
24840c8
0edf0de
8270875
10ca219
62bba49
472182d
f42969c
c5aaa1f
93c91e1
e786376
2c1282f
a1ef155
b6fb4e3
b2ba684
2aaf977
4fd7832
1cf34c1
8dbccab
9a424ca
a4d8ff9
f9fc1bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} ); | ||
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 ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' ) ); | ||
|
@@ -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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
'after' | ||
); | ||
} | ||
|
||
/** | ||
* Get the Customize_Snapshot instance. | ||
* | ||
|
@@ -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 ); | ||
} | ||
|
||
|
@@ -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; | ||
|
@@ -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' ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
* | ||
|
@@ -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; | ||
|
@@ -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' ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also important to check whether |
||
status_header( 400 ); | ||
wp_send_json_error( 'bad_nonce' ); | ||
} | ||
|
||
if ( ! isset( $_POST['uuid'] ) ) { | ||
return; | ||
} | ||
|
||
$this->current_snapshot_uuid = esc_attr( $_POST['uuid'] ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace |
||
$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 . '; '; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be replaced with: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) ); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 theswitch_themes
capability.