4856
* Determines which fields of posts are to be saved in revisions.
4858
* Does two things. If passed a post *array*, it will return a post array ready
4859
* to be inserted into the posts table as a post revision. Otherwise, returns
4860
* an array whose keys are the post fields to be saved for post revisions.
4862
* @package WordPress
4863
* @subpackage Post_Revisions
4866
* @uses apply_filters() Calls '_wp_post_revision_fields' on 'title', 'content' and 'excerpt' fields.
4868
* @param array $post Optional a post array to be processed for insertion as a post revision.
4869
* @param bool $autosave optional Is the revision an autosave?
4870
* @return array Post array ready to be inserted as a post revision or array of fields that can be versioned.
4872
function _wp_post_revision_fields( $post = null, $autosave = false ) {
4873
static $fields = false;
4876
// Allow these to be versioned
4878
'post_title' => __( 'Title' ),
4879
'post_content' => __( 'Content' ),
4880
'post_excerpt' => __( 'Excerpt' ),
4884
$fields = apply_filters( '_wp_post_revision_fields', $fields );
4886
// WP uses these internally either in versioning or elsewhere - they cannot be versioned
4887
foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect )
4888
unset( $fields[$protect] );
4891
if ( !is_array($post) )
4895
foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
4896
$return[$field] = $post[$field];
4898
$return['post_parent'] = $post['ID'];
4899
$return['post_status'] = 'inherit';
4900
$return['post_type'] = 'revision';
4901
$return['post_name'] = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision";
4902
$return['post_date'] = isset($post['post_modified']) ? $post['post_modified'] : '';
4903
$return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';
4909
* Saves an already existing post as a post revision.
4911
* Typically used immediately prior to post updates.
4913
* @package WordPress
4914
* @subpackage Post_Revisions
4917
* @uses _wp_put_post_revision()
4919
* @param int $post_id The ID of the post to save as a revision.
4920
* @return mixed Null or 0 if error, new revision ID, if success.
4922
function wp_save_post_revision( $post_id ) {
4923
// We do autosaves manually with wp_create_post_autosave()
4924
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
4927
// WP_POST_REVISIONS = 0, false
4928
if ( ! WP_POST_REVISIONS )
4931
if ( !$post = get_post( $post_id, ARRAY_A ) )
4934
if ( 'auto-draft' == $post['post_status'] )
4937
if ( !post_type_supports($post['post_type'], 'revisions') )
4940
$return = _wp_put_post_revision( $post );
4942
// WP_POST_REVISIONS = true (default), -1
4943
if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 )
4946
// all revisions and (possibly) one autosave
4947
$revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
4949
// WP_POST_REVISIONS = (int) (# of autosaves to save)
4950
$delete = count($revisions) - WP_POST_REVISIONS;
4955
$revisions = array_slice( $revisions, 0, $delete );
4957
for ( $i = 0; isset($revisions[$i]); $i++ ) {
4958
if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) )
4960
wp_delete_post_revision( $revisions[$i]->ID );
4967
* Retrieve the autosaved data of the specified post.
4969
* Returns a post object containing the information that was autosaved for the
4972
* @package WordPress
4973
* @subpackage Post_Revisions
4976
* @param int $post_id The post ID.
4977
* @return object|bool The autosaved data or false on failure or when no autosave exists.
4979
function wp_get_post_autosave( $post_id ) {
4981
if ( !$post = get_post( $post_id ) )
4985
'name' => "{$post->ID}-autosave",
4986
'post_parent' => $post->ID,
4987
'post_type' => 'revision',
4988
'post_status' => 'inherit'
4991
// Use WP_Query so that the result gets cached
4992
$autosave_query = new WP_Query;
4994
add_action( 'parse_query', '_wp_get_post_autosave_hack' );
4995
$autosave = $autosave_query->query( $q );
4996
remove_action( 'parse_query', '_wp_get_post_autosave_hack' );
4998
if ( $autosave && is_array($autosave) && is_object($autosave[0]) )
4999
return $autosave[0];
5005
* Internally used to hack WP_Query into submission.
5007
* @package WordPress
5008
* @subpackage Post_Revisions
5011
* @param object $query WP_Query object
5013
function _wp_get_post_autosave_hack( $query ) {
5014
$query->is_single = false;
5018
* Determines if the specified post is a revision.
5020
* @package WordPress
5021
* @subpackage Post_Revisions
5024
* @param int|object $post Post ID or post object.
5025
* @return bool|int False if not a revision, ID of revision's parent otherwise.
5027
function wp_is_post_revision( $post ) {
5028
if ( !$post = wp_get_post_revision( $post ) )
5030
return (int) $post->post_parent;
5034
* Determines if the specified post is an autosave.
5036
* @package WordPress
5037
* @subpackage Post_Revisions
5040
* @param int|object $post Post ID or post object.
5041
* @return bool|int False if not a revision, ID of autosave's parent otherwise
5043
function wp_is_post_autosave( $post ) {
5044
if ( !$post = wp_get_post_revision( $post ) )
5046
if ( "{$post->post_parent}-autosave" !== $post->post_name )
5048
return (int) $post->post_parent;
5052
* Inserts post data into the posts table as a post revision.
5054
* @package WordPress
5055
* @subpackage Post_Revisions
5058
* @uses wp_insert_post()
5060
* @param int|object|array $post Post ID, post object OR post array.
5061
* @param bool $autosave Optional. Is the revision an autosave?
5062
* @return mixed Null or 0 if error, new revision ID if success.
5064
function _wp_put_post_revision( $post = null, $autosave = false ) {
5065
if ( is_object($post) )
5066
$post = get_object_vars( $post );
5067
elseif ( !is_array($post) )
5068
$post = get_post($post, ARRAY_A);
5069
if ( !$post || empty($post['ID']) )
5072
if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
5073
return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
5075
$post = _wp_post_revision_fields( $post, $autosave );
5076
$post = add_magic_quotes($post); //since data is from db
5078
$revision_id = wp_insert_post( $post );
5079
if ( is_wp_error($revision_id) )
5080
return $revision_id;
5083
do_action( '_wp_put_post_revision', $revision_id );
5084
return $revision_id;
5088
* Gets a post revision.
5090
* @package WordPress
5091
* @subpackage Post_Revisions
5096
* @param int|object $post Post ID or post object
5097
* @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.
5098
* @param string $filter Optional sanitation filter. @see sanitize_post()
5099
* @return mixed Null if error or post object if success
5101
function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
5103
if ( !$revision = get_post( $post, OBJECT, $filter ) )
5105
if ( 'revision' !== $revision->post_type )
5108
if ( $output == OBJECT ) {
5110
} elseif ( $output == ARRAY_A ) {
5111
$_revision = get_object_vars($revision);
5113
} elseif ( $output == ARRAY_N ) {
5114
$_revision = array_values(get_object_vars($revision));
5122
* Restores a post to the specified revision.
5124
* Can restore a past revision using all fields of the post revision, or only selected fields.
5126
* @package WordPress
5127
* @subpackage Post_Revisions
5130
* @uses wp_get_post_revision()
5131
* @uses wp_update_post()
5132
* @uses do_action() Calls 'wp_restore_post_revision' on post ID and revision ID if wp_update_post()
5135
* @param int|object $revision_id Revision ID or revision object.
5136
* @param array $fields Optional. What fields to restore from. Defaults to all.
5137
* @return mixed Null if error, false if no fields to restore, (int) post ID if success.
5139
function wp_restore_post_revision( $revision_id, $fields = null ) {
5140
if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )
5143
if ( !is_array( $fields ) )
5144
$fields = array_keys( _wp_post_revision_fields() );
5147
foreach( array_intersect( array_keys( $revision ), $fields ) as $field )
5148
$update[$field] = $revision[$field];
5153
$update['ID'] = $revision['post_parent'];
5155
$update = add_magic_quotes( $update ); //since data is from db
5157
$post_id = wp_update_post( $update );
5158
if ( is_wp_error( $post_id ) )
5162
do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
5168
* Deletes a revision.
5170
* Deletes the row from the posts table corresponding to the specified revision.
5172
* @package WordPress
5173
* @subpackage Post_Revisions
5176
* @uses wp_get_post_revision()
5177
* @uses wp_delete_post()
5179
* @param int|object $revision_id Revision ID or revision object.
5180
* @return mixed Null or WP_Error if error, deleted post if success.
5182
function wp_delete_post_revision( $revision_id ) {
5183
if ( !$revision = wp_get_post_revision( $revision_id ) )
5186
$delete = wp_delete_post( $revision->ID );
5187
if ( is_wp_error( $delete ) )
5191
do_action( 'wp_delete_post_revision', $revision->ID, $revision );
5197
* Returns all revisions of specified post.
5199
* @package WordPress
5200
* @subpackage Post_Revisions
5203
* @uses get_children()
5205
* @param int|object $post_id Post ID or post object
5206
* @return array empty if no revisions
5208
function wp_get_post_revisions( $post_id = 0, $args = null ) {
5209
if ( ! WP_POST_REVISIONS )
5211
if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) )
5214
$defaults = array( 'order' => 'DESC', 'orderby' => 'date' );
5215
$args = wp_parse_args( $args, $defaults );
5216
$args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) );
5218
if ( !$revisions = get_children( $args ) )
5223
function _set_preview($post) {
5225
if ( ! is_object($post) )
5228
$preview = wp_get_post_autosave($post->ID);
5230
if ( ! is_object($preview) )
5233
$preview = sanitize_post($preview);
5235
$post->post_content = $preview->post_content;
5236
$post->post_title = $preview->post_title;
5237
$post->post_excerpt = $preview->post_excerpt;
5242
function _show_post_preview() {
5244
if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
5245
$id = (int) $_GET['preview_id'];
5247
if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )
5248
wp_die( __('You do not have permission to preview drafts.') );
5250
add_filter('the_preview', '_set_preview');
5255
4826
* Returns the post's parent's post_ID
5430
* Filters the request to allow for the format prefix.
5435
function _post_format_request( $qvs ) {
5436
if ( ! isset( $qvs['post_format'] ) )
5438
$slugs = get_post_format_slugs();
5439
if ( isset( $slugs[ $qvs['post_format'] ] ) )
5440
$qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];
5441
$tax = get_taxonomy( 'post_format' );
5443
$qvs['post_type'] = $tax->object_type;
5446
add_filter( 'request', '_post_format_request' );
5449
* Filters the post format term link to remove the format prefix.
5454
function _post_format_link( $link, $term, $taxonomy ) {
5456
if ( 'post_format' != $taxonomy )
5458
if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
5459
return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
5461
$link = remove_query_arg( 'post_format', $link );
5462
return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
5465
add_filter( 'term_link', '_post_format_link', 10, 3 );
5468
* Remove the post format prefix from the name property of the term object created by get_term().
5473
function _post_format_get_term( $term ) {
5474
if ( isset( $term->slug ) ) {
5475
$term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
5479
add_filter( 'get_post_format', '_post_format_get_term' );
5482
* Remove the post format prefix from the name property of the term objects created by get_terms().
5487
function _post_format_get_terms( $terms, $taxonomies, $args ) {
5488
if ( in_array( 'post_format', (array) $taxonomies ) ) {
5489
if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
5490
foreach( $terms as $order => $name ) {
5491
$terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
5494
foreach ( (array) $terms as $order => $term ) {
5495
if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
5496
$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
5503
add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );
5506
* Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
5511
function _post_format_wp_get_object_terms( $terms ) {
5512
foreach ( (array) $terms as $order => $term ) {
5513
if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
5514
$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
5519
add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );
5522
4935
* Update the custom taxonomies' term counts when a post's status is changed. For example, default posts term counts (for custom taxonomies) don't include private / draft posts.
5524
4937
* @access private