wp_maybe_update_user_counts()
云策文档标注
概述
wp_maybe_update_user_counts() 函数用于在启用实时用户计数时更新网站的用户总数。它检查是否满足更新条件,并调用 wp_update_user_counts() 执行更新。
关键要点
- 函数仅在启用实时网络计数(通过 enable_live_network_counts 过滤器控制)时更新用户计数。
- 参数 $network_id 可选,默认为 null,在非多站点环境中传递此参数会触发 _doing_it_wrong() 警告。
- 返回值是布尔类型,表示更新是否成功。
- 内部使用 wp_is_large_user_count() 判断网络规模,并依赖 wp_update_user_counts() 执行实际更新。
代码示例
function wp_maybe_update_user_counts( $network_id = null ) {
if ( ! is_multisite() && null !== $network_id ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %s: $network_id */
__( 'Unable to pass %s if not using multisite.' ),
'$network_id'
),
'6.0.0'
);
}
$is_small_network = ! wp_is_large_user_count( $network_id );
/** This filter is documented in wp-includes/ms-functions.php */
if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) ) {
return false;
}
return wp_update_user_counts( $network_id );
}注意事项
- 在非多站点环境中,不应传递 $network_id 参数,否则会触发错误警告。
- 更新是否执行取决于 enable_live_network_counts 过滤器的返回值,开发者可以通过此过滤器控制实时计数行为。
- 此函数自 WordPress 6.0.0 版本引入。
原文内容
Updates the total count of users on the site if live user counting is enabled.
Parameters
$network_idint|nulloptional-
ID of the network. Defaults to the current network.
Default:
null
Source
function wp_maybe_update_user_counts( $network_id = null ) {
if ( ! is_multisite() && null !== $network_id ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %s: $network_id */
__( 'Unable to pass %s if not using multisite.' ),
'<code>$network_id</code>'
),
'6.0.0'
);
}
$is_small_network = ! wp_is_large_user_count( $network_id );
/** This filter is documented in wp-includes/ms-functions.php */
if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) ) {
return false;
}
return wp_update_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.
Changelog
| Version | Description |
|---|---|
| 6.0.0 | Introduced. |