diff --git a/src/wp-admin/includes/class-wp-privacy-requests-table.php b/src/wp-admin/includes/class-wp-privacy-requests-table.php index 61a917c3f6579..ed1ad8f3d0ea1 100644 --- a/src/wp-admin/includes/class-wp-privacy-requests-table.php +++ b/src/wp-admin/includes/class-wp-privacy-requests-table.php @@ -109,8 +109,9 @@ protected function get_default_primary_column_name() { protected function get_request_counts() { global $wpdb; - $cache_key = $this->post_type . '-' . $this->request_type; - $counts = wp_cache_get( $cache_key, 'counts' ); + // This duplicates wp_count_posts() with an additional post_name clause. + $cache_key = wp_cache_get_last_changed( 'posts' ) . ':' . $this->post_type . '-' . $this->request_type; + $counts = wp_cache_get( $cache_key, 'persistent-counts' ); if ( false !== $counts ) { return $counts; @@ -131,7 +132,7 @@ protected function get_request_counts() { } $counts = (object) $counts; - wp_cache_set( $cache_key, $counts, 'counts' ); + wp_cache_set( $cache_key, $counts, 'persistent-counts' ); return $counts; } diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index cca3901207775..0221fc9306c3c 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -1414,7 +1414,9 @@ function wp_count_comments( $post_id = 0 ) { return $filtered; } - $count = wp_cache_get( "comments-{$post_id}", 'counts' ); + $cache_key = wp_cache_get_last_changed( 'comment' ) . ":comments-{$post_id}"; + + $count = wp_cache_get( $cache_key, 'persistent-counts' ); if ( false !== $count ) { return $count; } @@ -1424,7 +1426,7 @@ function wp_count_comments( $post_id = 0 ) { unset( $stats['awaiting_moderation'] ); $stats_object = (object) $stats; - wp_cache_set( "comments-{$post_id}", $stats_object, 'counts' ); + wp_cache_set( $cache_key, $stats_object, 'persistent-counts' ); return $stats_object; } @@ -1549,6 +1551,8 @@ function wp_trash_comment( $comment_id ) { add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved ); add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() ); + clean_comment_cache( $comment->comment_ID ); + /** * Fires immediately after a comment is sent to Trash. * @@ -1600,6 +1604,8 @@ function wp_untrash_comment( $comment_id ) { delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' ); delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' ); + clean_comment_cache( $comment->comment_ID ); + /** * Fires immediately after a comment is restored from the Trash. * @@ -1648,6 +1654,8 @@ function wp_spam_comment( $comment_id ) { add_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', $comment->comment_approved ); add_comment_meta( $comment->comment_ID, '_wp_trash_meta_time', time() ); + clean_comment_cache( $comment->comment_ID ); + /** * Fires immediately after a comment is marked as Spam. * @@ -1699,6 +1707,8 @@ function wp_unspam_comment( $comment_id ) { delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' ); delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' ); + clean_comment_cache( $comment->comment_ID ); + /** * Fires immediately after a comment is unmarked as Spam. * @@ -2723,9 +2733,6 @@ function wp_update_comment_count_now( $post_id ) { return false; } - wp_cache_delete( 'comments-0', 'counts' ); - wp_cache_delete( "comments-{$post_id}", 'counts' ); - $post = get_post( $post_id ); if ( ! $post ) { @@ -2755,6 +2762,8 @@ function wp_update_comment_count_now( $post_id ) { clean_post_cache( $post ); + wp_cache_set_comments_last_changed(); + /** * Fires immediately after a post's comment count is updated in the database. * diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 9946462a0bae3..8218c1ec544bb 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -3084,7 +3084,7 @@ function unstick_post( $post_id ) { * @return string The cache key. */ function _count_posts_cache_key( $type = 'post', $perm = '' ) { - $cache_key = 'posts-' . $type; + $cache_key = wp_cache_get_last_changed( 'posts' ) . ':posts-' . $type; if ( 'readable' === $perm && is_user_logged_in() ) { $post_type_object = get_post_type_object( $type ); @@ -3126,7 +3126,7 @@ function wp_count_posts( $type = 'post', $perm = '' ) { $cache_key = _count_posts_cache_key( $type, $perm ); - $counts = wp_cache_get( $cache_key, 'counts' ); + $counts = wp_cache_get( $cache_key, 'persistent-counts' ); if ( false !== $counts ) { // We may have cached this before every status was registered. foreach ( get_post_stati() as $status ) { @@ -3161,7 +3161,7 @@ function wp_count_posts( $type = 'post', $perm = '' ) { } $counts = (object) $counts; - wp_cache_set( $cache_key, $counts, 'counts' ); + wp_cache_set( $cache_key, $counts, 'persistent-counts' ); /** * Filters the post counts by status for the current post type. @@ -3197,11 +3197,12 @@ function wp_count_attachments( $mime_type = '' ) { global $wpdb; $cache_key = sprintf( - 'attachments%s', + '%s:attachments%s', + wp_cache_get_last_changed( 'posts' ), ! empty( $mime_type ) ? ':' . str_replace( '/', '_', implode( '-', (array) $mime_type ) ) : '' ); - $counts = wp_cache_get( $cache_key, 'counts' ); + $counts = wp_cache_get( $cache_key, 'persistent-counts' ); if ( false == $counts ) { $and = wp_post_mime_type_where( $mime_type ); $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A ); @@ -3212,7 +3213,7 @@ function wp_count_attachments( $mime_type = '' ) { } $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and" ); - wp_cache_set( $cache_key, (object) $counts, 'counts' ); + wp_cache_set( $cache_key, (object) $counts, 'persistent-counts' ); } /** @@ -7622,11 +7623,6 @@ function _transition_post_status( $new_status, $old_status, $post ) { } } - if ( $new_status !== $old_status ) { - wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' ); - wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' ); - } - // Always clears the hook in case the post status bounced from future to draft. wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); } diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index f2978644faf2a..98ef2d34ba7a1 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -427,7 +427,7 @@ public static function flush_cache() { ) ); - wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) ); + wp_cache_add_non_persistent_groups( array( 'plugins', 'theme_json' ) ); } /** diff --git a/tests/phpunit/tests/post/wpCountAttachments.php b/tests/phpunit/tests/post/wpCountAttachments.php index af895d3a7b7f4..ded9eceed8694 100644 --- a/tests/phpunit/tests/post/wpCountAttachments.php +++ b/tests/phpunit/tests/post/wpCountAttachments.php @@ -16,7 +16,6 @@ class Tests_Post_wpCountAttachments extends WP_UnitTestCase { */ public function test_wp_count_attachments_should_cache_the_result() { $mime_type = 'image/jpeg'; - $cache_key = 'attachments:image_jpeg'; self::factory()->post->create_many( 3, @@ -25,8 +24,10 @@ public function test_wp_count_attachments_should_cache_the_result() { 'post_mime_type' => $mime_type, ) ); - $expected = wp_count_attachments( $mime_type ); - $actual = wp_cache_get( $cache_key, 'counts' ); + + $expected = wp_count_attachments( $mime_type ); + $cache_key = wp_cache_get_last_changed( 'posts' ) . ':attachments:image_jpeg'; + $actual = wp_cache_get( $cache_key, 'persistent-counts' ); $this->assertEquals( $expected, $actual ); }