wp_maybe_update_network_site_counts()
云策文档标注
概述
wp_maybe_update_network_site_counts() 函数用于更新当前网络的站点数量。它通过 'enable_live_network_counts' 过滤器控制是否在站点创建或状态更新时自动更新计数。
关键要点
- 函数根据网络大小(通过 wp_is_large_network() 判断)和过滤器决定是否更新站点计数。
- 参数 $network_id 可选,默认为 null 表示当前网络,从 4.8.0 版本开始支持。
- 核心逻辑:如果网络较小且过滤器允许,则调用 wp_update_network_site_counts() 更新计数。
代码示例
function wp_maybe_update_network_site_counts( $network_id = null ) {
$is_small_network = ! wp_is_large_network( 'sites', $network_id );
if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'sites' ) ) {
return;
}
wp_update_network_site_counts( $network_id );
}注意事项
- 此函数依赖于 'enable_live_network_counts' 过滤器,默认仅在小网络中启用实时更新。
- 相关函数包括 wp_is_large_network() 和 wp_update_network_site_counts(),用于网络大小判断和实际计数更新。
原文内容
Updates the count of sites for the current network.
Description
If enabled through the ‘enable_live_network_counts’ filter, update the sites count on a network when a site is created or its status is updated.
Parameters
$network_idint|nulloptional-
ID of the network. Default is the current network.
Default:
null
Source
function wp_maybe_update_network_site_counts( $network_id = null ) {
$is_small_network = ! wp_is_large_network( 'sites', $network_id );
/**
* Filters whether to update network site or user counts when a new site is created.
*
* @since 3.7.0
*
* @see wp_is_large_network()
*
* @param bool $small_network Whether the network is considered small.
* @param string $context Context. Either 'users' or 'sites'.
*/
if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'sites' ) ) {
return;
}
wp_update_network_site_counts( $network_id );
}
Hooks
- apply_filters( ‘enable_live_network_counts’, bool $small_network, string $context )
-
Filters whether to update network site or user counts when a new site is created.