From 0233933f22e5c628efc07b5bfce30e5e848daf6a Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 26 Jun 2014 00:53:15 +0000 Subject: [PATCH] `get_blog_details()->post_count` should update on more actions than just `publish_post`. Adds unit test. Props 5um17, midxcat, strangerstudios. Fixes #27952. Built from https://develop.svn.wordpress.org/trunk@28835 git-svn-id: http://core.svn.wordpress.org/trunk@28639 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/ms-blogs.php | 35 ++++++++++++++++++++++++++++++ wp-includes/ms-default-filters.php | 3 ++- wp-includes/ms-functions.php | 2 +- wp-includes/version.php | 2 +- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/wp-includes/ms-blogs.php b/wp-includes/ms-blogs.php index 16a491818a..8aa702a937 100644 --- a/wp-includes/ms-blogs.php +++ b/wp-includes/ms-blogs.php @@ -892,3 +892,38 @@ function _update_blog_date_on_post_delete( $post_id ) { wpmu_update_blogs_date(); } +/** + * Handler for updating the blog posts count date when a post is deleted. + * + * @since 4.0 + * + * @param int $post_id Post ID + */ +function _update_posts_count_on_delete( $post_id ) { + if ( 'publish' !== get_post_field( 'post_status', $post_id ) ) { + return; + } + + update_posts_count(); +} + +/** + * Handler for updating the blog posts count date when a post status changes. + * + * @since 4.0 + * + * @param string $new_status The status the post is changing to. + * @param string $old_status The status the post is changing from. + */ +function _update_posts_count_on_transition_post_status( $new_status, $old_status ) { + if ( $new_status === $old_status ) { + return; + } + + if ( 'publish' !== $new_status && 'publish' !== $old_status ) { + return; + } + + update_posts_count(); +} + diff --git a/wp-includes/ms-default-filters.php b/wp-includes/ms-default-filters.php index 759c81f0ed..00b3bda982 100644 --- a/wp-includes/ms-default-filters.php +++ b/wp-includes/ms-default-filters.php @@ -34,9 +34,10 @@ add_filter( 'allowed_redirect_hosts', 'redirect_this_site' ); // Administration add_filter( 'term_id_filter', 'global_terms', 10, 2 ); -add_action( 'publish_post', 'update_posts_count' ); +add_action( 'delete_post', '_update_posts_count_on_delete' ); add_action( 'delete_post', '_update_blog_date_on_post_delete' ); add_action( 'transition_post_status', '_update_blog_date_on_post_publish', 10, 3 ); +add_action( 'transition_post_status', '_update_posts_count_on_transition_post_status', 10, 2 ); // Counts add_action( 'admin_init', 'wp_schedule_update_network_counts'); diff --git a/wp-includes/ms-functions.php b/wp-includes/ms-functions.php index 9166251a3c..6d7a2fbd52 100644 --- a/wp-includes/ms-functions.php +++ b/wp-includes/ms-functions.php @@ -1761,7 +1761,7 @@ function check_upload_mimes( $mimes ) { * WordPress MS stores a blog's post count as an option so as * to avoid extraneous COUNTs when a blog's details are fetched * with get_blog_details(). This function is called when posts - * are published to make sure the count stays current. + * are published or unpublished to make sure the count stays current. * * @since MU */ diff --git a/wp-includes/version.php b/wp-includes/version.php index 96677f6234..9522bdc96f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.0-alpha-20140625'; +$wp_version = '4.0-alpha-20140626'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.