Pass post_type to clean_post_cache() instead of attempting to fetch a post object since the post may have been deleted.

Props leewillis77
see #19690


git-svn-id: http://svn.automattic.com/wordpress/trunk@20423 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2012-04-10 16:09:44 +00:00
parent 5de5c9a64d
commit baf681233f
4 changed files with 65 additions and 82 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_page_cache( $page->ID );
clean_post_cache( $page->ID, $page->post_type );
}
if ( 0 == $page->post_parent )

View File

@ -1583,10 +1583,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) );
if ( 'page' == $post->post_type )
clean_page_cache( $post_id );
else
clean_post_cache( $post_id );
clean_post_cache( $post_id, $post->post_type );
do_action('wp_update_comment_count', $post_id, $new, $old);
do_action('edit_post', $post_id, $post);

View File

@ -3116,4 +3116,39 @@ function get_theme_data( $theme_file ) {
);
return $theme_data;
}
}
/**
* Alias of update_post_cache().
*
* @see update_post_cache() Posts and pages are the same, alias is intentional
*
* @since 1.5.1
* @deprecated 3.4.0
*
* @param array $pages list of page objects
*/
function update_page_cache( &$pages ) {
_deprecated_function( __FUNCTION__, 3.4, 'update_post_cache()' );
update_post_cache( $pages );
}
/**
* Will clean the page in the cache.
*
* Clean (read: delete) page from cache that matches $id. Will also clean cache
* associated with 'all_page_ids' and 'get_pages'.
*
* @since 2.0.0
* @deprecated 3.4.0
*
* @uses do_action() Will call the 'clean_page_cache' hook action.
*
* @param int $id Page ID to clean
*/
function clean_page_cache( $id ) {
_deprecated_function( __FUNCTION__, 3.4, 'clean_post_cache()' );
clean_post_cache( $id, 'page' );
}

View File

