Restricting upload of video for members (using s2member and rtMedia)

I'm building a membership site using the s2member plugin (it unfortunately HAS to be that one, because of its CCBill support - which is required). I'm also using rtMedia for users to upload media (photos/video).

Let's say there's a free tier (Level 1), and a paid one (Level 2). Level 2 users can upload both photos and video, but I want Level 1 users to only upload photos, restricting video to Level 2 only.

On a tight budget here, and can't really hire help to code this, so I'm trying to see if there's a way I can figure this out on my own. I contacted rtMedia support to see if they had any answers.

On whether or not I could hide certain media for certain users, they said:

You can use rtmedia-model-where-query filter in your theme or plugin and in that filter, you can check if current user is allowed to view the gallery or not. If he's allowed than simply return the query as it is and if not just put an 'AND' condition with SQL query which will result into false all the time for example 2=3. That way, SQL query won't be able to fetch any media if user is not set for required WP role.

On whether or not I could DENY said user to upload certain media (videos), they said:

rtMedia stores allowed upload types into $rtmedia global variable. You can access them as following.
global $rtmedia;
$rtmedia->allowed_types
You can hook into WordPress init action and can unset video from that if user does not fulfill required WP role.

Granted, I have absolutely NO idea what any of that means, or what to do with that info... :wink:

