/app/addon/bbpress/rule/class-ms-addon-bbpress-rule-model.php

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