Make WordPress Core

Changeset 9473


Ignore:
Timestamp:
11/02/2008 01:56:32 PM (16 years ago)
Author:
azaozz
Message:

Preview fixes: ensure autosave is run at least once on new posts before preview, properly show previews for pending and scheduled posts.

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/post.php

    r9469 r9473  
    10031003function post_preview() {
    10041004
    1005     $_POST['post_category'] = explode(",", $_POST['catslist']);
    1006     $_POST['tags_input'] = explode(",", $_POST['tags_input']);
    1007     if($_POST['post_type'] == 'page' || empty($_POST['post_category']))
     1005    $post_ID = (int) $_POST['post_ID'];
     1006    if ( $post_ID < 1 )
     1007        wp_die( __('Preview not available. Please save as draft first.') );
     1008   
     1009    if ( isset($_POST['catslist']) )
     1010        $_POST['post_category'] = explode(",", $_POST['catslist']);
     1011   
     1012    if ( isset($_POST['tags_input']) )
     1013        $_POST['tags_input'] = explode(",", $_POST['tags_input']);
     1014   
     1015    if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
    10081016        unset($_POST['post_category']);
    10091017
    1010     if($_POST['post_ID'] < 0) {
    1011         $_POST['post_status'] = 'draft';
    1012         $_POST['temp_ID'] = $_POST['post_ID'];
    1013         $id = wp_write_post();
     1018    $_POST['ID'] = $post_ID;
     1019    $post = get_post($post_ID);
     1020
     1021    if ( 'page' == $post->post_type ) {
     1022        if ( !current_user_can('edit_page', $post_ID) )
     1023            wp_die(__('You are not allowed to edit this page.'));
    10141024    } else {
    1015         $post_ID = (int) $_POST['post_ID'];
    1016         $_POST['ID'] = $post_ID;
    1017         $post = get_post($post_ID);
    1018 
    1019         if ( 'page' == $post->post_type ) {
    1020             if ( !current_user_can('edit_page', $post_ID) )
    1021                 die(__('You are not allowed to edit this page.'));
    1022         } else {
    1023             if ( !current_user_can('edit_post', $post_ID) )
    1024                 die(__('You are not allowed to edit this post.'));
    1025         }
    1026 
    1027         if ( 'draft' == $post->post_status ) {
    1028             $id = edit_post();
    1029         } else { // Non drafts are not overwritten.  The autosave is stored in a special post revision.
    1030             wp_create_post_autosave( $post->ID );
     1025        if ( !current_user_can('edit_post', $post_ID) )
     1026            wp_die(__('You are not allowed to edit this post.'));
     1027    }
     1028
     1029    if ( 'draft' == $post->post_status ) {
     1030        $id = edit_post();
     1031    } else { // Non drafts are not overwritten.  The autosave is stored in a special post revision.
     1032        $id = wp_create_post_autosave( $post->ID );
     1033        if ( ! is_wp_error($id) )
    10311034            $id = $post->ID;
    1032         }
    10331035    }
    10341036
  • trunk/wp-admin/js/page.js

    r9451 r9473  
    109109    // preview
    110110    $('#post-preview').click(function(e){
     111        if ( 1 > $('#post_ID').val() && autosaveFirst ) {
     112            autosaveDelayPreview = true;
     113            autosave();
     114            return false;
     115        }
     116
    111117        $('input#wp-preview').val('dopreview');
    112118        $('form#post').attr('target', 'wp-preview').submit().attr('target', '');
    113119        $('input#wp-preview').val('');
     120        return false;
    114121    });
    115122});
  • trunk/wp-admin/js/post.js

    r9451 r9473  
    264264    // preview
    265265    $('#post-preview').click(function(e){
     266        if ( 1 > $('#post_ID').val() && autosaveFirst ) {
     267            autosaveDelayPreview = true;
     268            autosave();
     269            return false;
     270        }
     271
    266272        $('input#wp-preview').val('dopreview');
    267273        $('form#post').attr('target', 'wp-preview').submit().attr('target', '');
    268274        $('input#wp-preview').val('');
     275        return false;
    269276    });
    270277
  • trunk/wp-admin/page.php

    r9468 r9473  
    177177    check_admin_referer( 'autosave', 'autosavenonce' );
    178178
    179     if ( empty($_POST['post_title']) )
    180         wp_die( __('Please enter a title before previewing this page.') );
    181 
    182179    $id = post_preview();
    183180
     
    185182        wp_die( $id->get_error_message() );
    186183
    187     if ( $_POST['post_status'] == 'publish'  ) {
     184    if ( $_POST['post_status'] == 'draft'  ) {
     185        $url = get_option('home') . '/?page_id=' . $id . '&preview=true';
     186    } else {
    188187        $nonce = wp_create_nonce('post_preview_' . $id);
    189         $url = site_url('?wp_preview=' . $id . '&preview_nonce=' . $nonce);
    190     } else {
    191         $url = site_url('?page_id=' . $id . '&preview=true');
     188        $url = get_option('home') . '/?wp_preview=' . $id . '&preview_nonce=' . $nonce;
    192189    }
    193190
  • trunk/wp-admin/post.php

    r9453 r9473  
    224224    check_admin_referer( 'autosave', 'autosavenonce' );
    225225
    226     if ( empty($_POST['post_title']) )
    227         wp_die( __('Please enter a title before previewing this post.') );
    228 
    229226    $id = post_preview();
    230227
     
    232229        wp_die( $id->get_error_message() );
    233230
    234     if ( $_POST['post_status'] == 'publish'  ) {
     231    if ( $_POST['post_status'] == 'draft'  ) {
     232        $url = get_option('home') . '/?p=' . $id . '&preview=true';
     233    } else {
    235234        $nonce = wp_create_nonce('post_preview_' . $id);
    236235        $url = get_option('home') . '/?wp_preview=' . $id . '&preview_nonce=' . $nonce;
    237     } else {
    238         $url = get_option('home') . '/?p=' . $id . '&preview=true';
    239236    }
    240237
  • trunk/wp-includes/js/autosave.js

    r8691 r9473  
    22var autosavePeriodical;
    33var autosaveOldMessage = '';
    4 var autosaveDelayURL = null;
    5 var previewwin;
     4var autosaveDelayPreview = false;
     5var autosaveFirst = true;
    66
    77jQuery(function($) {
     
    1111    //Disable autosave after the form has been submitted
    1212    $("#post").submit(function() { $.cancel(autosavePeriodical); });
    13 
    14     // Autosave when the preview button is clicked.
    15     $('#previewview a').click(function(e) {
    16         autosave();
    17         autosaveDelayURL = this.href;
    18         previewwin = window.open('','_blank');
    19 
    20         e.preventDefault();
    21         return false;
    22     });
    2313});
    2414
     
    4232        }
    4333
    44         // if no errors: add preview link and slug UI
     34        // if no errors: add slug UI
    4535        if ( !res.errors ) {
    4636            var postID = parseInt( res.responses[0].id );
    4737            if ( !isNaN(postID) && postID > 0 ) {
    48                 autosave_update_preview_link(postID);
    4938                autosave_update_slug(postID);
    5039            }
     
    7059        var postID = parseInt( res.responses[0].id );
    7160        autosave_update_post_ID( postID ); // disabled form buttons are re-enabled here
    72         if ( tempID < 0 && postID > 0) // update media buttons
     61        if ( tempID < 0 && postID > 0 ) // update media buttons
    7362            jQuery('#media-buttons a').each(function(){
    7463                this.href = this.href.replace(tempID, postID);
    7564            });
     65        // activate preview
     66        autosaveFirst = false;
     67        if ( autosaveDelayPreview )
     68            jQuery('#post-preview').click();
    7669    } else {
    7770        autosave_enable_buttons(); // re-enable disabled form buttons
     
    9891}
    9992
    100 function autosave_update_preview_link(post_id) {
    101     // Add preview button if not already there
    102     if ( !jQuery('#previewview > *').size() ) {
    103         var post_type = jQuery('#post_type').val();
    104         var previewText = 'page' == post_type ? autosaveL10n.previewPageText : autosaveL10n.previewPostText;
    105         jQuery.post(autosaveL10n.requestFile, {
    106             action: "get-permalink",
    107             post_id: post_id,
    108             getpermalinknonce: jQuery('#getpermalinknonce').val()
    109         }, function(permalink) {
    110             jQuery('#previewview').html('<a class="button" target="_blank" href="'+permalink+'" tabindex="4">'+previewText+'</a>');
    111 
    112             // Autosave when the preview button is clicked.
    113             jQuery('#previewview a').click(function(e) {
    114                 autosave();
    115                 autosaveDelayURL = this.href;
    116                 previewwin = window.open('','_blank');
    117 
    118                 e.preventDefault();
    119                 return false;
    120             });
    121         });
    122     }
    123 }
    124 
    12593function autosave_update_slug(post_id) {
    12694    // create slug area only if not already there
     
    148116function autosave_enable_buttons() {
    149117    jQuery("#submitpost :button:disabled, #submitpost :submit:disabled").attr('disabled', '');
    150     if ( autosaveDelayURL ) {
    151         previewwin.location = autosaveDelayURL;
    152         autosaveDelayURL = null;
    153     }
    154118}
    155119
     
    224188    if(parseInt(post_data["post_ID"]) < 1) {
    225189        post_data["temp_ID"] = post_data["post_ID"];
    226         var successCallback = autosave_saved_new;; // new post
     190        var successCallback = autosave_saved_new; // new post
    227191    } else {
    228192        var successCallback = autosave_saved; // pre-existing post
  • trunk/wp-includes/script-loader.php

    r9465 r9473  
    7676    ) );
    7777
    78     $scripts->add( 'autosave', '/wp-includes/js/autosave.js', array('schedule', 'wp-ajax-response'), '20080701' );
     78    $scripts->add( 'autosave', '/wp-includes/js/autosave.js', array('schedule', 'wp-ajax-response'), '20081102' );
    7979
    8080    $scripts->add( 'wp-lists', '/wp-includes/js/wp-lists.js', array('wp-ajax-response'), '20080729' );
     
    177177            'cancel' => __('Cancel'),
    178178        ) );
    179         $scripts->add( 'post', '/wp-admin/js/post.js', array('suggest', 'jquery-ui-tabs', 'wp-lists', 'postbox', 'slug', 'settings-box'), '20081031' );
     179        $scripts->add( 'post', '/wp-admin/js/post.js', array('suggest', 'jquery-ui-tabs', 'wp-lists', 'postbox', 'slug', 'settings-box'), '20081102' );
    180180        $scripts->localize( 'post', 'postL10n', array(
    181181            'tagsUsed' =>  __('Tags used on this post:'),
     
    196196            'saveDraft' => __('Save Draft')
    197197        ) );
    198         $scripts->add( 'page', '/wp-admin/js/page.js', array('jquery', 'slug', 'postbox', 'settings-box'), '20081031' );
     198        $scripts->add( 'page', '/wp-admin/js/page.js', array('jquery', 'slug', 'postbox', 'settings-box'), '20081102' );
    199199        $scripts->localize( 'page', 'postL10n', array(
    200200            'cancel' => __('Cancel'),
Note: See TracChangeset for help on using the changeset viewer.