This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Feature: Allow publishing from preview #115
Merged
Merged
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 be831ba
Add frontend JS.
miina e38959b
Add relevant scripts, actions.
miina 7f214b9
Add publishing logic. Use wp.ajax.post.
miina 24840c8
Remove todos.
miina 0edf0de
Fix JSCS.
miina 8270875
Fix phpcs. Fix ESLint.
miina 10ca219
Permission fixes.
miina 62bba49
Fix indent.
miina 472182d
Fix phpcs.
miina f42969c
Add support for theme change.
miina c5aaa1f
Fix coveralls phpunit.
miina 93c91e1
Change Snapshot to Changeset.
miina e786376
Merge develop.
miina 2c1282f
Resolve merge conflicts. Merge two frontend js files into one.
miina a1ef155
Remove changeset from storage after publishing.
miina b6fb4e3
Rework publishing changeset to use wp_nonce_url instead of AJAX.
miina b2ba684
Phpcs fixes.
miina 2aaf977
Fix logic for non-action cases.
miina 4fd7832
Remove obsolete ajax code; ensure valid redirect; improve query param…
westonruter 1cf34c1
Merge branch 'develop' into feature/allow_publishing_from_preview
westonruter 8dbccab
Rename references from snapshots to changesets
westonruter 9a424ca
Improve handling of error scenarios when publishing from frontend
westonruter a4d8ff9
Merge branch 'develop' of https://github.com/xwp/wp-customize-snapsho…
westonruter f9fc1bb
Limit admin bar links for unauthorized users
westonruter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) { | ||
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,14 @@ class Customize_Snapshot_Manager { | |
*/ | ||
public $original_stylesheet; | ||
|
||
/** | ||
* New active theme. | ||
* | ||
* @access public | ||
* @var string | ||
*/ | ||
public $stylesheet; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
|
@@ -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' ) ); | ||
|
@@ -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; | ||
|
@@ -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 ) ), | ||
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. | ||
* | ||
|
@@ -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 ); | ||
} | ||
|
||
|
@@ -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; | ||
|
@@ -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. | ||
* | ||
|
@@ -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; | ||
|
@@ -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' ) ) { | ||
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 ( ! 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. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@westonruter After merging
develop
there is an issue with publishing changeset from preview: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 oncustomize-base
but in this casewp.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.
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.
This
wp.customize.settings
value is actually set incustomize-preview
(andcustomize-controls
) notcustomize-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. Iscustomize-preview
itself not getting enqueued? It should be if you've loaded a changeset on the frontend.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.
Seems like the issue was because I was using the param
changeset_uuid=...
instead ofcustomize_changeset_uuid
in frontend.Note that
customize-snapshots-frontend.js
file was added todevelop
branch so I merged the frontend JS file from this branch (customize-snapshots-front.js
) into the one indevelop
(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 notcustomize_save
at that moment.Am I missing something or is creating another workaround necessary, e.g. reinitializing
WP_Customize_Manager
?