Protected Content - Functions & Hooks

I started creating a new membership site using Protected Content and need function like "current_user_has_subscription()" which will help me restrict what the user is allowed to do when it is not in content.

I was wondering if there was some sort of function and hook documentation, or would I have to reverse engineer the plugin to find these out.

Many thanks.

  • Ash
    • WordPress Hacker

    Hello @Ian Hulme

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    There is no documentation yet, but you can try like this:

    $model = MS_Model_Member::get_current_member();
    if( $model->has_membership($id) ){
    // Do stuff
    }

    You will find it in /plugins/protected-content/app/model/class-ms-model-member.php file.

    Cheers
    Ash

  • Ian Hulme
    • Design Lord, Child of Thor

    Next one is pretty straightforward, but useful:
    MS_Controller_Frontend::get_action();
    This returns the action request on pages like edit profile.

    So the following could be used to redirect the user to the wp-admin profile:

    $action = MS_Controller_Frontend::get_action();
    if($action == "edit_profile"){
    	wp_redirect( admin_url( 'profile.php' ) );
    	exit();
    	}
  • Ian Hulme
    • Design Lord, Child of Thor

    Also:
    MS_Factory::load( 'MS_Model_Pages' )->get_ms_pages( true );
    returns membership special pages.

    For example, the following can be used to redirect a user to the register page if they are trying to access an area when their subscription has ended:

    $pages = MS_Factory::load( 'MS_Model_Pages' )->get_ms_pages( true );
    if (!user_has_subcription() && is_admin()){
    	wp_redirect( get_permalink($pages['register']->id) );
    	exit();
    	}

    Please post if there are easy methods to achieve anything I enter.

  • Ian Hulme
    • Design Lord, Child of Thor

    Slight update on the "use_has_subscription()" function:

    function user_has_subcription($id=null){
    	if (!$is){
    		$member = MS_Model_Member::get_current_member();
    	} else {
    		$member = MS_Model_Member::get_members(array('search'=>$id));
    		$member = $member[0];
    		}
    	return $member->has_membership();
    	}

    also, if you do not wish cancelled memberships to have access you will need to add the following filter:

    add_filter('membership_model_member_allowed_status', 'membership_allowed_status',10,1);
    function membership_allowed_status($a){
    	$key = array_search(MS_Model_Membership_Relationship::STATUS_CANCELED, $a);
    	unset($a[$key]);
    return $a;
    }
  • 12southmusic
    • WPMU DEV Initiate

    Ian thanks for the code you've thrown up in here. A more detailed documentation about the PHP classes available to use would be fantastic if that's something the devs could work on. I will say the code is really nicely commented, but some actual docs on it would be awesome!

  • Ian Hulme
    • Design Lord, Child of Thor

    Slight Edit to the Pages Function above, the get_ms_pages() function has been changed to get_pages().

    So it now looks like this:

    $pages = MS_Factory::load( 'MS_Model_Pages' )->get_pages( true );
    if (!user_has_subcription() && is_admin()){
    	wp_redirect( get_permalink($pages['register']->id) );
    	exit();
    	}

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.