wp/wp-includes/revision.php
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Post revision functions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @subpackage Post_Revisions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 * Determines which fields of posts are to be saved in revisions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 * Does two things. If passed a post *array*, it will return a post array ready
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 * to be inserted into the posts table as a post revision. Otherwise, returns
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 * an array whose keys are the post fields to be saved for post revisions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 * @param array $post Optional a post array to be processed for insertion as a post revision.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 * @param bool $autosave optional Is the revision an autosave?
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 * @return array Post array ready to be inserted as a post revision or array of fields that can be versioned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
function _wp_post_revision_fields( $post = null, $autosave = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
	static $fields = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
	if ( !$fields ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
		// Allow these to be versioned
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
		$fields = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
			'post_title' => __( 'Title' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
			'post_content' => __( 'Content' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
			'post_excerpt' => __( 'Excerpt' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    34
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
		 * Filter the list of fields saved in post revisions.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    36
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    37
		 * Included by default: 'post_title', 'post_content' and 'post_excerpt'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    38
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    39
		 * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    40
		 * 'post_date_gmt', 'post_status', 'post_type', 'comment_count',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    41
		 * and 'post_author'.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    42
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    43
		 * @since 2.6.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    44
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    45
		 * @param array $fields List of fields to revision. Contains 'post_title',
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    46
		 *                      'post_content', and 'post_excerpt' by default.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    47
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
		$fields = apply_filters( '_wp_post_revision_fields', $fields );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
		// WP uses these internally either in versioning or elsewhere - they cannot be versioned
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
		foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
			unset( $fields[$protect] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
	if ( !is_array($post) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
		return $fields;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
	$return = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
	foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
		$return[$field] = $post[$field];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
	$return['post_parent']   = $post['ID'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
	$return['post_status']   = 'inherit';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
	$return['post_type']     = 'revision';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
	$return['post_name']     = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
	$return['post_date']     = isset($post['post_modified']) ? $post['post_modified'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
	$return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
	return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    73
 * Creates a revision for the current version of a post.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    75
 * Typically used immediately after a post update, as every update is a revision,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    76
 * and the most recent revision always matches the current post.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    80
 * @param  int $post_id The ID of the post to save as a revision.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    81
 * @return null|int Null or 0 if error, new revision ID, if success.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
function wp_save_post_revision( $post_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
	if ( ! $post = get_post( $post_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
	if ( ! post_type_supports( $post->post_type, 'revisions' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
	if ( 'auto-draft' == $post->post_status )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
	if ( ! wp_revisions_enabled( $post ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
	// Compare the proposed update with the last stored revision verifying that
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
	// they are different, unless a plugin tells us to always save regardless.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
	// If no previous revisions, save one
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
	if ( $revisions = wp_get_post_revisions( $post_id ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
		// grab the last revision, but not an autosave
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
		foreach ( $revisions as $revision ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
			if ( false !== strpos( $revision->post_name, "{$revision->post_parent}-revision" ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
				$last_revision = $revision;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   111
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
		 * Filter whether the post has changed since the last revision.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   113
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   114
		 * By default a revision is saved only if one of the revisioned fields has changed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   115
		 * This filter can override that so a revision is saved even if nothing has changed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   116
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   117
		 * @since 3.6.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   118
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   119
		 * @param bool    $check_for_changes Whether to check for changes before saving a new revision.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   120
		 *                                   Default true.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   121
		 * @param WP_Post $last_revision     The the last revision post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   122
		 * @param WP_Post $post              The post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   123
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   124
		 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   125
		if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
			$post_has_changed = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
			foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
				if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
					$post_has_changed = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
			}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   134
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   135
			/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   136
			 * Filter whether a post has changed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   137
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   138
			 * By default a revision is saved only if one of the revisioned fields has changed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   139
			 * This filter allows for additional checks to determine if there were changes.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   140
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   141
			 * @since 4.1.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   142
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   143
			 * @param bool    $post_has_changed Whether the post has changed.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   144
			 * @param WP_Post $last_revision    The last revision post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   145
			 * @param WP_Post $post             The post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   146
			 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   147
			 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   148
			$post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   149
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
			//don't save revision if post unchanged
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   151
			if( ! $post_has_changed ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
				return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   153
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
	$return = _wp_put_post_revision( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   159
	// If a limit for the number of revisions to keep has been set,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   160
	// delete the oldest ones.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
	$revisions_to_keep = wp_revisions_to_keep( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
	if ( $revisions_to_keep < 0 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
		return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
	$revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
	$delete = count($revisions) - $revisions_to_keep;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
	if ( $delete < 1 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
		return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
	$revisions = array_slice( $revisions, 0, $delete );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
	for ( $i = 0; isset( $revisions[$i] ); $i++ ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
		if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
		wp_delete_post_revision( $revisions[ $i ]->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
	return $return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
 * Retrieve the autosaved data of the specified post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
 * Returns a post object containing the information that was autosaved for the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
 * specified post. If the optional $user_id is passed, returns the autosave for that user
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
 * otherwise returns the latest autosave.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
 * @param int $post_id The post ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
 * @param int $user_id optional The post author ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
 * @return object|bool The autosaved data or false on failure or when no autosave exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
function wp_get_post_autosave( $post_id, $user_id = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
	$revisions = wp_get_post_revisions( $post_id, array( 'check_enabled' => false ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
	foreach ( $revisions as $revision ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
		if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
			if ( $user_id && $user_id != $revision->post_author )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
				continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
			return $revision;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
 * Determines if the specified post is a revision.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
 * @param int|object $post Post ID or post object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
 * @return bool|int False if not a revision, ID of revision's parent otherwise.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
function wp_is_post_revision( $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
	if ( !$post = wp_get_post_revision( $post ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
	return (int) $post->post_parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
 * Determines if the specified post is an autosave.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
 * @param int|object $post Post ID or post object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
 * @return bool|int False if not a revision, ID of autosave's parent otherwise
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
function wp_is_post_autosave( $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
	if ( !$post = wp_get_post_revision( $post ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
	if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
		return (int) $post->post_parent;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
 * Inserts post data into the posts table as a post revision.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
 * @param int|object|array $post Post ID, post object OR post array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
 * @param bool $autosave Optional. Is the revision an autosave?
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   254
 * @return mixed WP_Error or 0 if error, new revision ID if success.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
function _wp_put_post_revision( $post = null, $autosave = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
	if ( is_object($post) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
		$post = get_object_vars( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
	elseif ( !is_array($post) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
		$post = get_post($post, ARRAY_A);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   262
	if ( ! $post || empty($post['ID']) )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   263
		return new WP_Error( 'invalid_post', __( 'Invalid post ID' ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
	if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
		return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
	$post = _wp_post_revision_fields( $post, $autosave );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
	$post = wp_slash($post); //since data is from db
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
	$revision_id = wp_insert_post( $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
	if ( is_wp_error($revision_id) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
		return $revision_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   275
	if ( $revision_id ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   276
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
		 * Fires once a revision has been saved.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   278
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
		 * @since 2.6.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   280
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   281
		 * @param int $revision_id Post revision ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   282
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
		do_action( '_wp_put_post_revision', $revision_id );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   284
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
	return $revision_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
 * Gets a post revision.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
 * @param int|object $post The post ID or object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
 * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
 * @param string $filter Optional sanitation filter. @see sanitize_post().
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
 * @return mixed Null if error or post object if success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
	if ( !$revision = get_post( $post, OBJECT, $filter ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
		return $revision;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
	if ( 'revision' !== $revision->post_type )
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   303
		return null;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
	if ( $output == OBJECT ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
		return $revision;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
	} elseif ( $output == ARRAY_A ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
		$_revision = get_object_vars($revision);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
		return $_revision;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
	} elseif ( $output == ARRAY_N ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
		$_revision = array_values(get_object_vars($revision));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
		return $_revision;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
	return $revision;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
 * Restores a post to the specified revision.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
 * Can restore a past revision using all fields of the post revision, or only selected fields.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
 * @param int|object $revision_id Revision ID or revision object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
 * @param array $fields Optional. What fields to restore from. Defaults to all.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
 * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
function wp_restore_post_revision( $revision_id, $fields = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
	if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
		return $revision;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
	if ( !is_array( $fields ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
		$fields = array_keys( _wp_post_revision_fields() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
	$update = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
	foreach( array_intersect( array_keys( $revision ), $fields ) as $field ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
		$update[$field] = $revision[$field];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
	if ( !$update )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
	$update['ID'] = $revision['post_parent'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
	$update = wp_slash( $update ); //since data is from db
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
	$post_id = wp_update_post( $update );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
	if ( ! $post_id || is_wp_error( $post_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
		return $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
	// Add restore from details
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
	$restore_details = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
		'restored_revision_id' => $revision_id,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
		'restored_by_user'     => get_current_user_id(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
		'restored_time'        => time()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
	update_post_meta( $post_id, '_post_restored_from', $restore_details );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
	// Update last edit user
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
	update_post_meta( $post_id, '_edit_last', get_current_user_id() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   363
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   364
	 * Fires after a post revision has been restored.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   365
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   366
	 * @since 2.6.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   367
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   368
	 * @param int $post_id     Post ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   369
	 * @param int $revision_id Post revision ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   370
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
	do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
	return $post_id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
 * Deletes a revision.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
 * Deletes the row from the posts table corresponding to the specified revision.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
 * @param int|object $revision_id Revision ID or revision object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
 * @return mixed Null or WP_Error if error, deleted post if success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
function wp_delete_post_revision( $revision_id ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
	if ( !$revision = wp_get_post_revision( $revision_id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
		return $revision;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
	$delete = wp_delete_post( $revision->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
	if ( is_wp_error( $delete ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
		return $delete;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   394
	if ( $delete ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   395
		/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   396
		 * Fires once a post revision has been deleted.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   397
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   398
		 * @since 2.6.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   399
		 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   400
		 * @param int          $revision_id Post revision ID.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   401
		 * @param object|array $revision    Post revision object or array.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   402
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
		do_action( 'wp_delete_post_revision', $revision->ID, $revision );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   404
	}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
	return $delete;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
 * Returns all revisions of specified post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   414
 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
 * @return array An array of revisions, or an empty array if none.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
function wp_get_post_revisions( $post_id = 0, $args = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
	$post = get_post( $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
	if ( ! $post || empty( $post->ID ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   422
	$defaults = array( 'order' => 'DESC', 'orderby' => 'date ID', 'check_enabled' => true );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
	$args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
	if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
	$args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
	if ( ! $revisions = get_children( $args ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
	return $revisions;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
 * Determine if revisions are enabled for a given post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   441
 * @param WP_Post $post The post object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
 * @return bool True if number of revisions to keep isn't zero, false otherwise.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
function wp_revisions_enabled( $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
	return wp_revisions_to_keep( $post ) != 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
 * Determine how many revisions to retain for a given post.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   450
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   451
 * By default, an infinite number of revisions are kept.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   452
 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   453
 * The constant WP_POST_REVISIONS can be set in wp-config to specify the limit
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   454
 * of revisions to keep.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   458
 * @param WP_Post $post The post object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
 * @return int The number of revisions to keep.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
function wp_revisions_to_keep( $post ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
	$num = WP_POST_REVISIONS;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
	if ( true === $num )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
		$num = -1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
		$num = intval( $num );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
	if ( ! post_type_supports( $post->post_type, 'revisions' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
		$num = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   472
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   473
	 * Filter the number of revisions to save for the given post.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   474
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   475
	 * Overrides the value of WP_POST_REVISIONS.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   476
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   477
	 * @since 3.6.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   478
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   479
	 * @param int     $num  Number of revisions to store.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   480
	 * @param WP_Post $post Post object.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   481
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
	return (int) apply_filters( 'wp_revisions_to_keep', $num, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
 * Sets up the post object for preview based on the post autosave.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
function _set_preview($post) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
	if ( ! is_object($post) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
		return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
	$preview = wp_get_post_autosave($post->ID);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
	if ( ! is_object($preview) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
		return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
	$preview = sanitize_post($preview);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
	$post->post_content = $preview->post_content;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
	$post->post_title = $preview->post_title;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
	$post->post_excerpt = $preview->post_excerpt;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
	add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
	return $post;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
 * Filters the latest content for preview from the post autosave.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
function _show_post_preview() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
	if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
		$id = (int) $_GET['preview_id'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
		if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
			wp_die( __('You do not have permission to preview drafts.') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
		add_filter('the_preview', '_set_preview');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
 * Filters terms lookup to set the post format.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
	if ( ! $post = get_post() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
		return $terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
	if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id || 'post_format' != $taxonomy || 'revision' == $post->post_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
		return $terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
	if ( 'standard' == $_REQUEST['post_format'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
		$terms = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
	elseif ( $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
		$terms = array( $term ); // Can only have one post format
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
	return $terms;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
 * Gets the post revision version.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
*/
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
function _wp_get_post_revision_version( $revision ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
	if ( is_object( $revision ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
		$revision = get_object_vars( $revision );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
	elseif ( !is_array( $revision ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
	if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
		return (int) $matches[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
	return 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
 * Upgrade the revisions author, add the current post as a revision and set the revisions version to 1
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   575
 * @param WP_Post $post Post object
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
 * @param array $revisions Current revisions of the post
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
 * @return bool true if the revisions were upgraded, false if problems
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
function _wp_upgrade_revisions_of_post( $post, $revisions ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
	global $wpdb;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
	// Add post option exclusively
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
	$lock = "revision-upgrade-{$post->ID}";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
	$now = time();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
	$result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
	if ( ! $result ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
		// If we couldn't get a lock, see how old the previous lock is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
		$locked = get_option( $lock );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
		if ( ! $locked ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
			// Can't write to the lock, and can't read the lock.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
			// Something broken has happened
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
		if ( $locked > $now - 3600 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
			// Lock is not too old: some other process may be upgrading this post.  Bail.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
		// Lock is too old - update it (below) and continue
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
	// If we could get a lock, re-"add" the option to fire all the correct filters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
	update_option( $lock, $now );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
	reset( $revisions );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
	$add_last = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
	do {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
		$this_revision = current( $revisions );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
		$prev_revision = next( $revisions );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
		$this_revision_version = _wp_get_post_revision_version( $this_revision );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
		// Something terrible happened
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
		if ( false === $this_revision_version )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
		// 1 is the latest revision version, so we're already up to date.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
		// No need to add a copy of the post as latest revision.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
		if ( 0 < $this_revision_version ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
			$add_last = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
		// Always update the revision version
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
		$update = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
			'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
		// If this revision is the oldest revision of the post, i.e. no $prev_revision,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
		// the correct post_author is probably $post->post_author, but that's only a good guess.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
		// Update the revision version only and Leave the author as-is.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
		if ( $prev_revision ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
			$prev_revision_version = _wp_get_post_revision_version( $prev_revision );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
			// If the previous revision is already up to date, it no longer has the information we need :(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
			if ( $prev_revision_version < 1 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
				$update['post_author'] = $prev_revision->post_author;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
		// Upgrade this revision
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
		$result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
		if ( $result )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
			wp_cache_delete( $this_revision->ID, 'posts' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
	} while ( $prev_revision );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
	delete_option( $lock );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
	// Add a copy of the post as latest revision.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
	if ( $add_last )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
		wp_save_post_revision( $post->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
	return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
}