3

I'm using Wishlist Member plugin and I'm building a function which compares user_levels and post_levels, but I can't get my function to work:

check_user_access($userid, $postid){

     // get user_levels
     $user_levels = WLMAPI::GetUserLevels($userid);

     // get the post levels
     $post_levels = WLMAPI::GetPostLevels($postid);

     // Compare user_levels with post_level
     $result = array_search($user_levels, $post_levels);

     if ($result === false) {
         return false;
     }
     else {
         return true;
     }

 }

3 Answers 3

1

Maybe because $user_levels is an array?

Try something like:

// Compare user_levels with post_level
$result = '';
foreach($user_levels as $user_level) {
    $result .= array_search($user_level, $post_levels);
}
if ($result) {
    return true;
}
1

I think this is what you're looking for. This will let you find if a user can access a post, page, or category. This uses the new API functions in the latest versions of WishList Member and accepts 'post', 'page', or 'category' as the $object_type parameter.

function member_can_access($user_id, $object_type, $object_id) {
    $levels = wlmapi_get_member_levels($user_id);

    $map = array(
        'post' => 'wlmapi_get_level_posts',
        'page' => 'wlmapi_get_level_pages',
        'category' => 'wlmapi_get_level_categories'
    );

    $plurals = array(
        'post' => 'posts',
        'page' => 'pages',
        'category' => 'categories'
    );

    foreach ( $levels as $level ) {
        $objects[] = call_user_func($map[$object_type], $level->Level_ID);
    }

    foreach ( $objects as $object ) {
        foreach ( $object[$plurals[$object_type]][$object_type] as $item ) {
            $items[] = $item['ID'];
        }
    }

    $items = array_unique($items);

    if ( !in_array( $object_id, $items ) ) {
        return false;
    }

    return true;
}

?><pre><?php print_r(member_can_access(1, 'post', 1)); ?></pre>

You can see the documentation for all the used functions at http://codex.wishlistproducts.com

0

First of all, you are using the old Wishlist Member API, try using the new one it's much more comprehensive. You can find the new API at: http://wishlistproducts.com/api/api2/

The returned variable are object to find out their content use var_dump for debugging then just get the required information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.