RCP_Member::can_access PHP Method

can_access() public method

Determines if the member can access current content
Since: 2.1
public can_access ( $post_id )
    public function can_access($post_id = 0)
    {
        $subscription_levels = rcp_get_content_subscription_levels($post_id);
        $access_level = get_post_meta($post_id, 'rcp_access_level', true);
        $user_level = get_post_meta($post_id, 'rcp_user_level', true);
        $sub_id = $this->get_subscription_id();
        // Assume the user can until proven false
        $ret = true;
        if (rcp_is_paid_content($post_id) && $this->is_expired()) {
            $ret = false;
        }
        if (!empty($subscription_levels)) {
            if (is_string($subscription_levels)) {
                switch ($subscription_levels) {
                    case 'any':
                        $ret = !empty($sub_id) && !$this->is_expired();
                        break;
                    case 'any-paid':
                        $ret = $this->is_active();
                        break;
                }
            } else {
                if (in_array($sub_id, $subscription_levels)) {
                    $needs_paid = false;
                    foreach ($subscription_levels as $level) {
                        $price = rcp_get_subscription_price($level);
                        if (!empty($price) && $price > 0) {
                            $needs_paid = true;
                        }
                    }
                    if ($needs_paid) {
                        $ret = $this->is_active();
                    } else {
                        $ret = true;
                    }
                } else {
                    $ret = false;
                }
            }
        }
        if (!rcp_user_has_access($this->ID, $access_level) && $access_level > 0) {
            $ret = false;
        }
        if ($ret && !empty($user_level) && 'All' != $user_level) {
            if (!user_can($this->ID, strtolower($user_level))) {
                $ret = false;
            }
        }
        if (user_can($this->ID, 'manage_options')) {
            $ret = true;
        }
        return apply_filters('rcp_member_can_access', $ret, $this->ID, $post_id, $this);
    }

Usage Example

コード例 #1
0
/**
 * Wrapper function for RCP_Member->can_access()
 *
 * Returns true if user can access the current content
 *
 * @access      public
 * @since       2.1
 */
function rcp_user_can_access( $user_id = 0, $post_id = 0 ) {

	if( empty( $user_id ) ) {
		$user_id = get_current_user_id();
	}

	if( empty( $post_id ) ) {
		global $post;
		$post_id = $post->ID;
	}

	$member = new RCP_Member( $user_id );
	return $member->can_access( $post_id );
}