From 0db872b5a1f651a089f9e6aa8edaa612ca49fe3a Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 29 May 2008 22:21:36 +0000 Subject: [PATCH] Post revisions API cleanup from mdawaffe. see #6775 git-svn-id: http://svn.automattic.com/wordpress/trunk@8011 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/admin-ajax.php | 2 +- wp-admin/edit-form-advanced.php | 2 +- wp-admin/edit-page-form.php | 2 +- wp-admin/includes/post.php | 12 ++-- wp-admin/revision.php | 55 +++++++++++------ wp-includes/default-filters.php | 2 +- wp-includes/post-template.php | 25 ++++++-- wp-includes/post.php | 101 +++++++++++++++++++++----------- 8 files changed, 134 insertions(+), 67 deletions(-) diff --git a/wp-admin/admin-ajax.php b/wp-admin/admin-ajax.php index ddb3f306c..aef321fa7 100644 --- a/wp-admin/admin-ajax.php +++ b/wp-admin/admin-ajax.php @@ -519,7 +519,7 @@ case 'autosave' : // The name of this action is hardcoded in edit_post() if ( 'draft' == $post->post_status ) { $id = edit_post(); } else { // Non drafts are not overwritten. The autosave is stored in a special post revision. - $revision_id = wp_create_autosave( $post->ID ); + $revision_id = wp_create_post_autosave( $post->ID ); if ( is_wp_error($revision_id) ) $id = $revision_id; else diff --git a/wp-admin/edit-form-advanced.php b/wp-admin/edit-form-advanced.php index c0dd48e17..88980719f 100644 --- a/wp-admin/edit-form-advanced.php +++ b/wp-admin/edit-form-advanced.php @@ -21,7 +21,7 @@ if ( !isset($post_ID) || 0 == $post_ID ) { $post_ID = (int) $post_ID; $form_action = 'editpost'; $form_extra = ""; - $autosave = wp_get_autosave( $post_id ); + $autosave = wp_get_post_autosave( $post_id ); if ( $autosave && mysql2date( 'U', $autosave->post_modified_gmt ) > mysql2date( 'U', $post->post_modified_gmt ) ) $notice = sprintf( $notices[1], get_edit_post_link( $autosave->ID ) ); } diff --git a/wp-admin/edit-page-form.php b/wp-admin/edit-page-form.php index d808c535e..173d34d21 100644 --- a/wp-admin/edit-page-form.php +++ b/wp-admin/edit-page-form.php @@ -19,7 +19,7 @@ if (!isset($post_ID) || 0 == $post_ID) { $form_action = 'editpost'; $nonce_action = 'update-page_' . $post_ID; $form_extra = ""; - $autosave = wp_get_autosave( $post_id ); + $autosave = wp_get_post_autosave( $post_id ); if ( $autosave && mysql2date( 'U', $autosave->post_modified_gmt ) > mysql2date( 'U', $post->post_modified_gmt ) ) $notice = sprintf( $notices[1], get_edit_post_link( $autosave->ID ) ); } diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index 0f0ccb39f..e8f0dc461 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -649,27 +649,27 @@ function wp_set_post_lock( $post_id ) { } /** - * wp_create_autosave() - creates autosave data for the specified post from $_POST data + * wp_create_post_autosave() - creates autosave data for the specified post from $_POST data * * @package WordPress * @subpackage Post Revisions * @since 2.6 * * @uses _wp_translate_postdata() - * @uses _wp_revision_fields() + * @uses _wp_post_revision_fields() */ -function wp_create_autosave( $post_id ) { +function wp_create_post_autosave( $post_id ) { $translated = _wp_translate_postdata( true ); if ( is_wp_error( $translated ) ) return $translated; // Only store one autosave. If there is already an autosave, overwrite it. - if ( $old_autosave = wp_get_autosave( $post_id ) ) { - $new_autosave = _wp_revision_fields( $_POST, true ); + if ( $old_autosave = wp_get_post_autosave( $post_id ) ) { + $new_autosave = _wp_post_revision_fields( $_POST, true ); $new_autosave['ID'] = $old_autosave->ID; return wp_update_post( $new_autosave ); } // Otherwise create the new autosave as a special post revision - return _wp_put_revision( $_POST, true ); + return _wp_put_post_revision( $_POST, true ); } diff --git a/wp-admin/revision.php b/wp-admin/revision.php index b5e0d31f0..f7f627569 100644 --- a/wp-admin/revision.php +++ b/wp-admin/revision.php @@ -2,11 +2,6 @@ require_once('admin.php'); -if ( !constant('WP_POST_REVISIONS') ) { - wp_redirect( 'edit.php' ); - exit; -} - wp_reset_vars(array('revision', 'left', 'right', 'action')); $revision_id = absint($revision); $diff = absint($diff); @@ -18,19 +13,25 @@ $parent_file = $redirect = 'edit.php'; switch ( $action ) : case 'delete' : // stubs case 'edit' : - $redirect = remove_query_arg( 'action' ); + if ( constant('WP_POST_REVISIONS') ) // stub + $redirect = remove_query_arg( 'action' ); + else // Revisions disabled + $redirect = 'edit.php'; break; case 'restore' : - if ( !current_user_can( 'edit_post', $revision->post_parent ) ) + if ( !$revision = wp_get_post_revision( $revision_id ) ) break; - if ( !$revision = wp_get_revision( $revision_id ) ) + if ( !current_user_can( 'edit_post', $revision->post_parent ) ) break; if ( !$post = get_post( $revision->post_parent ) ) break; + if ( !constant('WP_POST_REVISIONS') && !wp_is_post_autosave( $revision ) ) // Revisions disabled and we're not looking at an autosave + break; + check_admin_referer( "restore-post_$post->ID|$revision->ID" ); - wp_restore_revision( $revision->ID ); + wp_restore_post_revision( $revision->ID ); $redirect = add_query_arg( array( 'message' => 5, 'revision' => $revision->ID ), get_edit_post_link( $post->ID, 'url' ) ); break; case 'diff' : @@ -57,12 +58,23 @@ case 'diff' : else break; // Don't diff two unrelated revisions + if ( !constant('WP_POST_REVISIONS') ) { // Revisions disabled + if ( + // we're not looking at an autosave + ( !wp_is_post_autosave( $left_revision ) && !wp_is_post_autosave( $right_revision ) ) + || + // we're not comparing an autosave to the current post + ( $post->ID !== $left_revision->ID && $post->ID !== $right_revision->ID ) + ) + break; + } + if ( // They're the same $left_revision->ID == $right_revision->ID || // Neither is a revision - ( !wp_get_revision( $left_revision->ID ) && !wp_get_revision( $right_revision->ID ) ) + ( !wp_get_post_revision( $left_revision->ID ) && !wp_get_post_revision( $right_revision->ID ) ) ) break; @@ -76,7 +88,7 @@ case 'diff' : break; case 'view' : default : - if ( !$revision = wp_get_revision( $revision_id ) ) + if ( !$revision = wp_get_post_revision( $revision_id ) ) break; if ( !$post = get_post( $revision->post_parent ) ) break; @@ -84,6 +96,9 @@ default : if ( !current_user_can( 'read_post', $revision->ID ) || !current_user_can( 'read_post', $post->ID ) ) break; + if ( !constant('WP_POST_REVISIONS') && !wp_is_post_autosave( $revision ) ) // Revisions disabled and we're not looking at an autosave + break; + $post_title = '' . get_the_title() . ''; $revision_title = wp_post_revision_title( $revision, false ); $h2 = sprintf( __( 'Post Revision for “%1$s” created on %2$s' ), $post_title, $revision_title ); @@ -113,7 +128,7 @@ if ( 'page' == $post->post_type ) { } // Converts post_author ID# into name -add_filter( '_wp_revision_field_post_author', 'get_author_name' ); +add_filter( '_wp_post_revision_field_post_author', 'get_author_name' ); require_once( 'admin-header.php' ); @@ -137,16 +152,16 @@ require_once( 'admin-header.php' ); // use get_post_to_edit filters? $identical = true; -foreach ( _wp_revision_fields() as $field => $field_title ) : +foreach ( _wp_post_revision_fields() as $field => $field_title ) : if ( 'diff' == $action ) { - $left_content = apply_filters( "_wp_revision_field_$field", $left_revision->$field, $field ); - $right_content = apply_filters( "_wp_revision_field_$field", $right_revision->$field, $field ); + $left_content = apply_filters( "_wp_post_revision_field_$field", $left_revision->$field, $field ); + $right_content = apply_filters( "_wp_post_revision_field_$field", $right_revision->$field, $field ); if ( !$content = wp_text_diff( $left_content, $right_content ) ) continue; // There is no difference between left and right $identical = false; } else { - add_filter( "_wp_revision_field_$field", 'htmlspecialchars' ); - $content = apply_filters( "_wp_revision_field_$field", $revision->$field, $field ); + add_filter( "_wp_post_revision_field_$field", 'htmlspecialchars' ); + $content = apply_filters( "_wp_post_revision_field_$field", $revision->$field, $field ); } ?> @@ -179,6 +194,10 @@ endif; 'form-table', 'parent' => true, 'right' => $right, 'left' => $left ) ); +$args = array( 'format' => 'form-table', 'parent' => true, 'right' => $right, 'left' => $left ); +if ( !constant( 'WP_POST_REVISIONS' ) ) + $args['type'] = 'autosave'; + +wp_list_post_revisions( $post, $args ); require_once( 'admin-footer.php' ); diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index fb6d6ed1e..bba6608c3 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -179,7 +179,7 @@ add_action('admin_print_styles', 'wp_print_styles', 20); add_action('init', 'smilies_init', 5); add_action( 'plugins_loaded', 'wp_maybe_load_widgets', 0 ); add_action( 'shutdown', 'wp_ob_end_flush_all', 1); -add_action( 'pre_post_update', 'wp_save_revision' ); +add_action( 'pre_post_update', 'wp_save_post_revision' ); add_action('publish_post', '_publish_post_hook', 5, 1); add_action('future_post', '_future_post_hook', 5, 2); add_action('future_page', '_future_post_hook', 5, 2); diff --git a/wp-includes/post-template.php b/wp-includes/post-template.php index 34c735c91..30bf02628 100644 --- a/wp-includes/post-template.php +++ b/wp-includes/post-template.php @@ -593,9 +593,9 @@ function wp_post_revision_title( $revision, $link = true ) { if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) ) $date = "$date"; - if ( 'revision' != $revision->post_type ) + if ( !wp_is_post_revision( $revision ) ) $date = sprintf( $currentf, $date ); - elseif ( "{$revision->post_parent}-autosave" == $revision->post_name ) + elseif ( wp_is_post_autosave( $revision ) ) $date = sprintf( $autosavef, $date ); return $date; @@ -628,12 +628,23 @@ function wp_list_post_revisions( $post_id = 0, $args = null ) { // TODO? split i if ( !$post = get_post( $post_id ) ) return; - if ( !$revisions = wp_get_post_revisions( $post->ID ) ) - return; - - $defaults = array( 'parent' => false, 'right' => false, 'left' => false, 'format' => 'list' ); + $defaults = array( 'parent' => false, 'right' => false, 'left' => false, 'format' => 'list', 'type' => 'all' ); extract( wp_parse_args( $args, $defaults ), EXTR_SKIP ); + switch ( $type ) { + case 'autosave' : + if ( !$autosave = wp_get_post_autosave( $post->ID ) ) + return; + $revisions = array( $autosave ); + break; + case 'revision' : // just revisions - remove autosave later + case 'all' : + default : + if ( !$revisions = wp_get_post_revisions( $post->ID ) ) + return; + break; + } + $titlef = _c( '%1$s by %2$s|post revision 1:datetime, 2:name' ); if ( $parent ) @@ -645,6 +656,8 @@ function wp_list_post_revisions( $post_id = 0, $args = null ) { // TODO? split i foreach ( $revisions as $revision ) { if ( !current_user_can( 'read_post', $revision->ID ) ) continue; + if ( 'revision' === $type && wp_is_post_autosave( $revision ) ) + continue; $date = wp_post_revision_title( $revision ); $name = get_author_name( $revision->post_author ); diff --git a/wp-includes/post.php b/wp-includes/post.php index aa12e622f..6b53b485b 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -956,9 +956,9 @@ function wp_delete_post($postid = 0) { // Do raw query. wp_get_post_revisions() is filtered $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) ); - // Use wp_delete_post (via wp_delete_revision) again. Ensures any meta/misplaced data gets cleaned up. + // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. foreach ( $revision_ids as $revision_id ) - wp_delete_revision( $revision_id ); + wp_delete_post_revision( $revision_id ); // Point all attachments to this post up one level $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); @@ -2951,7 +2951,7 @@ function _get_post_ancestors(&$_post) { /* Post Revisions */ /** - * _wp_revision_fields() - determines which fields of posts are to be saved in revisions + * _wp_post_revision_fields() - determines which fields of posts are to be saved in revisions * * Does two things. If passed a post *array*, it will return a post array ready to be * insterted into the posts table as a post revision. @@ -2965,7 +2965,7 @@ function _get_post_ancestors(&$_post) { * @param bool $autosave optional Is the revision an autosave? * @return array post array ready to be inserted as a post revision or array of fields that can be versioned */ -function _wp_revision_fields( $post = null, $autosave = false ) { +function _wp_post_revision_fields( $post = null, $autosave = false ) { static $fields = false; if ( !$fields ) { @@ -2978,7 +2978,7 @@ function _wp_revision_fields( $post = null, $autosave = false ) { ); // Runs only once - $fields = apply_filters( '_wp_revision_fields', $fields ); + $fields = apply_filters( '_wp_post_revision_fields', $fields ); // WP uses these internally either in versioning or elsewhere - they cannot be versioned foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count' ) as $protect ) @@ -3003,19 +3003,19 @@ function _wp_revision_fields( $post = null, $autosave = false ) { } /** - * wp_save_revision() - Saves an already existing post as a post revision. Typically used immediately prior to post updates. + * wp_save_post_revision() - Saves an already existing post as a post revision. Typically used immediately prior to post updates. * * @package WordPress * @subpackage Post Revisions * @since 2.6 * - * @uses _wp_put_revision() + * @uses _wp_put_post_revision() * * @param int $post_id The ID of the post to save as a revision * @return mixed null or 0 if error, new revision ID if success */ -function wp_save_revision( $post_id ) { - // We do autosaves manually with wp_create_autosave() +function wp_save_post_revision( $post_id ) { + // We do autosaves manually with wp_create_post_autosave() if ( @constant( 'DOING_AUTOSAVE' ) ) return; @@ -3029,7 +3029,7 @@ function wp_save_revision( $post_id ) { if ( !in_array( $post['post_type'], array( 'post', 'page' ) ) ) return; - $return = _wp_put_revision( $post ); + $return = _wp_put_post_revision( $post ); // WP_POST_REVISIONS = true (default), -1 if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 ) @@ -3049,14 +3049,14 @@ function wp_save_revision( $post_id ) { for ( $i = 0; isset($revisions[$i]); $i++ ) { if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) ) continue; - wp_delete_revision( $revisions[$i]->ID ); + wp_delete_post_revision( $revisions[$i]->ID ); } return $return; } /** - * wp_get_autosave() - returns the autosaved data of the specified post. + * wp_get_post_autosave() - returns the autosaved data of the specified post. * * Returns a post object containing the information that was autosaved for the specified post. * @@ -3067,7 +3067,7 @@ function wp_save_revision( $post_id ) { * @param int $post_id The post ID * @return object|bool the autosaved data or false on failure or when no autosave exists */ -function wp_get_autosave( $post_id ) { +function wp_get_post_autosave( $post_id ) { global $wpdb; if ( !$post = get_post( $post_id ) ) return false; @@ -3082,9 +3082,9 @@ function wp_get_autosave( $post_id ) { // Use WP_Query so that the result gets cached $autosave_query = new WP_Query; - add_action( 'parse_query', '_wp_get_autosave_hack' ); + add_action( 'parse_query', '_wp_get_post_autosave_hack' ); $autosave = $autosave_query->query( $q ); - remove_action( 'parse_query', '_wp_get_autosave_hack' ); + remove_action( 'parse_query', '_wp_get_post_autosave_hack' ); if ( $autosave && is_array($autosave) && is_object($autosave[0]) ) return $autosave[0]; @@ -3093,12 +3093,47 @@ function wp_get_autosave( $post_id ) { } // Internally used to hack WP_Query into submission -function _wp_get_autosave_hack( $query ) { +function _wp_get_post_autosave_hack( $query ) { $query->is_single = false; } + /** - * _wp_put_revision() - Inserts post data into the posts table as a post revision + * wp_is_post_revision() - Determines if the specified post is a revision. + * + * @package WordPress + * @subpackage Post Revisions + * @since 2.6 + * + * @param int|object $post post ID or post object + * @return bool|int false if not a revision, ID of revision's parent otherwise + */ +function wp_is_post_revision( $post ) { + if ( !$post = wp_get_post_revision( $post ) ) + return false; + return (int) $post->post_parent; +} + +/** + * wp_is_post_autosave() - Determines if the specified post is an autosave. + * + * @package WordPress + * @subpackage Post Revisions + * @since 2.6 + * + * @param int|object $post post ID or post object + * @return bool|int false if not a revision, ID of autosave's parent otherwise + */ +function wp_is_post_autosave( $post ) { + if ( !$post = wp_get_post_revision( $post ) ) + return false; + if ( "{$post->post_parent}-autosave" !== $post->post_name ) + return false; + return (int) $post->post_parent; +} + +/** + * _wp_put_post_revision() - Inserts post data into the posts table as a post revision * * @package WordPress * @subpackage Post Revisions @@ -3110,7 +3145,7 @@ function _wp_get_autosave_hack( $query ) { * @param bool $autosave optional Is the revision an autosave? * @return mixed null or 0 if error, new revision ID if success */ -function _wp_put_revision( $post = null, $autosave = false ) { +function _wp_put_post_revision( $post = null, $autosave = false ) { if ( is_object($post) ) $post = get_object_vars( $post ); elseif ( !is_array($post) ) @@ -3121,19 +3156,19 @@ function _wp_put_revision( $post = null, $autosave = false ) { if ( isset($post['post_type']) && 'revision' == $post_post['type'] ) return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) ); - $post = _wp_revision_fields( $post, $autosave ); + $post = _wp_post_revision_fields( $post, $autosave ); $revision_id = wp_insert_post( $post ); if ( is_wp_error($revision_id) ) return $revision_id; if ( $revision_id ) - do_action( '_wp_put_revision', $revision_id ); + do_action( '_wp_put_post_revision', $revision_id ); return $revision_id; } /** - * wp_get_revision() - Gets a post revision + * wp_get_post_revision() - Gets a post revision * * @package WordPress * @subpackage Post Revisions @@ -3146,7 +3181,7 @@ function _wp_put_revision( $post = null, $autosave = false ) { * @param string $filter optional sanitation filter. @see sanitize_post() * @return mixed null if error or post object if success */ -function &wp_get_revision(&$post, $output = OBJECT, $filter = 'raw') { +function &wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') { $null = null; if ( !$revision = get_post( $post, OBJECT, $filter ) ) return $revision; @@ -3167,7 +3202,7 @@ function &wp_get_revision(&$post, $output = OBJECT, $filter = 'raw') { } /** - * wp_restore_revision() - Restores a post to the specified revision + * wp_restore_post_revision() - Restores a post to the specified revision * * Can restore a past using all fields of the post revision, or only selected fields. * @@ -3175,19 +3210,19 @@ function &wp_get_revision(&$post, $output = OBJECT, $filter = 'raw') { * @subpackage Post Revisions * @since 2.6 * - * @uses wp_get_revision() + * @uses wp_get_post_revision() * @uses wp_update_post() * * @param int|object $revision_id revision ID or revision object * @param array $fields optional What fields to restore from. Defaults to all. * @return mixed null if error, false if no fields to restore, (int) post ID if success */ -function wp_restore_revision( $revision_id, $fields = null ) { - if ( !$revision = wp_get_revision( $revision_id, ARRAY_A ) ) +function wp_restore_post_revision( $revision_id, $fields = null ) { + if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) ) return $revision; if ( !is_array( $fields ) ) - $fields = array_keys( _wp_revision_fields() ); + $fields = array_keys( _wp_post_revision_fields() ); $update = array(); foreach( array_intersect( array_keys( $revision ), $fields ) as $field ) @@ -3203,13 +3238,13 @@ function wp_restore_revision( $revision_id, $fields = null ) { return $post_id; if ( $post_id ) - do_action( 'wp_restore_revision', $post_id, $revision['ID'] ); + do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] ); return $post_id; } /** - * wp_delete_revision() - Deletes a revision. + * wp_delete_post_revision() - Deletes a revision. * * Deletes the row from the posts table corresponding to the specified revision * @@ -3217,15 +3252,15 @@ function wp_restore_revision( $revision_id, $fields = null ) { * @subpackage Post Revisions * @since 2.6 * - * @uses wp_get_revision() + * @uses wp_get_post_revision() * @uses wp_delete_post() * * @param int|object $revision_id revision ID or revision object * @param array $fields optional What fields to restore from. Defaults to all. * @return mixed null if error, false if no fields to restore, (int) post ID if success */ -function wp_delete_revision( $revision_id ) { - if ( !$revision = wp_get_revision( $revision_id ) ) +function wp_delete_post_revision( $revision_id ) { + if ( !$revision = wp_get_post_revision( $revision_id ) ) return $revision; $delete = wp_delete_post( $revision->ID ); @@ -3233,7 +3268,7 @@ function wp_delete_revision( $revision_id ) { return $delete; if ( $delete ) - do_action( 'wp_delete_revision', $revision->ID, $revision ); + do_action( 'wp_delete_post_revision', $revision->ID, $revision ); return $delete; }