From 1e76fdfb4b594d7d0d6e57955f162f5a994e6ca6 Mon Sep 17 00:00:00 2001 From: duck_ Date: Mon, 23 Apr 2012 22:04:35 +0000 Subject: [PATCH] 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 --- .../includes/class-wp-posts-list-table.php | 2 +- wp-includes/comment.php | 2 +- wp-includes/deprecated.php | 2 +- wp-includes/post.php | 48 +++++++++---------- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/wp-admin/includes/class-wp-posts-list-table.php b/wp-admin/includes/class-wp-posts-list-table.php index f2c615f7f1..b9504003b9 100644 --- a/wp-admin/includes/class-wp-posts-list-table.php +++ b/wp-admin/includes/class-wp-posts-list-table.php @@ -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 ) diff --git a/wp-includes/comment.php b/wp-includes/comment.php index 46933f066e..49406f61c7 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -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); diff --git a/wp-includes/deprecated.php b/wp-includes/deprecated.php index 95ac99bc62..13b6bff39a 100644 --- a/wp-includes/deprecated.php +++ b/wp-includes/deprecated.php @@ -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 ); } diff --git a/wp-includes/post.php b/wp-includes/post.php index 8e8a387937..ad79941b48 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -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 ); } /**