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

Commit

Permalink
Merge 04ee25a into e43f9e8
Browse files Browse the repository at this point in the history
  • Loading branch information
Sayed Taqui committed Apr 25, 2017
2 parents e43f9e8 + 04ee25a commit f3f0a53
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 32 deletions.
40 changes: 25 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
sudo: false

notifications:
email:
on_success: never
on_failure: change

cache:
directories:
- node_modules
- vendor
- $HOME/phpunit-bin

language:
- php
- node_js
- php
- node_js

php:
- 5.2
- 7.0
- 5.3
- 7.0

env:
- WP_VERSION=4.5 WP_MULTISITE=1
- WP_VERSION=latest WP_MULTISITE=0
- WP_VERSION=trunk WP_MULTISITE=0
- WP_VERSION=trunk WP_MULTISITE=1
- WP_VERSION=trunk WP_MULTISITE=0
- WP_VERSION=latest WP_MULTISITE=0
- WP_VERSION=4.6.1 WP_MULTISITE=0
- WP_VERSION=latest WP_MULTISITE=1

install:
- nvm install 4 && nvm use 4
- export DEV_LIB_PATH=dev-lib
- if [ ! -e "$DEV_LIB_PATH" ] && [ -L .travis.yml ]; then export DEV_LIB_PATH=$( dirname $( readlink .travis.yml ) ); fi
- if [ ! -e "$DEV_LIB_PATH" ]; then git clone https://github.com/xwp/wp-dev-lib.git $DEV_LIB_PATH; fi
- source $DEV_LIB_PATH/travis.install.sh
- nvm install 6 && nvm use 6
- export DEV_LIB_PATH=dev-lib
- if [ ! -e "$DEV_LIB_PATH" ] && [ -L .travis.yml ]; then export DEV_LIB_PATH=$( dirname $( readlink .travis.yml ) ); fi
- source $DEV_LIB_PATH/travis.install.sh

script:
- source $DEV_LIB_PATH/travis.script.sh
- source $DEV_LIB_PATH/travis.script.sh

after_script:
- source $DEV_LIB_PATH/travis.after_script.sh
- source $DEV_LIB_PATH/travis.after_script.sh
3 changes: 3 additions & 0 deletions css/edit-post-preview-admin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#preview-action .spinner:not(.is-active-preview) {
visibility: hidden !important;
}
6 changes: 5 additions & 1 deletion css/edit-post-preview-customize.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
}

#customize-header-actions #save,
#customize-header-actions #snapshot-save {
#customize-header-actions #snapshot-save,
#snapshot-status-button-wrapper,
#snapshot-preview-link,
#snapshot-expand-button
{
display: none !important;
}

Expand Down
2 changes: 1 addition & 1 deletion dev-lib
68 changes: 57 additions & 11 deletions js/edit-post-preview-admin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/* global jQuery, _editPostPreviewAdminExports, JSON, tinymce */
/* global jQuery, _editPostPreviewAdminExports, JSON, tinymce, wp, alert */
/* exported EditPostPreviewAdmin */
/*eslint no-magic-numbers: ["error", { "ignore": [0, 1] }]*/

