MS_Rule_Category_Model

Membership Category Rule class.

Defined (1)

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

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