Make WordPress Core


Ignore:
Timestamp:
05/08/2008 05:25:07 PM (18 years ago)
Author:
ryan
Message:

Move autosave to post revisions. Props mdawaffe. see #6775

File:
1 edited

Legend:

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

    r7896 r7907  
    11<?php
    22
    3 // Update an existing post with values provided in $_POST.
    4 function edit_post() {
    5 
    6     $post_ID = (int) $_POST['post_ID'];
    7 
    8     if ( 'page' == $_POST['post_type'] ) {
    9         if ( !current_user_can( 'edit_page', $post_ID ) )
    10             wp_die( __('You are not allowed to edit this page.' ));
    11     } else {
    12         if ( !current_user_can( 'edit_post', $post_ID ) )
    13             wp_die( __('You are not allowed to edit this post.' ));
    14     }
    15 
    16     // Autosave shouldn't save too soon after a real save
    17     if ( 'autosave' == $_POST['action'] ) {
    18         $post =& get_post( $post_ID );
    19         $now = time();
    20         $then = strtotime($post->post_date_gmt . ' +0000');
    21         $delta = AUTOSAVE_INTERVAL / 2;
    22         if ( ($now - $then) < $delta )
    23             return $post_ID;
    24     }
    25 
    26     // Rename.
    27     $_POST['ID'] = (int) $_POST['post_ID'];
     3/**
     4 * _wp_translate_postdata() - Rename $_POST data from form names to DB post columns.
     5 *
     6 * Manipulates $_POST directly.
     7 *
     8 * @package WordPress
     9 * @since 2.6
     10 *
     11 * @param bool $update Are we updating a pre-existing post?
     12 * @return object|bool WP_Error on failure, true on success.
     13 */
     14function _wp_translate_postdata( $update = false ) {
     15    if ( $update )
     16        $_POST['ID'] = (int) $_POST['post_ID'];
    2817    $_POST['post_content'] = $_POST['content'];
    2918    $_POST['post_excerpt'] = $_POST['excerpt'];
     
    3322    if (!empty ( $_POST['post_author_override'] ) ) {
    3423        $_POST['post_author'] = (int) $_POST['post_author_override'];
    35     } else
     24    } else {
    3625        if (!empty ( $_POST['post_author'] ) ) {
    3726            $_POST['post_author'] = (int) $_POST['post_author'];
     
    3928            $_POST['post_author'] = (int) $_POST['user_ID'];
    4029        }
     30    }
    4131
    4232    if ( $_POST['post_author'] != $_POST['user_ID'] ) {
    4333        if ( 'page' == $_POST['post_type'] ) {
    44             if ( !current_user_can( 'edit_others_pages' ) )
    45                 wp_die( __('You are not allowed to edit pages as this user.' ));
     34            if ( !current_user_can( 'edit_others_pages' ) ) {
     35                return new WP_Error( 'edit_others_pages', $update ?
     36                    __( 'You are not allowed to edit pages as this user.' ) :
     37                    __( 'You are not allowed to create pages as this user.' )
     38                );
     39            }
    4640        } else {
    47             if ( !current_user_can( 'edit_others_posts' ) )
    48                 wp_die( __('You are not allowed to edit posts as this user.' ));
    49 
     41            if ( !current_user_can( 'edit_others_posts' ) ) {
     42                return new WP_Error( 'edit_others_posts', $update ?
     43                    __( 'You are not allowed to edit posts as this user.' ) :
     44                    __( 'You are not allowed to post as this user.' )
     45                );
     46            }
    5047        }
    5148    }
     
    6259
    6360    if ( 'page' == $_POST['post_type'] ) {
    64         if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_pages' ))
     61        if ( 'publish' == $_POST['post_status'] && !current_user_can( 'publish_pages' ) )
    6562            $_POST['post_status'] = 'pending';
    6663    } else {
    67         if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_posts' ))
     64        if ( 'publish' == $_POST['post_status'] && !current_user_can( 'publish_posts' ) )
    6865            $_POST['post_status'] = 'pending';
    6966    }
     
    7572        $_POST['ping_status'] = 'closed';
    7673
    77     foreach ( array ('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
     74    foreach ( array('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
    7875        if ( !empty( $_POST['hidden_' . $timeunit] ) && $_POST['hidden_' . $timeunit] != $_POST[$timeunit] ) {
    7976            $_POST['edit_date'] = '1';
     
    8279    }
    8380
    84     if (!empty ( $_POST['edit_date'] ) ) {
     81    if ( !empty( $_POST['edit_date'] ) ) {
    8582        $aa = $_POST['aa'];
    8683        $mm = $_POST['mm'];
     
    9390        $mn = ($mn > 59 ) ? $mn -60 : $mn;
    9491        $ss = ($ss > 59 ) ? $ss -60 : $ss;
    95         $_POST['post_date'] = "$aa-$mm-$jj $hh:$mn:$ss";
    96         $_POST['post_date_gmt'] = get_gmt_from_date( "$aa-$mm-$jj $hh:$mn:$ss" );
    97     }
     92        $_POST['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
     93        $_POST['post_date_gmt'] = get_gmt_from_date( $_POST['post_date'] );
     94    }
     95
     96    return true;
     97}
     98
     99
     100// Update an existing post with values provided in $_POST.
     101function edit_post() {
     102
     103    $post_ID = (int) $_POST['post_ID'];
     104
     105    if ( 'page' == $_POST['post_type'] ) {
     106        if ( !current_user_can( 'edit_page', $post_ID ) )
     107            wp_die( __('You are not allowed to edit this page.' ));
     108    } else {
     109        if ( !current_user_can( 'edit_post', $post_ID ) )
     110            wp_die( __('You are not allowed to edit this post.' ));
     111    }
     112
     113    // Autosave shouldn't save too soon after a real save
     114    if ( 'autosave' == $_POST['action'] ) {
     115        $post =& get_post( $post_ID );
     116        $now = time();
     117        $then = strtotime($post->post_date_gmt . ' +0000');
     118        $delta = AUTOSAVE_INTERVAL / 2;
     119        if ( ($now - $then) < $delta )
     120            return $post_ID;
     121    }
     122
     123    $translated = _wp_translate_postdata( true );
     124    if ( is_wp_error($translated) )
     125        wp_die( $translated->get_error_message() );
    98126
    99127    // Meta Stuff
     
    237265    }
    238266
    239     // Rename.
    240     $_POST['post_content'] = $_POST['content'];
    241     $_POST['post_excerpt'] = $_POST['excerpt'];
    242     $_POST['post_parent'] = isset($_POST['parent_id'])? $_POST['parent_id'] : '';
    243     $_POST['to_ping'] = $_POST['trackback_url'];
    244 
    245     if (!empty ( $_POST['post_author_override'] ) ) {
    246         $_POST['post_author'] = (int) $_POST['post_author_override'];
    247     } else {
    248         if (!empty ( $_POST['post_author'] ) ) {
    249             $_POST['post_author'] = (int) $_POST['post_author'];
    250         } else {
    251             $_POST['post_author'] = (int) $_POST['user_ID'];
    252         }
    253 
    254     }
    255 
    256     if ( $_POST['post_author'] != $_POST['user_ID'] ) {
    257         if ( 'page' == $_POST['post_type'] ) {
    258             if ( !current_user_can( 'edit_others_pages' ) )
    259                 return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) );
    260         } else {
    261             if ( !current_user_can( 'edit_others_posts' ) )
    262                 return new WP_Error( 'edit_others_posts', __( 'You are not allowed to post as this user.' ) );
    263 
    264         }
    265     }
    266 
    267     // What to do based on which button they pressed
    268     if ( isset($_POST['saveasdraft']) && '' != $_POST['saveasdraft'] )
    269         $_POST['post_status'] = 'draft';
    270     if ( isset($_POST['saveasprivate']) && '' != $_POST['saveasprivate'] )
    271         $_POST['post_status'] = 'private';
    272     if ( isset($_POST['publish']) && ( '' != $_POST['publish'] ) && ( $_POST['post_status'] != 'private' ) )
    273         $_POST['post_status'] = 'publish';
    274     if ( isset($_POST['advanced']) && '' != $_POST['advanced'] )
    275         $_POST['post_status'] = 'draft';
    276 
    277     if ( 'page' == $_POST['post_type'] ) {
    278         if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_pages' ) )
    279             $_POST['post_status'] = 'pending';
    280     } else {
    281         if ('publish' == $_POST['post_status'] && !current_user_can( 'publish_posts' ) )
    282             $_POST['post_status'] = 'pending';
    283     }
    284 
    285     if (!isset( $_POST['comment_status'] ))
    286         $_POST['comment_status'] = 'closed';
    287 
    288     if (!isset( $_POST['ping_status'] ))
    289         $_POST['ping_status'] = 'closed';
    290 
    291     foreach ( array ('aa', 'mm', 'jj', 'hh', 'mn') as $timeunit ) {
    292         if ( !empty( $_POST['hidden_' . $timeunit] ) && $_POST['hidden_' . $timeunit] != $_POST[$timeunit] ) {
    293             $_POST['edit_date'] = '1';
    294             break;
    295         }
    296     }
    297 
    298     if (!empty ( $_POST['edit_date'] ) ) {
    299         $aa = $_POST['aa'];
    300         $mm = $_POST['mm'];
    301         $jj = $_POST['jj'];
    302         $hh = $_POST['hh'];
    303         $mn = $_POST['mn'];
    304         $ss = $_POST['ss'];
    305         $jj = ($jj > 31 ) ? 31 : $jj;
    306         $hh = ($hh > 23 ) ? $hh -24 : $hh;
    307         $mn = ($mn > 59 ) ? $mn -60 : $mn;
    308         $ss = ($ss > 59 ) ? $ss -60 : $ss;
    309         $_POST['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
    310         $_POST['post_date_gmt'] = get_gmt_from_date( $_POST['post_date'] );
    311     }
     267    $translated = _wp_translate_postdata( false );
     268    if ( is_wp_error($translated) )
     269        return $translated;
    312270
    313271    // Create the post.
     
    688646}
    689647
    690 ?>
     648/**
     649 * wp_create_autosave() - creates autosave data for the specified post from $_POST data
     650 *
     651 * @package WordPress
     652 * @subpackage Post Revisions
     653 * @since 2.6
     654 *
     655 * @uses _wp_translate_postdata()
     656 * @uses _wp_revision_fields()
     657 */
     658function wp_create_autosave( $post_id ) {
     659    $translated = _wp_translate_postdata( true );
     660    if ( is_wp_error( $translated ) )
     661        return $translated;
     662
     663    // Only store one autosave.  If there is already an autosave, overwrite it.
     664    if ( $old_autosave = wp_get_autosave( $post_id ) ) {
     665        $new_autosave = _wp_revision_fields( $_POST, true );
     666        $new_autosave['ID'] = $old_autosave->ID;
     667        return wp_update_post( $new_autosave );
     668    }
     669
     670    // Otherwise create the new autosave as a special post revision
     671    return _wp_put_revision( $_POST, true );
     672}
Note: See TracChangeset for help on using the changeset viewer.