M_Posts

The Membership 2 M Posts class.

Defined (1)

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

/app_old/membershipincludes/includes/default.rules.php  
  1. class M_Posts extends M_Rule { 
  2.  
  3. var $name = 'posts'; 
  4. var $label = 'Posts'; 
  5. var $description = 'Allows specific posts to be protected.'; 
  6.  
  7. var $rulearea = 'public'; 
  8.  
  9. function admin_main($data) { 
  10. if(!$data) $data = array(); 
  11. ?> 
  12. <div class='level-operation' id='main-posts'> 
  13. <h2 class='sidebar-name'><?php _e('Posts', 'membership');?><span><a href='#remove' id='remove-posts' class='removelink' title='<?php _e("Remove Posts from this rules area.", 'membership'); ?>'><?php _e('Remove', 'membership'); ?></a></span></h2> 
  14. <div class='inner-operation'> 
  15. <p><?php _e('Select the posts to be covered by this rule by checking the box next to the relevant posts title.', 'membership'); ?></p> 
  16. <?php 
  17. $args = array( 
  18. 'numberposts' => MEMBERSHIP_POST_COUNT,  
  19. 'offset' => 0,  
  20. 'orderby' => 'post_date',  
  21. 'order' => 'DESC',  
  22. 'post_type' => 'post',  
  23. 'post_status' => 'publish' 
  24. ); 
  25.  
  26. $posts = get_posts($args); 
  27. if($posts) { 
  28. ?> 
  29. <table cellspacing="0" class="widefat fixed"> 
  30. <thead> 
  31. <tr> 
  32. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  33. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Post title', 'membership'); ?></th> 
  34. <th style="" class="manage-column column-date" id="date" scope="col"><?php _e('Post date', 'membership'); ?></th> 
  35. </tr> 
  36. </thead> 
  37.  
  38. <tfoot> 
  39. <tr> 
  40. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  41. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Post title', 'membership'); ?></th> 
  42. <th style="" class="manage-column column-date" id="date" scope="col"><?php _e('Post date', 'membership'); ?></th> 
  43. </tr> 
  44. </tfoot> 
  45.  
  46. <tbody> 
  47. <?php 
  48. foreach($posts as $key => $post) { 
  49. ?> 
  50. <tr valign="middle" class="alternate" id="post-<?php echo $post->ID; ?>"> 
  51. <th class="check-column" scope="row"> 
  52. <input type="checkbox" value="<?php echo $post->ID; ?>" name="posts[]" <?php if(in_array($post->ID, $data)) echo 'checked="checked"'; ?>> 
  53. </th> 
  54. <td class="column-name"> 
  55. <strong><?php echo esc_html($post->post_title); ?></strong> 
  56. </td> 
  57. <td class="column-date"> 
  58. <?php 
  59. echo date("Y/m/d", strtotime($post->post_date)); 
  60. ?> 
  61. </td> 
  62. </tr> 
  63. <?php 
  64. ?> 
  65. </tbody> 
  66. </table> 
  67. <?php 
  68.  
  69. ?> 
  70. <p class='description'><?php echo sprintf(__("Only the most recent %d posts are shown above, if you have more than that then you should consider using categories instead.", 'membership'), MEMBERSHIP_POST_COUNT); ?></p> 
  71. </div> 
  72. </div> 
  73. <?php 
  74.  
  75. function redirect() { 
  76.  
  77. membership_redirect_to_protected(); 
  78.  
  79.  
  80. function get_group() { 
  81.  
  82. global $wpdb; 
  83.  
  84. $sql = $wpdb->prepare( "SELECT id FROM " . membership_db_prefix($wpdb, 'urlgroups') . " WHERE groupname = %s ORDER BY id DESC LIMIT 0, 1", '_posts-' . $this->level_id ); 
  85.  
  86. $results = $wpdb->get_var( $sql ); 
  87.  
  88. if(!empty($results)) { 
  89. return $results; 
  90. } else { 
  91. return false; 
  92.  
  93. function on_positive($data) { 
  94.  
  95. $this->data = $data; 
  96.  
  97. add_action('pre_get_posts', array(&$this, 'add_viewable_posts'), 99 ); 
  98.  
  99. add_filter( 'the_posts', array(&$this, 'check_positive_posts')); 
  100.  
  101. function on_negative($data) { 
  102.  
  103. $this->data = $data; 
  104.  
  105. add_action('pre_get_posts', array(&$this, 'add_unviewable_posts'), 99 ); 
  106.  
  107. add_filter( 'the_posts', array(&$this, 'check_negative_posts')); 
  108.  
  109. function add_viewable_posts($wp_query) { 
  110.  
  111. global $M_options; 
  112.  
  113. if( !$wp_query->is_singular && empty($wp_query->query_vars['pagename']) && (!isset($wp_query->query_vars['post_type']) || in_array($wp_query->query_vars['post_type'], array('post', '')))) { 
  114.  
  115. // We are in a list rather than on a single post 
  116. foreach( (array) $this->data as $key => $value ) { 
  117. $wp_query->query_vars['post__in'][] = $value; 
  118.  
  119. $wp_query->query_vars['post__in'] = array_unique($wp_query->query_vars['post__in']); 
  120. } else { 
  121. // We are on a single post - wait until we get to the_posts 
  122.  
  123.  
  124.  
  125.  
  126. function add_unviewable_posts($wp_query) { 
  127.  
  128. global $M_options; 
  129.  
  130. if( !$wp_query->is_singular && empty($wp_query->query_vars['pagename']) && (!isset($wp_query->query_vars['post_type']) || in_array($wp_query->query_vars['post_type'], array('post', '')))) { 
  131.  
  132. // We are on a list rather than on a single post 
  133. foreach( (array) $this->data as $key => $value ) { 
  134. $wp_query->query_vars['post__not_in'][] = $value; 
  135.  
  136. $wp_query->query_vars['post__not_in'] = array_unique($wp_query->query_vars['post__not_in']); 
  137.  
  138. } else { 
  139. // We are on a single post - wait until we get to the_posts 
  140.  
  141.  
  142.  
  143. function check_negative_posts( $posts ) { 
  144.  
  145. global $wp_query, $M_options; 
  146.  
  147. if( !$wp_query->is_singular || count($posts) > 1) { 
  148. return $posts; 
  149.  
  150. if(!empty($posts) && count($posts) == 1) { 
  151. // we may be on a restricted post so check the URL and redirect if needed 
  152.  
  153. $redirect = false; 
  154. $url = ''; 
  155.  
  156. $exclude = array(); 
  157. if(!empty($M_options['registration_page'])) { 
  158. $exclude[] = get_permalink( (int) $M_options['registration_page'] ); 
  159. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['registration_page'] )); 
  160.  
  161. if(!empty($M_options['account_page'])) { 
  162. $exclude[] = get_permalink( (int) $M_options['account_page'] ); 
  163. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['account_page'] )); 
  164.  
  165. if(!empty($M_options['nocontent_page'])) { 
  166. $exclude[] = get_permalink( (int) $M_options['nocontent_page'] ); 
  167. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['nocontent_page'] )); 
  168.  
  169. if(!empty($wp_query->query_vars['protectedfile']) && !$forceviewing) { 
  170. $exclude[] = $host; 
  171. $exclude[] = untrailingslashit($host); 
  172.  
  173. foreach($posts as $post) { 
  174. if($post->post_type != 'post') { 
  175. continue; 
  176.  
  177. if(!in_array(strtolower( get_permalink($post->ID) ), $exclude)) { 
  178. $url = get_permalink($post->ID); 
  179.  
  180. // Check if we have a url available to check 
  181. if(empty($url)) { 
  182. return $posts; 
  183.  
  184. // we have the current page / url - get the groups selected 
  185. $group_id = $this->get_group(); 
  186.  
  187. if($group_id) { 
  188. $group = new M_Urlgroup( $group_id ); 
  189.  
  190. if( !empty($url) && $group->url_matches( $url ) ) { 
  191. $redirect = true; 
  192.  
  193. if($redirect === true && !empty($M_options['nocontent_page'])) { 
  194. // we need to redirect 
  195. $this->redirect(); 
  196. } else { 
  197. return $posts; 
  198.  
  199. return $posts; 
  200.  
  201.  
  202. function check_positive_posts( $posts ) { 
  203.  
  204. global $wp_query, $M_options; 
  205.  
  206. if( !$wp_query->is_singular || count($posts) > 1) { 
  207. return $posts; 
  208.  
  209. if(!empty($posts) && count($posts) == 1) { 
  210. // we may be on a restricted post so check the URL and redirect if needed 
  211.  
  212. $redirect = false; 
  213. $found = false; 
  214. $url = ''; 
  215.  
  216. $exclude = array(); 
  217. if(!empty($M_options['registration_page'])) { 
  218. $exclude[] = get_permalink( (int) $M_options['registration_page'] ); 
  219. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['registration_page'] )); 
  220.  
  221. if(!empty($M_options['account_page'])) { 
  222. $exclude[] = get_permalink( (int) $M_options['account_page'] ); 
  223. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['account_page'] )); 
  224.  
  225. if(!empty($M_options['nocontent_page'])) { 
  226. $exclude[] = get_permalink( (int) $M_options['nocontent_page'] ); 
  227. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['nocontent_page'] )); 
  228.  
  229. if(!empty($wp_query->query_vars['protectedfile']) && !$forceviewing) { 
  230. $exclude[] = $host; 
  231. $exclude[] = untrailingslashit($host); 
  232.  
  233. foreach($posts as $post) { 
  234. if($post->post_type != 'post') { 
  235. continue; 
  236.  
  237. if(!in_array(strtolower( get_permalink($post->ID) ), $exclude)) { 
  238. $url = get_permalink($post->ID); 
  239.  
  240. // Check if we have a url available to check 
  241. if(empty($url)) { 
  242. return $posts; 
  243.  
  244. // we have the current page / url - get the groups selected 
  245. $group_id = $this->get_group(); 
  246.  
  247. if($group_id) { 
  248. $group = new M_Urlgroup( $group_id ); 
  249.  
  250. if( $group->url_matches( $url ) ) { 
  251. $found = true; 
  252.  
  253. if($found !== true && !empty($M_options['nocontent_page'])) { 
  254. // we need to redirect 
  255. $this->redirect(); 
  256. } else { 
  257. return $posts; 
  258.  
  259.  
  260. return $posts; 
  261.  
  262.