wp/wp-includes/revision.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
   268  * If the optional $user_id is passed, returns the autosave for that user, otherwise
   268  * If the optional $user_id is passed, returns the autosave for that user, otherwise
   269  * returns the latest autosave.
   269  * returns the latest autosave.
   270  *
   270  *
   271  * @since 2.6.0
   271  * @since 2.6.0
   272  *
   272  *
   273  * @global wpdb $wpdb WordPress database abstraction object.
       
   274  *
       
   275  * @param int $post_id The post ID.
   273  * @param int $post_id The post ID.
   276  * @param int $user_id Optional. The post author ID. Default 0.
   274  * @param int $user_id Optional. The post author ID. Default 0.
   277  * @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
   275  * @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
   278  */
   276  */
   279 function wp_get_post_autosave( $post_id, $user_id = 0 ) {
   277 function wp_get_post_autosave( $post_id, $user_id = 0 ) {
   280 	global $wpdb;
   278 	$args = array(
   281 
   279 		'post_type'      => 'revision',
   282 	$autosave_name = $post_id . '-autosave-v1';
   280 		'post_status'    => 'inherit',
   283 	$user_id_query = ( 0 !== $user_id ) ? "AND post_author = $user_id" : null;
   281 		'post_parent'    => $post_id,
   284 
   282 		'name'           => $post_id . '-autosave-v1',
   285 	// Construct the autosave query.
   283 		'posts_per_page' => 1,
   286 	$autosave_query = "
   284 		'orderby'        => 'date',
   287 		SELECT *
   285 		'order'          => 'DESC',
   288 		FROM $wpdb->posts
   286 		'fields'         => 'ids',
   289 		WHERE post_parent = %d
   287 		'no_found_rows'  => true,
   290 		AND post_type = 'revision'
       
   291 		AND post_status = 'inherit'
       
   292 		AND post_name   = %s " . $user_id_query . '
       
   293 		ORDER BY post_date DESC
       
   294 		LIMIT 1';
       
   295 
       
   296 	$autosave = $wpdb->get_results(
       
   297 		$wpdb->prepare(
       
   298 			$autosave_query,
       
   299 			$post_id,
       
   300 			$autosave_name
       
   301 		)
       
   302 	);
   288 	);
   303 
   289 
   304 	if ( ! $autosave ) {
   290 	if ( 0 !== $user_id ) {
       
   291 		$args['author'] = $user_id;
       
   292 	}
       
   293 
       
   294 	$query = new WP_Query( $args );
       
   295 
       
   296 	if ( ! $query->have_posts() ) {
   305 		return false;
   297 		return false;
   306 	}
   298 	}
   307 
   299 
   308 	return get_post( $autosave[0] );
   300 	return get_post( $query->posts[0] );
   309 }
   301 }
   310 
   302 
   311 /**
   303 /**
   312  * Determines if the specified post is a revision.
   304  * Determines if the specified post is a revision.
   313  *
   305  *
  1105  * @since 6.4.0
  1097  * @since 6.4.0
  1106  *
  1098  *
  1107  * @param mixed  $value     Meta value to filter.
  1099  * @param mixed  $value     Meta value to filter.
  1108  * @param int    $object_id Object ID.
  1100  * @param int    $object_id Object ID.
  1109  * @param string $meta_key  Meta key to filter a value for.
  1101  * @param string $meta_key  Meta key to filter a value for.
  1110  * @param bool   $single    Whether to return a single value. Default false.
  1102  * @param bool   $single    Whether to return a single value.
  1111  * @return mixed Original meta value if the meta key isn't revisioned, the object doesn't exist,
  1103  * @return mixed Original meta value if the meta key isn't revisioned, the object doesn't exist,
  1112  *               the post type is a revision or the post ID doesn't match the object ID.
  1104  *               the post type is a revision or the post ID doesn't match the object ID.
  1113  *               Otherwise, the revisioned meta value is returned for the preview.
  1105  *               Otherwise, the revisioned meta value is returned for the preview.
  1114  */
  1106  */
  1115 function _wp_preview_meta_filter( $value, $object_id, $meta_key, $single ) {
  1107 function _wp_preview_meta_filter( $value, $object_id, $meta_key, $single ) {
  1116 
       
  1117 	$post = get_post();
  1108 	$post = get_post();
  1118 	if (
  1109 
  1119 		empty( $post ) ||
  1110 	if ( empty( $post )
  1120 		$post->ID !== $object_id ||
  1111 		|| $post->ID !== $object_id
  1121 		! in_array( $meta_key, wp_post_revision_meta_keys( $post->post_type ), true ) ||
  1112 		|| ! in_array( $meta_key, wp_post_revision_meta_keys( $post->post_type ), true )
  1122 		'revision' === $post->post_type
  1113 		|| 'revision' === $post->post_type
  1123 	) {
  1114 	) {
  1124 		return $value;
  1115 		return $value;
  1125 	}
  1116 	}
  1126 
  1117 
  1127 	$preview = wp_get_post_autosave( $post->ID );
  1118 	$preview = wp_get_post_autosave( $post->ID );
       
  1119 
  1128 	if ( false === $preview ) {
  1120 	if ( false === $preview ) {
  1129 		return $value;
  1121 		return $value;
  1130 	}
  1122 	}
  1131 
  1123 
  1132 	return get_post_meta( $preview->ID, $meta_key, $single );
  1124 	return get_post_meta( $preview->ID, $meta_key, $single );