/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. if( is_category() || is_home() || is_search() ) { 
  56. $post_type = self::get_post_type( $wp_query ); 
  57.  
  58.  
  59. /** 
  60. * '' .. when post type is unknown assume 'post' 
  61. * 'post' .. obviously protect certain posts 
  62. * 'page' .. when front page is set to static page 
  63. */ 
  64. if ( in_array( $post_type, array( 'post', 'page', '' ) ) ) { 
  65. // This list is already filtered (see the get_terms filter!) 
  66. $contents = get_categories( 'get=all' ); 
  67. $categories = array(); 
  68.  
  69. foreach ( $contents as $content ) { 
  70. $categories[] = absint( $content->term_id ); 
  71.  
  72. $wp_query->query_vars['category__in'] = $categories; 
  73.  
  74. do_action( 
  75. 'ms_rule_category_model_protect_posts',  
  76. $wp_query,  
  77. $this 
  78. ); 
  79.  
  80. /** 
  81. * Filters categories and removes all not accessible categories. 
  82. * 
  83. * @since 1.0.0 
  84. * 
  85. * @param array $terms The terms array. 
  86. * @param array $taxonomies The taxonomies array. 
  87. * @return array Filtered terms array. 
  88. */ 
  89. public function protect_categories( $terms, $taxonomies ) { 
  90. $new_terms = array(); 
  91.  
  92. // Bail - not fetching category taxonomy. 
  93. if ( ! in_array( 'category', $taxonomies ) ) { 
  94. return $terms; 
  95.  
  96. if ( ! is_array( $terms ) ) { 
  97. $terms = (array) $terms; 
  98.  
  99. foreach ( $terms as $key => $term ) { 
  100. if ( ! empty( $term->taxonomy ) && 'category' === $term->taxonomy ) { 
  101. if ( $this->has_access( $term->term_id ) ) { 
  102. $new_terms[ $key ] = $term; 
  103. } else { 
  104. // Taxonomy is no category: Add it so custom taxonomies don't break. 
  105. $new_terms[ $key ] = $term; 
  106.  
  107. return apply_filters( 
  108. 'ms_rule_category_model_protect_categories',  
  109. $new_terms 
  110. ); 
  111.  
  112. /** 
  113. * Verify access to the current category or post belonging to a catogory. 
  114. * 
  115. * @since 1.0.0 
  116. * 
  117. * @param int $id The current post_id. 
  118. * @return bool|null True if has access, false otherwise. 
  119. * Null means: Rule not relevant for current page. 
  120. */ 
  121. public function has_access( $id, $admin_has_access = true ) { 
  122. $has_access = null; 
  123.  
  124. $taxonomies = get_object_taxonomies( get_post_type() ); 
  125.  
  126. // Verify post access accordingly to category rules. 
  127. if ( ! empty( $id ) 
  128. || ( is_single() && in_array( 'category', $taxonomies ) ) 
  129. ) { 
  130. if ( empty( $id ) ) { 
  131. $id = get_the_ID(); 
  132.  
  133. $categories = wp_get_post_categories( $id ); 
  134. foreach ( $categories as $category_id ) { 
  135. $has_access = parent::has_access( $category_id, $admin_has_access ); 
  136.  
  137. if ( $has_access ) { 
  138. break; 
  139. } elseif ( is_category() ) { 
  140. // Category page. 
  141. $category = get_queried_object_id(); 
  142. $has_access = parent::has_access( $category, $admin_has_access ); 
  143.  
  144. return apply_filters( 
  145. 'ms_rule_category_model_has_access',  
  146. $has_access,  
  147. $id,  
  148. $admin_has_access,  
  149. $this 
  150. ); 
  151.  
  152. /** 
  153. * Get content to protect. 
  154. * 
  155. * @since 1.0.0 
  156. * 
  157. * @param string $args The default query args. 
  158. * @return array The content. 
  159. */ 
  160. public function get_contents( $args = null ) { 
  161. $args = $this->get_query_args( $args ); 
  162.  
  163. $categories = get_categories( $args ); 
  164. $cont = array(); 
  165.  
  166. foreach ( $categories as $key => $category ) { 
  167. $category->id = $category->term_id; 
  168.  
  169. $category->type = MS_Rule_Category::RULE_ID; 
  170. $category->access = $this->get_rule_value( $category->id ); 
  171.  
  172. if ( array_key_exists( $category->id, $this->dripped ) ) { 
  173. $category->delayed_period = 
  174. $this->dripped[ $category->id ]['period_unit'] . ' ' . 
  175. $this->dripped[ $category->id ]['period_type']; 
  176. $category->dripped = $this->dripped[ $category->id ]; 
  177. } else { 
  178. $category->delayed_period = ''; 
  179.  
  180. $cont[ $key ] = $category; 
  181.  
  182. return $cont; 
  183.  
  184. /** 
  185. * Get the total content count. 
  186. * 
  187. * @since 1.0.0 
  188. * 
  189. * @param $args The query post args 
  190. * @return int The total content count. 
  191. */ 
  192. public function get_content_count( $args = null ) { 
  193. unset( $args['number'] ); 
  194. $args = $this->get_query_args( $args ); 
  195. $categories = get_categories( $args ); 
  196.  
  197. $count = count( $categories ); 
  198.  
  199. return apply_filters( 
  200. 'ms_rule_category_model_get_content_count',  
  201. $count,  
  202. $args 
  203. ); 
  204.  
  205. /** 
  206. * Get the default query args. 
  207. * 
  208. * @since 1.0.0 
  209. * 
  210. * @param string $args The query post args. 
  211. * @return array The parsed args. 
  212. */ 
  213. public function get_query_args( $args = null ) { 
  214. return parent::prepare_query_args( $args, 'get_categories' ); 
  215.  
.