upload_is_user_over_quota()
云策文档标注
概述
upload_is_user_over_quota() 函数用于检查当前站点是否已超出其分配的存储空间配额。它通过比较允许的空间和已使用的空间来判断,并可选择性地显示警告消息。
关键要点
- 函数接受一个可选布尔参数 $display_message,默认为 true,用于控制是否在超出配额时显示警告消息。
- 返回布尔值:如果用户超出上传空间配额则返回 true,否则返回 false。
- 函数首先检查 upload_space_check_disabled 选项,如果禁用则直接返回 false。
- 使用 get_space_allowed() 获取允许的空间,如果非数字则默认为 10 MB。
- 使用 get_space_used() 获取已使用的空间,并进行比较计算。
代码示例
function upload_is_user_over_quota( $display_message = true ) {
if ( get_site_option( 'upload_space_check_disabled' ) ) {
return false;
}
$space_allowed = get_space_allowed();
if ( ! is_numeric( $space_allowed ) ) {
$space_allowed = 10; // Default space allowed is 10 MB.
}
$space_used = get_space_used();
if ( ( $space_allowed - $space_used ) <= 0 ) {
if ( $display_message ) {
printf(
__( 'Sorry, you have used your space allocation. Please delete some files to upload more files.' ),
size_format( $space_allowed * MB_IN_BYTES )
);
}
return true;
} else {
return false;
}
}注意事项
- 该函数主要用于多站点环境,检查单个站点的上传空间配额。
- 相关函数包括 get_space_allowed()、get_space_used() 和 size_format(),用于辅助计算和格式化显示。
- 在 WordPress MU 3.0.0 版本中引入,历史版本兼容性需注意。
原文内容
Checks whether a site has used its allotted upload space.
Parameters
$display_messagebooloptional-
If set to true and the quota is exceeded, a warning message is displayed.
Default:
true
Source
function upload_is_user_over_quota( $display_message = true ) {
if ( get_site_option( 'upload_space_check_disabled' ) ) {
return false;
}
$space_allowed = get_space_allowed();
if ( ! is_numeric( $space_allowed ) ) {
$space_allowed = 10; // Default space allowed is 10 MB.
}
$space_used = get_space_used();
if ( ( $space_allowed - $space_used ) < 0 ) {
if ( $display_message ) {
printf(
/* translators: %s: Allowed space allocation. */
__( 'Sorry, you have used your space allocation of %s. Please delete some files to upload more files.' ),
size_format( $space_allowed * MB_IN_BYTES )
);
}
return true;
} else {
return false;
}
}
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | Introduced. |