var EditPostPreviewAdmin = (function( $ ) {
'use strict';

var component = {
data: {
customize_url: null
customize_url: null,
is_compat: false,
previewed_post: null,
customize_posts_update_changeset_nonce: null
}
};

Expand All @@ -14,7 +19,10 @@ var EditPostPreviewAdmin = (function( $ ) {
}

component.init = function() {
$( '#post-preview' )
component.previewButton = $( '#post-preview' );
component.previewButtonSpinner = $( 'span.spinner' ).first().clone();
component.previewButton.after( component.previewButtonSpinner );
component.previewButton
.off( 'click.post-preview' )
.on( 'click.post-preview', component.onClickPreviewBtn );
};
Expand All @@ -29,7 +37,8 @@ var EditPostPreviewAdmin = (function( $ ) {
editor = tinymce.get( 'content' ),
wasMobile,
parentId,
menuOrder;
menuOrder,
request;

event.preventDefault();

Expand All @@ -55,7 +64,6 @@ var EditPostPreviewAdmin = (function( $ ) {
menuOrder = 0;
}

// Send the current input fields from the edit post page to the Customizer via sessionStorage.
postSettingValue = {
post_title: $( '#title' ).val(),
post_name: $( '#post_name' ).val(),
Expand All @@ -73,11 +81,51 @@ var EditPostPreviewAdmin = (function( $ ) {
// Allow plugins to inject additional settings to preview.
wp.customize.trigger( 'settings-from-edit-post-screen', settings );

sessionStorage.setItem( 'previewedCustomizePostSettings[' + postId + ']', JSON.stringify( settings ) );

wp.customize.Loader.open( component.data.customize_url );
// For backward compatibility send the current input fields from the edit post page to the Customizer via sessionStorage.
if ( component.data.is_compat ) {
sessionStorage.setItem( 'previewedCustomizePostSettings[' + postId + ']', JSON.stringify( settings ) );
wp.customize.Loader.open( component.data.customize_url );
wp.customize.Loader.settings.browser.mobile = wasMobile;
component.bindChangesFromCustomizer( postSettingId, editor );
} else {
$btn.addClass( 'disabled' );
component.previewButtonSpinner.addClass( 'is-active is-active-preview' );
request = wp.ajax.post( 'customize_posts_update_changeset', {
customize_posts_update_changeset_nonce: component.data.customize_posts_update_changeset_nonce,
previewed_post: component.data.previewed_post,
customize_url: component.data.customize_url,
input_data: postSettingValue
} );

request.fail( function( resp ) {
var error = JSON.parse( resp.responseText ), errorText;
if ( error.data ) {
errorText = error.data.replace( /_/g, ' ' );
alert( errorText.charAt( 0 ).toUpperCase() + errorText.slice( 1 ) ); // eslint-disable-line no-alert
}
} );

request.done( function( resp ) {
wp.customize.Loader.open( resp.customize_url );
wp.customize.Loader.settings.browser.mobile = wasMobile;
component.bindChangesFromCustomizer( postSettingId, editor );
} );

request.always( function() {
$btn.removeClass( 'disabled' );
component.previewButtonSpinner.removeClass( 'is-active is-active-preview' );
} );
}
};

// Sync changes from the Customizer to the post input fields.
/**
* Sync changes from the Customizer to the post input fields.
*
* @param {string} postSettingId post setting id.
* @param {object} editor Tinymce object.
* @return {void}
*/
component.bindChangesFromCustomizer = function( postSettingId, editor ) {
wp.customize.Loader.messenger.bind( 'customize-post-settings-data', function( data ) {
var settingParentId;
if ( data[ postSettingId ] ) {
Expand All @@ -103,8 +151,6 @@ var EditPostPreviewAdmin = (function( $ ) {
// Let plugins handle updates.
wp.customize.trigger( 'settings-from-customizer', data );
} );

wp.customize.Loader.settings.browser.mobile = wasMobile;
};

/**
Expand Down
11 changes: 8 additions & 3 deletions js/edit-post-preview-customize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global jQuery, _editPostPreviewCustomizeExports, JSON, console */
/* global jQuery, _editPostPreviewCustomizeExports, JSON, console, wp, _ */
/* exported EditPostPreviewCustomize */
var EditPostPreviewCustomize = (function( $, api ) {
'use strict';
Expand Down Expand Up @@ -64,8 +64,13 @@ var EditPostPreviewCustomize = (function( $, api ) {
*/
component.ready = function() {

// Wait to populate the settings until the customized-posts message is received so we know the preview is ready to receive mesages.
api.previewer.bind( 'customized-posts', component.populateSettings );
/**
* For backward compatibility.
* Wait to populate the settings until the customized-posts message is received so we know the preview is ready to receive messages.
*/
if ( ! api.state.has( 'changesetStatus' ) ) {
api.previewer.bind( 'customized-posts', component.populateSettings );
}

// Prevent 'saved' state from becoming false, since we only want to save from the admin page.
api.state( 'saved' ).set( true ).validate = function() {
Expand Down
5 changes: 5 additions & 0 deletions php/class-customize-posts-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ public function register_styles( WP_Styles $wp_styles ) {
$src = plugins_url( 'css/edit-post-preview-customize' . $suffix, dirname( __FILE__ ) );
$deps = array( 'customize-controls' );
$wp_styles->add( $handle, $src, $deps, $this->version );

$handle = 'edit-post-preview-admin';
$src = plugins_url( 'css/edit-post-preview-admin' . $suffix, dirname( __FILE__ ) );
$deps = array( 'common' );
$wp_styles->add( $handle, $src, $deps, $this->version );
}

/**
Expand Down
124 changes: 123 additions & 1 deletion php/class-edit-post-preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Edit_Post_Preview {

const PREVIEW_POST_NONCE_ACTION = 'customize_preview_post';
const PREVIEW_POST_NONCE_QUERY_VAR = 'customize_preview_post_nonce';
const UPDATE_CHANGESET_NONCE_ACTION = 'customize_posts_update_changeset';
const UPDATE_CHANGESET_NONCE = 'customize_posts_update_changeset_nonce';

/**
* Plugin instance.
Expand All @@ -36,6 +38,7 @@ public function __construct( Customize_Posts_Plugin $plugin ) {
add_action( 'customize_controls_init', array( $this, 'remove_static_controls_and_sections' ), 100 );
add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_customize_scripts' ) );
add_action( 'customize_preview_init', array( $this, 'make_auto_draft_status_previewable' ) );
add_action( 'wp_ajax_' . self::UPDATE_CHANGESET_NONCE_ACTION, array( $this, 'update_post_changeset' ) );
}

/**
Expand Down Expand Up @@ -122,10 +125,11 @@ public static function get_preview_post_link( $post ) {
* Enqueue scripts for post edit screen.
*/
public function enqueue_admin_scripts() {
if ( ! function_exists( 'get_current_screen' ) || ! get_current_screen() || 'post' !== get_current_screen()->base ) {
if ( ! function_exists( 'get_current_screen' ) || ! get_current_screen() || 'post' !== get_current_screen()->base || ! current_user_can( 'customize' ) ) {
return;
}
wp_enqueue_script( 'edit-post-preview-admin' );
wp_enqueue_style( 'edit-post-preview-admin' );
$post = $this->get_previewed_post();

$customize_url = add_query_arg(
Expand All @@ -137,9 +141,14 @@ public function enqueue_admin_scripts() {
),
wp_customize_url()
);

$data = array(
'customize_url' => $customize_url,
self::UPDATE_CHANGESET_NONCE => wp_create_nonce( self::UPDATE_CHANGESET_NONCE_ACTION ),
'previewed_post' => $post->ID,
'is_compat' => version_compare( strtok( get_bloginfo( 'version' ), '-' ), '4.7', '<' ),
);

wp_scripts()->add_data( 'edit-post-preview-admin', 'data', sprintf( 'var _editPostPreviewAdminExports = %s;', wp_json_encode( $data ) ) );
wp_enqueue_script( 'customize-loader' );
wp_add_inline_script( 'edit-post-preview-admin', 'jQuery( function() { EditPostPreviewAdmin.init(); } );', 'after' );
Expand Down Expand Up @@ -197,4 +206,117 @@ public function make_auto_draft_status_previewable() {
global $wp_post_statuses;
$wp_post_statuses['auto-draft']->protected = true;
}

/**
* Updates changeset via ajax when preview button is clicked.
*
* @global WP_Customize_Manager $wp_customize
*/
public function update_post_changeset() {
global $wp_customize;

if ( ! check_ajax_referer( self::UPDATE_CHANGESET_NONCE_ACTION, self::UPDATE_CHANGESET_NONCE, false ) ) {
status_header( 400 );
wp_send_json_error( 'bad_nonce' );
} elseif ( ! current_user_can( 'customize' ) ) {
status_header( 403 );
wp_send_json_error( 'customize_not_allowed' );
} elseif ( ! isset( $_POST['previewed_post'] ) ) {
status_header( 400 );
wp_send_json_error( 'missing_previewed_post' );
} elseif ( empty( $_POST['customize_url'] ) ) {
status_header( 400 );
wp_send_json_error( 'missing_customize_url' );
} elseif ( empty( $_POST['input_data'] ) || ! is_array( $_POST['input_data'] ) ) {
status_header( 400 );
wp_send_json_error( 'missing_input_data' );
}

$previewed_post_id = intval( wp_unslash( $_POST['previewed_post'] ) );
if ( empty( $previewed_post_id ) || ! get_post( $previewed_post_id ) ) {
status_header( 404 );
wp_send_json_error( 'post_not_found' );
} elseif ( ! current_user_can( 'edit_post', $previewed_post_id ) ) {
status_header( 403 );
wp_send_json_error( 'missing_previewed_post' );
}

$changeset_uuid = get_post_meta( $previewed_post_id, '_preview_changeset_uuid', true );

if ( empty( $wp_customize ) || ! ( $wp_customize instanceof WP_Customize_Manager ) ) {
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-posts.php';
}

if ( $changeset_uuid ) {
$wp_customize = new \WP_Customize_Manager( array(
'changeset_uuid' => $changeset_uuid,
) );
$changeset_post_id = $wp_customize->changeset_post_id();

if ( $changeset_post_id ) {
if ( 'publish' === get_post_status( $changeset_post_id ) ) {
status_header( 400 );
wp_send_json_error( 'changeset_already_published' );
}

if ( ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->edit_post, $changeset_post_id ) ) {
status_header( 403 );
wp_send_json_error( 'cannot_edit_changeset_post' );
}
}
}

if ( empty( $changeset_post_id ) ) {
if ( ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->create_posts ) ) {
status_header( 403 );
wp_send_json_error( 'cannot_create_changeset_post' );
}
$wp_customize = new \WP_Customize_Manager();
$changeset_uuid = $wp_customize->changeset_uuid();
update_post_meta( $previewed_post_id, '_preview_changeset_uuid', $changeset_uuid );
}

$customize_url = add_query_arg(
compact( 'changeset_uuid' ),
wp_unslash( $_POST['customize_url'] )
);

if ( ! isset( $wp_customize->posts ) || ! ( $wp_customize->posts instanceof WP_Customize_Posts ) ) {
wp_send_json_error( 'missing_posts_component' );
}

/**
* Posts component.
*
* @var WP_Customize_Posts $wp_customize_posts
*/
$wp_customize_posts = $wp_customize->posts;

$settings = $wp_customize_posts->get_settings( array( $previewed_post_id ) );
$setting = array_shift( $settings );

if ( ! $setting ) {
status_header( 404 );
wp_send_json_error( 'setting_not_found' );
return;
} elseif ( ! $setting->check_capabilities() ) {
status_header( 403 );
wp_send_json_error( 'changeset_already_published' );
return;
}
$setting->preview();

// Note that save_changeset_post() will handle validation and sanitization.
$wp_customize->set_post_value( $setting->id, wp_array_slice_assoc(
array_merge( $setting->value(), wp_unslash( $_POST['input_data'] ) ),
array_keys( $setting->default )
) );
$response = $wp_customize->save_changeset_post();
if ( is_wp_error( $response ) ) {
wp_send_json_error( $response->get_error_code() );
}

wp_send_json_success( compact( 'customize_url', 'response' ) );
}
}

0 comments on commit f3f0a53

Please sign in to comment.