BuddyDev

Search

[Resolved] Limit the Number of Media Uploads Per Gallery

  • Participant
    Level: Enlightened
    Posts: 31
    Brian on #6998

    Hello!

    Finishing up my project.
    Was wondering if there is a way to limit the number of actual uploads per MediaPress gallery?
    Not talking upload quota, but in terms of numbers.

    For example, I’d like to make it to restrict the number of photo’s a user can upload to 8, and the number of Audio uploads to 10 (Kind of strange, but that’s how I have it).

    Thoughts?

    Thank you!

  • Participant
    Level: Enlightened
    Posts: 31
    Brian on #7014

    Looked into this a little bit more. Seems this is the proper hook that I’m going to be wanting to latch onto:

    ——————–
    mpp_user_can_upload:
    ——————–

    function mpp_user_can_upload( $component, $component_id, $gallery = null ) {
    if ( ! is_user_logged_in() ) {
    return false;
    }
    $can_do = false;
    $user_id = get_current_user_id();
    if ( is_super_admin() ) {
    $can_do = true;
    } elseif ( mediapress()->is_bp_active() && $component == ‘members’ && $component_id == $user_id ) {
    $can_do = true;
    } elseif ( mpp_is_active_component( ‘groups’ ) && $component == ‘groups’ && function_exists( ‘groups_is_user_member’ ) && groups_is_user_member( $user_id, $component_id ) ) {
    $can_do = true;
    } elseif ( mpp_is_active_component( ‘sitewide’ ) && $component == ‘sitewide’ && $component_id == $user_id ) {
    $can_do = true;
    }
    $can_do = apply_filters( ‘mpp_user_can_upload’, $can_do, $component, $component_id, $gallery );
    return apply_filters( “mpp_can_user_upload_to_{$component}”, $can_do, $gallery );
    }

    ——————–
    ——————–

    Also I’m getting a current count of the number of media items in the gallery on one of my pages by using:

    $audioattach = get_children( array( ‘post_parent’ => $audiogalleryid ) );
    $audiocount = count( $audioattach );

    &

    $photoattach = get_children( array( ‘post_parent’ => $photogalleryid ) );
    $photocount = count( $photoattach );

    So I’m thinking it is possible to simply use PHP to remove the upload link once a user gets over their limit.
    But this may be a band-aid solution.

    And I’m not familiar with properly using hooks quite well, but I can try and mess around with this.

    If you have any thoughts Brajesh, I’d love to hear it. Also an FYI be having my boss buy a related plugin soon when this is getting ready to launch.

  • Keymaster
    (BuddyDev Team)
    Posts: 22211
    Brajesh Singh on #7022

    Hi Brian,
    You are looking in the right direction.

    I suggest filtering on

    mpp_user_can_create_gallery to restrict gallery creation

    and

    mpp_user_can_upload' for restricting upload.
    
    In both the cases, you should check for the total uploaded media by the logged in user.
    

    $total_uploaded = mpp_get_media_count(array( ‘component_id’ => bp_loggedin_user_id(), ‘component’=> ‘members’ ) );

    `

    I believe that should allow you to do it. You can avoid the last function and do a query on posts table for attachment where post_author is logged in user id and that will work too.

    Hope that helps.

  • Participant
    Level: Enlightened
    Posts: 31
    Brian on #7027

    Thank you Brajesh.
    So is there no way to disallow audio uploads while allowing image uploads in MediaPress?

    Meaning, separate the two?

    I am not looking for ‘$total_uploaded’ (combination of both audio and image), but rather restrict audio uploads to 10 and restricting image uploads to 8.

    I’ve created the following hook and it works

    ————————————-

    function mpp_custom_upload_permission( $can_do, $component, $component_id, $gallery ) {

    $audiogalleryid2 = mpp_get_gallery_ids( array( ‘component’=> ‘members’, ‘component_id’=> bp_loggedin_user_id(), ‘type’=> ‘audio’ ) );
    list($audiogalleryid) = $audiogalleryid2;

    $photogalleryid2 = mpp_get_gallery_ids( array( ‘component’=> ‘members’, ‘component_id’=> bp_loggedin_user_id(), ‘type’=> ‘photo’ ) );
    list($photogalleryid) = $photogalleryid2;

    $audioattach = get_children( array( ‘post_parent’ => $audiogalleryid ) );
    $audiocount = count( $audioattach );

    $photoattach = get_children( array( ‘post_parent’ => $photogalleryid ) );
    $photocount = count( $photoattach );

    if ($photocount < ‘8’) {
    return $can_do;
    }

    else {
    $can_do = false;
    }

    if ($audiocount < ’10’) {
    return $can_do;
    }

    else {
    $can_do = false;
    }

    }

    add_filter( ‘mpp_user_can_upload’, ‘mpp_custom_upload_permission’, 10, 4 );

    ————————————-

    But as I was saying, if a user has, for example, 8 photo uploads.. and only 2 audio uploads, it still restricts users from uploading across the board (rather than restricting photo uploads and allowing 8 more audio uploads).

    Hope that makes sense

    • This reply was modified 5 years, 10 months ago by Brian.
    • This reply was modified 5 years, 10 months ago by Brian.
  • Participant
    Level: Enlightened
    Posts: 31
    Brian on #7030

    I think I got it! Just tested it and it is working.

    ——————————–

    function mpp_custom_upload_permission( $can_do, $component, $component_id, $gallery ) {

    $total_uploaded_audio = mpp_get_media_count(array( ‘component_id’ => bp_loggedin_user_id(), ‘component’=> ‘members’, ‘type’ => ‘audio’ ) );

    $total_uploaded_photo = mpp_get_media_count(array( ‘component_id’ => bp_loggedin_user_id(), ‘component’=> ‘members’, ‘type’ => ‘photo’ ) );

    if ( empty( $gallery ) ) {
    return $can_do;
    }

    $type = $gallery->type;

    if ( $type == ‘photo’ && $total_uploaded_photo >= ‘8’ ) {
    $can_do = false;
    }

    if ( $type == ‘audio’ && $total_uploaded_audio >= ’10’ ) {
    $can_do = false;
    }

    return $can_do;

    }

    add_filter( ‘mpp_user_can_upload’, ‘mpp_custom_upload_permission’, 10, 4 );

    ——————————–

    Let me know if I should be aware of anything else with this, otherwise I will mark as resolved.
    Thanks again

    • This reply was modified 5 years, 10 months ago by Brian.
  • Keymaster
    (BuddyDev Team)
    Posts: 22211
    Brajesh Singh on #7032

    Hi Brian,
    Checking for the gallery type is the right strategy and you are doing it correctly.

    All the best 🙂

The topic ‘ [Resolved] Limit the Number of Media Uploads Per Gallery’ is closed to new replies.

This topic is: resolved