Make WordPress Core


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

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

File:
1 edited

Legend:

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

    r7900 r7907  
    8989
    9090    $children = get_posts( $r );
    91 
    9291    if ( !$children )
    9392        return false;
     
    29582957 * _wp_revision_fields() - determines which fields of posts are to be saved in revisions
    29592958 *
    2960  * Does two things. If passed a postn *array*, it will return a post array ready to be
     2959 * Does two things. If passed a post *array*, it will return a post array ready to be
    29612960 * insterted into the posts table as a post revision.
    2962  * Otherwise, returns an array whose keys are the post fields to be saved post revisions.
     2961 * Otherwise, returns an array whose keys are the post fields to be saved for post revisions.
    29632962 *
    29642963 * @package WordPress
     
    29672966 *
    29682967 * @param array $post optional a post array to be processed for insertion as a post revision
     2968 * @param bool $autosave optional Is the revision an autosave?
    29692969 * @return array post array ready to be inserted as a post revision or array of fields that can be versioned
    29702970 */
    2971 function _wp_revision_fields( $post = null ) {
     2971function _wp_revision_fields( $post = null, $autosave = false ) {
    29722972    static $fields = false;
    29732973
     
    29812981        );
    29822982
     2983        // Runs only once
     2984        $fields = apply_filters( '_wp_revision_fields', $fields );
     2985
    29832986        // WP uses these internally either in versioning or elsewhere - they cannot be versioned
    29842987        foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count' ) as $protect )
     
    29962999    $return['post_status']   = 'inherit';
    29973000    $return['post_type']     = 'revision';
    2998     $return['post_name']     = "$post[ID]-revision";
     3001    $return['post_name']     = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision";
    29993002    $return['post_date']     = $post['post_modified'];
    30003003    $return['post_date_gmt'] = $post['post_modified_gmt'];
     
    30163019 */
    30173020function wp_save_revision( $post_id ) {
    3018     // TODO: rework autosave to use special type of post revision
     3021    // We do autosaves manually with wp_create_autosave()
    30193022    if ( @constant( 'DOING_AUTOSAVE' ) )
    30203023        return;
     
    30233026        return;
    30243027
    3025     // TODO: open this up for pages also
    3026     if ( 'post' != $post->post_type )
     3028    if ( !in_array( $post['post_type'], array( 'post', 'page' ) ) )
    30273029        return;
    30283030
     
    30313033
    30323034/**
    3033  * _wp_put_revision() - Inserts post data into the posts table as a post revision
     3035 * wp_get_autosave() - returns the autosaved data of the specified post.
     3036 *
     3037 * Returns a post object containing the information that was autosaved for the specified post.
    30343038 *
    30353039 * @package WordPress
     
    30373041 * @since 2.6
    30383042 *
     3043 * @param int $post_id The post ID
     3044 * @return object|bool the autosaved data or false on failure or when no autosave exists
     3045 */
     3046function wp_get_autosave( $post_id ) {
     3047    global $wpdb;
     3048    if ( !$post = get_post( $post_id ) )
     3049        return false;
     3050
     3051    $q = array(
     3052        'name' => "{$post->ID}-autosave",
     3053        'post_parent' => $post->ID,
     3054        'post_type' => 'revision',
     3055        'post_status' => 'inherit'
     3056    );
     3057
     3058    // Use WP_Query so that the result gets cached
     3059    $autosave_query = new WP_Query;
     3060
     3061    add_action( 'parse_query', '_wp_get_autosave_hack' );
     3062    $autosave = $autosave_query->query( $q );
     3063    remove_action( 'parse_query', '_wp_get_autosave_hack' );
     3064
     3065    if ( $autosave && is_array($autosave) && is_object($autosave[0]) )
     3066        return $autosave[0];
     3067
     3068    return false;
     3069}
     3070
     3071// Internally used to hack WP_Query into submission
     3072function _wp_get_autosave_hack( $query ) {
     3073    $query->is_single = false;
     3074}
     3075
     3076/**
     3077 * _wp_put_revision() - Inserts post data into the posts table as a post revision
     3078 *
     3079 * @package WordPress
     3080 * @subpackage Post Revisions
     3081 * @since 2.6
     3082 *
    30393083 * @uses wp_insert_post()
    30403084 *
    30413085 * @param int|object|array $post post ID, post object OR post array
     3086 * @param bool $autosave optional Is the revision an autosave?
    30423087 * @return mixed null or 0 if error, new revision ID if success
    30433088 */
    3044 function _wp_put_revision( $post = null ) {
     3089function _wp_put_revision( $post = null, $autosave = false ) {
    30453090    if ( is_object($post) )
    30463091        $post = get_object_vars( $post );
    30473092    elseif ( !is_array($post) )
    30483093        $post = get_post($post, ARRAY_A);
    3049 
    30503094    if ( !$post || empty($post['ID']) )
    30513095        return;
     
    30543098        return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
    30553099
    3056     $post = _wp_revision_fields( $post );
    3057 
    3058     if ( $revision_id = wp_insert_post( $post ) )
     3100    $post = _wp_revision_fields( $post, $autosave );
     3101
     3102    $revision_id = wp_insert_post( $post );
     3103    if ( is_wp_error($revision_id) )
     3104        return $revision_id;
     3105
     3106    if ( $revision_id )
    30593107        do_action( '_wp_put_revision', $revision_id );
    3060 
    30613108    return $revision_id;
    30623109}
     
    31283175    $update['ID'] = $revision['post_parent'];
    31293176
    3130     if ( $post_id = wp_update_post( $update ) )
     3177    $post_id = wp_update_post( $update );
     3178    if ( is_wp_error( $post_id ) )
     3179        return $post_id;
     3180
     3181    if ( $post_id )
    31313182        do_action( 'wp_restore_revision', $post_id, $revision['ID'] );
    31323183
     
    31543205        return $revision;
    31553206
    3156     if ( $delete = wp_delete_post( $revision->ID ) )
     3207    $delete = wp_delete_post( $revision->ID );
     3208    if ( is_wp_error( $delete ) )
     3209        return $delete;
     3210
     3211    if ( $delete )
    31573212        do_action( 'wp_delete_revision', $revision->ID, $revision );
    31583213
     
    31753230    if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) )
    31763231        return array();
    3177 
    3178     if ( !$revisions = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'revision' ) ) )
     3232    if ( !$revisions = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) ) )
    31793233        return array();
    31803234    return $revisions;
    31813235}
    3182 
    3183 ?>
Note: See TracChangeset for help on using the changeset viewer.