[wp-trac] [WordPress Trac] #19834: More Robust Capabilities for Attachments

WordPress Trac noreply at wordpress.org
Fri Mar 27 13:00:08 UTC 2015


#19834: More Robust Capabilities for Attachments
-----------------------------+------------------------------
 Reporter:  walkinonwat3r    |       Owner:
     Type:  feature request  |      Status:  new
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  Media            |     Version:
 Severity:  normal           |  Resolution:
 Keywords:                   |     Focuses:
-----------------------------+------------------------------

Comment (by Stagger Lee):

 Can something of these help you ?


 {{{
 // Show only own items in media uploader
 add_filter('ajax_query_attachments_args', 'sb_my_attachments_only');

 function sb_my_attachments_only($query) {
 if ($user_id = get_current_user_id()) {
 if (!current_user_can('administrator')) {
 $query['author'] = $user_id;
 }
 }

 return $query;
 }
 }}}



 {{{
 function filter_my_attachments( $wp_query ) {
     if (is_admin() && ($wp_query->query_vars['post_type'] ==
 'attachment')) {
         if ( !current_user_can( 'activate_plugins' ) ) {
             global $current_user;
             $wp_query->set( 'author', $current_user->id );
         }
     }
 }
 add_filter('parse_query', 'filter_my_attachments' );
 }}}



 {{{
 // Set a maximum upload count for users on a specific user role:
 add_filter( 'wp_handle_upload_prefilter', 'limit_uploads_for_user_roles'
 );

 function limit_uploads_for_user_roles( $file ) {
   $user = wp_get_current_user();
   // add the role you want to limit in the array
   $limit_roles = array('contributor');
   $filtered = apply_filters( 'limit_uploads_for_roles', $limit_roles,
 $user );
   if ( array_intersect( $limit_roles, $user->roles ) ) {
     $upload_count = get_user_meta( $user->ID, 'upload_count', true ) ? :
 0;
     $limit = apply_filters( 'limit_uploads_for_user_roles_limit', 10,
 $user,$upload_count, $file );
     if ( ( $upload_count + 1 ) > $limit ) {
       $file['error'] = __('Upload limit has been reached for this
 account!','yourtxtdomain');
     } else {
       update_user_meta( $user->ID, 'upload_count', $upload_count + 1 );
     }
   }
   return $file;
 }
 }}}



 {{{
 // This action will fire when user delete attachment (countdown):
 add_action('delete_attachment', 'decrease_limit_uploads_for_user');
 function decrease_limit_uploads_for_user( $id ) {
    $user = wp_get_current_user();
    // add the role you want to limit in the array
    $limit_roles = array('contributor');
    $filtered = apply_filters( 'limit_uploads_for_roles', $limit_roles,
 $user );
    if ( array_intersect( $limit_roles, $user->roles ) ) {
      $post = get_post( $id);
      if ( $post->post_author != $user->ID ) return;
      $count = get_user_meta( $user->ID, 'upload_count', true ) ? : 0;
      if ( $count ) update_user_meta( $user->ID, 'upload_count', $count - 1
 );
    }
 }
 }}}



 {{{
 // Limit user upload by KB size:
 add_filter('wp_handle_upload_prefilter', 'f711_image_size_prevent');
 function f711_image_size_prevent($file) {

     // get filesize of upload
     $size = $file['size'];
     $size = $size / 1024; // Calculate down to KB

     // get imagetype of upload
     $type = $file['type'];
     $is_image = strpos($type, 'image');

     // set sizelimit
     $limit = 700; // Your Filesize in KB

     // set imagelimit
     $imagelimit = 7;

     // set allowed imagetype
     $imagetype = 'image/jpeg';

     // query how many images the current user already uploaded
     global $current_user;
     $args = array(
         'orderby'         => 'post_date',
         'order'           => 'DESC',
         'numberposts'     => -1,
         'post_type'       => 'attachment',
         'author'          => $current_user->ID,
     );
     $attachmentsbyuser = get_posts( $args );

     if ( ( $size > $limit ) && ($is_image !== false) ) { // check if the
 image is small enough
         $file['error'] = 'Image files must be smaller than '.$limit.'KB';
     } elseif ( $type != $imagetype ) { // check if image type is allowed
         $file['error'] = 'Image must be ' . $imagetype . '.';
     } elseif ( count( $attachmentsbyuser ) >= $imagelimit ) { // check if
 the user has exceeded the image limit
         $file['error'] = 'Image limit of ' . $imagelimit . ' is exceeded
 for this user.';
     }
     return $file;

 }
 }}}



 {{{
 function ter_limit_image_uploads($file){
         $error_message = 'KB is to large. Please reduce the file size of
 your image to ' . TER_MAX_IMAGE_SIZE_KB . 'KB or less. Hosting space is
 limited and uploading large images will slow down your page loads. Use a
 tool such as http://toki-woki.net/p/Shrink-O-Matic/ to help you to resize
 your images. Target size: Width 1024px, file size 100KB-200KB';
         $size_in_kb = $file['size'] / 1024;
         if(preg_match('/image/',$file['type'])) if($size_in_kb >
 TER_MAX_IMAGE_SIZE_KB) $file['error'] = round($size_in_kb,2) .
 $error_message;
         return $file;
 }
 add_filter('wp_handle_upload_prefilter','ter_limit_image_uploads');
 //Limit uploaded image size
 }}}


 Tested and they work well.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/19834#comment:30>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list