Accept a post object in clean_post_cache(). Fixes #20486.

The post_type can then be accessed to properly clean the taxonomy relationships cache.
The full object is useful in situations when an ID might reference a post that has been
removed from the database (e.g. wp_delete_post()).


git-svn-id: http://svn.automattic.com/wordpress/trunk@20569 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
duck_ 2012-04-23 22:04:35 +00:00
parent 0bffacac50
commit 1e76fdfb4b
4 changed files with 26 additions and 28 deletions

View File

@ -360,7 +360,7 @@ class WP_Posts_List_Table extends WP_List_Table {
if ( $page->post_parent == $page->ID ) {
$page->post_parent = 0;
$wpdb->update( $wpdb->posts, array( 'post_parent' => 0 ), array( 'ID' => $page->ID ) );
clean_post_cache( $page->ID, $page->post_type );
clean_post_cache( $page );
}
if ( 0 == $page->post_parent )

View File

@ -1579,7 +1579,7 @@ function wp_update_comment_count_now($post_id) {
$new = (int) $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id) );
$wpdb->update( $wpdb->posts, array('comment_count' => $new), array('ID' => $post_id) );
clean_post_cache( $post_id, $post->post_type );
clean_post_cache( $post );
do_action('wp_update_comment_count', $post_id, $new, $old);
do_action('edit_post', $post_id, $post);

View File

@ -3150,5 +3150,5 @@ function update_page_cache( &$pages ) {
function clean_page_cache( $id ) {
_deprecated_function( __FUNCTION__, 3.4, 'clean_post_cache()' );
clean_post_cache( $id, 'page' );
clean_post_cache( $id );
}

View File

@ -1361,7 +1361,7 @@ function set_post_type( $post_id = 0, $post_type = 'post' ) {
$post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db');
$return = $wpdb->update( $wpdb->posts, array('post_type' => $post_type), array('ID' => $post_id) );
clean_post_cache( $post_id, $post_type );
clean_post_cache( $post_id );
return $return;
}
@ -2045,11 +2045,11 @@ function wp_delete_post( $postid = 0, $force_delete = false ) {
$wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) );
do_action( 'deleted_post', $postid );
clean_post_cache( $postid, $post->post_type );
clean_post_cache( $post );
if ( is_post_type_hierarchical( $post->post_type ) ) {
foreach ( (array) $children as $child )
clean_post_cache( $child->ID, $child->post_type );
clean_post_cache( $child );
}
wp_clear_scheduled_hook('publish_future_post', array( $postid ) );
@ -2628,7 +2628,7 @@ function wp_insert_post($postarr, $wp_error = false) {
$current_guid = get_post_field( 'guid', $post_ID );
clean_post_cache( $post_ID, $data['post_type'] );
clean_post_cache( $post_ID );
// Set GUID
if ( !$update && '' == $current_guid )
@ -3733,7 +3733,7 @@ function wp_insert_attachment($object, $file = false, $parent = 0) {
if ( $file )
update_attached_file( $post_ID, $file );
clean_post_cache( $post_ID, $post_type );
clean_post_cache( $post_ID );
if ( ! empty( $context ) )
add_post_meta( $post_ID, '_wp_attachment_context', $context, true );
@ -3838,7 +3838,7 @@ function wp_delete_attachment( $post_id, $force_delete = false ) {
if ( ! empty($file) )
@ unlink($file);
clean_post_cache( $post_id, $post->post_type );
clean_post_cache( $post );
return $post;
}
@ -4319,46 +4319,44 @@ function update_post_cache( &$posts ) {
*
* @uses do_action() Calls 'clean_post_cache' on $id before adding children (if any).
*
* @param int $id The Post ID in the cache to clean
* @param string $post_type The post_type of the post. Defaults to "post"
* @param object|int $post The post object or ID to remove from the cache
*/
function clean_post_cache($id, $post_type = 'post') {
function clean_post_cache( $post ) {
global $_wp_suspend_cache_invalidation, $wpdb;
if ( ! empty( $_wp_suspend_cache_invalidation ) )
return;
$id = (int) $id;
if ( 0 === $id )
$post = get_post( $post );
if ( empty( $post ) )
return;
wp_cache_delete($id, 'posts');
wp_cache_delete($id, 'post_meta');
wp_cache_delete( $post->ID, 'posts' );
wp_cache_delete( $post->ID, 'post_meta' );
clean_object_term_cache( $id, $post_type );
clean_object_term_cache( $post->ID, $post->post_type );
wp_cache_delete( 'wp_get_archives', 'general' );
do_action( 'clean_post_cache', $id, $post_type );
do_action( 'clean_post_cache', $post->ID, $post );
if ( 'page' == $post_type ) {
if ( 'page' == $post->post_type ) {
wp_cache_delete( 'all_page_ids', 'posts' );
wp_cache_delete( 'get_pages', 'posts' );
do_action( 'clean_page_cache', $id );
do_action( 'clean_page_cache', $post->ID );
}
if ( $children = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_type FROM $wpdb->posts WHERE post_parent = %d", $id) ) ) {
foreach ( $children as $cid ) {
if ( $children = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_type FROM $wpdb->posts WHERE post_parent = %d", $post->ID) ) ) {
foreach ( $children as $child ) {
// Loop detection
if ( $cid->ID == $id )
if ( $child->ID == $post->ID )
continue;
clean_post_cache( $cid->ID, $cid->post_type );
clean_post_cache( $child );
}
}
if ( is_multisite() )
wp_cache_delete( $wpdb->blogid . '-' . $id, 'global-posts' );
wp_cache_delete( $wpdb->blogid . '-' . $post->ID, 'global-posts' );
}
/**
@ -4563,8 +4561,8 @@ function _publish_post_hook($post_id) {
* @param int $post_id The ID in the database table for the $post
* @param object $post Object type containing the post information
*/
function _save_post_hook($post_id, $post) {
clean_post_cache($post_id, $post->post_type);
function _save_post_hook( $post_id, $post ) {
clean_post_cache( $post );
}
/**