4 * Site/blog functions that work with the blogs table and related data.
7 * @subpackage Multisite
12 * Update the last_updated field for the current blog.
18 function wpmu_update_blogs_date() {
21 update_blog_details( $wpdb->blogid, array('last_updated' => current_time('mysql', true)) );
23 * Fires after the blog details are updated.
27 * @param int $blog_id Blog ID.
29 do_action( 'wpmu_blog_updated', $wpdb->blogid );
33 * Get a full blog URL, given a blog id.
37 * @param int $blog_id Blog ID
38 * @return string Full URL of the blog if found. Empty string if not.
40 function get_blogaddress_by_id( $blog_id ) {
41 $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
42 return ( $bloginfo ) ? esc_url( 'http://' . $bloginfo->domain . $bloginfo->path ) : '';
46 * Get a full blog URL, given a blog name.
50 * @param string $blogname The (subdomain or directory) name
53 function get_blogaddress_by_name( $blogname ) {
54 if ( is_subdomain_install() ) {
55 if ( $blogname == 'main' )
57 $url = rtrim( network_home_url(), '/' );
58 if ( !empty( $blogname ) )
59 $url = preg_replace( '|^([^\.]+://)|', "\${1}" . $blogname . '.', $url );
61 $url = network_home_url( $blogname );
63 return esc_url( $url . '/' );
67 * Given a blog's (subdomain or directory) slug, retrieve its id.
74 * @return int A blog id
76 function get_id_from_blogname( $slug ) {
79 $current_site = get_current_site();
80 $slug = trim( $slug, '/' );
82 $blog_id = wp_cache_get( 'get_id_from_blogname_' . $slug, 'blog-details' );
86 if ( is_subdomain_install() ) {
87 $domain = $slug . '.' . $current_site->domain;
88 $path = $current_site->path;
90 $domain = $current_site->domain;
91 $path = $current_site->path . $slug . '/';
94 $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
95 wp_cache_set( 'get_id_from_blogname_' . $slug, $blog_id, 'blog-details' );
100 * Retrieve the details for a blog from the blogs table and blog options.
106 * @param int|string|array $fields Optional. A blog ID, a blog slug, or an array of fields to query against.
107 * If not specified the current blog ID is used.
108 * @param bool $get_all Whether to retrieve all details or only the details in the blogs table.
110 * @return object|false Blog details on success. False on failure.
112 function get_blog_details( $fields = null, $get_all = true ) {
115 if ( is_array($fields ) ) {
116 if ( isset($fields['blog_id']) ) {
117 $blog_id = $fields['blog_id'];
118 } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
119 $key = md5( $fields['domain'] . $fields['path'] );
120 $blog = wp_cache_get($key, 'blog-lookup');
121 if ( false !== $blog )
123 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
124 $nowww = substr( $fields['domain'], 4 );
125 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) );
127 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
130 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
131 $blog_id = $blog->blog_id;
135 } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
136 $key = md5( $fields['domain'] );
137 $blog = wp_cache_get($key, 'blog-lookup');
138 if ( false !== $blog )
140 if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
141 $nowww = substr( $fields['domain'], 4 );
142 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
144 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
147 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
148 $blog_id = $blog->blog_id;
157 $blog_id = get_current_blog_id();
158 elseif ( ! is_numeric( $fields ) )
159 $blog_id = get_id_from_blogname( $fields );
164 $blog_id = (int) $blog_id;
166 $all = $get_all == true ? '' : 'short';
167 $details = wp_cache_get( $blog_id . $all, 'blog-details' );
170 if ( ! is_object( $details ) ) {
171 if ( $details == -1 ) {
174 // Clear old pre-serialized objects. Cache clients do better with that.
175 wp_cache_delete( $blog_id . $all, 'blog-details' );
183 // Try the other cache.
185 $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
187 $details = wp_cache_get( $blog_id, 'blog-details' );
188 // If short was requested and full cache is set, we can return.
190 if ( ! is_object( $details ) ) {
191 if ( $details == -1 ) {
194 // Clear old pre-serialized objects. Cache clients do better with that.
195 wp_cache_delete( $blog_id, 'blog-details' );
204 if ( empty($details) ) {
205 $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) );
207 // Set the full cache.
208 wp_cache_set( $blog_id, -1, 'blog-details' );
214 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
218 switch_to_blog( $blog_id );
219 $details->blogname = get_option( 'blogname' );
220 $details->siteurl = get_option( 'siteurl' );
221 $details->post_count = get_option( 'post_count' );
222 restore_current_blog();
225 * Filter a blog's details.
229 * @param object $details The blog details.
231 $details = apply_filters( 'blog_details', $details );
233 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
235 $key = md5( $details->domain . $details->path );
236 wp_cache_set( $key, $details, 'blog-lookup' );
242 * Clear the blog details cache.
246 * @param int $blog_id Optional. Blog ID. Defaults to current blog.
248 function refresh_blog_details( $blog_id = 0 ) {
249 $blog_id = (int) $blog_id;
251 $blog_id = get_current_blog_id();
254 $details = get_blog_details( $blog_id, false );
256 // Make sure clean_blog_cache() gets the blog ID
257 // when the blog has been previously cached as
259 $details = (object) array(
260 'blog_id' => $blog_id,
266 clean_blog_cache( $details );
269 * Fires after the blog details cache is cleared.
273 * @param int $blog_id Blog ID.
275 do_action( 'refresh_blog_details', $blog_id );
279 * Update the details for a blog. Updates the blogs table for a given blog id.
285 * @param int $blog_id Blog ID
286 * @param array $details Array of details keyed by blogs table field names.
287 * @return bool True if update succeeds, false otherwise.
289 function update_blog_details( $blog_id, $details = array() ) {
292 if ( empty($details) )
295 if ( is_object($details) )
296 $details = get_object_vars($details);
298 $current_details = get_blog_details($blog_id, false);
299 if ( empty($current_details) )
302 $current_details = get_object_vars($current_details);
304 $details = array_merge($current_details, $details);
305 $details['last_updated'] = current_time('mysql', true);
307 $update_details = array();
308 $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
309 foreach ( array_intersect( array_keys( $details ), $fields ) as $field ) {
310 if ( 'path' === $field ) {
311 $details[ $field ] = trailingslashit( '/' . trim( $details[ $field ], '/' ) );
314 $update_details[ $field ] = $details[ $field ];
317 $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
319 if ( false === $result )
322 // If spam status changed, issue actions.
323 if ( $details['spam'] != $current_details['spam'] ) {
324 if ( $details['spam'] == 1 ) {
326 * Fires when the blog status is changed to 'spam'.
330 * @param int $blog_id Blog ID.
332 do_action( 'make_spam_blog', $blog_id );
335 * Fires when the blog status is changed to 'ham'.
339 * @param int $blog_id Blog ID.
341 do_action( 'make_ham_blog', $blog_id );
345 // If mature status changed, issue actions.
346 if ( $details['mature'] != $current_details['mature'] ) {
347 if ( $details['mature'] == 1 ) {
349 * Fires when the blog status is changed to 'mature'.
353 * @param int $blog_id Blog ID.
355 do_action( 'mature_blog', $blog_id );
358 * Fires when the blog status is changed to 'unmature'.
362 * @param int $blog_id Blog ID.
364 do_action( 'unmature_blog', $blog_id );
368 // If archived status changed, issue actions.
369 if ( $details['archived'] != $current_details['archived'] ) {
370 if ( $details['archived'] == 1 ) {
372 * Fires when the blog status is changed to 'archived'.
376 * @param int $blog_id Blog ID.
378 do_action( 'archive_blog', $blog_id );
381 * Fires when the blog status is changed to 'unarchived'.
385 * @param int $blog_id Blog ID.
387 do_action( 'unarchive_blog', $blog_id );
391 // If deleted status changed, issue actions.
392 if ( $details['deleted'] != $current_details['deleted'] ) {
393 if ( $details['deleted'] == 1 ) {
395 * Fires when the blog status is changed to 'deleted'.
399 * @param int $blog_id Blog ID.
401 do_action( 'make_delete_blog', $blog_id );
404 * Fires when the blog status is changed to 'undeleted'.
408 * @param int $blog_id Blog ID.
410 do_action( 'make_undelete_blog', $blog_id );
414 if ( isset( $details['public'] ) ) {
415 switch_to_blog( $blog_id );
416 update_option( 'blog_public', $details['public'] );
417 restore_current_blog();
420 refresh_blog_details($blog_id);
426 * Clean the blog cache
430 * @param stdClass $blog The blog details as returned from get_blog_details()
432 function clean_blog_cache( $blog ) {
433 $blog_id = $blog->blog_id;
434 $domain_path_key = md5( $blog->domain . $blog->path );
436 wp_cache_delete( $blog_id , 'blog-details' );
437 wp_cache_delete( $blog_id . 'short' , 'blog-details' );
438 wp_cache_delete( $domain_path_key, 'blog-lookup' );
439 wp_cache_delete( 'current_blog_' . $blog->domain, 'site-options' );
440 wp_cache_delete( 'current_blog_' . $blog->domain . $blog->path, 'site-options' );
441 wp_cache_delete( 'get_id_from_blogname_' . trim( $blog->path, '/' ), 'blog-details' );
442 wp_cache_delete( $domain_path_key, 'blog-id-cache' );
446 * Retrieve option value for a given blog id based on name of option.
448 * If the option does not exist or does not have a value, then the return value
449 * will be false. This is useful to check whether you need to install an option
450 * and is commonly used during installation of plugin options and to test
451 * whether upgrading is required.
453 * If the option was serialized then it will be unserialized when it is returned.
457 * @param int $id A blog ID. Can be null to refer to the current blog.
458 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
459 * @param mixed $default Optional. Default value to return if the option does not exist.
460 * @return mixed Value set for the option.
462 function get_blog_option( $id, $option, $default = false ) {
466 $id = get_current_blog_id();
468 if ( get_current_blog_id() == $id )
469 return get_option( $option, $default );
471 switch_to_blog( $id );
472 $value = get_option( $option, $default );
473 restore_current_blog();
476 * Filter a blog option value.
478 * The dynamic portion of the hook name, `$option`, refers to the blog option name.
482 * @param string $value The option value.
483 * @param int $id Blog ID.
485 return apply_filters( "blog_option_{$option}", $value, $id );
489 * Add a new option for a given blog id.
491 * You do not need to serialize values. If the value needs to be serialized, then
492 * it will be serialized before it is inserted into the database. Remember,
493 * resources can not be serialized or added as an option.
495 * You can create options without values and then update the values later.
496 * Existing options will not be updated and checks are performed to ensure that you
497 * aren't adding a protected WordPress option. Care should be taken to not name
498 * options the same as the ones which are protected.
502 * @param int $id A blog ID. Can be null to refer to the current blog.
503 * @param string $option Name of option to add. Expected to not be SQL-escaped.
504 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
505 * @return bool False if option was not added and true if option was added.
507 function add_blog_option( $id, $option, $value ) {
511 $id = get_current_blog_id();
513 if ( get_current_blog_id() == $id )
514 return add_option( $option, $value );
516 switch_to_blog( $id );
517 $return = add_option( $option, $value );
518 restore_current_blog();
524 * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
528 * @param int $id A blog ID. Can be null to refer to the current blog.
529 * @param string $option Name of option to remove. Expected to not be SQL-escaped.
530 * @return bool True, if option is successfully deleted. False on failure.
532 function delete_blog_option( $id, $option ) {
536 $id = get_current_blog_id();
538 if ( get_current_blog_id() == $id )
539 return delete_option( $option );
541 switch_to_blog( $id );
542 $return = delete_option( $option );
543 restore_current_blog();
549 * Update an option for a particular blog.
553 * @param int $id The blog id
554 * @param string $option The option key
555 * @param mixed $value The option value
556 * @return bool True on success, false on failure.
558 function update_blog_option( $id, $option, $value, $deprecated = null ) {
561 if ( null !== $deprecated )
562 _deprecated_argument( __FUNCTION__, '3.1' );
564 if ( get_current_blog_id() == $id )
565 return update_option( $option, $value );
567 switch_to_blog( $id );
568 $return = update_option( $option, $value );
569 restore_current_blog();
571 refresh_blog_details( $id );
577 * Switch the current blog.
579 * This function is useful if you need to pull posts, or other information,
580 * from other blogs. You can switch back afterwards using restore_current_blog().
582 * Things that aren't switched:
583 * - autoloaded options. See #14992
584 * - plugins. See #14941
586 * @see restore_current_blog()
590 * @global int $blog_id
591 * @global array $_wp_switched_stack
592 * @global bool $switched
593 * @global string $table_prefix
594 * @global WP_Object_Cache $wp_object_cache
596 * @param int $new_blog The id of the blog you want to switch to. Default: current blog
597 * @param bool $deprecated Deprecated argument
598 * @return true Always returns True.
600 function switch_to_blog( $new_blog, $deprecated = null ) {
603 if ( empty( $new_blog ) )
604 $new_blog = $GLOBALS['blog_id'];
606 $GLOBALS['_wp_switched_stack'][] = $GLOBALS['blog_id'];
609 * If we're switching to the same blog id that we're on,
610 * set the right vars, do the associated actions, but skip
611 * the extra unnecessary work
613 if ( $new_blog == $GLOBALS['blog_id'] ) {
615 * Fires when the blog is switched.
619 * @param int $new_blog New blog ID.
620 * @param int $new_blog Blog ID.
622 do_action( 'switch_blog', $new_blog, $new_blog );
623 $GLOBALS['switched'] = true;
627 $wpdb->set_blog_id( $new_blog );
628 $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
629 $prev_blog_id = $GLOBALS['blog_id'];
630 $GLOBALS['blog_id'] = $new_blog;
632 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
633 wp_cache_switch_to_blog( $new_blog );
635 global $wp_object_cache;
637 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
638 $global_groups = $wp_object_cache->global_groups;
640 $global_groups = false;
644 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
645 if ( is_array( $global_groups ) ) {
646 wp_cache_add_global_groups( $global_groups );
648 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache' ) );
650 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
654 if ( did_action( 'init' ) ) {
655 wp_roles()->reinit();
656 $current_user = wp_get_current_user();
657 $current_user->for_blog( $new_blog );
660 /** This filter is documented in wp-includes/ms-blogs.php */
661 do_action( 'switch_blog', $new_blog, $prev_blog_id );
662 $GLOBALS['switched'] = true;
668 * Restore the current blog, after calling switch_to_blog()
670 * @see switch_to_blog()
674 * @global array $_wp_switched_stack
675 * @global int $blog_id
676 * @global bool $switched
677 * @global string $table_prefix
678 * @global WP_Object_Cache $wp_object_cache
680 * @return bool True on success, false if we're already on the current blog
682 function restore_current_blog() {
685 if ( empty( $GLOBALS['_wp_switched_stack'] ) )
688 $blog = array_pop( $GLOBALS['_wp_switched_stack'] );
690 if ( $GLOBALS['blog_id'] == $blog ) {
691 /** This filter is documented in wp-includes/ms-blogs.php */
692 do_action( 'switch_blog', $blog, $blog );
693 // If we still have items in the switched stack, consider ourselves still 'switched'
694 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
698 $wpdb->set_blog_id( $blog );
699 $prev_blog_id = $GLOBALS['blog_id'];
700 $GLOBALS['blog_id'] = $blog;
701 $GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
703 if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
704 wp_cache_switch_to_blog( $blog );
706 global $wp_object_cache;
708 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
709 $global_groups = $wp_object_cache->global_groups;
711 $global_groups = false;
715 if ( function_exists( 'wp_cache_add_global_groups' ) ) {
716 if ( is_array( $global_groups ) ) {
717 wp_cache_add_global_groups( $global_groups );
719 wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache' ) );
721 wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
725 if ( did_action( 'init' ) ) {
726 wp_roles()->reinit();
727 $current_user = wp_get_current_user();
728 $current_user->for_blog( $blog );
731 /** This filter is documented in wp-includes/ms-blogs.php */
732 do_action( 'switch_blog', $blog, $prev_blog_id );
734 // If we still have items in the switched stack, consider ourselves still 'switched'
735 $GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
741 * Determines if switch_to_blog() is in effect
745 * @global array $_wp_switched_stack
747 * @return bool True if switched, false otherwise.
749 function ms_is_switched() {
750 return ! empty( $GLOBALS['_wp_switched_stack'] );
754 * Check if a particular blog is archived.
758 * @param int $id The blog id
759 * @return string Whether the blog is archived or not
761 function is_archived( $id ) {
762 return get_blog_status($id, 'archived');
766 * Update the 'archived' status of a particular blog.
770 * @param int $id The blog id
771 * @param string $archived The new status
772 * @return string $archived
774 function update_archived( $id, $archived ) {
775 update_blog_status($id, 'archived', $archived);
780 * Update a blog details field.
786 * @param int $blog_id BLog ID
787 * @param string $pref A field name
788 * @param string $value Value for $pref
789 * @param null $deprecated
790 * @return string|false $value
792 function update_blog_status( $blog_id, $pref, $value, $deprecated = null ) {
795 if ( null !== $deprecated )
796 _deprecated_argument( __FUNCTION__, '3.1' );
798 if ( ! in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
801 $result = $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
803 if ( false === $result )
806 refresh_blog_details( $blog_id );
808 if ( 'spam' == $pref ) {
810 /** This filter is documented in wp-includes/ms-blogs.php */
811 do_action( 'make_spam_blog', $blog_id );
813 /** This filter is documented in wp-includes/ms-blogs.php */
814 do_action( 'make_ham_blog', $blog_id );
816 } elseif ( 'mature' == $pref ) {
818 /** This filter is documented in wp-includes/ms-blogs.php */
819 do_action( 'mature_blog', $blog_id );
821 /** This filter is documented in wp-includes/ms-blogs.php */
822 do_action( 'unmature_blog', $blog_id );
824 } elseif ( 'archived' == $pref ) {
826 /** This filter is documented in wp-includes/ms-blogs.php */
827 do_action( 'archive_blog', $blog_id );
829 /** This filter is documented in wp-includes/ms-blogs.php */
830 do_action( 'unarchive_blog', $blog_id );
832 } elseif ( 'deleted' == $pref ) {
834 /** This filter is documented in wp-includes/ms-blogs.php */
835 do_action( 'make_delete_blog', $blog_id );
837 /** This filter is documented in wp-includes/ms-blogs.php */
838 do_action( 'make_undelete_blog', $blog_id );
840 } elseif ( 'public' == $pref ) {
842 * Fires after the current blog's 'public' setting is updated.
846 * @param int $blog_id Blog ID.
847 * @param string $value The value of blog status.
849 do_action( 'update_blog_public', $blog_id, $value ); // Moved here from update_blog_public().
856 * Get a blog details field.
862 * @param int $id The blog id
863 * @param string $pref A field name
864 * @return bool|string|null $value
866 function get_blog_status( $id, $pref ) {
869 $details = get_blog_details( $id, false );
871 return $details->$pref;
873 return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
877 * Get a list of most recently updated blogs.
883 * @param mixed $deprecated Not used
884 * @param int $start The offset
885 * @param int $quantity The maximum number of blogs to retrieve. Default is 40.
886 * @return array The list of blogs
888 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
891 if ( ! empty( $deprecated ) )
892 _deprecated_argument( __FUNCTION__, 'MU' ); // never used
894 return $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' ORDER BY last_updated DESC limit %d, %d", $wpdb->siteid, $start, $quantity ) , ARRAY_A );
898 * Handler for updating the blog date when a post is published or an already published post is changed.
902 * @param string $new_status The new post status
903 * @param string $old_status The old post status
904 * @param object $post Post object
906 function _update_blog_date_on_post_publish( $new_status, $old_status, $post ) {
907 $post_type_obj = get_post_type_object( $post->post_type );
908 if ( ! $post_type_obj || ! $post_type_obj->public ) {
912 if ( 'publish' != $new_status && 'publish' != $old_status ) {
916 // Post was freshly published, published post was saved, or published post was unpublished.
918 wpmu_update_blogs_date();
922 * Handler for updating the blog date when a published post is deleted.
926 * @param int $post_id Post ID
928 function _update_blog_date_on_post_delete( $post_id ) {
929 $post = get_post( $post_id );
931 $post_type_obj = get_post_type_object( $post->post_type );
932 if ( ! $post_type_obj || ! $post_type_obj->public ) {
936 if ( 'publish' != $post->post_status ) {
940 wpmu_update_blogs_date();
944 * Handler for updating the blog posts count date when a post is deleted.
948 * @param int $post_id Post ID.
950 function _update_posts_count_on_delete( $post_id ) {
951 $post = get_post( $post_id );
953 if ( ! $post || 'publish' !== $post->post_status ) {
957 update_posts_count();
961 * Handler for updating the blog posts count date when a post status changes.
965 * @param string $new_status The status the post is changing to.
966 * @param string $old_status The status the post is changing from.
968 function _update_posts_count_on_transition_post_status( $new_status, $old_status ) {
969 if ( $new_status === $old_status ) {
973 if ( 'publish' !== $new_status && 'publish' !== $old_status ) {
977 update_posts_count();