pmpro_has_membership_access

Functions to detect member content and protect it.

Description

pmpro_has_membership_access( (constant) $post_id = NULL, (constant) $user_id = NULL, (constant) $return_membership_levels = false ); 

Parameters (3)

0. $post_id — Optional. (constant) => NULL
The post id.
1. $user_id — Optional. (constant) => NULL
The user id.
2. $return_membership_levels — Optional. (constant) => false
The return membership levels.

Usage

  1. if ( !function_exists( 'pmpro_has_membership_access' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/includes/content.php'; 
  3.  
  4. // The post id. 
  5. $post_id = NULL; 
  6.  
  7. // The user id. 
  8. $user_id = NULL; 
  9.  
  10. // The return membership levels. 
  11. $return_membership_levels = false; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = pmpro_has_membership_access($post_id, $user_id, $return_membership_levels); 
  15.  

Defined (1)

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

/includes/content.php  
  1. function pmpro_has_membership_access($post_id = NULL, $user_id = NULL, $return_membership_levels = false) 
  2. global $post, $wpdb, $current_user; 
  3.  
  4. //get queried object in case we check against that 
  5. if(!is_admin()) 
  6. $queried_object = get_queried_object(); 
  7. else 
  8. $queried_object = NULL; 
  9.  
  10. //use post global or queried object if no $post_id was passed in 
  11. if(!$post_id && !empty($post) && !empty($post->ID)) 
  12. $post_id = $post->ID; 
  13. elseif(!$post_id && !empty($queried_object) && !empty($queried_object->ID)) 
  14. $post_id = $queried_object->ID; 
  15.  
  16. //no post, return true (changed from false inversion1.7.2) 
  17. if(!$post_id) 
  18. return true; 
  19.  
  20. //use current user if no value is supplied 
  21. if(!$user_id) 
  22. $user_id = $current_user->ID; 
  23.  
  24. //if no post or current_user object,setthem up 
  25. if(isset($queried_object->ID) && !empty($queried_object->ID) && $post_id == $queried_object->ID) 
  26. $mypost = $queried_object; 
  27. elseif(isset($post->ID) && !empty($post->ID) && $post_id == $post->ID) 
  28. $mypost = $post; 
  29. else 
  30. $mypost = get_post($post_id); 
  31.  
  32. if($user_id == $current_user->ID) 
  33. $myuser = $current_user; 
  34. else 
  35. $myuser = get_userdata($user_id); 
  36.  
  37. //for these post types, we want to check the parent 
  38. if(isset($mypost->post_type) && in_array( $mypost->post_type, array("attachment", "revision"))) 
  39. $mypost = get_post($mypost->post_parent); 
  40.  
  41. // Allow plugins and themes to find the protected post  
  42. $mypost = apply_filters( 'pmpro_membership_access_post', $mypost, $myuser ); 
  43.  
  44. if(isset($mypost->post_type) && $mypost->post_type == "post") 
  45. $post_categories = wp_get_post_categories($mypost->ID); 
  46.  
  47. if(!$post_categories) 
  48. //just check for entries in the memberships_pages table 
  49. $sqlQuery = "SELECT m.id, m.name FROM $wpdb->pmpro_memberships_pages mp LEFT JOIN $wpdb->pmpro_membership_levels m ON mp.membership_id = m.id WHERE mp.page_id = '" . $mypost->ID . "'"; 
  50. else 
  51. //are any of the post categories associated with membership levels? also check the memberships_pages table 
  52. $sqlQuery = "(SELECT m.id, m.name FROM $wpdb->pmpro_memberships_categories mc LEFT JOIN $wpdb->pmpro_membership_levels m ON mc.membership_id = m.id WHERE mc.category_id IN(" . implode(", ", $post_categories) . ") AND m.id IS NOT NULL) UNION (SELECT m.id, m.name FROM $wpdb->pmpro_memberships_pages mp LEFT JOIN $wpdb->pmpro_membership_levels m ON mp.membership_id = m.id WHERE mp.page_id = '" . $mypost->ID . "')"; 
  53. else 
  54. //are any membership levels associated with this page? 
  55. $sqlQuery = "SELECT m.id, m.name FROM $wpdb->pmpro_memberships_pages mp LEFT JOIN $wpdb->pmpro_membership_levels m ON mp.membership_id = m.id WHERE mp.page_id = '" . $mypost->ID . "'"; 
  56.  
  57.  
  58. $post_membership_levels = $wpdb->get_results($sqlQuery); 
  59.  
  60. $post_membership_levels_ids = array(); 
  61. $post_membership_levels_names = array(); 
  62.  
  63. if(!$post_membership_levels) 
  64. $hasaccess = true; 
  65. else 
  66. //we need to see if the user has access 
  67. foreach($post_membership_levels as $level) 
  68. $post_membership_levels_ids[] = $level->id; 
  69. $post_membership_levels_names[] = $level->name; 
  70.  
  71. //levels found. check if this is in a feed or if the current user is in at least one of those membership levels 
  72. if(is_feed()) 
  73. //always block restricted feeds 
  74. $hasaccess = false; 
  75. elseif(!empty($myuser->ID)) 
  76. $myuser->membership_level = pmpro_getMembershipLevelForUser($myuser->ID); // kept in for legacy filter users below. 
  77. $myuser->membership_levels = pmpro_getMembershipLevelsForUser($myuser->ID); 
  78. $mylevelids = array(); 
  79. foreach($myuser->membership_levels as $curlevel) { 
  80. $mylevelids[] = $curlevel->id; 
  81. if(count($myuser->membership_levels)>0 && count(array_intersect($mylevelids, $post_membership_levels_ids))>0) 
  82. //the users membership id is one that will grant access 
  83. $hasaccess = true; 
  84. else 
  85. //user isn't a member of a level with access 
  86. $hasaccess = false; 
  87. else 
  88. //user is not logged in and this content requires membership 
  89. $hasaccess = false; 
  90.  
  91. /** 
  92. Filters 
  93. The generic filter is run first. Then if there is a filter for this post type, that is run. 
  94. */ 
  95. //general filter for all posts 
  96. $hasaccess = apply_filters("pmpro_has_membership_access_filter", $hasaccess, $mypost, $myuser, $post_membership_levels); 
  97. //filter for this post type 
  98. if( isset($mypost->post_type) && has_filter("pmpro_has_membership_access_filter_" . $mypost->post_type)) 
  99. $hasaccess = apply_filters("pmpro_has_membership_access_filter_" . $mypost->post_type, $hasaccess, $mypost, $myuser, $post_membership_levels); 
  100.  
  101. //return 
  102. if($return_membership_levels) 
  103. return array($hasaccess, $post_membership_levels_ids, $post_membership_levels_names); 
  104. else 
  105. return $hasaccess;