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