/app/rule/category/class-ms-rule-category-model.php

  1. <?php 
  2. /** 
  3. * Membership Category Rule class. 
  4. * 
  5. * Persisted by Membership class. 
  6. * 
  7. * @since 1.0.0 
  8. * @package Membership2 
  9. * @subpackage Model 
  10. */ 
  11. class MS_Rule_Category_Model extends MS_Rule { 
  12.  
  13. /** 
  14. * Rule type. 
  15. * 
  16. * @since 1.0.0 
  17. * 
  18. * @var string $rule_type 
  19. */ 
  20. protected $rule_type = MS_Rule_Category::RULE_ID; 
  21.  
  22. /** 
  23. * Returns the active flag for a specific rule. 
  24. * State depends on Add-on 
  25. * 
  26. * @since 1.0.0 
  27. * @return bool 
  28. */ 
  29. static public function is_active() { 
  30. return MS_Model_Addon::is_enabled( MS_Addon_Category::ID ); 
  31.  
  32. /** 
  33. * Set initial protection. 
  34. * 
  35. * @since 1.0.0 
  36. */ 
  37. public function protect_content() { 
  38. parent::protect_content(); 
  39.  
  40. $this->add_action( 'pre_get_posts', 'protect_posts', 98 ); 
  41. $this->add_filter( 'get_terms', 'protect_categories', 99, 2 ); 
  42.  
  43. /** 
  44. * Adds category__in filter for posts query to remove all posts which not 
  45. * belong to allowed categories. 
  46. * 
  47. * Related Filters: 
  48. * - pre_get_posts 
  49. * 
  50. * @since 1.0.0 
  51. * 
  52. * @param WP_Query $query The WP_Query object to filter. 
  53. */ 
  54. public function protect_posts( $wp_query ) { 
  55. $post_type = self::get_post_type( $wp_query ); 
  56.  
  57. /** 
  58. * '' .. when post type is unknown assume 'post' 
  59. * 'post' .. obviously protect certain posts 
  60. * 'page' .. when front page is set to static page 
  61. */ 
  62. if ( in_array( $post_type, array( 'post', 'page', '' ) ) ) { 
  63. // This list is already filtered (see the get_terms filter!) 
  64. $contents = get_categories( 'get=all' ); 
  65. $categories = array(); 
  66.  
  67. foreach ( $contents as $content ) { 
  68. $categories[] = absint( $content->term_id ); 
  69.  
  70. $wp_query->query_vars['category__in'] = $categories; 
  71.  
  72. do_action( 
  73. 'ms_rule_category_model_protect_posts',  
  74. $wp_query,  
  75. $this 
  76. ); 
  77.  
  78. /** 
  79. * Filters categories and removes all not accessible categories. 
  80. * 
  81. * @since 1.0.0 
  82. * 
  83. * @param array $terms The terms array. 
  84. * @param array $taxonomies The taxonomies array. 
  85. * @return array Filtered terms array. 
  86. */ 
  87. public function protect_categories( $terms, $taxonomies ) { 
  88. $new_terms = array(); 
  89.  
  90. // Bail - not fetching category taxonomy. 
  91. if ( ! in_array( 'category', $taxonomies ) ) { 
  92. return $terms; 
  93.  
  94. if ( ! is_array( $terms ) ) { 
  95. $terms = (array) $terms; 
  96.  
  97. foreach ( $terms as $key => $term ) { 
  98. if ( ! empty( $term->taxonomy ) && 'category' === $term->taxonomy ) { 
  99. if ( parent::has_access( $term->term_id ) ) { 
  100. $new_terms[ $key ] = $term; 
  101. } else { 
  102. // Taxonomy is no category: Add it so custom taxonomies don't break. 
  103. $new_terms[ $key ] = $term; 
  104.  
  105. return apply_filters( 
  106. 'ms_rule_category_model_protect_categories',  
  107. $new_terms 
  108. ); 
  109.  
  110. /** 
  111. * Verify access to the current category or post belonging to a catogory. 
  112. * 
  113. * @since 1.0.0 
  114. * 
  115. * @param int $id The current post_id. 
  116. * @return bool|null True if has access, false otherwise. 
  117. * Null means: Rule not relevant for current page. 
  118. */ 
  119. public function has_access( $id, $admin_has_access = true ) { 
  120. $has_access = null; 
  121.  
  122. $taxonomies = get_object_taxonomies( get_post_type() ); 
  123.  
  124. // Verify post access accordingly to category rules. 
  125. if ( ! empty( $id ) 
  126. || ( is_single() && in_array( 'category', $taxonomies ) ) 
  127. ) { 
  128. if ( empty( $id ) ) { 
  129. $id = get_the_ID(); 
  130.  
  131. $categories = wp_get_post_categories( $id ); 
  132. foreach ( $categories as $category_id ) { 
  133. $has_access = parent::has_access( $category_id, $admin_has_access ); 
  134.  
  135. if ( $has_access ) { 
  136. break; 
  137. } elseif ( is_category() ) { 
  138. // Category page. 
  139. $category = get_queried_object_id(); 
  140. $has_access = parent::has_access( $category, $admin_has_access ); 
  141.  
  142. return apply_filters( 
  143. 'ms_rule_category_model_has_access',  
  144. $has_access,  
  145. $id,  
  146. $this 
  147. ); 
  148.  
  149. /** 
  150. * Get content to protect. 
  151. * 
  152. * @since 1.0.0 
  153. * 
  154. * @param string $args The default query args. 
  155. * @return array The content. 
  156. */ 
  157. public function get_contents( $args = null ) { 
  158. $args = $this->get_query_args( $args ); 
  159.  
  160. $categories = get_categories( $args ); 
  161. $cont = array(); 
  162.  
  163. foreach ( $categories as $key => $category ) { 
  164. $category->id = $category->term_id; 
  165.  
  166. $category->type = MS_Rule_Category::RULE_ID; 
  167. $category->access = $this->get_rule_value( $category->id ); 
  168.  
  169. if ( array_key_exists( $category->id, $this->dripped ) ) { 
  170. $category->delayed_period = 
  171. $this->dripped[ $category->id ]['period_unit'] . ' ' . 
  172. $this->dripped[ $category->id ]['period_type']; 
  173. $category->dripped = $this->dripped[ $category->id ]; 
  174. } else { 
  175. $category->delayed_period = ''; 
  176.  
  177. $cont[ $key ] = $category; 
  178.  
  179. return $cont; 
  180.  
  181. /** 
  182. * Get the total content count. 
  183. * 
  184. * @since 1.0.0 
  185. * 
  186. * @param $args The query post args 
  187. * @return int The total content count. 
  188. */ 
  189. public function get_content_count( $args = null ) { 
  190. unset( $args['number'] ); 
  191. $args = $this->get_query_args( $args ); 
  192. $categories = get_categories( $args ); 
  193.  
  194. $count = count( $categories ); 
  195.  
  196. return apply_filters( 
  197. 'ms_rule_category_model_get_content_count',  
  198. $count,  
  199. $args 
  200. ); 
  201.  
  202. /** 
  203. * Get the default query args. 
  204. * 
  205. * @since 1.0.0 
  206. * 
  207. * @param string $args The query post args. 
  208. * @return array The parsed args. 
  209. */ 
  210. public function get_query_args( $args = null ) { 
  211. return parent::prepare_query_args( $args, 'get_categories' ); 
  212.  
.