Make WordPress Core

Changeset 31654


Ignore:
Timestamp:
03/07/2015 01:16:05 AM (12 years ago)
Author:
azaozz
Message:

PressThis: add preview functionality. Opens the preview in a new window or a tab next to the source tab.
Fixes #31458.

Location:
trunk/src/wp-admin
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/css/press-this.css

    r31651 r31654  
    350350        outline: 0;
    351351        text-decoration: underline;
     352}
     353
     354.preview-button {
     355        margin-right: 1em;
    352356}
    353357
  • trunk/src/wp-admin/includes/class-wp-press-this.php

    r31651 r31654  
    134134         */
    135135        public function save_post() {
    136                 if ( empty( $_POST['pressthis-nonce'] ) || ! wp_verify_nonce( $_POST['pressthis-nonce'], 'press-this' ) ) {
    137                         wp_send_json_error( array( 'errorMessage' => __( 'Cheatin’ uh?' ) ) );
    138                 }
    139 
    140136                if ( empty( $_POST['post_ID'] ) || ! $post_id = (int) $_POST['post_ID'] ) {
    141137                        wp_send_json_error( array( 'errorMessage' => __( 'Missing post ID.' ) ) );
    142138                }
    143139
    144                 if ( ! current_user_can( 'edit_post', $post_id ) ) {
    145                         wp_send_json_error( array( 'errorMessage' => __( 'Cheatin’ uh?' ) ) );
     140                if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'update-post_' . $post_id ) ||
     141                        ! current_user_can( 'edit_post', $post_id ) ) {
     142
     143                        wp_send_json_error( array( 'errorMessage' => __( 'Invalid post.' ) ) );
    146144                }
    147145
    148146                $post = array(
    149147                        'ID'            => $post_id,
    150                         'post_title'    => ( ! empty( $_POST['title'] ) ) ? sanitize_text_field( trim( $_POST['title'] ) ) : '',
    151                         'post_content'  => ( ! empty( $_POST['pressthis'] ) ) ? trim( $_POST['pressthis'] ) : '',
     148                        'post_title'    => ( ! empty( $_POST['post_title'] ) ) ? sanitize_text_field( trim( $_POST['post_title'] ) ) : '',
     149                        'post_content'  => ( ! empty( $_POST['post_content'] ) ) ? trim( $_POST['post_content'] ) : '',
    152150                        'post_type'     => 'post',
    153151                        'post_status'   => 'draft',
     
    888886<!--[if gt IE 8]><!--> <html <?php language_attributes(); ?>> <!--<![endif]-->
    889887<head>
    890         <meta http-equiv="Content-Type" content="<?php esc_attr( bloginfo( 'html_type' ) ); ?>; charset=<?php echo esc_attr( get_option( 'blog_charset' ) ); ?>" />
     888        <meta http-equiv="Content-Type" content="<?php echo esc_attr( get_bloginfo( 'html_type' ) ); ?>; charset=<?php echo esc_attr( get_option( 'blog_charset' ) ); ?>" />
    891889        <meta name="viewport" content="width=device-width">
    892890        <title><?php esc_html_e( 'Press This!' ) ?></title>
     
    955953</head>
    956954<?php
    957 $admin_body_class  = 'press-this';
    958 $admin_body_class .= ( is_rtl() ) ? ' rtl' : '';
    959 $admin_body_class .= ' branch-' . str_replace( array( '.', ',' ), '-', floatval( $wp_version ) );
    960 $admin_body_class .= ' version-' . str_replace( '.', '-', preg_replace( '/^([.0-9]+).*/', '$1', $wp_version ) );
    961 $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
    962 $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
    963 
    964 /** This filter is documented in wp-admin/admin-header.php */
    965 $admin_body_classes = apply_filters( 'admin_body_class', '' );
     955
     956        $admin_body_class  = 'press-this';
     957        $admin_body_class .= ( is_rtl() ) ? ' rtl' : '';
     958        $admin_body_class .= ' branch-' . str_replace( array( '.', ',' ), '-', floatval( $wp_version ) );
     959        $admin_body_class .= ' version-' . str_replace( '.', '-', preg_replace( '/^([.0-9]+).*/', '$1', $wp_version ) );
     960        $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
     961        $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
     962       
     963        /** This filter is documented in wp-admin/admin-header.php */
     964        $admin_body_classes = apply_filters( 'admin_body_class', '' );
     965
    966966?>
    967967<body class="wp-admin wp-core-ui <?php echo $admin_body_classes . ' ' . $admin_body_class; ?>">
     
    985985        </div>
    986986
    987         <form id="pressthis-form" name="pressthis-form" method="POST" autocomplete="off">
     987        <form id="pressthis-form" method="post" action="post.php" autocomplete="off">
    988988                <input type="hidden" name="post_ID" id="post_ID" value="<?php echo $post_ID; ?>" />
    989989                <input type="hidden" name="action" value="press-this-save-post" />
    990990                <input type="hidden" name="post_status" id="post_status" value="draft" />
     991                <input type="hidden" name="wp-preview" id="wp-preview" value="" />
     992                <input type="hidden" name="post_title" id="post_title" value="" />
    991993                <?php
    992                 wp_nonce_field( 'press-this', 'pressthis-nonce', false );
     994
     995                wp_nonce_field( 'update-post_' . $post_ID, '_wpnonce', false );
    993996                wp_nonce_field( 'add-category', '_ajax_nonce-add-category', false );
     997
    994998                ?>
    995                 <input type="hidden" name="title" id="title-field" value="" />
    996999
    9971000        <div class="wrapper">
     
    10171020                                        'editor_height'    => 600,
    10181021                                        'media_buttons'    => false,
     1022                                        'textarea_name'    => 'post_content',
    10191023                                        'teeny'            => true,
    10201024                                        'tinymce'          => array(
     
    10291033                                                'toolbar2'              => 'undo,redo',
    10301034                                        ),
    1031                                         'quicktags'        => false,
     1035                                        'quicktags' => false,
    10321036                                ) );
    10331037
     
    10521056                                        <span class="dashicons dashicons-category"></span>
    10531057                                        <span class="post-option-title"><?php _e( 'Categories' ); ?></span>
    1054                                         <span class="post-option-contents" id="post-option-category"></span>
    10551058                                        <span class="dashicons post-option-forward"></span>
    10561059                                </button>
     
    10591062                                        <span class="dashicons dashicons-tag"></span>
    10601063                                        <span class="post-option-title"><?php _e( 'Tags' ); ?></span>
    1061                                         <span class="post-option-contents" id="post-option-tags"></span>
    10621064                                        <span class="dashicons post-option-forward"></span>
    10631065                                </button>
     
    11031105                </div>
    11041106                <div class="post-actions">
    1105                         <button type="button" class="button-subtle" id="draft-field"><?php _e( 'Save Draft' ); ?></button>
    1106                         <button type="button" class="button-primary" id="publish-field"><?php _e( 'Publish' ); ?></button>
     1107                        <button type="button" class="button-subtle draft-button"><?php _e( 'Save Draft' ); ?></button>
     1108                        <button type="button" class="button preview-button"><?php _e( 'Preview' ); ?></button>
     1109                        <button type="button" class="button-primary publish-button"><?php _e( 'Publish' ); ?></button>
    11071110                </div>
    11081111        </div>
  • trunk/src/wp-admin/js/press-this.js

    r31651 r31654  
    304304
    305305                /**
    306                  * Submit the post form via AJAX, and redirect to the proper screen if published vs saved as a draft.
    307                  *
    308                  * @param action string publish|draft
    309                  */
    310                 function submitPost( action ) {
    311                         saveAlert = false;
    312                         showSpinner();
    313 
    314                         var $form = $( '#pressthis-form' );
    315 
    316                         if ( 'publish' === action ) {
    317                                 $( '#post_status' ).val( 'publish' );
    318                         }
    319 
     306                 * Prepare the form data for saving.
     307                 */                             
     308                function prepareFormData() {
    320309                        editor && editor.save();
    321310
    322                         $( '#title-field' ).val( sanitizeText( $( '#title-container' ).text() ) );
     311                        $( '#post_title' ).val( sanitizeText( $( '#title-container' ).text() ) );
    323312
    324313                        // Make sure to flush out the tags with tagBox before saving
     
    328317                                } );
    329318                        }
    330 
    331                         var data = $form.serialize();
     319                }
     320
     321                /**
     322                 * Submit the post form via AJAX, and redirect to the proper screen if published vs saved as a draft.
     323                 *
     324                 * @param action string publish|draft
     325                 */
     326                function submitPost( action ) {
     327                        var data;
     328
     329                        saveAlert = false;
     330                        showSpinner();
     331
     332                        if ( 'publish' === action ) {
     333                                $( '#post_status' ).val( 'publish' );
     334                        }
     335
     336                        prepareFormData();
     337                        data = $( '#pressthis-form' ).serialize();
    332338
    333339                        $.ajax( {
     
    503509
    504510                        if ( ! hasEmptyTitleStr ) {
    505                                 $( '#title-field' ).val( suggestedTitle );
     511                                $( '#post_title' ).val( suggestedTitle );
    506512                                $title.text( suggestedTitle );
    507513                                $( '.post-title-placeholder' ).addClass( 'is-hidden' );
     
    757763
    758764                        // Publish and Draft buttons and submit
    759 
    760                         $( '#draft-field' ).on( 'click', function() {
    761                                 submitPost( 'draft' );
    762                         } );
    763 
    764                         $( '#publish-field' ).on( 'click', function() {
    765                                 submitPost( 'publish' );
    766                         } );
     765                       
     766
     767                        $( '.post-actions' ).on( 'click.press-this', function( event ) {
     768                                var $target = $( event.target );
     769
     770                                if ( $target.hasClass( 'draft-button' ) ) {
     771                                        submitPost( 'draft' );
     772                                } else if ( $target.hasClass( 'publish-button' ) ) {
     773                                        submitPost( 'publish' );
     774                                } else if ( $target.hasClass( 'preview-button' ) ) {
     775                                        prepareFormData();
     776                                        window.opener && window.opener.focus();
     777
     778                                        $( '#wp-preview' ).val( 'dopreview' );
     779                                        $( '#pressthis-form' ).attr( 'target', '_blank' ).submit().attr( 'target', '' );
     780                                        $( '#wp-preview' ).val( '' );
     781                                }
     782                        });
    767783
    768784                        monitorOptionsModal();
Note: See TracChangeset for help on using the changeset viewer.