pmpro_hasMembershipLevel

Function to check if a user has specified membership levels.

Description

pmpro_hasMembershipLevel( (constant) $levels = NULL, (constant) $user_id = NULL ); 

http://hookr.io/functions/pmpro_hasMembershipLevel" title="pmpro_hasMembershipLevel" class="code function">pmpro_hasMembershipLevel() checks if the passed user is a member of the passed level $level may either be the ID or name of the desired membership_level. (or an array of such) If $user_id is omitted, the value will be retrieved from $current_user.

Return values:

Parameters (2)

0. $levels — Optional. (constant) => NULL
The levels to check.
1. $user_id — Optional. (constant) => NULL
The user ID to check.

Usage

  1. if ( !function_exists( 'pmpro_hasMembershipLevel' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/includes/functions.php'; 
  3.  
  4. // The levels to check. 
  5. $levels = NULL; 
  6.  
  7. // The user ID to check. 
  8. $user_id = NULL; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = pmpro_hasMembershipLevel($levels, $user_id); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/functions.php  
  1. function pmpro_hasMembershipLevel($levels = NULL, $user_id = NULL) 
  2. global $current_user, $wpdb; 
  3.  
  4. $return = false; 
  5.  
  6. if(empty($user_id)) //no user_id passed, check the current user 
  7. $user_id = $current_user->ID; 
  8.  
  9. if(!empty($user_id) && is_numeric($user_id)) //get membership levels for given user 
  10. $membership_levels = pmpro_getMembershipLevelsForUser($user_id); 
  11. else 
  12. $membership_levels = NULL; //non-users don't have levels 
  13.  
  14. if($levels === "0" || $levels === 0) //if 0 was passed, return true if they have no level and false if they have any 
  15. $return = empty($membership_levels); 
  16. elseif(empty($levels)) //if no level var was passed, we're just checking if they have any level 
  17. $return = !empty($membership_levels); 
  18. else 
  19. if(!is_array($levels)) //make an array out of a single element so we can use the same code 
  20. $levels = array($levels); 
  21.  
  22. if(empty($membership_levels)) //user has no levels just check if 0, L, -1, or e was sent in one of the levels 
  23. //check for negative level 
  24. $negative_level = false; 
  25. foreach($levels as $level) { 
  26. if(intval($level) < 0) { 
  27. $negative_level = true; 
  28. break; 
  29.  
  30. //are we looking for non-members or not? 
  31. if($negative_level) 
  32. return true; //-1/etc, negative level 
  33. elseif(in_array(0, $levels, true) || in_array("0", $levels)) 
  34. $return = true; //0 level 
  35. elseif(in_array("L", $levels) || in_array("l", $levels)) 
  36. $return = (!empty($user_id) && $user_id == $current_user->ID); //L, logged in users 
  37. elseif(in_array("-L", $levels) || in_array("-l", $levels)) 
  38. $return = (empty($user_id) || $user_id != $current_user->ID); //-L, not logged in users 
  39. elseif(in_array("E", $levels) || in_array("e", $levels)) { 
  40. $sql = "SELECT id FROM $wpdb->pmpro_memberships_users WHERE user_id=$user_id AND status='expired' LIMIT 1"; 
  41. $expired = $wpdb->get_var($sql); //E, expired members 
  42. $return = !empty($expired); 
  43. else 
  44. foreach($levels as $level) 
  45. if(strtoupper($level) == "L") 
  46. //checking if user is logged in 
  47. if(!empty($user_id) && $user_id == $current_user->ID) 
  48. $return = true; 
  49. elseif(strtoupper($level) == "-L") 
  50. //checking if user is logged out 
  51. if(empty($user_id) || $user_id != $current_user->ID) 
  52. $return = true; 
  53. elseif($level === "0" || $level === 0 || strtoupper($level) === "E") 
  54. continue; //user with levels so not a "non-member" or expired 
  55. else 
  56. //checking a level id 
  57. $level_obj = pmpro_getLevel(is_numeric($level) ? abs(intval($level)) : $level); //make sure our level is in a proper format 
  58. if(empty($level_obj)) {continue;} //invalid level 
  59. $found_level = false; 
  60.  
  61. foreach($membership_levels as $membership_level) 
  62. if($membership_level->id == $level_obj->id) //found a match 
  63. $found_level = true; 
  64.  
  65. if(is_numeric($level) && intval($level) < 0 && !$found_level) //checking for the absence of this level and they don't have it 
  66. $return = true; 
  67. elseif(is_numeric($level) && intval($level) > 0 && $found_level) //checking for the presence of this level and they have it 
  68. $return = true; 
  69. elseif(!is_numeric($level)) //if a level name was passed 
  70. $return = $found_level; 
  71.  
  72. $return = apply_filters("pmpro_has_membership_level", $return, $user_id, $levels); 
  73. return $return;