BP_Docs_Access_Query

The BuddyPress Docs BP Docs Access Query class.

Defined (1)

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

/includes/access-query.php  
  1. class BP_Docs_Access_Query { 
  2. protected $user_id; 
  3. protected $tax_query = array(); 
  4. protected $user_groups = array(); 
  5. protected $levels = array(); 
  6.  
  7. public static function init( $user_id = 0 ) { 
  8. static $instance; 
  9.  
  10. if ( empty( $instance ) ) { 
  11. $instance = new BP_Docs_Access_Query( $user_id ); 
  12.  
  13. return $instance; 
  14.  
  15. public function __construct( $user_id = 0 ) { 
  16. $this->user_id = intval( $user_id ); 
  17. $this->set_up_levels(); 
  18. $this->prepare_tax_query(); 
  19.  
  20. protected function set_up_levels() { 
  21. // Everyone can see 'anyone' docs 
  22. $this->levels[] = bp_docs_get_access_term_anyone(); 
  23.  
  24. // Logged-in users 
  25. // Note that we're not verifying that the user actually exists 
  26. // For now this kind of check is up to whoever's instantiating 
  27. if ( $this->user_id != 0 ) { 
  28. $this->levels[] = bp_docs_get_access_term_loggedin(); 
  29.  
  30. if ( bp_is_active('groups') ) { 
  31.  
  32. $this->set_up_user_groups(); 
  33.  
  34. if ( isset($this->user_groups['groups']) ) { 
  35. foreach ( $this->user_groups['groups'] as $member_group ) { 
  36. $this->levels[] = bp_docs_get_access_term_group_member( $member_group ); 
  37.  
  38. // admins-mods 
  39. if ( isset($this->user_groups['admin_mod_of']) ) { 
  40. foreach ( $this->user_groups['admin_mod_of'] as $adminmod_group ) { 
  41. $this->levels[] = bp_docs_get_access_term_group_adminmod( $adminmod_group ); 
  42.  
  43. // no-one 
  44. // creator 
  45. // @todo What's the difference? 
  46. $this->levels[] = bp_docs_get_access_term_user( $this->user_id ); 
  47.  
  48. protected function prepare_tax_query() { 
  49. $this->tax_query[] = array( 
  50. 'terms' => $this->levels,  
  51. 'taxonomy' => bp_docs_get_access_tax_name(),  
  52. 'field' => 'slug',  
  53. 'operator' => 'IN',  
  54. ); 
  55.  
  56. /** 
  57. * Get a list of a user's groups, as well as those groups of which 
  58. * the user is an admin or mod 
  59. * @since 1.2 
  60. */ 
  61. protected function set_up_user_groups() { 
  62. $groups = BP_Groups_Member::get_group_ids( $this->user_id ); 
  63. $this->user_groups['groups'] = $groups['groups']; 
  64.  
  65. $admin_groups = BP_Groups_Member::get_is_admin_of( $this->user_id ); 
  66. $mod_groups = BP_Groups_Member::get_is_mod_of( $this->user_id ); 
  67. $this->user_groups['admin_mod_of'] = array_merge( wp_list_pluck( $admin_groups['groups'], 'id' ), wp_list_pluck( $mod_groups['groups'], 'id' ) ); 
  68.  
  69. /** 
  70. * Returns the tax_query param for the WP_Query args 
  71. * @since 1.2 
  72. * @return array 
  73. */ 
  74. public function get_tax_query() { 
  75. // bp_moderate users can see anything, so no query needed 
  76. if ( user_can( $this->user_id, 'bp_moderate' ) ) { 
  77. return array(); 
  78.  
  79. return $this->tax_query; 
  80.  
  81. /** 
  82. * Fetch a list of Doc IDs that are forbidden for the user 
  83. * @since 1.2.8 
  84. */ 
  85. public function get_doc_ids() { 
  86. remove_action( 'pre_get_posts', 'bp_docs_general_access_protection', 28 ); 
  87.  
  88. $tax_query = $this->get_tax_query(); 
  89. foreach ( $tax_query as &$tq ) { 
  90. $tq['operator'] = "NOT IN"; 
  91.  
  92. // If the tax_query is empty, no docs are forbidden 
  93. if ( empty( $tax_query ) ) { 
  94. $forbidden_fruit_ids = array(); 
  95. } else { 
  96. $forbidden_fruit = new WP_Query( array( 
  97. 'post_type' => bp_docs_get_post_type_name(),  
  98. 'posts_per_page' => -1,  
  99. 'nopaging' => true,  
  100. 'tax_query' => $tax_query,  
  101. 'update_post_term_cache' => false,  
  102. 'update_post_meta_cache' => false,  
  103. 'no_found_rows' => 1,  
  104. 'fields' => 'ids',  
  105. ) ); 
  106. $forbidden_fruit_ids = $forbidden_fruit->posts; 
  107.  
  108. add_action( 'pre_get_posts', 'bp_docs_general_access_protection', 28 ); 
  109.  
  110. return $forbidden_fruit_ids;