From fba4d3e296109b41de729f91425057e754dab770 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Sat, 7 Mar 2015 01:16:05 +0000 Subject: [PATCH] PressThis: add preview functionality. Opens the preview in a new window or a tab next to the source tab. Fixes #31458. git-svn-id: https://develop.svn.wordpress.org/trunk@31654 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/css/press-this.css | 4 ++ src/wp-admin/includes/class-wp-press-this.php | 53 ++++++++-------- src/wp-admin/js/press-this.js | 62 ++++++++++++------- 3 files changed, 71 insertions(+), 48 deletions(-) diff --git a/src/wp-admin/css/press-this.css b/src/wp-admin/css/press-this.css index f1dad4006a..d395580b4d 100644 --- a/src/wp-admin/css/press-this.css +++ b/src/wp-admin/css/press-this.css @@ -351,6 +351,10 @@ strong { text-decoration: underline; } +.preview-button { + margin-right: 1em; +} + .button-reset { margin: 0; padding: 0; diff --git a/src/wp-admin/includes/class-wp-press-this.php b/src/wp-admin/includes/class-wp-press-this.php index c5e9eb378d..5494ae741f 100644 --- a/src/wp-admin/includes/class-wp-press-this.php +++ b/src/wp-admin/includes/class-wp-press-this.php @@ -133,22 +133,20 @@ class WP_Press_This { * @access public */ public function save_post() { - if ( empty( $_POST['pressthis-nonce'] ) || ! wp_verify_nonce( $_POST['pressthis-nonce'], 'press-this' ) ) { - wp_send_json_error( array( 'errorMessage' => __( 'Cheatin’ uh?' ) ) ); - } - if ( empty( $_POST['post_ID'] ) || ! $post_id = (int) $_POST['post_ID'] ) { wp_send_json_error( array( 'errorMessage' => __( 'Missing post ID.' ) ) ); } - if ( ! current_user_can( 'edit_post', $post_id ) ) { - wp_send_json_error( array( 'errorMessage' => __( 'Cheatin’ uh?' ) ) ); + if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'update-post_' . $post_id ) || + ! current_user_can( 'edit_post', $post_id ) ) { + + wp_send_json_error( array( 'errorMessage' => __( 'Invalid post.' ) ) ); } $post = array( 'ID' => $post_id, - 'post_title' => ( ! empty( $_POST['title'] ) ) ? sanitize_text_field( trim( $_POST['title'] ) ) : '', - 'post_content' => ( ! empty( $_POST['pressthis'] ) ) ? trim( $_POST['pressthis'] ) : '', + 'post_title' => ( ! empty( $_POST['post_title'] ) ) ? sanitize_text_field( trim( $_POST['post_title'] ) ) : '', + 'post_content' => ( ! empty( $_POST['post_content'] ) ) ? trim( $_POST['post_content'] ) : '', 'post_type' => 'post', 'post_status' => 'draft', 'post_format' => ( ! empty( $_POST['post_format'] ) ) ? sanitize_text_field( $_POST['post_format'] ) : '', @@ -887,7 +885,7 @@ class WP_Press_This { > - + <?php esc_html_e( 'Press This!' ) ?> @@ -954,15 +952,17 @@ class WP_Press_This { ?>
@@ -984,15 +984,18 @@ $admin_body_classes = apply_filters( 'admin_body_class', '' );
-
+ + + -
@@ -1016,6 +1019,7 @@ $admin_body_classes = apply_filters( 'admin_body_class', '' ); 'drag_drop_upload' => true, 'editor_height' => 600, 'media_buttons' => false, + 'textarea_name' => 'post_content', 'teeny' => true, 'tinymce' => array( 'resize' => false, @@ -1028,7 +1032,7 @@ $admin_body_classes = apply_filters( 'admin_body_class', '' ); 'toolbar1' => 'bold,italic,bullist,numlist,blockquote,link,unlink', 'toolbar2' => 'undo,redo', ), - 'quicktags' => false, + 'quicktags' => false, ) ); ?> @@ -1051,14 +1055,12 @@ $admin_body_classes = apply_filters( 'admin_body_class', '' );
@@ -1102,8 +1104,9 @@ $admin_body_classes = apply_filters( 'admin_body_class', '' );
- - + + +
diff --git a/src/wp-admin/js/press-this.js b/src/wp-admin/js/press-this.js index 6c00faa5f9..c69378766b 100644 --- a/src/wp-admin/js/press-this.js +++ b/src/wp-admin/js/press-this.js @@ -303,23 +303,12 @@ } /** - * Submit the post form via AJAX, and redirect to the proper screen if published vs saved as a draft. - * - * @param action string publish|draft - */ - function submitPost( action ) { - saveAlert = false; - showSpinner(); - - var $form = $( '#pressthis-form' ); - - if ( 'publish' === action ) { - $( '#post_status' ).val( 'publish' ); - } - + * Prepare the form data for saving. + */ + function prepareFormData() { editor && editor.save(); - $( '#title-field' ).val( sanitizeText( $( '#title-container' ).text() ) ); + $( '#post_title' ).val( sanitizeText( $( '#title-container' ).text() ) ); // Make sure to flush out the tags with tagBox before saving if ( window.tagBox ) { @@ -327,8 +316,25 @@ window.tagBox.flushTags( this, false, 1 ); } ); } + } - var data = $form.serialize(); + /** + * Submit the post form via AJAX, and redirect to the proper screen if published vs saved as a draft. + * + * @param action string publish|draft + */ + function submitPost( action ) { + var data; + + saveAlert = false; + showSpinner(); + + if ( 'publish' === action ) { + $( '#post_status' ).val( 'publish' ); + } + + prepareFormData(); + data = $( '#pressthis-form' ).serialize(); $.ajax( { type: 'post', @@ -502,7 +508,7 @@ $title = $( '#title-container' ); if ( ! hasEmptyTitleStr ) { - $( '#title-field' ).val( suggestedTitle ); + $( '#post_title' ).val( suggestedTitle ); $title.text( suggestedTitle ); $( '.post-title-placeholder' ).addClass( 'is-hidden' ); } @@ -756,14 +762,24 @@ } ); // Publish and Draft buttons and submit + - $( '#draft-field' ).on( 'click', function() { - submitPost( 'draft' ); - } ); + $( '.post-actions' ).on( 'click.press-this', function( event ) { + var $target = $( event.target ); - $( '#publish-field' ).on( 'click', function() { - submitPost( 'publish' ); - } ); + if ( $target.hasClass( 'draft-button' ) ) { + submitPost( 'draft' ); + } else if ( $target.hasClass( 'publish-button' ) ) { + submitPost( 'publish' ); + } else if ( $target.hasClass( 'preview-button' ) ) { + prepareFormData(); + window.opener && window.opener.focus(); + + $( '#wp-preview' ).val( 'dopreview' ); + $( '#pressthis-form' ).attr( 'target', '_blank' ).submit().attr( 'target', '' ); + $( '#wp-preview' ).val( '' ); + } + }); monitorOptionsModal(); monitorPlaceholder();