Any help, suggestions, clues, leads, links to tutorials, whatever, would be greatly appreciated!!

  • Michael Bissett

    Hey @Anton! :slight_smile:

    I thought that this request seemed familiar, we had spoken about this last month. :slight_smile:

    Given that you're presently using S2Member on your site presently, we'd need to work with their filters (as we'd be tying the rtMedia functions into those).

    To try to explain this:

    You can use rtmedia-model-where-query filter in your theme or plugin and in that filter, you can check if current user is allowed to view the gallery or not. If he's allowed than simply return the query as it is and if not just put an 'AND' condition with SQL query which will result into false all the time for example 2=3. That way, SQL query won't be able to fetch any media if user is not set for required WP role.

    rtMedia has a filter called "rtmedia-model-where-query", that you can tie a function of your own into:

    add_filter('rtmedia-model-where-query', 'name-of-your-function');
    
    function name-of-your-function () {
            if ( enter code here to check for user level in S2Member) {
                    do this
            }
            else {
                    do this instead
            }
    }

    As to this:

    rtMedia stores allowed upload types into $rtmedia global variable. You can access them as following.
    global $rtmedia;
    $rtmedia->allowed_types
    You can hook into WordPress init action and can unset video from that if user does not fulfill required WP role.

    Hmm... he's talking about a WordPress user role here, instead of a S2Member membership level here... still, if S2Member allows for assigning a user role to a membership, you'd be looking to create a function to tie into the "init" action:

    add_action ('init', 'name-of-media-upload-restriction-function');

    And in the writing of the function, you'd be checking to see whether a user has a particular capability, and if he doesn't, then he can't upload. You can learn more about roles and capabilities over here:

    https://codex.wordpress.org/Roles_and_Capabilities

    All in all though, while it's one thing to explain the more general side of things, you're looking for specifics. My thoughts on that?

    I'd try to see if I could get a bit more information out of rtMedia, but I would also reach out to S2Member as to the creating of a function that would check to see if a user has a certain membership level, and do X, Y & Z.

    You may still find yourself needing to head off to the Jobs Board (given the intricacies of what you're trying to do), but for what it's worth, I hope this helps! :slight_smile:

    Kind Regards,
    Michael

  • Anton

    Hi @Michael!

    I thought that this request seemed familiar, we had spoken about this last month. :slight_smile:

    Yeah - it was part of a much longer post about a bunch of things -- but this part of it was kind of buried within it, and it was the only problem I wasn't able to find a solution to.
    Unfortunately I can't afford to hire coding help right now, and this is the kind of thing that I really should learn how to do myself anyway. It feels like it's "doable," if I can just wrap my head around the concepts well enough. I'm starting to get there.... :wink:

    rtMedia has a filter called "rtmedia-model-where-query", that you can tie a function of your own into:

    Thanks for breaking it down in a way that I understand a little bit more. Your example at least gets my foot in the door (whereas before, with what the rtMedia guy had said, I was locked outside). It's slowly starting to come together, I think. Now I just need to figure out how to fully walk through the door, by figuring out what to put in the "do this" and "do this instead" sections of your example...

    if S2Member allows for assigning a user role to a membership, you'd be looking to create a function to tie into the "init" action:

    I know that s2Member assigns the "subscriber" role to "Level 0" (free membership) -- but for all other levels it creates new roles, instead of assigning default WP roles to the levels. Would that still work for this?

    I'd try to see if I could get a bit more information out of rtMedia, but I would also reach out to S2Member as to the creating of a function that would check to see if a user has a certain membership level, and do X, Y & Z.

    I will keep bugging both rtMedia and s2Member support until I can figure this out. I feel like they assume I know much more than I do (especially the rtMedia guys), and so I may need some help with "translating" what they tell me... :wink:

    You may still find yourself needing to head off to the Jobs Board (given the intricacies of what you're trying to do), but for what it's worth, I hope this helps! :slight_smile:

    It definitely helps a bit! I feel a wee bit closer towards my goal of figuring this out on my own. Like I said, unfortunately I can't go to the job boards at the moment. And I'll never learn how to do this stuff unless I can finally understand how these things work...

  • Michael Bissett

    Hey @Anton,

    Glad I could help clear up some of this for you! :slight_smile:

    I know that s2Member assigns the "subscriber" role to "Level 0" (free membership) -- but for all other levels it creates new roles, instead of assigning default WP roles to the levels. Would that still work for this?

    It looks like it, I'm seeing that they have a guide on the roles and capabilities generated over here:

    https://s2member.com/kb-article/s2member-rolescapabilities/

    That should help offer some insight on the matter. :slight_smile:

    Kind Regards,
    Michael

  • Anton

    Thanks, @Michael Bissett!

    I contacted rtMedia, and they were able to help me part of the way, at least. They offered this:

    add_filter( 'rtmedia_allowed_types', 'restric_allowed_types', 10, 1 );
    function restric_allowed_types( $types ) {
    
    	$Logged_in_user_role = "role1"; // Get current user role using s2member filter.
    
    	if($Logged_in_user_role == "role1"){
    		unset( $types[ 'video' ] );
    	}
    	else if ( $Logged_in_user_role == "role2") {
    		unset( $types[ 'photo' ] );
    	}
       return $types;
    }

    So going by what rtMedia gave me above, and the link you posted here, does that mean I should replace "role1" in the code above with "subscriber" (which is the same as "s2Member Level 0"), and "role2" with "s2member_level1,s2member_level2" (the two paid memberships that DO have video upload capability)?? Not sure if both paid membership levels can be written like that, with just a comma in-between...or if it should be written differently...

    Would that be all I need for this to work???? :wink:

  • Michael Bissett

    Hey @Anton,

    Assuming that code is governing whether or not certain user roles are able to upload certain types of media, then it means that those with a role of "role1" would not be able to upload videos, whereas those with a role of "role2" are not able to upload photos (as the unset function is designed to unset a given variable).

    Going upon your earlier question:

    Let's say there's a free tier (Level 1), and a paid one (Level 2). Level 2 users can upload both photos and video, but I want Level 1 users to only upload photos, restricting video to Level 2 only.

    It sounds like you'd want to have it written up like this:

    add_filter( 'rtmedia_allowed_types', 'restric_allowed_types', 10, 1 );
    function restric_allowed_types( $types ) {
    
    	if(current_user_is('s2member_level0')){
    		unset( $types[ 'video' ] );
    	}
       return $types;
    }

    The current_user_is function gets a mention over here, I used that in the snippet here as that would detect which role is in use, I'm inclined to think that this earlier code:

    $Logged_in_user_role = "role1";

    Was part of an example, to be substituted with S2Member's method.

    Hope this helps! :slight_smile:

    Kind Regards,
    Michael

  • Anton

    @Michael Bissett -

    It sounds like you'd want to have it written up like this:

    That....didn't seem to work, unfortunately. :slight_frown:
    I tried replacing "s2member_level0" with "subscriber" (as it's one and the same with s2member), and that didn't work either. I also tried it with other levels, and it still didn't work. Guess there needs to be more to the code?

    By the way...what do the numbers 10 and 1 in the first line have to do with anything?

  • Michael Bissett

    Hey @Anton,

    I tried replacing "s2member_level0" with "subscriber" (as it's one and the same with s2member), and that didn't work either. I also tried it with other levels, and it still didn't work. Guess there needs to be more to the code?

    Hmm... just to test, could you try altering it, to see if this works:

    add_filter( 'rtmedia_allowed_types', 'restric_allowed_types', 10, 1 );
    function restric_allowed_types( $types ) {
    	global $current_user;
    
    	if (!current_user_can( 'manage_options' )) {
    		unset( $types[ 'video' ] );
    	}
       return $types;
    }

    Typically, the Administrator role is the one with the "manage_options" capability, so if a user isn't an Administrator, the ability to upload videos should be disabled by this code.

    By the way...what do the numbers 10 and 1 in the first line have to do with anything?

    10 sets the priority for the function (which determines how soon that function gets run), and the 1 specifies how many arguments are being passed to the function (the argument being the $types variable).

    Kind Regards,
    Michael

  • Anton

    Hmm... just to test, could you try altering it, to see if this works:

    That....didn't work either, I'm afraid. :slight_frown:

    But even if it did work...I don't want non-admins to not have the ability to upload video. I just want to restrict free members (subscribers/Level 0) from uploading videos.

    Back to the drawing board.... (but not the job board -- I still can't use it, and I'm determined on discovering a solution somehow!!) :wink:

  • Michael Bissett

    Hey @Anton, you're welcome, glad to have helped you learn more about this! :slight_smile:

    Just to clarify this briefly:

    But even if it did work...I don't want non-admins to not have the ability to upload video. I just want to restrict free members (subscribers/Level 0) from uploading videos.

    Right, like I said earlier, the purpose of trying that most recent code of mine just purely to see if it actually worked, the goal was to start from there, in case there was something up with using the earlier S2Member functions. :slight_smile:

    Kind Regards,
    Michael

  • Anton

    @Michael Bissett

    Right, like I said earlier, the purpose of trying that most recent code of mine just purely to see if it actually worked, the goal was to start from there, in case there was something up with using the earlier S2Member functions. :slight_smile:

    Thanks, Michael.

    So any other possible suggestions from you or other staff members? I feel like we're sooooooo close here. :wink:

  • Michael Bissett

    Hey @Anton,

    I'm seeing that this code removes the ability for free users to upload videos (as I'm not able to upload a mp4 when I have it active), could you try inserting the following into your site as a mu-plugin:

    add_filter( 'rtmedia_allowed_types', 'restric_allowed_types', 10, 1 );
    function restric_allowed_types( $types ) {
    	global $current_user;
    
    	if (current_user_is("s2member_level0")) {
    		unset( $types[ 'video' ] );
    	}
       return $types;
    }

    And let me know if it helps please? :slight_smile:

    Kind Regards,
    Michael

  • Anton

    Thank you, @Michael Bissett !!!

    ...could you try inserting the following into your site as a mu-plugin:

    I did, but at first I got the following error that filled the top of the screen:

    Warning: implode(): Invalid arguments passed in /Users/antonkawasaki/Documents/Websites/www.testsite.dev/wp-content/plugins/buddypress-media/app/services/RTMediaEncoding.php on line 221

    Warning: implode(): Invalid arguments passed in /Users/antonkawasaki/Documents/Websites/www.testsite.dev/wp-content/plugins/buddypress-media/app/services/RTMediaEncoding.php on line 221

    That was because I opted in on rtMedia's video encoding service.
    When I went into the settings and opted OUT, then the error message disappeared after refreshing. And most importantly....the must-use plugin worked!! My free subscriber was not able to upload video. HURRAH!!! (P.S. -- I used "subscriber" in the code instead of "s2member_level0," as they are apparently the same thing).

    This made me wonder about the need for video encoding, what it means, and how necessary it is. For the site I'm developing, I don't forsee too many big videos being uploaded. Actually, I have rtMedia Pro, so I can limit the size/number of files anyway. But I'm wondering if this encoding service will be needed down the line. It just looks like another way for rtCamp to make money...

  • Michael Bissett

    Hey @Anton, glad to hear that it worked for you here! :smiley:

    This made me wonder about the need for video encoding, what it means, and how necessary it is. For the site I'm developing, I don't forsee too many big videos being uploaded. Actually, I have rtMedia Pro, so I can limit the size/number of files anyway. But I'm wondering if this encoding service will be needed down the line. It just looks like another way for rtCamp to make money...

    To refer to their 2nd FAQ entry for this:

    https://rtcamp.com/rtmedia/audio-video-encoding-service/#q2

    Audio and video files come in many formats. Not all file formats are created equal, each format has its own set of advantages and disadvantages.

    Some formats are universal, meaning they are more widely used over others. Some formats take up less storage space while at the same time maintaining audio and/or video quality. You can view the complete list of file formats here.

    If you want your site users to have a smooth experience while listening and viewing audio and video files respectively, it is recommended that you convert or encode your audio and video files to popular and memory-efficient formats.

    If this isn't a big concern for you, then you won't need to keep it enabled. :slight_smile:

    Hope this helps!

    Kind Regards,
    Michael

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.