Skip to content

Commit b917e79

Browse files
committed
get_blog_details()->post_count should update on more actions than just publish_post.
Adds unit test. Props 5um17, midxcat, strangerstudios. Fixes #27952. git-svn-id: https://develop.svn.wordpress.org/trunk@28835 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 8621b2c commit b917e79

4 files changed

Lines changed: 50 additions & 2 deletions

File tree

src/wp-includes/ms-blogs.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,3 +892,38 @@ function _update_blog_date_on_post_delete( $post_id ) {
892892
wpmu_update_blogs_date();
893893
}
894894

895+
/**
896+
* Handler for updating the blog posts count date when a post is deleted.
897+
*
898+
* @since 4.0
899+
*
900+
* @param int $post_id Post ID
901+
*/
902+
function _update_posts_count_on_delete( $post_id ) {
903+
if ( 'publish' !== get_post_field( 'post_status', $post_id ) ) {
904+
return;
905+
}
906+
907+
update_posts_count();
908+
}
909+
910+
/**
911+
* Handler for updating the blog posts count date when a post status changes.
912+
*
913+
* @since 4.0
914+
*
915+
* @param string $new_status The status the post is changing to.
916+
* @param string $old_status The status the post is changing from.
917+
*/
918+
function _update_posts_count_on_transition_post_status( $new_status, $old_status ) {
919+
if ( $new_status === $old_status ) {
920+
return;
921+
}
922+
923+
if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
924+
return;
925+
}
926+
927+
update_posts_count();
928+
}
929+

src/wp-includes/ms-default-filters.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434

3535
// Administration
3636
add_filter( 'term_id_filter', 'global_terms', 10, 2 );
37-
add_action( 'publish_post', 'update_posts_count' );
37+
add_action( 'delete_post', '_update_posts_count_on_delete' );
3838
add_action( 'delete_post', '_update_blog_date_on_post_delete' );
3939
add_action( 'transition_post_status', '_update_blog_date_on_post_publish', 10, 3 );
40+
add_action( 'transition_post_status', '_update_posts_count_on_transition_post_status', 10, 2 );
4041

4142
// Counts
4243
add_action( 'admin_init', 'wp_schedule_update_network_counts');

src/wp-includes/ms-functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ function check_upload_mimes( $mimes ) {
17611761
* WordPress MS stores a blog's post count as an option so as
17621762
* to avoid extraneous COUNTs when a blog's details are fetched
17631763
* with get_blog_details(). This function is called when posts
1764-
* are published to make sure the count stays current.
1764+
* are published or unpublished to make sure the count stays current.
17651765
*
17661766
* @since MU
17671767
*/

tests/phpunit/tests/ms.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,18 @@ function test_granting_super_admins() {
13851385
$GLOBALS['super_admins'] = $old_global;
13861386
}
13871387
}
1388+
1389+
/**
1390+
* @ticket 27952
1391+
*/
1392+
function test_posts_count() {
1393+
$this->factory->post->create();
1394+
$post2 = $this->factory->post->create();
1395+
$this->assertEquals( 2, get_blog_details()->post_count );
1396+
1397+
wp_delete_post( $post2 );
1398+
$this->assertEquals( 1, get_blog_details()->post_count );
1399+
}
13881400
}
13891401

13901402
endif;

0 commit comments

Comments
 (0)