MS_Addon_Bbpress_Rule_Model

Membership bbPress Rule class.

Defined (1)

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

/app/addon/bbpress/rule/class-ms-addon-bbpress-rule-model.php  
  1. class MS_Addon_Bbpress_Rule_Model extends MS_Rule { 
  2.  
  3. /** 
  4. * Custom Post Type names that are used by bbPress 
  5. * @since 1.0.0 
  6. */ 
  7. const CPT_BB_FORUM = 'forum'; 
  8. const CPT_BB_TOPIC = 'topic'; 
  9. const CPT_BB_REPLY = 'reply'; 
  10.  
  11. /** 
  12. * Rule type. 
  13. * @since 1.0.0 
  14. * @var string $rule_type 
  15. */ 
  16. protected $rule_type = MS_Addon_BbPress_Rule::RULE_ID; 
  17.  
  18. /** 
  19. * Verify access to the current content. 
  20. * @since 1.0.0 
  21. * @param int $id The content post ID to verify access. 
  22. * @return bool|null True if has access, false otherwise. 
  23. * Null means: Rule not relevant for current page. 
  24. */ 
  25. public function has_access( $id, $admin_has_access = true ) { 
  26. global $wp_query; 
  27. $has_access = null; 
  28.  
  29. if ( empty( $id ) ) { 
  30. $id = $this->get_current_post_id(); 
  31.  
  32. if ( ! empty( $id ) ) { 
  33. $post_type = get_post_type( $id ); 
  34.  
  35. if ( in_array( $post_type, self::get_bb_cpt() ) ) { 
  36. $has_access = false; 
  37.  
  38. // Only verify permission if addon is enabled. 
  39. if ( MS_Addon_Bbpress::is_active() ) { 
  40. switch ( $post_type ) { 
  41. case self::CPT_BB_FORUM: 
  42. $has_access = parent::has_access( $id, $admin_has_access ); 
  43. break; 
  44.  
  45. case self::CPT_BB_TOPIC: 
  46. if ( function_exists( 'bbp_get_topic_forum_id' ) ) { 
  47. $forum_id = bbp_get_topic_forum_id( $id ); 
  48. $has_access = parent::has_access( $forum_id, $admin_has_access ); 
  49. break; 
  50.  
  51. case self::CPT_BB_REPLY: 
  52. if ( function_exists( 'bbp_get_reply_forum_id' ) ) { 
  53. $forum_id = bbp_get_reply_forum_id( $id ); 
  54. $has_access = parent::has_access( $forum_id, $admin_has_access ); 
  55. break; 
  56. } else { 
  57. $has_access = true; 
  58. } else { 
  59. /** 
  60. * If post type is forum and no post_id, it is the forum list page, give access. 
  61. * @todo Find another way to verify if the current page is the forum list page. 
  62. */ 
  63. if ( self::CPT_BB_FORUM === $wp_query->get( 'post_type' ) ) { 
  64. $has_access = true; 
  65.  
  66. return apply_filters( 
  67. 'ms_addon_bbpress_model_rule_has_access',  
  68. $has_access,  
  69. $id,  
  70. $this 
  71. ); 
  72.  
  73. /** 
  74. * Set initial protection. 
  75. * @since 1.0.0 
  76. * @param MS_Model_Relationship $ms_relationship Optional. Not used. 
  77. */ 
  78. public function protect_content( $ms_relationship = false ) { 
  79. parent::protect_content( $ms_relationship ); 
  80.  
  81. $this->add_action( 'pre_get_posts', 'protect_posts', 98 ); 
  82.  
  83. /** 
  84. * Adds filter for posts query to remove all protected bbpress custom post types. 
  85. * Related Action Hooks: 
  86. * - pre_get_posts 
  87. * @since 1.0.0 
  88. * @param WP_Query $query The WP_Query object to filter. 
  89. */ 
  90. public function protect_posts( $wp_query ) { 
  91. do_action( 
  92. 'ms_addon_bbpress_model_rule_protect_posts_before',  
  93. $wp_query,  
  94. $this 
  95. ); 
  96.  
  97. $post_type = $wp_query->get( 'post_type' ); 
  98.  
  99. /** 
  100. * Only protect if add-on is enabled. 
  101. * Restrict query to show only has_access cpt posts. 
  102. */ 
  103. if ( MS_Addon_Bbpress::is_active() ) { 
  104. if ( ! $wp_query->is_singular 
  105. && empty( $wp_query->query_vars['pagename'] ) 
  106. && ! empty( $post_type ) 
  107. && self::CPT_BB_FORUM == $post_type 
  108. ) { 
  109. foreach ( $this->rule_value as $id => $value ) { 
  110. if ( ! $this->has_access( $id ) ) { 
  111. $wp_query->query_vars['post__not_in'][] = $id; 
  112.  
  113. do_action( 
  114. 'ms_addon_bbpress_model_rule_protect_posts_after',  
  115. $wp_query,  
  116. $this 
  117. ); 
  118.  
  119. /** 
  120. * Get the current post id. 
  121. * @since 1.0.0 
  122. * @return int The post id, or null if it is not a post. 
  123. */ 
  124. private function get_current_post_id() { 
  125. $post_id = null; 
  126. $post = get_queried_object(); 
  127.  
  128. if ( is_a( $post, 'WP_Post' ) ) { 
  129. $post_id = $post->ID; 
  130.  
  131. return apply_filters( 
  132. 'ms_addon_bbpress_model_rule_get_current_post_id',  
  133. $post_id,  
  134. $this 
  135. ); 
  136.  
  137. /** 
  138. * Get the total content count. 
  139. * @since 1.0.0 
  140. * @param $args The query post args 
  141. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  142. * @return int The total content count. 
  143. */ 
  144. public function get_content_count( $args = null ) { 
  145. $defaults = array( 
  146. 'posts_per_page' => -1,  
  147. 'post_type' => self::CPT_BB_FORUM,  
  148. 'post_status' => 'publish',  
  149. ); 
  150. $args = wp_parse_args( $args, $defaults ); 
  151.  
  152. $query = new WP_Query( $args ); 
  153. $count = $query->found_posts; 
  154.  
  155. return apply_filters( 
  156. 'ms_addon_bbpress_model_rule_get_content_count',  
  157. $count 
  158. ); 
  159.  
  160. /** 
  161. * Get content to protect. 
  162. * @since 1.0.0 
  163. * @param string $args The default query post args. 
  164. * @return array The content. 
  165. */ 
  166. public function get_contents( $args = null ) { 
  167. $args = self::get_query_args( $args ); 
  168.  
  169. $query = new WP_Query( $args ); 
  170. $posts = $query->posts; 
  171.  
  172. $contents = array(); 
  173. foreach ( $posts as $content ) { 
  174. $content->id = $content->ID; 
  175. $content->name = $content->post_title; 
  176. $content->type = $this->rule_type; 
  177. $content->access = $this->get_rule_value( $content->id ); 
  178.  
  179. $contents[ $content->id ] = $content; 
  180.  
  181. if ( ! empty( $args['rule_status'] ) ) { 
  182. $contents = $this->filter_content( $args['rule_status'], $contents ); 
  183.  
  184. return apply_filters( 
  185. 'ms_addon_bbpress_model_rule_get_contents',  
  186. $contents 
  187. ); 
  188.  
  189. /** 
  190. * Get WP_Query object arguments. 
  191. * Return default search arguments. 
  192. * @since 1.0.0 
  193. * @param $args The query post args 
  194. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  195. * @return array $args The parsed args. 
  196. */ 
  197. public function get_query_args( $args = null ) { 
  198. $defaults = array( 
  199. 'posts_per_page' => -1,  
  200. 'offset' => 0,  
  201. 'orderby' => 'ID',  
  202. 'order' => 'DESC',  
  203. 'post_type' => self::CPT_BB_FORUM,  
  204. 'post_status' => 'publish',  
  205. ); 
  206.  
  207. $args = wp_parse_args( $args, $defaults ); 
  208. $args = parent::prepare_query_args( $args ); 
  209.  
  210. return apply_filters( 
  211. 'ms_addon_bbpress_model_rule_get_query_args',  
  212. $args 
  213. ); 
  214.  
  215. /** 
  216. * Get BBPress custom post types. 
  217. * @since 1.0.0 
  218. * @return array The bbpress custom post types. 
  219. */ 
  220. public static function get_bb_cpt() { 
  221. return apply_filters( 
  222. 'ms_addon_bbpress_rule_model_get_bb_cpt',  
  223. array( 
  224. self::CPT_BB_FORUM,  
  225. self::CPT_BB_TOPIC,  
  226. self::CPT_BB_REPLY,  
  227. );