假设您通过WordPress的本机功能提供上传功能,例如wp_handle_upload
或者更高层次的东西,我们得出结论,几个钩子将被拉。
http://core.trac.wordpress.org/browser/tags/3.3/wp-admin/includes/file.php#L212
这个
wp_handle_upload
函数可能是最后一个接触该文件的本机函数,并且知道跟踪所需的所有信息。
此函数中有两个挂钩:wp_handle_upload
和wp_handle_upload_prefilter
. 后者是第一位的,这可能会检查当前限制并阻止文件上载。前者将跟踪文件大小和计数。存储信息将由update_user_meta
.
add_filter( \'wp_handle_upload\', \'wpse47580_update_upload_stats\' );
function wpse47580_update_upload_stats( $args ) {
$file = $args[\'file\'];
$size = filesize( $file ); // bytes
$user_id = get_current_user_id();
$upload_count = get_user_meta( $user_id, \'upload_count\', $single = true );
$upload_bytes = get_user_meta( $user_id, \'upload_bytes\', $single = true );
update_user_meta( $user_id, \'upload_count\', $upload_count + 1 );
update_user_meta( $user_id, \'upload_bytes\', $upload_bytes + $size );
}
add_filter( \'wp_handle_upload_prefilter\', \'wpse47580_check_upload_limits\' );
function wpse47580_check_upload_limits( $file ) {
$user_id = get_current_user_id();
$upload_count = get_user_meta( $user_id, \'upload_count\', $single = true );
$upload_bytes = get_user_meta( $user_id, \'upload_bytes\', $single = true );
$filesize = /* get filesize from $file array */;
$upload_bytes_limit_reached = apply_filters( \'wpse47580_upload_bytes_limit_reached\', 1024*1024*10 ) > ( $filesize + $upload_bytes );
$upload_count_limit_reached = apply_filters( \'wpse47580_upload_count_limit_reached\', 100 ) > ( $upload_count + 1 );
if ( $upload_count_limit_reached || $upload_bytes_limit_reached )
$file[\'error\'] = \'Upload limit has been reached for this account!\';
return $file;
}
理论上,这是可行的;几乎没有经过测试。让我们知道进展如何。
每个帖子的上传限制将保留在帖子元中,可能类似于{$user_id}_upload_count
等等,我不明白为什么这样做行不通。
如果您使用自定义代码来处理上传(我怀疑),那么您可以实现自己的操作和过滤器,就像wp_handle_uploads
做