M_Categories

The Membership 2 M Categories class.

Defined (1)

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

/app_old/membershipincludes/includes/default.rules.php  
  1. class M_Categories extends M_Rule { 
  2.  
  3. var $name = 'categories'; 
  4. var $label = 'Categories'; 
  5. var $description = 'Allows posts to be protected based on their assigned categories.'; 
  6.  
  7. var $rulearea = 'public'; 
  8.  
  9. function admin_main($data) { 
  10. if(!$data) $data = array(); 
  11. ?> 
  12. <div class='level-operation' id='main-categories'> 
  13. <h2 class='sidebar-name'><?php _e('Categories', 'membership');?><span><a href='#remove' class='removelink' id='remove-categories' title='<?php _e("Remove Categories from this rules area.", 'membership'); ?>'><?php _e('Remove', 'membership'); ?></a></span></h2> 
  14. <div class='inner-operation'> 
  15. <p><?php _e('Select the Categories to be covered by this rule by checking the box next to the relevant categories name.', 'membership'); ?></p> 
  16. <?php 
  17. $categories = get_categories('get=all'); 
  18.  
  19. if($categories) { 
  20. ?> 
  21. <table cellspacing="0" class="widefat fixed"> 
  22. <thead> 
  23. <tr> 
  24. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  25. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Category name', 'membership'); ?></th> 
  26. </tr> 
  27. </thead> 
  28.  
  29. <tfoot> 
  30. <tr> 
  31. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  32. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Category name', 'membership'); ?></th> 
  33. </tr> 
  34. </tfoot> 
  35.  
  36. <tbody> 
  37. <?php 
  38. foreach($categories as $key => $category) { 
  39. ?> 
  40. <tr valign="middle" class="alternate" id="post-<?php echo $category->term_id; ?>"> 
  41. <th class="check-column" scope="row"> 
  42. <input type="checkbox" value="<?php echo $category->term_id; ?>" name="categories[]" <?php if(in_array($category->term_id, $data)) echo 'checked="checked"'; ?>> 
  43. </th> 
  44. <td class="column-name"> 
  45. <strong><?php echo esc_html($category->name); ?></strong> 
  46. </td> 
  47. </tr> 
  48. <?php 
  49. ?> 
  50. </tbody> 
  51. </table> 
  52. <?php 
  53. ?> 
  54. </div> 
  55. </div> 
  56. <?php 
  57.  
  58. function on_positive($data) { 
  59.  
  60. $this->data = $data; 
  61.  
  62. add_action( 'pre_get_posts', array(&$this, 'add_viewable_posts'), 1 ); 
  63. add_filter( 'get_terms', array(&$this, 'add_viewable_categories'), 1, 3 ); 
  64.  
  65. add_filter( 'the_posts', array(&$this, 'check_positive_posts')); 
  66.  
  67. function on_negative($data) { 
  68.  
  69. $this->data = $data; 
  70.  
  71. add_action('pre_get_posts', array(&$this, 'add_unviewable_posts'), 1 ); 
  72. add_filter( 'get_terms', array(&$this, 'add_unviewable_categories'), 1, 3 ); 
  73.  
  74. add_filter( 'the_posts', array(&$this, 'check_negative_posts')); 
  75.  
  76. function redirect() { 
  77.  
  78. membership_redirect_to_protected(); 
  79.  
  80.  
  81. function check_negative_posts( $posts ) { 
  82.  
  83. global $wp_query, $M_options; 
  84.  
  85. $redirect = false; 
  86.  
  87. if(is_category() && count($posts) == 0 && MEMBERSHIP_REDIRECT_ON_EMPTY_CATEGORYPAGE === true) { 
  88. $redirect = true; 
  89.  
  90. if((!$wp_query->is_singular || count($posts) > 1) && $redirect != true) { 
  91. return $posts; 
  92.  
  93. foreach($posts as $post) { 
  94. // should only be one as otherwise the single check above didn't work very well. 
  95. if($post->post_type != 'post') { 
  96. // Not a post so ignore 
  97. return $posts; 
  98. } else { 
  99. // Check the categories 
  100. if(has_category( $this->data, $post )) { 
  101. $redirect = true; 
  102.  
  103. if($redirect === true && !empty($M_options['nocontent_page'])) { 
  104. // we need to redirect 
  105. $this->redirect(); 
  106. } else { 
  107. return $posts; 
  108.  
  109.  
  110. function check_positive_posts( $posts ) { 
  111.  
  112. global $wp_query, $M_options; 
  113.  
  114. $redirect = false; 
  115.  
  116. if(is_category() && count($posts) == 0 && MEMBERSHIP_REDIRECT_ON_EMPTY_CATEGORYPAGE === true) { 
  117. $redirect = true; 
  118.  
  119. if((!$wp_query->is_singular || count($posts) > 1) && $redirect != true) { 
  120. return $posts; 
  121.  
  122. foreach($posts as $post) { 
  123. // should only be one as otherwise the single check above didn't work very well. 
  124. if($post->post_type != 'post') { 
  125. // Not a post so ignore 
  126. return $posts; 
  127. } else { 
  128. // Check the categories 
  129. if(!has_category( $this->data, $post )) { 
  130. $redirect = true; 
  131.  
  132.  
  133. if($redirect === true && !empty($M_options['nocontent_page'])) { 
  134. // we need to redirect 
  135. $this->redirect(); 
  136. } else { 
  137. return $posts; 
  138.  
  139.  
  140. function add_viewable_posts($wp_query) { 
  141.  
  142. //print_r($wp_query); 
  143.  
  144. if((isset($wp_query->query_vars['post_type']) && !in_array($wp_query->query_vars['post_type'], array('post', ''))) || !empty($wp_query->query_vars['pagename'])) { 
  145. return; 
  146.  
  147. foreach( (array) $this->data as $key => $value ) { 
  148. $wp_query->query_vars['category__in'][] = $value; 
  149.  
  150. $wp_query->query_vars['category__in'] = array_unique($wp_query->query_vars['category__in']); 
  151.  
  152.  
  153. function add_unviewable_posts($wp_query) { 
  154.  
  155. if( (isset($wp_query->query_vars['post_type']) && !in_array($wp_query->query_vars['post_type'], array('post', ''))) || !empty($wp_query->query_vars['pagename'])) { 
  156. return; 
  157.  
  158. foreach( (array) $this->data as $key => $value ) { 
  159. $wp_query->query_vars['category__not_in'][] = $value; 
  160.  
  161. $wp_query->query_vars['category__not_in'] = array_unique($wp_query->query_vars['category__not_in']); 
  162.  
  163.  
  164. function add_viewable_categories($terms, $taxonomies, $args) { 
  165.  
  166. foreach( (array) $terms as $key => $value ) { 
  167. if($value->taxonomy == 'category') { 
  168. if(!in_array($value->term_id, $this->data)) { 
  169. unset($terms[$key]); 
  170.  
  171. return $terms; 
  172.  
  173. function add_unviewable_categories($terms, $taxonomies, $args) { 
  174.  
  175. foreach( (array) $terms as $key => $value ) { 
  176. if($value->taxonomy == 'category') { 
  177. if(in_array($value->term_id, $this->data)) { 
  178. unset($terms[$key]); 
  179.  
  180. return $terms; 
  181.