MS_Addon_BuddyPress_Rule_Model

Membership BuddyPress Rule class.

Defined (1)

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

/app/addon/buddypress/rule/class-ms-addon-buddypress-rule-model.php  
  1. class MS_Addon_BuddyPress_Rule_Model extends MS_Rule { 
  2.  
  3. /** 
  4. * Rule type. 
  5. * @since 1.0.0 
  6. * @var string $rule_type 
  7. */ 
  8. protected $rule_type = MS_Addon_BuddyPress_Rule::RULE_ID; 
  9.  
  10. /** 
  11. * Verify access to the current content. 
  12. * Related: 
  13. * A reference of available BuddyPress template tags 
  14. * https://codex.buddypress.org/developer/template-tag-reference/ 
  15. * @since 1.0.0 
  16. * @param int $id The content post ID to verify access. 
  17. * @return bool|null True if has access, false otherwise. 
  18. * Null means: Rule not relevant for current page. 
  19. */ 
  20. public function has_access( $id, $admin_has_access = true ) { 
  21. global $bp; 
  22. $has_access = null; 
  23.  
  24. if ( ! function_exists( 'bp_current_component' ) ) { 
  25. return null; 
  26.  
  27. if ( is_buddypress() ) { 
  28. // Check if access to *all* BuddyPress pages is restricted 
  29. $has_access = parent::has_access( 
  30. MS_Addon_BuddyPress_Rule::PROTECT_ALL,  
  31. $admin_has_access 
  32. ); 
  33.  
  34. if ( $has_access ) { 
  35. // General BuddyPress access is either *allowed* or *not denied* 
  36. $component = bp_current_component(); 
  37.  
  38. if ( ! empty( $component ) ) { 
  39. if ( 'members' == $component || bp_is_user() ) { 
  40. // Member listing or member profile access. 
  41. $has_access = parent::has_access( 
  42. MS_Addon_BuddyPress_Rule::PROTECT_MEMBERS,  
  43. $admin_has_access 
  44. ); 
  45. } elseif ( 'messages' == $component ) { 
  46. // Private messaging direct access. 
  47. if ( 'compose' == $bp->current_action ) { 
  48. $has_access = parent::has_access( 
  49. MS_Addon_BuddyPress_Rule::PROTECT_PRIVATE_MSG,  
  50. $admin_has_access 
  51. ); 
  52. } elseif ( 'messages' == $component ) { 
  53. // Don't modify, handled by MS_Addon_Buddypress_Rule_Group 
  54. } else { 
  55. // Other BP pages can be handled by other rules. 
  56. $has_access = null; 
  57.  
  58. return apply_filters( 
  59. 'ms_rule_buddypress_has_access',  
  60. $has_access,  
  61. $id,  
  62. $this 
  63. ); 
  64.  
  65. /** 
  66. * Set initial protection. 
  67. * @since 1.0.0 
  68. * @param MS_Model_Relationship $ms_relationship Optional. Not used. 
  69. */ 
  70. public function protect_content( $ms_relationship = false ) { 
  71. parent::protect_content( $ms_relationship ); 
  72.  
  73. $this->add_filter( 
  74. 'bp_user_can_create_groups',  
  75. 'protect_create_bp_group' 
  76. ); 
  77. $this->protect_friendship_request(); 
  78. $this->protect_private_messaging(); 
  79.  
  80. /** 
  81. * Protect private messaging. 
  82. * @since 1.0.0 
  83. */ 
  84. protected function protect_private_messaging() { 
  85. if ( ! parent::has_access( MS_Addon_BuddyPress_Rule::PROTECT_PRIVATE_MSG ) ) { 
  86. $this->add_filter( 
  87. 'bp_get_send_message_button',  
  88. 'hide_private_message_button' 
  89. ); 
  90.  
  91. do_action( 
  92. 'ms_rule_buddypress_protect_private_messaging',  
  93. $this 
  94. ); 
  95.  
  96. /** 
  97. * Adds filter to prevent friendship button rendering. 
  98. * Related Action Hooks: 
  99. * - bp_get_send_message_button 
  100. * @since 1.0.0 
  101. * @param array $button The button settings. 
  102. * @return bool false to hide button. 
  103. */ 
  104. public function hide_private_message_button( $button ) { 
  105. return apply_filters( 
  106. 'ms_rule_buddypress_hide_private_message_button',  
  107. false,  
  108. $button,  
  109. $this 
  110. ); 
  111.  
  112. /** 
  113. * Protect friendship request. 
  114. * @since 1.0.0 
  115. */ 
  116. protected function protect_friendship_request() { 
  117. if ( ! parent::has_access( MS_Addon_BuddyPress_Rule::PROTECT_FRIENDSHIP ) ) { 
  118. $this->add_filter( 
  119. 'bp_get_add_friend_button',  
  120. 'hide_add_friend_button' 
  121. ); 
  122.  
  123. do_action( 
  124. 'ms_rule_buddypress_protect_friendship_request',  
  125. $this 
  126. ); 
  127.  
  128. /** 
  129. * Adds filter to prevent friendship button rendering. 
  130. * Related Action Hooks: 
  131. * - bp_get_add_friend_button 
  132. * @since 1.0.0 
  133. * @param array $button The button settings. 
  134. * @return array The current button settings. 
  135. */ 
  136. public function hide_add_friend_button( $button ) { 
  137. $this->add_filter( 'bp_get_button', 'prevent_button_rendering' ); 
  138.  
  139. return apply_filters( 
  140. 'ms_rule_buddypress_hide_add_friend_button',  
  141. $button,  
  142. $this 
  143. ); 
  144.  
  145. /** 
  146. * Prevents button rendering. 
  147. * Related Action Hooks: 
  148. * - bp_get_button 
  149. * @since 1.0.0 
  150. * @return boolean false to prevent button rendering. 
  151. */ 
  152. public function prevent_button_rendering() { 
  153. $this->remove_filter( 'bp_get_button', 'prevent_button_rendering' ); 
  154.  
  155. return apply_filters( 
  156. 'ms_rule_buddypress_prevent_button_rendering',  
  157. false,  
  158. $this 
  159. ); 
  160.  
  161. /** 
  162. * Checks the ability to create groups. 
  163. * Related Action Hooks: 
  164. * - bp_user_can_create_groups 
  165. * @since 1.0.0 
  166. * @param string $can_create The initial access. 
  167. * @return string The initial template if current user can create groups, otherwise blocking message. 
  168. */ 
  169. public function protect_create_bp_group( $can_create ) { 
  170. $can_create = false; 
  171.  
  172. if ( parent::has_access( MS_Addon_BuddyPress_Rule::PROTECT_GROUP_CREATION ) ) { 
  173. $can_create = true; 
  174.  
  175. return apply_filters( 
  176. 'ms_rule_buddypress_protect_create_bp_group',  
  177. $can_create,  
  178. $this 
  179. ); 
  180.  
  181. /** 
  182. * Get content to protect. 
  183. * @since 1.0.0 
  184. * @param $args Not used, but kept due to method override. 
  185. * @return array The content eligible to protect by this rule domain. 
  186. */ 
  187. public function get_contents( $args = null ) { 
  188. $contents = array(); 
  189.  
  190. $contents[MS_Addon_BuddyPress_Rule::PROTECT_ALL] = (object) array( 
  191. 'id' => MS_Addon_BuddyPress_Rule::PROTECT_ALL,  
  192. 'name' => __( 'All BuddyPress Pages', 'membership2' ),  
  193. 'type' => $this->rule_type,  
  194. 'description' => __( 'Protect all BuddyPress pages. This rule can be combined with any of the other rules.', 'membership2' ),  
  195. 'access' => $this->get_rule_value( MS_Addon_BuddyPress_Rule::PROTECT_ALL ),  
  196. ); 
  197.  
  198. $contents[MS_Addon_BuddyPress_Rule::PROTECT_GROUP_CREATION] = (object) array( 
  199. 'id' => MS_Addon_BuddyPress_Rule::PROTECT_GROUP_CREATION,  
  200. 'name' => __( 'Group creation', 'membership2' ),  
  201. 'type' => $this->rule_type,  
  202. 'description' => __( 'Only members can create new groups.', 'membership2' ),  
  203. 'access' => $this->get_rule_value( MS_Addon_BuddyPress_Rule::PROTECT_GROUP_CREATION ),  
  204. ); 
  205.  
  206. $contents[MS_Addon_BuddyPress_Rule::PROTECT_FRIENDSHIP] = (object) array( 
  207. 'id' => MS_Addon_BuddyPress_Rule::PROTECT_FRIENDSHIP,  
  208. 'name' => __( 'Friendship request', 'membership2' ),  
  209. 'type' => $this->rule_type,  
  210. 'description' => __( 'Only allow members to send friendship requests.', 'membership2' ),  
  211. 'access' => $this->get_rule_value( MS_Addon_BuddyPress_Rule::PROTECT_FRIENDSHIP ),  
  212. ); 
  213.  
  214. $contents[MS_Addon_BuddyPress_Rule::PROTECT_PRIVATE_MSG] = (object) array( 
  215. 'id' => MS_Addon_BuddyPress_Rule::PROTECT_PRIVATE_MSG,  
  216. 'name' => __( 'Private messaging', 'membership2' ),  
  217. 'type' => $this->rule_type,  
  218. 'description' => __( 'Only allow members to send private messages.', 'membership2' ),  
  219. 'access' => $this->get_rule_value( MS_Addon_BuddyPress_Rule::PROTECT_PRIVATE_MSG ),  
  220. ); 
  221.  
  222. $contents[MS_Addon_BuddyPress_Rule::PROTECT_MEMBERS] = (object) array( 
  223. 'id' => MS_Addon_BuddyPress_Rule::PROTECT_MEMBERS,  
  224. 'name' => __( 'Member listing', 'membership2' ),  
  225. 'type' => $this->rule_type,  
  226. 'description' => __( 'Only members can see the BuddyPress Member Directory and Member Profiles.', 'membership2' ),  
  227. 'access' => $this->get_rule_value( MS_Addon_BuddyPress_Rule::PROTECT_MEMBERS ),  
  228. ); 
  229.  
  230. return apply_filters( 
  231. 'ms_rule_buddypress_get_content',  
  232. $contents,  
  233. $this 
  234. );