wp/wp-includes/revision.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     7  */
     7  */
     8 
     8 
     9 /**
     9 /**
    10  * Determines which fields of posts are to be saved in revisions.
    10  * Determines which fields of posts are to be saved in revisions.
    11  *
    11  *
    12  * Does two things. If passed a post *array*, it will return a post array ready
    12  * @since 2.6.0
    13  * to be inserted into the posts table as a post revision. Otherwise, returns
    13  * @since 4.5.0 A `WP_Post` object can now be passed to the `$post` parameter.
    14  * an array whose keys are the post fields to be saved for post revisions.
    14  * @since 4.5.0 The optional `$autosave` parameter was deprecated and renamed to `$deprecated`.
    15  *
    15  * @access private
    16  * @since 2.6.0
    16  *
    17  * @access private
    17  * @staticvar array $fields
    18  *
    18  *
    19  * @param array $post Optional a post array to be processed for insertion as a post revision.
    19  * @param array|WP_Post $post       Optional. A post array or a WP_Post object being processed
    20  * @param bool $autosave optional Is the revision an autosave?
    20  *                                  for insertion as a post revision. Default empty array.
    21  * @return array Post array ready to be inserted as a post revision or array of fields that can be versioned.
    21  * @param bool          $deprecated Not used.
    22  */
    22  * @return array Array of fields that can be versioned.
    23 function _wp_post_revision_fields( $post = null, $autosave = false ) {
    23  */
    24 	static $fields = false;
    24 function _wp_post_revision_fields( $post = array(), $deprecated = false ) {
    25 
    25 	static $fields = null;
    26 	if ( !$fields ) {
    26 
       
    27 	if ( ! is_array( $post ) ) {
       
    28 		$post = get_post( $post, ARRAY_A );
       
    29 	}
       
    30 
       
    31 	if ( is_null( $fields ) ) {
    27 		// Allow these to be versioned
    32 		// Allow these to be versioned
    28 		$fields = array(
    33 		$fields = array(
    29 			'post_title' => __( 'Title' ),
    34 			'post_title' => __( 'Title' ),
    30 			'post_content' => __( 'Content' ),
    35 			'post_content' => __( 'Content' ),
    31 			'post_excerpt' => __( 'Excerpt' ),
    36 			'post_excerpt' => __( 'Excerpt' ),
    32 		);
    37 		);
    33 
    38 	}
    34 		/**
    39 
    35 		 * Filter the list of fields saved in post revisions.
    40 	/**
    36 		 *
    41 	 * Filters the list of fields saved in post revisions.
    37 		 * Included by default: 'post_title', 'post_content' and 'post_excerpt'.
    42 	 *
    38 		 *
    43 	 * Included by default: 'post_title', 'post_content' and 'post_excerpt'.
    39 		 * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date',
    44 	 *
    40 		 * 'post_date_gmt', 'post_status', 'post_type', 'comment_count',
    45 	 * Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date',
    41 		 * and 'post_author'.
    46 	 * 'post_date_gmt', 'post_status', 'post_type', 'comment_count',
    42 		 *
    47 	 * and 'post_author'.
    43 		 * @since 2.6.0
    48 	 *
    44 		 *
    49 	 * @since 2.6.0
    45 		 * @param array $fields List of fields to revision. Contains 'post_title',
    50 	 * @since 4.5.0 The `$post` parameter was added.
    46 		 *                      'post_content', and 'post_excerpt' by default.
    51 	 *
    47 		 */
    52 	 * @param array $fields List of fields to revision. Contains 'post_title',
    48 		$fields = apply_filters( '_wp_post_revision_fields', $fields );
    53 	 *                      'post_content', and 'post_excerpt' by default.
    49 
    54 	 * @param array $post   A post array being processed for insertion as a post revision.
    50 		// WP uses these internally either in versioning or elsewhere - they cannot be versioned
    55 	 */
    51 		foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect )
    56 	$fields = apply_filters( '_wp_post_revision_fields', $fields, $post );
    52 			unset( $fields[$protect] );
    57 
    53 	}
    58 	// WP uses these internally either in versioning or elsewhere - they cannot be versioned
    54 
    59 	foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) {
    55 	if ( !is_array($post) )
    60 		unset( $fields[ $protect ] );
    56 		return $fields;
    61 	}
    57 
    62 
    58 	$return = array();
    63 
    59 	foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )
    64 	return $fields;
    60 		$return[$field] = $post[$field];
    65 }
    61 
    66 
    62 	$return['post_parent']   = $post['ID'];
    67 /**
    63 	$return['post_status']   = 'inherit';
    68  * Returns a post array ready to be inserted into the posts table as a post revision.
    64 	$return['post_type']     = 'revision';
    69  *
    65 	$return['post_name']     = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version
    70  * @since 4.5.0
    66 	$return['post_date']     = isset($post['post_modified']) ? $post['post_modified'] : '';
    71  * @access private
    67 	$return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';
    72  *
    68 
    73  * @param array|WP_Post $post     Optional. A post array or a WP_Post object to be processed
    69 	return $return;
    74  *                                for insertion as a post revision. Default empty array.
       
    75  * @param bool          $autosave Optional. Is the revision an autosave? Default false.
       
    76  * @return array Post array ready to be inserted as a post revision.
       
    77  */
       
    78 function _wp_post_revision_data( $post = array(), $autosave = false ) {
       
    79 	if ( ! is_array( $post ) ) {
       
    80 		$post = get_post( $post, ARRAY_A );
       
    81 	}
       
    82 
       
    83 	$fields = _wp_post_revision_fields( $post );
       
    84 
       
    85 	$revision_data = array();
       
    86 
       
    87 	foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field ) {
       
    88 		$revision_data[ $field ] = $post[ $field ];
       
    89 	}
       
    90 
       
    91 	$revision_data['post_parent']   = $post['ID'];
       
    92 	$revision_data['post_status']   = 'inherit';
       
    93 	$revision_data['post_type']     = 'revision';
       
    94 	$revision_data['post_name']     = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version
       
    95 	$revision_data['post_date']     = isset( $post['post_modified'] ) ? $post['post_modified'] : '';
       
    96 	$revision_data['post_date_gmt'] = isset( $post['post_modified_gmt'] ) ? $post['post_modified_gmt'] : '';
       
    97 
       
    98 	return $revision_data;
    70 }
    99 }
    71 
   100 
    72 /**
   101 /**
    73  * Creates a revision for the current version of a post.
   102  * Creates a revision for the current version of a post.
    74  *
   103  *
    75  * Typically used immediately after a post update, as every update is a revision,
   104  * Typically used immediately after a post update, as every update is a revision,
    76  * and the most recent revision always matches the current post.
   105  * and the most recent revision always matches the current post.
    77  *
   106  *
    78  * @since 2.6.0
   107  * @since 2.6.0
    79  *
   108  *
    80  * @param  int $post_id The ID of the post to save as a revision.
   109  * @param int $post_id The ID of the post to save as a revision.
    81  * @return null|int Null or 0 if error, new revision ID, if success.
   110  * @return int|WP_Error|void Void or 0 if error, new revision ID, if success.
    82  */
   111  */
    83 function wp_save_post_revision( $post_id ) {
   112 function wp_save_post_revision( $post_id ) {
    84 	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
   113 	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    85 		return;
   114 		return;
    86 
   115 
   107 				break;
   136 				break;
   108 			}
   137 			}
   109 		}
   138 		}
   110 
   139 
   111 		/**
   140 		/**
   112 		 * Filter whether the post has changed since the last revision.
   141 		 * Filters whether the post has changed since the last revision.
   113 		 *
   142 		 *
   114 		 * By default a revision is saved only if one of the revisioned fields has changed.
   143 		 * By default a revision is saved only if one of the revisioned fields has changed.
   115 		 * This filter can override that so a revision is saved even if nothing has changed.
   144 		 * This filter can override that so a revision is saved even if nothing has changed.
   116 		 *
   145 		 *
   117 		 * @since 3.6.0
   146 		 * @since 3.6.0
   118 		 *
   147 		 *
   119 		 * @param bool    $check_for_changes Whether to check for changes before saving a new revision.
   148 		 * @param bool    $check_for_changes Whether to check for changes before saving a new revision.
   120 		 *                                   Default true.
   149 		 *                                   Default true.
   121 		 * @param WP_Post $last_revision     The the last revision post object.
   150 		 * @param WP_Post $last_revision     The last revision post object.
   122 		 * @param WP_Post $post              The post object.
   151 		 * @param WP_Post $post              The post object.
   123 		 *
   152 		 *
   124 		 */
   153 		 */
   125 		if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) {
   154 		if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) {
   126 			$post_has_changed = false;
   155 			$post_has_changed = false;
   127 
   156 
   128 			foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
   157 			foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) {
   129 				if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) {
   158 				if ( normalize_whitespace( $post->$field ) != normalize_whitespace( $last_revision->$field ) ) {
   130 					$post_has_changed = true;
   159 					$post_has_changed = true;
   131 					break;
   160 					break;
   132 				}
   161 				}
   133 			}
   162 			}
   134 
   163 
   135 			/**
   164 			/**
   136 			 * Filter whether a post has changed.
   165 			 * Filters whether a post has changed.
   137 			 *
   166 			 *
   138 			 * By default a revision is saved only if one of the revisioned fields has changed.
   167 			 * By default a revision is saved only if one of the revisioned fields has changed.
   139 			 * This filter allows for additional checks to determine if there were changes.
   168 			 * This filter allows for additional checks to determine if there were changes.
   140 			 *
   169 			 *
   141 			 * @since 4.1.0
   170 			 * @since 4.1.0
   146 			 *
   175 			 *
   147 			 */
   176 			 */
   148 			$post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post );
   177 			$post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post );
   149 
   178 
   150 			//don't save revision if post unchanged
   179 			//don't save revision if post unchanged
   151 			if( ! $post_has_changed ) {
   180 			if ( ! $post_has_changed ) {
   152 				return;
   181 				return;
   153 			}
   182 			}
   154 		}
   183 		}
   155 	}
   184 	}
   156 
   185 
   190  * otherwise returns the latest autosave.
   219  * otherwise returns the latest autosave.
   191  *
   220  *
   192  * @since 2.6.0
   221  * @since 2.6.0
   193  *
   222  *
   194  * @param int $post_id The post ID.
   223  * @param int $post_id The post ID.
   195  * @param int $user_id optional The post author ID.
   224  * @param int $user_id Optional The post author ID.
   196  * @return object|bool The autosaved data or false on failure or when no autosave exists.
   225  * @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
   197  */
   226  */
   198 function wp_get_post_autosave( $post_id, $user_id = 0 ) {
   227 function wp_get_post_autosave( $post_id, $user_id = 0 ) {
   199 	$revisions = wp_get_post_revisions( $post_id, array( 'check_enabled' => false ) );
   228 	$revisions = wp_get_post_revisions( $post_id, array( 'check_enabled' => false ) );
   200 
   229 
   201 	foreach ( $revisions as $revision ) {
   230 	foreach ( $revisions as $revision ) {
   213 /**
   242 /**
   214  * Determines if the specified post is a revision.
   243  * Determines if the specified post is a revision.
   215  *
   244  *
   216  * @since 2.6.0
   245  * @since 2.6.0
   217  *
   246  *
   218  * @param int|object $post Post ID or post object.
   247  * @param int|WP_Post $post Post ID or post object.
   219  * @return bool|int False if not a revision, ID of revision's parent otherwise.
   248  * @return false|int False if not a revision, ID of revision's parent otherwise.
   220  */
   249  */
   221 function wp_is_post_revision( $post ) {
   250 function wp_is_post_revision( $post ) {
   222 	if ( !$post = wp_get_post_revision( $post ) )
   251 	if ( !$post = wp_get_post_revision( $post ) )
   223 		return false;
   252 		return false;
   224 
   253 
   228 /**
   257 /**
   229  * Determines if the specified post is an autosave.
   258  * Determines if the specified post is an autosave.
   230  *
   259  *
   231  * @since 2.6.0
   260  * @since 2.6.0
   232  *
   261  *
   233  * @param int|object $post Post ID or post object.
   262  * @param int|WP_Post $post Post ID or post object.
   234  * @return bool|int False if not a revision, ID of autosave's parent otherwise
   263  * @return false|int False if not a revision, ID of autosave's parent otherwise
   235  */
   264  */
   236 function wp_is_post_autosave( $post ) {
   265 function wp_is_post_autosave( $post ) {
   237 	if ( !$post = wp_get_post_revision( $post ) )
   266 	if ( !$post = wp_get_post_revision( $post ) )
   238 		return false;
   267 		return false;
   239 
   268 
   247  * Inserts post data into the posts table as a post revision.
   276  * Inserts post data into the posts table as a post revision.
   248  *
   277  *
   249  * @since 2.6.0
   278  * @since 2.6.0
   250  * @access private
   279  * @access private
   251  *
   280  *
   252  * @param int|object|array $post Post ID, post object OR post array.
   281  * @param int|WP_Post|array|null $post     Post ID, post object OR post array.
   253  * @param bool $autosave Optional. Is the revision an autosave?
   282  * @param bool                   $autosave Optional. Is the revision an autosave?
   254  * @return mixed WP_Error or 0 if error, new revision ID if success.
   283  * @return int|WP_Error WP_Error or 0 if error, new revision ID if success.
   255  */
   284  */
   256 function _wp_put_post_revision( $post = null, $autosave = false ) {
   285 function _wp_put_post_revision( $post = null, $autosave = false ) {
   257 	if ( is_object($post) )
   286 	if ( is_object($post) )
   258 		$post = get_object_vars( $post );
   287 		$post = get_object_vars( $post );
   259 	elseif ( !is_array($post) )
   288 	elseif ( !is_array($post) )
   260 		$post = get_post($post, ARRAY_A);
   289 		$post = get_post($post, ARRAY_A);
   261 
   290 
   262 	if ( ! $post || empty($post['ID']) )
   291 	if ( ! $post || empty($post['ID']) )
   263 		return new WP_Error( 'invalid_post', __( 'Invalid post ID' ) );
   292 		return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
   264 
   293 
   265 	if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
   294 	if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
   266 		return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
   295 		return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
   267 
   296 
   268 	$post = _wp_post_revision_fields( $post, $autosave );
   297 	$post = _wp_post_revision_data( $post, $autosave );
   269 	$post = wp_slash($post); //since data is from db
   298 	$post = wp_slash($post); //since data is from db
   270 
   299 
   271 	$revision_id = wp_insert_post( $post );
   300 	$revision_id = wp_insert_post( $post );
   272 	if ( is_wp_error($revision_id) )
   301 	if ( is_wp_error($revision_id) )
   273 		return $revision_id;
   302 		return $revision_id;
   289 /**
   318 /**
   290  * Gets a post revision.
   319  * Gets a post revision.
   291  *
   320  *
   292  * @since 2.6.0
   321  * @since 2.6.0
   293  *
   322  *
   294  * @param int|object $post The post ID or object.
   323  * @param int|WP_Post $post   The post ID or object.
   295  * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.
   324  * @param string      $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
   296  * @param string $filter Optional sanitation filter. @see sanitize_post().
   325  *                            a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT.
   297  * @return mixed Null if error or post object if success.
   326  * @param string      $filter Optional sanitation filter. See sanitize_post().
       
   327  * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
   298  */
   328  */
   299 function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
   329 function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
   300 	if ( !$revision = get_post( $post, OBJECT, $filter ) )
   330 	if ( !$revision = get_post( $post, OBJECT, $filter ) )
   301 		return $revision;
   331 		return $revision;
   302 	if ( 'revision' !== $revision->post_type )
   332 	if ( 'revision' !== $revision->post_type )
   320  *
   350  *
   321  * Can restore a past revision using all fields of the post revision, or only selected fields.
   351  * Can restore a past revision using all fields of the post revision, or only selected fields.
   322  *
   352  *
   323  * @since 2.6.0
   353  * @since 2.6.0
   324  *
   354  *
   325  * @param int|object $revision_id Revision ID or revision object.
   355  * @param int|WP_Post $revision_id Revision ID or revision object.
   326  * @param array $fields Optional. What fields to restore from. Defaults to all.
   356  * @param array       $fields      Optional. What fields to restore from. Defaults to all.
   327  * @return mixed Null if error, false if no fields to restore, (int) post ID if success.
   357  * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success.
   328  */
   358  */
   329 function wp_restore_post_revision( $revision_id, $fields = null ) {
   359 function wp_restore_post_revision( $revision_id, $fields = null ) {
   330 	if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )
   360 	if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )
   331 		return $revision;
   361 		return $revision;
   332 
   362 
   333 	if ( !is_array( $fields ) )
   363 	if ( !is_array( $fields ) )
   334 		$fields = array_keys( _wp_post_revision_fields() );
   364 		$fields = array_keys( _wp_post_revision_fields( $revision ) );
   335 
   365 
   336 	$update = array();
   366 	$update = array();
   337 	foreach( array_intersect( array_keys( $revision ), $fields ) as $field ) {
   367 	foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) {
   338 		$update[$field] = $revision[$field];
   368 		$update[$field] = $revision[$field];
   339 	}
   369 	}
   340 
   370 
   341 	if ( !$update )
   371 	if ( !$update )
   342 		return false;
   372 		return false;
   346 	$update = wp_slash( $update ); //since data is from db
   376 	$update = wp_slash( $update ); //since data is from db
   347 
   377 
   348 	$post_id = wp_update_post( $update );
   378 	$post_id = wp_update_post( $update );
   349 	if ( ! $post_id || is_wp_error( $post_id ) )
   379 	if ( ! $post_id || is_wp_error( $post_id ) )
   350 		return $post_id;
   380 		return $post_id;
   351 
       
   352 	// Add restore from details
       
   353 	$restore_details = array(
       
   354 		'restored_revision_id' => $revision_id,
       
   355 		'restored_by_user'     => get_current_user_id(),
       
   356 		'restored_time'        => time()
       
   357 	);
       
   358 	update_post_meta( $post_id, '_post_restored_from', $restore_details );
       
   359 
   381 
   360 	// Update last edit user
   382 	// Update last edit user
   361 	update_post_meta( $post_id, '_edit_last', get_current_user_id() );
   383 	update_post_meta( $post_id, '_edit_last', get_current_user_id() );
   362 
   384 
   363 	/**
   385 	/**
   378  *
   400  *
   379  * Deletes the row from the posts table corresponding to the specified revision.
   401  * Deletes the row from the posts table corresponding to the specified revision.
   380  *
   402  *
   381  * @since 2.6.0
   403  * @since 2.6.0
   382  *
   404  *
   383  * @param int|object $revision_id Revision ID or revision object.
   405  * @param int|WP_Post $revision_id Revision ID or revision object.
   384  * @return mixed Null or WP_Error if error, deleted post if success.
   406  * @return array|false|WP_Post|WP_Error|null Null or WP_Error if error, deleted post if success.
   385  */
   407  */
   386 function wp_delete_post_revision( $revision_id ) {
   408 function wp_delete_post_revision( $revision_id ) {
   387 	if ( !$revision = wp_get_post_revision( $revision_id ) )
   409 	if ( ! $revision = wp_get_post_revision( $revision_id ) ) {
   388 		return $revision;
   410 		return $revision;
       
   411 	}
   389 
   412 
   390 	$delete = wp_delete_post( $revision->ID );
   413 	$delete = wp_delete_post( $revision->ID );
   391 	if ( is_wp_error( $delete ) )
       
   392 		return $delete;
       
   393 
       
   394 	if ( $delete ) {
   414 	if ( $delete ) {
   395 		/**
   415 		/**
   396 		 * Fires once a post revision has been deleted.
   416 		 * Fires once a post revision has been deleted.
   397 		 *
   417 		 *
   398 		 * @since 2.6.0
   418 		 * @since 2.6.0
   409 /**
   429 /**
   410  * Returns all revisions of specified post.
   430  * Returns all revisions of specified post.
   411  *
   431  *
   412  * @since 2.6.0
   432  * @since 2.6.0
   413  *
   433  *
   414  * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
   434  * @see get_children()
       
   435  *
       
   436  * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global `$post`.
       
   437  * @param array|null  $args    Optional. Arguments for retrieving post revisions. Default null.
   415  * @return array An array of revisions, or an empty array if none.
   438  * @return array An array of revisions, or an empty array if none.
   416  */
   439  */
   417 function wp_get_post_revisions( $post_id = 0, $args = null ) {
   440 function wp_get_post_revisions( $post_id = 0, $args = null ) {
   418 	$post = get_post( $post_id );
   441 	$post = get_post( $post_id );
   419 	if ( ! $post || empty( $post->ID ) )
   442 	if ( ! $post || empty( $post->ID ) )
   440  *
   463  *
   441  * @param WP_Post $post The post object.
   464  * @param WP_Post $post The post object.
   442  * @return bool True if number of revisions to keep isn't zero, false otherwise.
   465  * @return bool True if number of revisions to keep isn't zero, false otherwise.
   443  */
   466  */
   444 function wp_revisions_enabled( $post ) {
   467 function wp_revisions_enabled( $post ) {
   445 	return wp_revisions_to_keep( $post ) != 0;
   468 	return wp_revisions_to_keep( $post ) !== 0;
   446 }
   469 }
   447 
   470 
   448 /**
   471 /**
   449  * Determine how many revisions to retain for a given post.
   472  * Determine how many revisions to retain for a given post.
   450  *
   473  *
   468 
   491 
   469 	if ( ! post_type_supports( $post->post_type, 'revisions' ) )
   492 	if ( ! post_type_supports( $post->post_type, 'revisions' ) )
   470 		$num = 0;
   493 		$num = 0;
   471 
   494 
   472 	/**
   495 	/**
   473 	 * Filter the number of revisions to save for the given post.
   496 	 * Filters the number of revisions to save for the given post.
   474 	 *
   497 	 *
   475 	 * Overrides the value of WP_POST_REVISIONS.
   498 	 * Overrides the value of WP_POST_REVISIONS.
   476 	 *
   499 	 *
   477 	 * @since 3.6.0
   500 	 * @since 3.6.0
   478 	 *
   501 	 *
   485 /**
   508 /**
   486  * Sets up the post object for preview based on the post autosave.
   509  * Sets up the post object for preview based on the post autosave.
   487  *
   510  *
   488  * @since 2.7.0
   511  * @since 2.7.0
   489  * @access private
   512  * @access private
   490  */
   513  *
   491 function _set_preview($post) {
   514  * @param WP_Post $post
   492 
   515  * @return WP_Post|false
   493 	if ( ! is_object($post) )
   516  */
       
   517 function _set_preview( $post ) {
       
   518 	if ( ! is_object( $post ) ) {
   494 		return $post;
   519 		return $post;
   495 
   520 	}
   496 	$preview = wp_get_post_autosave($post->ID);
   521 
   497 
   522 	$preview = wp_get_post_autosave( $post->ID );
   498 	if ( ! is_object($preview) )
   523 	if ( ! is_object( $preview ) ) {
   499 		return $post;
   524 		return $post;
   500 
   525 	}
   501 	$preview = sanitize_post($preview);
   526 
       
   527 	$preview = sanitize_post( $preview );
   502 
   528 
   503 	$post->post_content = $preview->post_content;
   529 	$post->post_content = $preview->post_content;
   504 	$post->post_title = $preview->post_title;
   530 	$post->post_title = $preview->post_title;
   505 	$post->post_excerpt = $preview->post_excerpt;
   531 	$post->post_excerpt = $preview->post_excerpt;
   506 
   532 
   507 	add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 );
   533 	add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 );
       
   534 	add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 );
   508 
   535 
   509 	return $post;
   536 	return $post;
   510 }
   537 }
   511 
   538 
   512 /**
   539 /**
   514  *
   541  *
   515  * @since 2.7.0
   542  * @since 2.7.0
   516  * @access private
   543  * @access private
   517  */
   544  */
   518 function _show_post_preview() {
   545 function _show_post_preview() {
   519 
       
   520 	if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
   546 	if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
   521 		$id = (int) $_GET['preview_id'];
   547 		$id = (int) $_GET['preview_id'];
   522 
   548 
   523 		if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )
   549 		if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )
   524 			wp_die( __('You do not have permission to preview drafts.') );
   550 			wp_die( __('Sorry, you are not allowed to preview drafts.') );
   525 
   551 
   526 		add_filter('the_preview', '_set_preview');
   552 		add_filter('the_preview', '_set_preview');
   527 	}
   553 	}
   528 }
   554 }
   529 
   555 
   530 /**
   556 /**
   531  * Filters terms lookup to set the post format.
   557  * Filters terms lookup to set the post format.
   532  *
   558  *
   533  * @since 3.6.0
   559  * @since 3.6.0
   534  * @access private
   560  * @access private
       
   561  *
       
   562  * @param array  $terms
       
   563  * @param int    $post_id
       
   564  * @param string $taxonomy
       
   565  * @return array
   535  */
   566  */
   536 function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
   567 function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
   537 	if ( ! $post = get_post() )
   568 	if ( ! $post = get_post() )
   538 		return $terms;
   569 		return $terms;
   539 
   570 
   547 
   578 
   548 	return $terms;
   579 	return $terms;
   549 }
   580 }
   550 
   581 
   551 /**
   582 /**
       
   583  * Filters post thumbnail lookup to set the post thumbnail.
       
   584  *
       
   585  * @since 4.6.0
       
   586  * @access private
       
   587  *
       
   588  * @param null|array|string $value    The value to return - a single metadata value, or an array of values.
       
   589  * @param int               $post_id  Post ID.
       
   590  * @param string            $meta_key Meta key.
       
   591  * @return null|array The default return value or the post thumbnail meta array.
       
   592  */
       
   593 function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) {
       
   594 	if ( ! $post = get_post() ) {
       
   595 		return $value;
       
   596 	}
       
   597 
       
   598 	if ( empty( $_REQUEST['_thumbnail_id'] ) ||
       
   599 	     empty( $_REQUEST['preview_id'] ) ||
       
   600 	     $post->ID != $post_id ||
       
   601 	     '_thumbnail_id' != $meta_key ||
       
   602 	     'revision' == $post->post_type ||
       
   603 	     $post_id != $_REQUEST['preview_id']
       
   604 	) {
       
   605 		return $value;
       
   606 	}
       
   607 
       
   608 	$thumbnail_id = intval( $_REQUEST['_thumbnail_id'] );
       
   609 	if ( $thumbnail_id <= 0 ) {
       
   610 		return '';
       
   611 	}
       
   612 
       
   613 	return strval( $thumbnail_id );
       
   614 }
       
   615 
       
   616 /**
   552  * Gets the post revision version.
   617  * Gets the post revision version.
   553  *
   618  *
   554  * @since 3.6.0
   619  * @since 3.6.0
   555  * @access private
   620  * @access private
   556 */
   621  *
       
   622  * @param WP_Post $revision
       
   623  * @return int|false
       
   624  */
   557 function _wp_get_post_revision_version( $revision ) {
   625 function _wp_get_post_revision_version( $revision ) {
   558 	if ( is_object( $revision ) )
   626 	if ( is_object( $revision ) )
   559 		$revision = get_object_vars( $revision );
   627 		$revision = get_object_vars( $revision );
   560 	elseif ( !is_array( $revision ) )
   628 	elseif ( !is_array( $revision ) )
   561 		return false;
   629 		return false;
   570  * Upgrade the revisions author, add the current post as a revision and set the revisions version to 1
   638  * Upgrade the revisions author, add the current post as a revision and set the revisions version to 1
   571  *
   639  *
   572  * @since 3.6.0
   640  * @since 3.6.0
   573  * @access private
   641  * @access private
   574  *
   642  *
   575  * @param WP_Post $post Post object
   643  * @global wpdb $wpdb WordPress database abstraction object.
   576  * @param array $revisions Current revisions of the post
   644  *
       
   645  * @param WP_Post $post      Post object
       
   646  * @param array   $revisions Current revisions of the post
   577  * @return bool true if the revisions were upgraded, false if problems
   647  * @return bool true if the revisions were upgraded, false if problems
   578  */
   648  */
   579 function _wp_upgrade_revisions_of_post( $post, $revisions ) {
   649 function _wp_upgrade_revisions_of_post( $post, $revisions ) {
   580 	global $wpdb;
   650 	global $wpdb;
   581 
   651