wp_maybe_update_network_user_counts()
云策文档标注
概述
wp_maybe_update_network_user_counts() 函数用于在网络中更新用户计数,但仅在通过 'enable_live_network_counts' 过滤器启用时执行。它检查网络规模,并在条件满足时调用 wp_update_network_user_counts() 来更新计数。
关键要点
- 函数功能:有条件地更新网络范围内的用户计数,基于 'enable_live_network_counts' 过滤器和网络规模判断。
- 参数:$network_id(可选,整数或 null),指定网络 ID,默认为当前网络。
- 依赖过滤器:apply_filters('enable_live_network_counts', bool $small_network, string $context),控制是否在用户创建或状态更新时更新计数。
- 相关函数:wp_is_large_network() 用于判断网络是否为大网络,wp_update_network_user_counts() 用于实际更新计数。
- 版本变更:从 4.8.0 版本开始添加了 $network_id 参数,最初在 3.7.0 版本引入。
代码示例
function wp_maybe_update_network_user_counts( $network_id = null ) {
$is_small_network = ! wp_is_large_network( 'users', $network_id );
/** This filter is documented in wp-includes/ms-functions.php */
if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) ) {
return;
}
wp_update_network_user_counts( $network_id );
}
原文内容
Updates the network-wide users count.
Description
If enabled through the ‘enable_live_network_counts’ filter, update the users count on a network when a user 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_user_counts( $network_id = null ) {
$is_small_network = ! wp_is_large_network( 'users', $network_id );
/** This filter is documented in wp-includes/ms-functions.php */
if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) ) {
return;
}
wp_update_network_user_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.