Answers:
假设您是通过WordPress的本机功能(如lik 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
。
apply_filters
代码将允许其他插件挂接到那里,这将很有用。您能否描述问题的性质?
我已经修改了Soulseekah的代码,因为apply_filter
变量对我不起作用-可能是因为我不理解它们!
# [File Upload]
#
# Two filters to give users a maximum upload limit of 10Mb and 100 files.
# This function runs after the file has been uploaded.
add_filter( 'wp_handle_upload', 'wpse47580_update_upload_stats' );
function wpse47580_update_upload_stats( $args ) {
$size = filesize( $args['file'] );
$user_id = get_current_user_id();
$upload_count = get_user_meta( $user_id, 'upload_count', true );
$upload_bytes = get_user_meta( $user_id, 'upload_bytes', true );
update_user_meta( $user_id, 'upload_count', $upload_count + 1 );
update_user_meta( $user_id, 'upload_bytes', $upload_bytes + $size );
}
# This function runs before the file is uploaded.
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', true );
$upload_bytes = get_user_meta( $user_id, 'upload_bytes', true );
$filesize = $file['size']; // bytes
$upload_bytes_limit_reached = ( ( $filesize + $upload_bytes ) > ( 1024 * 1024 * 10 ) );
$upload_count_limit_reached = ( $upload_count + 1 ) > 100;
if ( $upload_count_limit_reached || $upload_bytes_limit_reached )
$file['error'] = 'Upload limit has been reached for this account!';
return $file;
}
从中制作一个插件真的很简单,所以当我为它开发一个接口时,我可能会在将来的某个时候发布它。
$upload_bytes_limit_reached = apply_filters( 'wpse47580_upload_bytes_limit_reached', 1024*1024*10 ) > ( $filesize + $upload_bytes );