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 all 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
30 changes: 29 additions & 1 deletion js/customize-snapshots-frontend.js
Expand Up @@ -11,7 +11,8 @@ var CustomizeSnapshotsFrontend = ( function( $ ) {
uuid: '',
l10n: {
restoreSessionPrompt: ''
}
},
confirmationMsg: ''
}
};

Expand All @@ -30,6 +31,12 @@ var CustomizeSnapshotsFrontend = ( function( $ ) {
component.keepSessionAlive();
component.rememberSessionSnapshot();
component.handleExitSnapshotSessionLink();

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

/**
Expand Down Expand Up @@ -98,6 +105,27 @@ var CustomizeSnapshotsFrontend = ( function( $ ) {
} );
};

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

if ( ! publishBtn.length ) {
return;
}

publishBtn.click( function() {
if ( ! window.confirm( component.data.confirmationMsg ) ) { // eslint-disable-line no-alert
return false;
}
sessionStorage.removeItem( 'customize_changeset_uuid' );
return true;
} );
};

return component;
} )( jQuery );

157 changes: 149 additions & 8 deletions php/class-customize-snapshot-manager.php
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 @@ -99,6 +107,8 @@ function hooks() {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );

add_action( 'load-edit.php', array( $this, 'handle_frontend_changset_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' ) );
add_action( 'admin_bar_menu', array( $this, 'customize_menu' ), 41 );
Expand Down Expand Up @@ -187,15 +197,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 @@ -359,6 +374,7 @@ public function enqueue_frontend_scripts() {
'l10n' => array(
'restoreSessionPrompt' => __( 'It seems you may have inadvertently navigated away from previewing a customized state. Would you like to restore the changeset context?', 'customize-snapshots' ),
),
'confirmationMsg' => __( 'Are you sure that you want to publish the Changeset?', 'customize-snapshots' ),
);
wp_add_inline_script(
$handle,
Expand Down Expand Up @@ -502,6 +518,7 @@ public function customize_menu( $wp_admin_bar ) {
$this->add_changesets_admin_bar_link( $wp_admin_bar );
$this->add_resume_snapshot_link( $wp_admin_bar );
$this->add_post_edit_screen_link( $wp_admin_bar );
$this->add_publish_changeset_link( $wp_admin_bar );
$this->add_snapshot_exit_link( $wp_admin_bar );
}

Expand All @@ -523,6 +540,10 @@ public function print_admin_bar_styles() {
content: "\f179";
top: 2px;
}
#wpadminbar #wp-admin-bar-publish-customize-changeset > .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 @@ -621,7 +642,7 @@ public function add_resume_snapshot_link( $wp_admin_bar ) {
* @param \WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance.
*/
public function add_post_edit_screen_link( $wp_admin_bar ) {
if ( ! $this->snapshot ) {
if ( ! $this->snapshot || ! current_user_can( get_post_type_object( $this->post_type->get_slug() )->cap->edit_posts ) ) {
return;
}
$post = $this->snapshot->post();
Expand All @@ -638,6 +659,39 @@ 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_changeset_link( $wp_admin_bar ) {
if ( ! $this->snapshot || ! current_user_can( get_post_type_object( $this->post_type->get_slug() )->cap->publish_posts ) ) {
return;
}
$post = $this->snapshot->post();
if ( ! $post ) {
return;
}

$href = add_query_arg(
array(
'post_type' => $this->post_type->get_slug(),
'action' => 'frontend_publish',
'uuid' => $this->current_snapshot_uuid,
'stylesheet' => get_stylesheet(),
),
admin_url( 'edit.php' )
);
$wp_admin_bar->add_menu( array(
'id' => 'publish-customize-changeset',
'title' => __( 'Publish Changeset', 'customize-snapshots' ),
'href' => wp_nonce_url( $href, 'publish-changeset_' . $this->current_snapshot_uuid ),
'meta' => array(
'class' => 'ab-item ab-customize-snapshots-item',
),
) );
}

/**
* Adds an "Exit Changeset Preview" link to the Toolbar when previewing a changeset.
*
Expand All @@ -663,10 +717,15 @@ public function add_snapshot_exit_link( $wp_admin_bar ) {
* @param \WP_Admin_Bar $wp_admin_bar Admin bar.
*/
public function remove_all_non_snapshot_admin_bar_links( $wp_admin_bar ) {
if ( empty( $this->snapshot ) ) {
if ( empty( $this->snapshot ) || ! current_user_can( 'customize' ) ) {
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-changeset',
);
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 @@ -998,6 +1057,88 @@ public function get_customize_uuid_param() {
return constant( get_class( $this->post_type ) . '::CUSTOMIZE_UUID_PARAM_NAME' );
}

/**
* Handles request to publish changeset from frontend.
*/
public function handle_frontend_changset_publish() {

if ( ! isset( $_GET['uuid'] ) || ! isset( $_GET['action'] ) || 'frontend_publish' !== $_GET['action'] ) {
return;
}
$uuid = sanitize_key( wp_unslash( $_GET['uuid'] ) );
if ( ! static::is_valid_uuid( $uuid ) ) {
return;
}
$this->current_snapshot_uuid = $uuid;

$is_user_authorized = (
check_ajax_referer( 'publish-changeset_' . $this->current_snapshot_uuid, false, false )
&&
current_user_can( get_post_type_object( $this->post_type->get_slug() )->cap->publish_posts )
);
if ( ! $is_user_authorized ) {
wp_die(
esc_html__( 'Oops. Unable to publish the changeset due to an expired user session. Please go back, reload the page, and try publishing again.', 'customize-snapshots' ),
esc_html__( 'Changeset publishing failed', 'customize-snapshots' ),
array(
'back_link' => true,
'response' => 401,
)
);
}

if ( isset( $_GET['stylesheet'] ) ) {
$theme = wp_get_theme( wp_unslash( $_GET['stylesheet'] ) );
if ( $theme->errors() ) {
$msg = __( 'Oops. Unable to publish the changeset. The following error(s) occurred: ', 'customize-snapshots' );
$msg .= join( '; ', array_keys( $theme->errors()->errors ) );
wp_die(
'<p>' . esc_html( $msg ) . '</p>',
esc_html__( 'Changeset publishing failed', 'customize-snapshots' ),
array(
'back_link' => true,
'response' => 400,
)
);
}
$this->stylesheet = $theme->get_stylesheet();
}

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

if ( is_wp_error( $r ) ) {
$msg = __( 'Oops. Unable to publish the changeset. The following error(s) occurred: ', 'customize-snapshots' );
$msg .= join( '; ', array_keys( $r->errors ) );
wp_die(
'<p>' . esc_html( $msg ) . '</p>',
esc_html__( 'Changeset publishing failed', 'customize-snapshots' ),
array(
'back_link' => true,
'response' => 500,
)
);
} else {
$referer = wp_get_referer();

// Ensure redirect is set to frontend.
if ( empty( $referer ) || false !== strpos( parse_url( $referer, PHP_URL_PATH ), '/wp-admin/' ) ) {
$referer = home_url();
}

$sendback = remove_query_arg( array(
$this->get_front_uuid_param(),
'customize_theme',
$this->get_customize_uuid_param(),
), $referer );

wp_redirect( $sendback );
exit();
}
}

/**
* Save the preview url query vars in changeset meta.
*
Expand Down
5 changes: 5 additions & 0 deletions php/class-plugin.php
Expand Up @@ -134,6 +134,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
9 changes: 9 additions & 0 deletions php/class-post-type.php
Expand Up @@ -65,6 +65,15 @@ public function __construct( Customize_Snapshot_Manager $snapshot_manager ) {
$this->snapshot_manager = $snapshot_manager;
}

/**
* Get the post type slug.
*
* @return string Post type slug.
*/
public function get_slug() {
return static::SLUG;
}

/**
* Calls common hooks Actions and filters
*/
Expand Down