@ -1359,12 +1359,9 @@ function set_post_type( $post_id = 0, $post_type = 'post' ) {
global $wpdb;
$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) );
$return = $wpdb->update( $wpdb->posts, array('post_type' => $post_type), array('ID' => $post_id) );
if ( 'page' == $post_type )
clean_page_cache($post_id);
else
clean_post_cache($post_id);
clean_post_cache( $post_id, $post_type );
return $return;
}
@ -2056,15 +2053,11 @@ function wp_delete_post( $postid = 0, $force_delete = false ) {
$wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) );
do_action( 'deleted_post', $postid );
if ( 'page' == $post->post_type ) {
clean_page_cache($postid);
} else {
clean_post_cache($postid);
}
clean_post_cache( $postid, $post->post_type );
if ( is_post_type_hierarchical( $post->post_type ) ) {
foreach ( (array) $children as $child )
clean_post_cache( $child->ID );
clean_post_cache( $child->ID, $child->post_type );
}
wp_clear_scheduled_hook('publish_future_post', array( $postid ) );
@ -2643,10 +2636,7 @@ function wp_insert_post($postarr, $wp_error = false) {
$current_guid = get_post_field( 'guid', $post_ID );
if ( 'page' == $data['post_type'] )
clean_page_cache($post_ID);
else
clean_post_cache($post_ID);
clean_post_cache( $post_ID, $data['post_type'] );
// Set GUID
if ( !$update && '' == $current_guid )
@ -3535,7 +3525,7 @@ function &get_pages($args = '') {
}
// Update cache.
update_page_cache($pages);
update_post_cache( $pages );
if ( $child_of || $hierarchical )
$pages = & get_page_children($child_of, $pages);
@ -3751,7 +3741,7 @@ function wp_insert_attachment($object, $file = false, $parent = 0) {
if ( $file )
update_attached_file( $post_ID, $file );
clean_post_cache($post_ID);
clean_post_cache( $post_ID, $post_type );
if ( ! empty( $context ) )
add_post_meta( $post_ID, '_wp_attachment_context', $context, true );
@ -3864,7 +3854,7 @@ function wp_delete_attachment( $post_id, $force_delete = false ) {
if ( ! empty($file) )
@ unlink($file);
clean_post_cache($post_id);
clean_post_cache( $post_id, $post->post_type );
return $post;
}
@ -4314,20 +4304,18 @@ function _get_last_post_time( $timezone, $field ) {
/**
* Updates posts in cache.
*
* @usedby update_page_cache() Aliased by this function.
*
* @package WordPress
* @subpackage Cache
* @since 1.5.1
*
* @param array $posts Array of post objects
*/
function update_post_cache(&$posts) {
if ( !$posts )
function update_post_cache( &$posts ) {
if ( ! $posts )
return;
foreach ( $posts as $post )
wp_cache_add($post->ID, $post, 'posts');
wp_cache_add( $post->ID, $post, 'posts' );
}
/**
@ -4348,11 +4336,12 @@ 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"
*/
function clean_post_cache($id) {
function clean_post_cache($id, $post_type = 'post') {
global $_wp_suspend_cache_invalidation, $wpdb;
if ( !empty($_wp_suspend_cache_invalidation) )
if ( ! empty( $_wp_suspend_cache_invalidation ) )
return;
$id = (int) $id;
@ -4360,23 +4349,27 @@ function clean_post_cache($id) {
if ( 0 === $id )
return;
$post = get_post( $id );
wp_cache_delete($id, 'posts');
wp_cache_delete($id, 'post_meta');
clean_object_term_cache( $id, $post->post_type );
clean_object_term_cache( $id, $post_type );
wp_cache_delete( 'wp_get_archives', 'general' );
do_action('clean_post_cache', $id);
do_action( 'clean_post_cache', $id, $post_type );
if ( $children = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_parent = %d", $id) ) ) {
if ( 'page' == $post_type ) {
wp_cache_delete( 'all_page_ids', 'posts' );
wp_cache_delete( 'get_pages', 'posts' );
do_action( 'clean_page_cache', $id );
}
if ( $children = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_type FROM $wpdb->posts WHERE post_parent = %d", $id) ) ) {
foreach ( $children as $cid ) {
// Loop detection
if ( $cid == $id )
if ( $cid->ID == $id )
continue;
clean_post_cache( $cid );
clean_post_cache( $cid->ID, $cid->post_type );
}
}
@ -4384,44 +4377,6 @@ function clean_post_cache($id) {
wp_cache_delete( $wpdb->blogid . '-' . $id, 'global-posts' );
}
/**
* Alias of update_post_cache().
*
* @see update_post_cache() Posts and pages are the same, alias is intentional
*
* @package WordPress
* @subpackage Cache
* @since 1.5.1
*
* @param array $pages list of page objects
*/
function update_page_cache(&$pages) {
update_post_cache($pages);
}
/**
* Will clean the page in the cache.
*
* Clean (read: delete) page from cache that matches $id. Will also clean cache
* associated with 'all_page_ids' and 'get_pages'.
*
* @package WordPress
* @subpackage Cache
* @since 2.0.0
*
* @uses do_action() Will call the 'clean_page_cache' hook action.
*
* @param int $id Page ID to clean
*/
function clean_page_cache($id) {
clean_post_cache($id);
wp_cache_delete( 'all_page_ids', 'posts' );
wp_cache_delete( 'get_pages', 'posts' );
do_action('clean_page_cache', $id);
}
/**
* Call major cache updating functions for list of Post objects.
*
@ -4629,11 +4584,7 @@ function _publish_post_hook($post_id) {
* @param object $post Object type containing the post information
*/
function _save_post_hook($post_id, $post) {
if ( $post->post_type == 'page' ) {
clean_page_cache($post_id);
} else {
clean_post_cache($post_id);
}
clean_post_cache($post_id, $post->post_type);
}
/**
@ -5361,4 +5312,4 @@ function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache
update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
}
}
}