WordPress.org

Make WordPress Core


Ignore:
Timestamp:
04/18/2008 11:38:21 PM (13 years ago)
Author:
ryan
Message:

Post revisions from mdawaffe. see #6775

File:
1 edited

Legend:

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

    r7746 r7747  
    29912991}
    29922992
     2993/* Post Revisions */
     2994
     2995/**
     2996 * _wp_revision_fields() - determines which fields of posts are to be saved in revisions
     2997 *
     2998 * Does two things. If passed a postn *array*, it will return a post array ready to be
     2999 * insterted into the posts table as a post revision.
     3000 * Otherwise, returns an array whose keys are the post fields to be saved post revisions.
     3001 *
     3002 * @package WordPress
     3003 * @subpackage Post Revisions
     3004 * @since 2.6
     3005 *
     3006 * @param array $post optional a post array to be processed for insertion as a post revision
     3007 * @return array post array ready to be inserted as a post revision or array of fields that can be versioned
     3008 */
     3009function _wp_revision_fields( $post = null ) {
     3010    static $fields = false;
     3011
     3012    if ( !$fields ) {
     3013        // Allow these to be versioned
     3014        $fields = array(
     3015            'post_title' => __( 'Title' ),
     3016            'post_author' => __( 'Author' ),
     3017            'post_content' => __( 'Content' ),
     3018            'post_excerpt' => __( 'Excerpt' ),
     3019        );
     3020
     3021        // WP uses these internally either in versioning or elsewhere - they cannot be versioned
     3022        foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count' ) as $protect )
     3023            unset( $fields[$protect] );
     3024    }
     3025
     3026    if ( !is_array($post) )
     3027        return $fields;
     3028
     3029    $return = array();
     3030    foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
     3031        $return[$field] = $post[$field];
     3032
     3033    $return['post_parent']   = $post['ID'];
     3034    $return['post_status']   = 'inherit';
     3035    $return['post_type']     = 'revision';
     3036    $return['post_name']     = "$post[ID]-revision";
     3037    $return['post_date']     = $post['post_modified'];
     3038    $return['post_date_gmt'] = $post['post_modified_gmt'];
     3039
     3040    return $return;
     3041}
     3042
     3043/**
     3044 * wp_save_revision() - Saves an already existing post as a post revision.  Typically used immediately prior to post updates.
     3045 *
     3046 * @package WordPress
     3047 * @subpackage Post Revisions
     3048 * @since 2.6
     3049 *
     3050 * @uses _wp_put_revision()
     3051 *
     3052 * @param int $post_id The ID of the post to save as a revision
     3053 * @return mixed null or 0 if error, new revision ID if success
     3054 */
     3055function wp_save_revision( $post_id ) {
     3056    // TODO: rework autosave to use special type of post revision
     3057    if ( @constant( 'DOING_AUTOSAVE' ) )
     3058        return;
     3059
     3060    if ( !$post = get_post( $post_id, ARRAY_A ) )
     3061        return;
     3062
     3063    // TODO: open this up for pages also
     3064    if ( 'post' != $post->post_type )
     3065        retun;
     3066
     3067    return _wp_put_revision( $post );
     3068}
     3069
     3070/**
     3071 * _wp_put_revision() - Inserts post data into the posts table as a post revision
     3072 *
     3073 * @package WordPress
     3074 * @subpackage Post Revisions
     3075 * @since 2.6
     3076 *
     3077 * @uses wp_insert_post()
     3078 *
     3079 * @param int|object|array $post post ID, post object OR post array
     3080 * @return mixed null or 0 if error, new revision ID if success
     3081 */
     3082function _wp_put_revision( $post = null ) {
     3083    if ( is_object($post) )
     3084        $post = get_object_vars( $post );
     3085    elseif ( !is_array($post) )
     3086        $post = get_post($post, ARRAY_A);
     3087
     3088    if ( !$post || empty($post['ID']) )
     3089        return;
     3090
     3091    if ( isset($post['post_type']) && 'revision' == $post_post['type'] )
     3092        return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
     3093
     3094    $post = _wp_revision_fields( $post );
     3095
     3096    if ( $revision_id = wp_insert_post( $post ) )
     3097        do_action( '_wp_put_revision', $revision_id );
     3098
     3099    return $revision_id;
     3100}
     3101
     3102/**
     3103 * wp_get_revision() - Gets a post revision
     3104 *
     3105 * @package WordPress
     3106 * @subpackage Post Revisions
     3107 * @since 2.6
     3108 *
     3109 * @uses get_post()
     3110 *
     3111 * @param int|object $post post ID or post object
     3112 * @param $output optional OBJECT, ARRAY_A, or ARRAY_N
     3113 * @param string $filter optional sanitation filter.  @see sanitize_post()
     3114 * @return mixed null if error or post object if success
     3115 */
     3116function &wp_get_revision(&$post, $output = OBJECT, $filter = 'raw') {
     3117    $null = null;
     3118    if ( !$revision = get_post( $post, OBJECT, $filter ) )
     3119        return $revision;
     3120    if ( 'revision' !== $revision->post_type )
     3121        return $null;
     3122
     3123    if ( $output == OBJECT ) {
     3124        return $revision;
     3125    } elseif ( $output == ARRAY_A ) {
     3126        $_revision = get_object_vars($revision);
     3127        return $_revision;
     3128    } elseif ( $output == ARRAY_N ) {
     3129        $_revision = array_values(get_object_vars($revision));
     3130        return $_revision;
     3131    }
     3132
     3133    return $revision;
     3134}
     3135
     3136/**
     3137 * wp_restore_revision() - Restores a post to the specified revision
     3138 *
     3139 * Can restore a past using all fields of the post revision, or only selected fields.
     3140 *
     3141 * @package WordPress
     3142 * @subpackage Post Revisions
     3143 * @since 2.6
     3144 *
     3145 * @uses wp_get_revision()
     3146 * @uses wp_update_post()
     3147 *
     3148 * @param int|object $revision_id revision ID or revision object
     3149 * @param array $fields optional What fields to restore from.  Defaults to all.
     3150 * @return mixed null if error, false if no fields to restore, (int) post ID if success
     3151 */
     3152function wp_restore_revision( $revision_id, $fields = null ) {
     3153    if ( !$revision = wp_get_revision( $revision_id, ARRAY_A ) )
     3154        return $revision;
     3155
     3156    if ( !is_array( $fields ) )
     3157        $fields = array_keys( _wp_revision_fields() );
     3158
     3159    $update = array();
     3160    foreach( array_intersect( array_keys( $revision ), $fields ) as $field )
     3161        $update[$field] = $revision[$field];
     3162
     3163    if ( !$update )
     3164        return false;
     3165
     3166    $update['ID'] = $revision['post_parent'];
     3167
     3168    if ( $post_id = wp_update_post( $update ) )
     3169        do_action( 'wp_restore_revision', $post_id, $revision['ID'] );
     3170
     3171    return $post_id;
     3172}
     3173
     3174/**
     3175 * wp_delete_revision() - Deletes a revision.
     3176 *
     3177 * Deletes the row from the posts table corresponding to the specified revision
     3178 *
     3179 * @package WordPress
     3180 * @subpackage Post Revisions
     3181 * @since 2.6
     3182 *
     3183 * @uses wp_get_revision()
     3184 * @uses wp_delete_post()
     3185 *
     3186 * @param int|object $revision_id revision ID or revision object
     3187 * @param array $fields optional What fields to restore from.  Defaults to all.
     3188 * @return mixed null if error, false if no fields to restore, (int) post ID if success
     3189 */
     3190function wp_delete_revision( $revision_id ) {
     3191    if ( !$revision = wp_get_revision( $revision_id ) )
     3192        return $revision;
     3193
     3194    if ( $delete = wp_delete_post( $revision->ID ) )
     3195        do_action( 'wp_delete_revision', $revision->ID, $revision );
     3196
     3197    return $delete;
     3198}
     3199
     3200/**
     3201 * wp_get_post_revisions() - Returns all revisions of specified post
     3202 *
     3203 * @package WordPress
     3204 * @subpackage Post Revisions
     3205 * @since 2.6
     3206 *
     3207 * @uses get_children()
     3208 *
     3209 * @param int|object $post_id post ID or post object
     3210 * @return array empty if no revisions
     3211 */
     3212function wp_get_post_revisions( $post_id = 0 ) {
     3213    if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) )
     3214        return array();
     3215
     3216    if ( !$revisions = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'revision' ) ) )
     3217        return array();
     3218    return $revisions;
     3219}
     3220
    29933221?>
Note: See TracChangeset for help on using the changeset viewer.