/app_old/membershipincludes/includes/default.rules.php

  1. <?php 
  2.  
  3. class M_Posts extends M_Rule { 
  4.  
  5. var $name = 'posts'; 
  6. var $label = 'Posts'; 
  7. var $description = 'Allows specific posts to be protected.'; 
  8.  
  9. var $rulearea = 'public'; 
  10.  
  11. function admin_main($data) { 
  12. if(!$data) $data = array(); 
  13. ?> 
  14. <div class='level-operation' id='main-posts'> 
  15. <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> 
  16. <div class='inner-operation'> 
  17. <p><?php _e('Select the posts to be covered by this rule by checking the box next to the relevant posts title.', 'membership'); ?></p> 
  18. <?php 
  19. $args = array( 
  20. 'numberposts' => MEMBERSHIP_POST_COUNT,  
  21. 'offset' => 0,  
  22. 'orderby' => 'post_date',  
  23. 'order' => 'DESC',  
  24. 'post_type' => 'post',  
  25. 'post_status' => 'publish' 
  26. ); 
  27.  
  28. $posts = get_posts($args); 
  29. if($posts) { 
  30. ?> 
  31. <table cellspacing="0" class="widefat fixed"> 
  32. <thead> 
  33. <tr> 
  34. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  35. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Post title', 'membership'); ?></th> 
  36. <th style="" class="manage-column column-date" id="date" scope="col"><?php _e('Post date', 'membership'); ?></th> 
  37. </tr> 
  38. </thead> 
  39.  
  40. <tfoot> 
  41. <tr> 
  42. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  43. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Post title', 'membership'); ?></th> 
  44. <th style="" class="manage-column column-date" id="date" scope="col"><?php _e('Post date', 'membership'); ?></th> 
  45. </tr> 
  46. </tfoot> 
  47.  
  48. <tbody> 
  49. <?php 
  50. foreach($posts as $key => $post) { 
  51. ?> 
  52. <tr valign="middle" class="alternate" id="post-<?php echo $post->ID; ?>"> 
  53. <th class="check-column" scope="row"> 
  54. <input type="checkbox" value="<?php echo $post->ID; ?>" name="posts[]" <?php if(in_array($post->ID, $data)) echo 'checked="checked"'; ?>> 
  55. </th> 
  56. <td class="column-name"> 
  57. <strong><?php echo esc_html($post->post_title); ?></strong> 
  58. </td> 
  59. <td class="column-date"> 
  60. <?php 
  61. echo date("Y/m/d", strtotime($post->post_date)); 
  62. ?> 
  63. </td> 
  64. </tr> 
  65. <?php 
  66. ?> 
  67. </tbody> 
  68. </table> 
  69. <?php 
  70.  
  71. ?> 
  72. <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> 
  73. </div> 
  74. </div> 
  75. <?php 
  76.  
  77. function redirect() { 
  78.  
  79. membership_redirect_to_protected(); 
  80.  
  81.  
  82. function get_group() { 
  83.  
  84. global $wpdb; 
  85.  
  86. $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 ); 
  87.  
  88. $results = $wpdb->get_var( $sql ); 
  89.  
  90. if(!empty($results)) { 
  91. return $results; 
  92. } else { 
  93. return false; 
  94.  
  95. function on_positive($data) { 
  96.  
  97. $this->data = $data; 
  98.  
  99. add_action('pre_get_posts', array(&$this, 'add_viewable_posts'), 99 ); 
  100.  
  101. add_filter( 'the_posts', array(&$this, 'check_positive_posts')); 
  102.  
  103. function on_negative($data) { 
  104.  
  105. $this->data = $data; 
  106.  
  107. add_action('pre_get_posts', array(&$this, 'add_unviewable_posts'), 99 ); 
  108.  
  109. add_filter( 'the_posts', array(&$this, 'check_negative_posts')); 
  110.  
  111. function add_viewable_posts($wp_query) { 
  112.  
  113. global $M_options; 
  114.  
  115. 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', '')))) { 
  116.  
  117. // We are in a list rather than on a single post 
  118. foreach( (array) $this->data as $key => $value ) { 
  119. $wp_query->query_vars['post__in'][] = $value; 
  120.  
  121. $wp_query->query_vars['post__in'] = array_unique($wp_query->query_vars['post__in']); 
  122. } else { 
  123. // We are on a single post - wait until we get to the_posts 
  124.  
  125.  
  126.  
  127.  
  128. function add_unviewable_posts($wp_query) { 
  129.  
  130. global $M_options; 
  131.  
  132. 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', '')))) { 
  133.  
  134. // We are on a list rather than on a single post 
  135. foreach( (array) $this->data as $key => $value ) { 
  136. $wp_query->query_vars['post__not_in'][] = $value; 
  137.  
  138. $wp_query->query_vars['post__not_in'] = array_unique($wp_query->query_vars['post__not_in']); 
  139.  
  140. } else { 
  141. // We are on a single post - wait until we get to the_posts 
  142.  
  143.  
  144.  
  145. function check_negative_posts( $posts ) { 
  146.  
  147. global $wp_query, $M_options; 
  148.  
  149. if( !$wp_query->is_singular || count($posts) > 1) { 
  150. return $posts; 
  151.  
  152. if(!empty($posts) && count($posts) == 1) { 
  153. // we may be on a restricted post so check the URL and redirect if needed 
  154.  
  155. $redirect = false; 
  156. $url = ''; 
  157.  
  158. $exclude = array(); 
  159. if(!empty($M_options['registration_page'])) { 
  160. $exclude[] = get_permalink( (int) $M_options['registration_page'] ); 
  161. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['registration_page'] )); 
  162.  
  163. if(!empty($M_options['account_page'])) { 
  164. $exclude[] = get_permalink( (int) $M_options['account_page'] ); 
  165. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['account_page'] )); 
  166.  
  167. if(!empty($M_options['nocontent_page'])) { 
  168. $exclude[] = get_permalink( (int) $M_options['nocontent_page'] ); 
  169. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['nocontent_page'] )); 
  170.  
  171. if(!empty($wp_query->query_vars['protectedfile']) && !$forceviewing) { 
  172. $exclude[] = $host; 
  173. $exclude[] = untrailingslashit($host); 
  174.  
  175. foreach($posts as $post) { 
  176. if($post->post_type != 'post') { 
  177. continue; 
  178.  
  179. if(!in_array(strtolower( get_permalink($post->ID) ), $exclude)) { 
  180. $url = get_permalink($post->ID); 
  181.  
  182. // Check if we have a url available to check 
  183. if(empty($url)) { 
  184. return $posts; 
  185.  
  186. // we have the current page / url - get the groups selected 
  187. $group_id = $this->get_group(); 
  188.  
  189. if($group_id) { 
  190. $group = new M_Urlgroup( $group_id ); 
  191.  
  192. if( !empty($url) && $group->url_matches( $url ) ) { 
  193. $redirect = true; 
  194.  
  195. if($redirect === true && !empty($M_options['nocontent_page'])) { 
  196. // we need to redirect 
  197. $this->redirect(); 
  198. } else { 
  199. return $posts; 
  200.  
  201. return $posts; 
  202.  
  203.  
  204. function check_positive_posts( $posts ) { 
  205.  
  206. global $wp_query, $M_options; 
  207.  
  208. if( !$wp_query->is_singular || count($posts) > 1) { 
  209. return $posts; 
  210.  
  211. if(!empty($posts) && count($posts) == 1) { 
  212. // we may be on a restricted post so check the URL and redirect if needed 
  213.  
  214. $redirect = false; 
  215. $found = false; 
  216. $url = ''; 
  217.  
  218. $exclude = array(); 
  219. if(!empty($M_options['registration_page'])) { 
  220. $exclude[] = get_permalink( (int) $M_options['registration_page'] ); 
  221. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['registration_page'] )); 
  222.  
  223. if(!empty($M_options['account_page'])) { 
  224. $exclude[] = get_permalink( (int) $M_options['account_page'] ); 
  225. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['account_page'] )); 
  226.  
  227. if(!empty($M_options['nocontent_page'])) { 
  228. $exclude[] = get_permalink( (int) $M_options['nocontent_page'] ); 
  229. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['nocontent_page'] )); 
  230.  
  231. if(!empty($wp_query->query_vars['protectedfile']) && !$forceviewing) { 
  232. $exclude[] = $host; 
  233. $exclude[] = untrailingslashit($host); 
  234.  
  235. foreach($posts as $post) { 
  236. if($post->post_type != 'post') { 
  237. continue; 
  238.  
  239. if(!in_array(strtolower( get_permalink($post->ID) ), $exclude)) { 
  240. $url = get_permalink($post->ID); 
  241.  
  242. // Check if we have a url available to check 
  243. if(empty($url)) { 
  244. return $posts; 
  245.  
  246. // we have the current page / url - get the groups selected 
  247. $group_id = $this->get_group(); 
  248.  
  249. if($group_id) { 
  250. $group = new M_Urlgroup( $group_id ); 
  251.  
  252. if( $group->url_matches( $url ) ) { 
  253. $found = true; 
  254.  
  255. if($found !== true && !empty($M_options['nocontent_page'])) { 
  256. // we need to redirect 
  257. $this->redirect(); 
  258. } else { 
  259. return $posts; 
  260.  
  261.  
  262. return $posts; 
  263.  
  264.  
  265.  
  266. class M_Pages extends M_Rule { 
  267.  
  268. var $name = 'pages'; 
  269. var $label = 'Pages'; 
  270. var $description = 'Allows specific pages to be protected.'; 
  271.  
  272. var $rulearea = 'public'; 
  273.  
  274. function admin_main($data) { 
  275. if(!$data) $data = array(); 
  276.  
  277. ?> 
  278. <div class='level-operation' id='main-pages'> 
  279. <h2 class='sidebar-name'><?php _e('Pages', 'membership');?><span><a href='#remove' id='remove-pages' class='removelink' title='<?php _e("Remove Pages from this rules area.", 'membership'); ?>'><?php _e('Remove', 'membership'); ?></a></span></h2> 
  280. <div class='inner-operation'> 
  281. <p><?php _e('Select the Pages to be covered by this rule by checking the box next to the relevant pages title.', 'membership'); ?></p> 
  282. <?php 
  283. $args = array( 
  284. 'numberposts' => MEMBERSHIP_PAGE_COUNT,  
  285. 'offset' => 0,  
  286. 'orderby' => 'post_date',  
  287. 'order' => 'DESC',  
  288. 'post_type' => 'page',  
  289. 'post_status' => 'publish' 
  290. ); 
  291.  
  292. $posts = get_posts($args); 
  293.  
  294. // to remove bp specified pages - should be listed on the bp pages group 
  295. $posts = apply_filters( 'staypress_hide_protectable_pages', $posts ); 
  296.  
  297. if($posts) { 
  298. ?> 
  299. <table cellspacing="0" class="widefat fixed"> 
  300. <thead> 
  301. <tr> 
  302. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  303. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Page title', 'membership'); ?></th> 
  304. </tr> 
  305. </thead> 
  306.  
  307. <tfoot> 
  308. <tr> 
  309. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  310. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Page title', 'membership'); ?></th> 
  311. </tr> 
  312. </tfoot> 
  313.  
  314. <tbody> 
  315. <?php 
  316. foreach($posts as $key => $post) { 
  317.  
  318. ?> 
  319. <tr valign="middle" class="alternate" id="post-<?php echo $post->ID; ?>"> 
  320. <th class="check-column" scope="row"> 
  321. <input type="checkbox" value="<?php echo $post->ID; ?>" name="pages[]" <?php if(in_array($post->ID, $data)) echo 'checked="checked"'; ?>> 
  322. </th> 
  323. <td class="column-name"> 
  324. <strong><?php echo esc_html($post->post_title); ?></strong> 
  325. </td> 
  326. </tr> 
  327. <?php 
  328. ?> 
  329. </tbody> 
  330. </table> 
  331. <?php 
  332.  
  333. ?> 
  334. <p class='description'><?php echo sprintf(__("Only the most recent %d pages are shown above.", 'membership'), MEMBERSHIP_PAGE_COUNT); ?></p> 
  335. </div> 
  336. </div> 
  337. <?php 
  338.  
  339. function on_positive($data) { 
  340.  
  341. $this->data = $data; 
  342.  
  343. add_filter('get_pages', array(&$this, 'add_viewable_pages_menu'), 1); 
  344.  
  345. add_filter( 'the_posts', array(&$this, 'check_positive_pages')); 
  346.  
  347.  
  348. function on_negative($data) { 
  349.  
  350. $this->data = $data; 
  351.  
  352. add_filter('get_pages', array(&$this, 'add_unviewable_pages_menu'), 1); 
  353.  
  354. add_filter( 'the_posts', array(&$this, 'check_negative_pages')); 
  355.  
  356.  
  357. function redirect() { 
  358.  
  359. membership_redirect_to_protected(); 
  360.  
  361.  
  362. function get_group() { 
  363.  
  364. global $wpdb; 
  365.  
  366. $sql = $wpdb->prepare( "SELECT id FROM " . membership_db_prefix($wpdb, 'urlgroups') . " WHERE groupname = %s ORDER BY id DESC LIMIT 0, 1", '_pages-' . $this->level_id ); 
  367.  
  368. $results = $wpdb->get_var( $sql ); 
  369.  
  370. if(!empty($results)) { 
  371. return $results; 
  372. } else { 
  373. return false; 
  374.  
  375. function add_viewable_pages($wp_query) { 
  376.  
  377. global $M_options; 
  378.  
  379. print_r($wp_query); 
  380.  
  381. if(!$wp_query->is_single && !empty($wp_query->query_vars['post__in'])) { 
  382. // We are not on a single page - so just limit the viewing 
  383. foreach( (array) $this->data as $key => $value ) { 
  384. $wp_query->query_vars['post__in'][] = $value; 
  385.  
  386. $wp_query->query_vars['post__in'] = array_unique($wp_query->query_vars['post__in']); 
  387. } else { 
  388. // We are on a single page - so check for restriction on the_posts 
  389.  
  390.  
  391. function add_viewable_pages_menu($pages) { 
  392.  
  393. $override_pages = apply_filters( 'membership_override_viewable_pages_menu', array() ); 
  394.  
  395. foreach( (array) $pages as $key => $page ) { 
  396. if(!in_array($page->ID, (array) $this->data) && !in_array($page->ID, (array) $override_pages)) { 
  397. unset($pages[$key]); 
  398.  
  399. return $pages; 
  400.  
  401.  
  402. function add_unviewable_pages($wp_query) { 
  403.  
  404. global $M_options; 
  405.  
  406. return; 
  407.  
  408.  
  409. function add_unviewable_pages_menu($pages) { 
  410. foreach( (array) $pages as $key => $page ) { 
  411. if(in_array($page->ID, (array) $this->data)) { 
  412. unset($pages[$key]); 
  413.  
  414. return $pages; 
  415.  
  416. function check_negative_pages( $posts ) { 
  417.  
  418. global $wp_query, $M_options; 
  419.  
  420. if(!$wp_query->is_singular || count($posts) > 1) { 
  421. return $posts; 
  422.  
  423. if(!empty($posts) && count($posts) == 1) { 
  424. // we may be on a restricted post so check the URL and redirect if needed 
  425.  
  426. $redirect = false; 
  427. $url = ''; 
  428.  
  429. $exclude = array(); 
  430. if(!empty($M_options['registration_page'])) { 
  431. $exclude[] = get_permalink( (int) $M_options['registration_page'] ); 
  432. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['registration_page'] )); 
  433.  
  434. if(!empty($M_options['account_page'])) { 
  435. $exclude[] = get_permalink( (int) $M_options['account_page'] ); 
  436. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['account_page'] )); 
  437.  
  438. if(!empty($M_options['nocontent_page'])) { 
  439. $exclude[] = get_permalink( (int) $M_options['nocontent_page'] ); 
  440. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['nocontent_page'] )); 
  441.  
  442. if(!empty($wp_query->query_vars['protectedfile']) && !$forceviewing) { 
  443. $exclude[] = $host; 
  444. $exclude[] = untrailingslashit($host); 
  445.  
  446. foreach($posts as $post) { 
  447. if($post->post_type != 'page') { 
  448. continue; 
  449.  
  450. if(!in_array(strtolower( get_permalink($post->ID) ), $exclude)) { 
  451. $url = get_permalink($post->ID); 
  452.  
  453. // Check if we have a url available to check 
  454. if(empty($url)) { 
  455. return $posts; 
  456.  
  457. // we have the current page / url - get the groups selected 
  458. $group_id = $this->get_group(); 
  459.  
  460. if($group_id) { 
  461. $group = new M_Urlgroup( $group_id ); 
  462.  
  463. if( $group->url_matches( $url ) ) { 
  464. $redirect = true; 
  465.  
  466. if($redirect === true && !empty($M_options['nocontent_page'])) { 
  467. // we need to redirect 
  468. $this->redirect(); 
  469. } else { 
  470. return $posts; 
  471.  
  472.  
  473. return $posts; 
  474.  
  475.  
  476. function check_positive_pages( $posts ) { 
  477.  
  478. global $wp_query, $M_options; 
  479.  
  480. if(!$wp_query->is_singular || count($posts) > 1) { 
  481. return $posts; 
  482.  
  483. if(!empty($posts) && count($posts) == 1) { 
  484. // we may be on a restricted post so check the URL and redirect if needed 
  485.  
  486. $redirect = false; 
  487. $found = false; 
  488. $url = ''; 
  489.  
  490. $exclude = array(); 
  491. if(!empty($M_options['registration_page'])) { 
  492. $exclude[] = get_permalink( (int) $M_options['registration_page'] ); 
  493. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['registration_page'] )); 
  494.  
  495. if(!empty($M_options['account_page'])) { 
  496. $exclude[] = get_permalink( (int) $M_options['account_page'] ); 
  497. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['account_page'] )); 
  498.  
  499. if(!empty($M_options['nocontent_page'])) { 
  500. $exclude[] = get_permalink( (int) $M_options['nocontent_page'] ); 
  501. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['nocontent_page'] )); 
  502.  
  503. if(!empty($wp_query->query_vars['protectedfile']) && !$forceviewing) { 
  504. $exclude[] = $host; 
  505. $exclude[] = untrailingslashit($host); 
  506.  
  507. foreach($posts as $post) { 
  508. if($post->post_type != 'page') { 
  509. continue; 
  510.  
  511. if(!in_array(strtolower( get_permalink($post->ID) ), $exclude)) { 
  512. $url = get_permalink($post->ID); 
  513.  
  514. // Check if we have a url available to check 
  515. if(empty($url)) { 
  516. return $posts; 
  517.  
  518. // we have the current page / url - get the groups selected 
  519. $group_id = $this->get_group(); 
  520.  
  521. if($group_id) { 
  522. $group = new M_Urlgroup( $group_id ); 
  523.  
  524. if( $group->url_matches( $url ) ) { 
  525. $found = true; 
  526.  
  527. if($found !== true && !empty($M_options['nocontent_page'])) { 
  528. // we need to redirect 
  529. $this->redirect(); 
  530. } else { 
  531. return $posts; 
  532.  
  533.  
  534. return $posts; 
  535.  
  536.  
  537.  
  538.  
  539. class M_Categories extends M_Rule { 
  540.  
  541. var $name = 'categories'; 
  542. var $label = 'Categories'; 
  543. var $description = 'Allows posts to be protected based on their assigned categories.'; 
  544.  
  545. var $rulearea = 'public'; 
  546.  
  547. function admin_main($data) { 
  548. if(!$data) $data = array(); 
  549. ?> 
  550. <div class='level-operation' id='main-categories'> 
  551. <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> 
  552. <div class='inner-operation'> 
  553. <p><?php _e('Select the Categories to be covered by this rule by checking the box next to the relevant categories name.', 'membership'); ?></p> 
  554. <?php 
  555. $categories = get_categories('get=all'); 
  556.  
  557. if($categories) { 
  558. ?> 
  559. <table cellspacing="0" class="widefat fixed"> 
  560. <thead> 
  561. <tr> 
  562. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  563. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Category name', 'membership'); ?></th> 
  564. </tr> 
  565. </thead> 
  566.  
  567. <tfoot> 
  568. <tr> 
  569. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  570. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Category name', 'membership'); ?></th> 
  571. </tr> 
  572. </tfoot> 
  573.  
  574. <tbody> 
  575. <?php 
  576. foreach($categories as $key => $category) { 
  577. ?> 
  578. <tr valign="middle" class="alternate" id="post-<?php echo $category->term_id; ?>"> 
  579. <th class="check-column" scope="row"> 
  580. <input type="checkbox" value="<?php echo $category->term_id; ?>" name="categories[]" <?php if(in_array($category->term_id, $data)) echo 'checked="checked"'; ?>> 
  581. </th> 
  582. <td class="column-name"> 
  583. <strong><?php echo esc_html($category->name); ?></strong> 
  584. </td> 
  585. </tr> 
  586. <?php 
  587. ?> 
  588. </tbody> 
  589. </table> 
  590. <?php 
  591. ?> 
  592. </div> 
  593. </div> 
  594. <?php 
  595.  
  596. function on_positive($data) { 
  597.  
  598. $this->data = $data; 
  599.  
  600. add_action( 'pre_get_posts', array(&$this, 'add_viewable_posts'), 1 ); 
  601. add_filter( 'get_terms', array(&$this, 'add_viewable_categories'), 1, 3 ); 
  602.  
  603. add_filter( 'the_posts', array(&$this, 'check_positive_posts')); 
  604.  
  605. function on_negative($data) { 
  606.  
  607. $this->data = $data; 
  608.  
  609. add_action('pre_get_posts', array(&$this, 'add_unviewable_posts'), 1 ); 
  610. add_filter( 'get_terms', array(&$this, 'add_unviewable_categories'), 1, 3 ); 
  611.  
  612. add_filter( 'the_posts', array(&$this, 'check_negative_posts')); 
  613.  
  614. function redirect() { 
  615.  
  616. membership_redirect_to_protected(); 
  617.  
  618.  
  619. function check_negative_posts( $posts ) { 
  620.  
  621. global $wp_query, $M_options; 
  622.  
  623. $redirect = false; 
  624.  
  625. if(is_category() && count($posts) == 0 && MEMBERSHIP_REDIRECT_ON_EMPTY_CATEGORYPAGE === true) { 
  626. $redirect = true; 
  627.  
  628. if((!$wp_query->is_singular || count($posts) > 1) && $redirect != true) { 
  629. return $posts; 
  630.  
  631. foreach($posts as $post) { 
  632. // should only be one as otherwise the single check above didn't work very well. 
  633. if($post->post_type != 'post') { 
  634. // Not a post so ignore 
  635. return $posts; 
  636. } else { 
  637. // Check the categories 
  638. if(has_category( $this->data, $post )) { 
  639. $redirect = true; 
  640.  
  641. if($redirect === true && !empty($M_options['nocontent_page'])) { 
  642. // we need to redirect 
  643. $this->redirect(); 
  644. } else { 
  645. return $posts; 
  646.  
  647.  
  648. function check_positive_posts( $posts ) { 
  649.  
  650. global $wp_query, $M_options; 
  651.  
  652. $redirect = false; 
  653.  
  654. if(is_category() && count($posts) == 0 && MEMBERSHIP_REDIRECT_ON_EMPTY_CATEGORYPAGE === true) { 
  655. $redirect = true; 
  656.  
  657. if((!$wp_query->is_singular || count($posts) > 1) && $redirect != true) { 
  658. return $posts; 
  659.  
  660. foreach($posts as $post) { 
  661. // should only be one as otherwise the single check above didn't work very well. 
  662. if($post->post_type != 'post') { 
  663. // Not a post so ignore 
  664. return $posts; 
  665. } else { 
  666. // Check the categories 
  667. if(!has_category( $this->data, $post )) { 
  668. $redirect = true; 
  669.  
  670.  
  671. if($redirect === true && !empty($M_options['nocontent_page'])) { 
  672. // we need to redirect 
  673. $this->redirect(); 
  674. } else { 
  675. return $posts; 
  676.  
  677.  
  678. function add_viewable_posts($wp_query) { 
  679.  
  680. //print_r($wp_query); 
  681.  
  682. if((isset($wp_query->query_vars['post_type']) && !in_array($wp_query->query_vars['post_type'], array('post', ''))) || !empty($wp_query->query_vars['pagename'])) { 
  683. return; 
  684.  
  685. foreach( (array) $this->data as $key => $value ) { 
  686. $wp_query->query_vars['category__in'][] = $value; 
  687.  
  688. $wp_query->query_vars['category__in'] = array_unique($wp_query->query_vars['category__in']); 
  689.  
  690.  
  691. function add_unviewable_posts($wp_query) { 
  692.  
  693. if( (isset($wp_query->query_vars['post_type']) && !in_array($wp_query->query_vars['post_type'], array('post', ''))) || !empty($wp_query->query_vars['pagename'])) { 
  694. return; 
  695.  
  696. foreach( (array) $this->data as $key => $value ) { 
  697. $wp_query->query_vars['category__not_in'][] = $value; 
  698.  
  699. $wp_query->query_vars['category__not_in'] = array_unique($wp_query->query_vars['category__not_in']); 
  700.  
  701.  
  702. function add_viewable_categories($terms, $taxonomies, $args) { 
  703.  
  704. foreach( (array) $terms as $key => $value ) { 
  705. if($value->taxonomy == 'category') { 
  706. if(!in_array($value->term_id, $this->data)) { 
  707. unset($terms[$key]); 
  708.  
  709. return $terms; 
  710.  
  711. function add_unviewable_categories($terms, $taxonomies, $args) { 
  712.  
  713. foreach( (array) $terms as $key => $value ) { 
  714. if($value->taxonomy == 'category') { 
  715. if(in_array($value->term_id, $this->data)) { 
  716. unset($terms[$key]); 
  717.  
  718. return $terms; 
  719.  
  720.  
  721. class M_More extends M_Rule { 
  722.  
  723. var $name = 'more'; 
  724. var $label = 'More tag'; 
  725. var $description = 'Allows content placed after the More tag to be protected.'; 
  726.  
  727. var $rulearea = 'public'; 
  728.  
  729. function admin_main($data) { 
  730. if(!$data) $data = array(); 
  731. ?> 
  732. <div class='level-operation' id='main-more'> 
  733. <h2 class='sidebar-name'><?php _e('More tag', 'membership');?><span><a href='#remove' class='removelink' id='remove-more' title='<?php _e("Remove More tag from this rules area.", 'membership'); ?>'><?php _e('Remove', 'membership'); ?></a></span></h2> 
  734. <div class='inner-operation'> 
  735. <p><strong><?php _e('Positive : ', 'membership'); ?></strong><?php _e('User can read full post content beyond the More tag.', 'membership'); ?></p> 
  736. <p><strong><?php _e('Negative : ', 'membership'); ?></strong><?php _e('User is unable to read full post content beyond the More tag.', 'membership'); ?></p> 
  737. <input type='hidden' name='more[]' value='yes' /> 
  738. </div> 
  739. </div> 
  740. <?php 
  741.  
  742. function on_positive($data) { 
  743.  
  744. global $M_options, $wp_filter; 
  745.  
  746. $this->data = $data; 
  747.  
  748. if(isset($M_options['moretagdefault']) && $M_options['moretagdefault'] == 'no' ) { 
  749.  
  750. // remove the filters - otherwise we don't need to do anything 
  751. if(isset($wp_filter['the_content_more_link'][99])) { 
  752. foreach($wp_filter['the_content_more_link'][99] as $key => $value) { 
  753. if(strstr($key, 'show_moretag_protection') !== false) { 
  754. unset($wp_filter['the_content_more_link'][99][$key]); 
  755. if(empty($wp_filter['the_content_more_link'][99])) { 
  756. unset($wp_filter['the_content_more_link'][99]); 
  757.  
  758. if(isset($wp_filter['the_content'][1])) { 
  759. foreach($wp_filter['the_content'][1] as $key => $value) { 
  760. if(strstr($key, 'replace_moretag_content') !== false) { 
  761. unset($wp_filter['the_content'][1][$key]); 
  762. if(empty($wp_filter['the_content'][1])) { 
  763. unset($wp_filter['the_content'][1]); 
  764.  
  765. if(isset($wp_filter['the_content_feed'][1])) { 
  766. foreach($wp_filter['the_content_feed'][1] as $key => $value) { 
  767. if(strstr($key, 'replace_moretag_content') !== false) { 
  768. unset($wp_filter['the_content_feed'][1][$key]); 
  769. if(empty($wp_filter['the_content_feed'][1])) { 
  770. unset($wp_filter['the_content_feed'][1]); 
  771.  
  772.  
  773. function on_negative($data) { 
  774.  
  775. global $M_options; 
  776.  
  777. $this->data = $data; 
  778.  
  779. if(isset($M_options['moretagdefault']) && $M_options['moretagdefault'] != 'no' ) { 
  780. // add the filters - otherwise we don't need to do anything 
  781. add_filter('the_content_more_link', array(&$this, 'show_moretag_protection'), 99, 2); 
  782. add_filter('the_content', array(&$this, 'replace_moretag_content'), 1); 
  783.  
  784. function show_moretag_protection($more_tag_link, $more_tag) { 
  785.  
  786. global $M_options; 
  787.  
  788. return stripslashes($M_options['moretagmessage']); 
  789.  
  790.  
  791. function replace_moretag_content($the_content) { 
  792.  
  793. global $M_options; 
  794.  
  795. $morestartsat = strpos($the_content, '<span id="more-'); 
  796.  
  797. if($morestartsat !== false) { 
  798. $the_content = substr($the_content, 0, $morestartsat); 
  799. $the_content .= stripslashes($M_options['moretagmessage']); 
  800.  
  801. return $the_content; 
  802.  
  803.  
  804.  
  805. class M_Comments extends M_Rule { 
  806.  
  807. var $name = 'comments'; 
  808. var $label = 'Comments'; 
  809. var $description = 'Allows the display of, or ability to comment on posts to be protected.'; 
  810.  
  811. var $rulearea = 'public'; 
  812.  
  813. function admin_main($data) { 
  814. if(!$data) $data = array(); 
  815. ?> 
  816. <div class='level-operation' id='main-comments'> 
  817. <h2 class='sidebar-name'><?php _e('Comments', 'membership');?><span><a href='#remove' id='remove-comments' class='removelink' title='<?php _e("Remove Comments from this rules area.", 'membership'); ?>'><?php _e('Remove', 'membership'); ?></a></span></h2> 
  818. <div class='inner-operation'> 
  819. <p><strong><?php _e('Positive : ', 'membership'); ?></strong><?php _e('User gets read and make comments of posts.', 'membership'); ?></p> 
  820. <p><strong><?php _e('Negative : ', 'membership'); ?></strong><?php _e('User can not read or comment on posts.', 'membership'); ?></p> 
  821. <input type='hidden' name='comments[]' value='yes' /> 
  822. </div> 
  823. </div> 
  824. <?php 
  825.  
  826. function on_positive($data) { 
  827.  
  828. $this->data = $data; 
  829.  
  830. add_filter('comments_open', array(&$this, 'open_comments'), 99, 2); 
  831.  
  832.  
  833. function on_negative($data) { 
  834.  
  835. $this->data = $data; 
  836.  
  837. add_filter('comments_open', array(&$this, 'close_comments'), 99, 2); 
  838.  
  839. if(defined('MEMBERSHIP_VIEW_COMMENTS') && MEMBERSHIP_VIEW_COMMENTS == true) { 
  840. // We want users to be able to see the comments but not add to them 
  841. } else { 
  842. add_filter( 'comments_array', array(&$this, 'hide_comments'), 99, 2 ); 
  843.  
  844.  
  845. function hide_comments($comments, $post_id) { 
  846.  
  847. return array(); 
  848.  
  849.  
  850. function close_comments($open, $postid) { 
  851.  
  852. return false; 
  853.  
  854.  
  855. function open_comments($open, $postid) { 
  856.  
  857. return $open; 
  858.  
  859.  
  860.  
  861.  
  862. class M_Downloads extends M_Rule { 
  863.  
  864. var $name = 'downloads'; 
  865. var $label = 'Downloads'; 
  866. var $description = 'Allows media uploaded to the WordPress media library to be protected.'; 
  867.  
  868. var $rulearea = 'public'; 
  869.  
  870. function admin_main($data) { 
  871.  
  872. global $wpdb, $M_options; 
  873.  
  874. if(!$data) $data = array(); 
  875.  
  876. ?> 
  877. <div class='level-operation' id='main-downloads'> 
  878. <h2 class='sidebar-name'><?php _e('Downloads', 'membership');?><span><a href='#remove' id='remove-downloads' class='removelink' title='<?php _e("Remove Downloads from this rules area.", 'membership'); ?>'><?php _e('Remove', 'membership'); ?></a></span></h2> 
  879. <div class='inner-operation'> 
  880. <p><?php _e('Select the Downloads / Media to be covered by this rule by checking the box next to the relevant group name.', 'membership'); ?></p> 
  881. <?php 
  882. $mediasql = $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s", '_membership_protected_content' ); 
  883. $mediaids = $wpdb->get_col( $mediasql ); 
  884.  
  885. if(!empty($mediaids)) { 
  886. // We have some ids so grab the information 
  887. $attachmentsql = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type = %s AND post_status != %s AND ID IN(" . implode(", ", $mediaids) . ")", 'attachment', 'trash' ); 
  888.  
  889. $attachments = $wpdb->get_results( $attachmentsql ); 
  890. ?> 
  891. <table cellspacing="0" class="widefat fixed"> 
  892. <thead> 
  893. <tr> 
  894. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  895. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Download / Group name', 'membership'); ?></th> 
  896. </tr> 
  897. </thead> 
  898. <tfoot> 
  899. <tr> 
  900. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  901. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Download / Group name', 'membership'); ?></th> 
  902. </tr> 
  903. </tfoot> 
  904.  
  905. <tbody> 
  906. <?php 
  907. if(!empty($M_options['membershipdownloadgroups'])) { 
  908.  
  909. foreach($M_options['membershipdownloadgroups'] as $key => $value) { 
  910. if(!empty($value)) { 
  911. ?> 
  912. <tr valign="middle" class="alternate" id="group-<?php echo esc_attr(stripslashes(trim($value))); ?>"> 
  913. <th class="check-column" scope="row"> 
  914. <input type="checkbox" value="<?php echo esc_attr(stripslashes(trim($value))); ?>" name="downloads[]" <?php if(in_array(esc_attr(stripslashes(trim($value))), $data)) echo 'checked="checked"'; ?>> 
  915. </th> 
  916. <td class="column-name"> 
  917. <strong><?php echo esc_html(stripslashes(trim($value))); ?></strong> 
  918. </td> 
  919. </tr> 
  920. <?php 
  921.  
  922. } else { 
  923. ?> 
  924. <tr valign="middle" class="alternate" id="group-nogroup"> 
  925. <td class="column-name" colspan='2'> 
  926. <?php echo __('You have no download groups set, please visit the membership options page to set them up.', 'membership'); ?> 
  927. </td> 
  928. </tr> 
  929. <?php 
  930.  
  931. ?> 
  932. </tbody> 
  933. </table> 
  934.  
  935. </div> 
  936. </div> 
  937. <?php 
  938.  
  939. function can_view_download($area, $group) { 
  940.  
  941. switch($area) { 
  942.  
  943. case 'positive': if(in_array($group, (array) $this->data)) { 
  944. return true; 
  945. break; 
  946.  
  947. case 'negative': if(in_array($group, (array) $this->data)) { 
  948. return false; 
  949. break; 
  950.  
  951. default: return false; 
  952.  
  953.  
  954.  
  955.  
  956. //shortcode_tags 
  957. class M_Shortcodes extends M_Rule { 
  958.  
  959. var $name = 'shortcodes'; 
  960. var $label = 'Shortcodes'; 
  961. var $description = 'Allows specific shortcodes and contained content to be protected.'; 
  962.  
  963. var $rulearea = 'public'; 
  964.  
  965. function admin_main($data) { 
  966.  
  967. global $shortcode_tags; 
  968.  
  969. if(!$data) $data = array(); 
  970. ?> 
  971. <div class='level-operation' id='main-shortcodes'> 
  972. <h2 class='sidebar-name'><?php _e('Shortcodes', 'membership');?><span><a href='#remove' id='remove-shortcodes' class='removelink' title='<?php _e("Remove Shortcodes from this rules area.", 'membership'); ?>'><?php _e('Remove', 'membership'); ?></a></span></h2> 
  973. <div class='inner-operation'> 
  974. <p><?php _e('Select the Shortcodes to be covered by this rule by checking the box next to the relevant shortcode tag.', 'membership'); ?></p> 
  975. <?php 
  976. if($shortcode_tags) { 
  977. ?> 
  978. <table cellspacing="0" class="widefat fixed"> 
  979. <thead> 
  980. <tr> 
  981. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  982. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Shortcode tag', 'membership'); ?></th> 
  983. </tr> 
  984. </thead> 
  985.  
  986. <tfoot> 
  987. <tr> 
  988. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  989. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Shortcode tag', 'membership'); ?></th> 
  990. </tr> 
  991. </tfoot> 
  992.  
  993. <tbody> 
  994. <?php 
  995. foreach($shortcode_tags as $key => $function) { 
  996. ?> 
  997. <tr valign="middle" class="alternate" id="post-<?php echo $key; ?>"> 
  998. <th class="check-column" scope="row"> 
  999. <input type="checkbox" value="<?php echo esc_attr(trim($key)); ?>" name="shortcodes[]" <?php if(in_array(trim($key), $data)) echo 'checked="checked"'; ?>> 
  1000. </th> 
  1001. <td class="column-name"> 
  1002. <strong>[<?php echo esc_html(trim($key)); ?>]</strong> 
  1003. </td> 
  1004. </tr> 
  1005. <?php 
  1006. ?> 
  1007. </tbody> 
  1008. </table> 
  1009. <?php 
  1010. ?> 
  1011. </div> 
  1012. </div> 
  1013. <?php 
  1014.  
  1015. function on_creation() { 
  1016. //add_filter('the_content', array(&$this, 'override_shortcodes'), 1); 
  1017.  
  1018. function override_shortcodes() { 
  1019.  
  1020. global $M_shortcode_tags, $shortcode_tags; 
  1021.  
  1022. $M_shortcode_tags = $shortcode_tags; 
  1023.  
  1024. foreach($shortcode_tags as $key => $function) { 
  1025. if($key != 'subscriptionform') { 
  1026. $shortcode_tags[$key] = array(&$this, 'do_protected_shortcode'); 
  1027.  
  1028. return $content; 
  1029.  
  1030. function on_positive($data) { 
  1031.  
  1032. global $M_options, $M_shortcode_tags, $shortcode_tags; 
  1033.  
  1034. $this->data = $data; 
  1035.  
  1036. if($M_options['shortcodedefault'] == 'no' ) { 
  1037. // Need to re-enable some shortcodes 
  1038. foreach( (array) $data as $key => $code ) { 
  1039. if(isset($M_shortcode_tags[$code]) && isset($shortcode_tags[$code])) { 
  1040. $shortcode_tags[$code] = $M_shortcode_tags[$code]; 
  1041.  
  1042.  
  1043. function on_negative($data) { 
  1044.  
  1045. global $M_options, $M_shortcode_tags, $shortcode_tags; 
  1046.  
  1047. $M_shortcode_tags = $shortcode_tags; 
  1048.  
  1049. $this->data = $data; 
  1050.  
  1051. if($M_options['shortcodedefault'] != 'no' ) { 
  1052. // Need to disable some shortcodes 
  1053. foreach( (array) $data as $key => $code ) { 
  1054. if(isset($M_shortcode_tags[$code]) && isset($shortcode_tags[$code])) { 
  1055. if($code != 'subscriptionform') { 
  1056. $shortcode_tags[$code] = array(&$this, 'do_protected_shortcode'); 
  1057.  
  1058.  
  1059. // Show the protected shortcode message 
  1060. function do_protected_shortcode($atts, $content = null, $code = "") { 
  1061.  
  1062. global $M_options; 
  1063.  
  1064. return stripslashes($M_options['shortcodemessage']); 
  1065.  
  1066.  
  1067.  
  1068. class M_Menu extends M_Rule { 
  1069.  
  1070. var $name = 'menu'; 
  1071. var $label = 'Menu'; 
  1072. var $description = 'Allows specific menu items to be protected.'; 
  1073.  
  1074. var $rulearea = 'public'; 
  1075.  
  1076. function admin_main($data) { 
  1077. if(!$data) $data = array(); 
  1078. ?> 
  1079. <div class='level-operation' id='main-menu'> 
  1080. <h2 class='sidebar-name'><?php _e('Menu', 'membership');?><span><a href='#remove' id='remove-menu' class='removelink' title='<?php _e("Remove Menu from this rules area.", 'membership'); ?>'><?php _e('Remove', 'membership'); ?></a></span></h2> 
  1081. <div class='inner-operation'> 
  1082. <p><?php _e('Select the Menu items to be covered by this rule by checking the box next to the relevant menu labels.', 'membership'); ?></p> 
  1083. <?php 
  1084.  
  1085. $navs = wp_get_nav_menus( array('orderby' => 'name') ); 
  1086.  
  1087. if(!empty($navs)) { 
  1088. ?> 
  1089. <table cellspacing="0" class="widefat fixed"> 
  1090. <thead> 
  1091. <tr> 
  1092. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  1093. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Menu / Item title', 'membership'); ?></th> 
  1094. </tr> 
  1095. </thead> 
  1096.  
  1097. <tfoot> 
  1098. <tr> 
  1099. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  1100. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Menu / Item title', 'membership'); ?></th> 
  1101. </tr> 
  1102. </tfoot> 
  1103.  
  1104. <tbody> 
  1105. <?php 
  1106. foreach($navs as $key => $nav) { 
  1107. ?> 
  1108. <tr valign="middle" class="alternate" id="menu-<?php echo $nav->term_id; ?>-0"> 
  1109. <td class="column-name" colspan='2'> 
  1110. <strong><?php echo __('MENU', 'membership') . " - " . esc_html($nav->name); ?></strong> 
  1111. </td> 
  1112. </tr> 
  1113. <?php 
  1114. $items = wp_get_nav_menu_items($nav->term_id); 
  1115. if(!empty($items)) { 
  1116. foreach($items as $ikey => $item) { 
  1117. ?> 
  1118. <tr valign="middle" class="alternate" id="menu-<?php //echo $nav->term_id . '-'; ?><?php echo $item->ID; ?>"> 
  1119. <th class="check-column" scope="row"> 
  1120. <input type="checkbox" value="<?php //echo $nav->term_id . '-'; ?><?php echo $item->ID; ?>" name="menu[]" <?php if(in_array($item->ID, $data)) echo 'checked="checked"'; ?>> 
  1121. </th> 
  1122. <td class="column-name"> 
  1123.  
  1124. <strong> – <?php if($item->menu_item_parent != 0) echo "– "; ?><?php echo esc_html($item->title); ?></strong> 
  1125. </td> 
  1126. </tr> 
  1127. <?php 
  1128. ?> 
  1129. </tbody> 
  1130. </table> 
  1131. <?php 
  1132. ?> 
  1133. </div> 
  1134. </div> 
  1135. <?php 
  1136.  
  1137. function on_positive($data) { 
  1138.  
  1139. $this->data = $data; 
  1140.  
  1141. add_filter( 'wp_get_nav_menu_items', array(&$this, 'filter_viewable_menus'), 10, 3 ); 
  1142.  
  1143.  
  1144. function on_negative($data) { 
  1145.  
  1146. $this->data = $data; 
  1147.  
  1148. add_filter( 'wp_get_nav_menu_items', array(&$this, 'filter_unviewable_menus'), 10, 3 ); 
  1149.  
  1150. function filter_viewable_menus($items, $menu, $args) { 
  1151.  
  1152. if(!empty($items)) { 
  1153. foreach($items as $key => $item) { 
  1154. if(!in_array($item->ID, $this->data) || ($item->menu_item_parent != 0 && !in_array($item->menu_item_parent, $this->data))) { 
  1155. unset($items[$key]); 
  1156.  
  1157.  
  1158. return $items; 
  1159.  
  1160.  
  1161. function filter_unviewable_menus($items, $menu, $args) { 
  1162.  
  1163. if(!empty($items)) { 
  1164. foreach($items as $key => $item) { 
  1165. if(in_array($item->ID, $this->data) || ($item->menu_item_parent != 0 && in_array($item->menu_item_parent, $this->data))) { 
  1166. unset($items[$key]); 
  1167.  
  1168.  
  1169. return $items; 
  1170.  
  1171.  
  1172.  
  1173. class M_Blogcreation extends M_Rule { 
  1174.  
  1175. var $name = 'blogcreation'; 
  1176. var $label = 'Blog Creation'; 
  1177. var $description = 'Allows the creation of blogs to be limited to members.'; 
  1178.  
  1179. var $rulearea = 'core'; 
  1180.  
  1181. function admin_main($data) { 
  1182. if(!$data) $data = array(); 
  1183. ?> 
  1184. <div class='level-operation' id='main-blogcreation'> 
  1185. <h2 class='sidebar-name'><?php _e('Blog Creation', 'membership');?><span><a href='#remove' id='remove-blogcreation' class='removelink' title='<?php _e("Remove Blog Creation from this rules area.", 'membership'); ?>'><?php _e('Remove', 'membership'); ?></a></span></h2> 
  1186. <div class='inner-operation'> 
  1187. <?php 
  1188. if(!isset($data['number'])) { 
  1189. $data['number'] = ''; 
  1190. ?> 
  1191. <p><strong><?php _e('Positive : ', 'membership'); ?></strong><?php _e('User can create ', 'membership'); ?><input type='text' name='blogcreation[number]' value='<?php echo esc_attr($data['number']); ?>' /><?php _e(' blogs.', 'membership'); ?><br/><em><?php _e('Leave blank for unlimited blogs.', 'membership'); ?></em></p> 
  1192. <p><strong><?php _e('Negative : ', 'membership'); ?></strong><?php _e('User is unable to create any blogs.', 'membership'); ?></p> 
  1193. <input type='hidden' name='blogcreation[]' value='yes' /> 
  1194. </div> 
  1195. </div> 
  1196. <?php 
  1197.  
  1198. function on_creation() { 
  1199.  
  1200.  
  1201. function on_positive($data) { 
  1202.  
  1203. $this->data = $data; 
  1204.  
  1205. add_filter( 'site_option_registration', array(&$this, 'pos_blog_creation')); 
  1206. add_filter( 'wpmu_active_signup', array(&$this, 'pos_blog_creation') ); 
  1207.  
  1208. function on_negative($data) { 
  1209.  
  1210. $this->data = $data; 
  1211.  
  1212. add_filter( 'site_option_registration', array(&$this, 'neg_blog_creation')); 
  1213. add_filter( 'wpmu_active_signup', array(&$this, 'neg_blog_creation') ); 
  1214.  
  1215.  
  1216. function neg_blog_creation( $active = 'all' ) { 
  1217.  
  1218. if($active == 'user' || $active == 'none') { 
  1219. return $active; 
  1220. } else { 
  1221. return 'none'; 
  1222.  
  1223.  
  1224. function pos_blog_creation( $active = 'all' ) { 
  1225.  
  1226. if($active == 'user' || $active == 'none') { 
  1227. return $active; 
  1228. } else { 
  1229. // Check our count 
  1230. if(empty($this->data['number'])) { 
  1231. // unlimited 
  1232. return $active; 
  1233. } else { 
  1234. $thelimit = (int) $this->data['number']; 
  1235.  
  1236. if( $thelimit > (int) $this->current_blog_count() ) { 
  1237. return $active; 
  1238. } else { 
  1239. return $this->neg_blog_creation( $active ); 
  1240.  
  1241.  
  1242.  
  1243. function current_blog_count() { 
  1244.  
  1245. global $member, $wpdb; 
  1246.  
  1247. if(!empty($member) && method_exists($member, 'has_cap')) { 
  1248. // We have a member and it is a correct object 
  1249. $count = 0; 
  1250. $blogs = get_blogs_of_user( $member->ID ); 
  1251. foreach( $blogs as $blog ) { 
  1252. if( $this->is_user_blog_admin( $member->ID, $blog->userblog_id ) ) { 
  1253. $count++; 
  1254.  
  1255. return (int) $count; 
  1256. } else { 
  1257. return 0; 
  1258.  
  1259.  
  1260. function is_user_blog_admin( $user_id, $blog_id ) { 
  1261. global $wpdb; 
  1262.  
  1263. $meta_key = $wpdb->base_prefix . $blog_id . "_capabilities"; 
  1264.  
  1265. $role_sql = $wpdb->prepare( "SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = %s", $meta_key ); 
  1266.  
  1267. $role = $wpdb->get_results( $role_sql ); 
  1268.  
  1269. //clean the role 
  1270. foreach($role as $key => $r) { 
  1271. $role[$key]->meta_value = maybe_unserialize($r->meta_value); 
  1272.  
  1273. foreach($role as $key => $r) { 
  1274. if( $r->meta_value['administrator'] == 1 && $r->user_id == $user_id ) { 
  1275. return true; 
  1276.  
  1277. return false; 
  1278.  
  1279.  
  1280.  
  1281. class M_URLGroups extends M_Rule { 
  1282.  
  1283. var $name = 'urlgroups'; 
  1284. var $label = 'URL Groups'; 
  1285. var $description = "Allows specific URL's to be protected (includes ability to protect using regular expressions)."; 
  1286.  
  1287. var $rulearea = 'core'; 
  1288.  
  1289. function get_groups() { 
  1290.  
  1291. global $wpdb; 
  1292.  
  1293. $sql = $wpdb->prepare( "SELECT * FROM " . membership_db_prefix($wpdb, 'urlgroups') . " WHERE groupname NOT LIKE (%s) ORDER BY id ASC", '\_%' ); 
  1294.  
  1295. $results = $wpdb->get_results( $sql ); 
  1296.  
  1297. if(!empty($results)) { 
  1298. return $results; 
  1299. } else { 
  1300. return false; 
  1301.  
  1302. function admin_main($data) { 
  1303. if(!$data) $data = array(); 
  1304. ?> 
  1305. <div class='level-operation' id='main-urlgroups'> 
  1306. <h2 class='sidebar-name'><?php _e('URL Groups', 'membership');?><span><a href='#remove' id='remove-urlgroups' class='removelink' title='<?php _e("Remove URL Groups from this rules area.", 'membership'); ?>'><?php _e('Remove', 'membership'); ?></a></span></h2> 
  1307. <div class='inner-operation'> 
  1308. <p><?php _e('Select the URL Groups to be covered by this rule by checking the box next to the relevant URL Group name.', 'membership'); ?></p> 
  1309. <?php 
  1310. $urlgroups = $this->get_groups(); 
  1311.  
  1312. if(!empty($urlgroups)) { 
  1313. ?> 
  1314. <table cellspacing="0" class="widefat fixed"> 
  1315. <thead> 
  1316. <tr> 
  1317. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  1318. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('URL Group', 'membership'); ?></th> 
  1319. </tr> 
  1320. </thead> 
  1321.  
  1322. <tfoot> 
  1323. <tr> 
  1324. <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th> 
  1325. <th style="" class="manage-column column-name" id="name" scope="col"><?php _e('URL Group', 'membership'); ?></th> 
  1326. </tr> 
  1327. </tfoot> 
  1328.  
  1329. <tbody> 
  1330. <?php 
  1331. foreach($urlgroups as $key => $urlgroup) { 
  1332. ?> 
  1333. <tr valign="middle" class="alternate" id="urlgroup-<?php echo $urlgroup->id; ?>"> 
  1334. <th class="check-column" scope="row"> 
  1335. <input type="checkbox" value="<?php echo $urlgroup->id; ?>" name="urlgroups[]" <?php if(in_array($urlgroup->id, $data)) echo 'checked="checked"'; ?>> 
  1336. </th> 
  1337. <td class="column-name"> 
  1338. <strong><?php echo esc_html($urlgroup->groupname); ?></strong> 
  1339. </td> 
  1340. </tr> 
  1341. <?php 
  1342. ?> 
  1343. </tbody> 
  1344. </table> 
  1345. <?php 
  1346.  
  1347. ?> 
  1348. </div> 
  1349. </div> 
  1350. <?php 
  1351.  
  1352. function on_positive($data) { 
  1353.  
  1354. $this->data = $data; 
  1355.  
  1356. add_action( 'pre_get_posts', array(&$this, 'positive_check_request'), 1 ); 
  1357.  
  1358.  
  1359.  
  1360. function on_negative($data) { 
  1361.  
  1362. $this->data = $data; 
  1363.  
  1364. add_action( 'pre_get_posts', array(&$this, 'negative_check_request'), 1 ); 
  1365.  
  1366. function positive_check_request($wp) { 
  1367.  
  1368. global $M_options, $wp_query; 
  1369.  
  1370. $redirect = false; 
  1371. $found = false; 
  1372. $host = ''; 
  1373. if(is_ssl()) { 
  1374. $host = "https://"; 
  1375. } else { 
  1376. $host = "http://"; 
  1377. $host .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 
  1378.  
  1379. $exclude = array(); 
  1380. if(!empty($M_options['registration_page'])) { 
  1381. $exclude[] = get_permalink( (int) $M_options['registration_page'] ); 
  1382. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['registration_page'] )); 
  1383.  
  1384. if(!empty($M_options['account_page'])) { 
  1385. $exclude[] = get_permalink( (int) $M_options['account_page'] ); 
  1386. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['account_page'] )); 
  1387.  
  1388. if(!empty($M_options['nocontent_page'])) { 
  1389. $exclude[] = get_permalink( (int) $M_options['nocontent_page'] ); 
  1390. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['nocontent_page'] )); 
  1391.  
  1392. if(!empty($wp_query->query_vars['protectedfile']) && !$forceviewing) { 
  1393. $exclude[] = $host; 
  1394. $exclude[] = untrailingslashit($host); 
  1395.  
  1396. // we have the current page / url - get the groups selected 
  1397. foreach((array) $this->data as $group_id) { 
  1398. $group = new M_Urlgroup( $group_id ); 
  1399.  
  1400. if($group->url_matches( $host ) && !in_array(strtolower($host), $exclude)) { 
  1401. // We've found a pge in the positive rules so can let the user see it 
  1402. $found = true; 
  1403.  
  1404. if($found !== true) { 
  1405. // we need to redirect 
  1406. $this->redirect(); 
  1407.  
  1408.  
  1409. function negative_check_request($wp) { 
  1410.  
  1411. $redirect = false; 
  1412. $host = ''; 
  1413. if(is_ssl()) { 
  1414. $host = "https://"; 
  1415. } else { 
  1416. $host = "http://"; 
  1417. $host .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 
  1418.  
  1419. $exclude = array(); 
  1420. if(!empty($M_options['registration_page'])) { 
  1421. $exclude[] = get_permalink( (int) $M_options['registration_page'] ); 
  1422. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['registration_page'] )); 
  1423.  
  1424. if(!empty($M_options['account_page'])) { 
  1425. $exclude[] = get_permalink( (int) $M_options['account_page'] ); 
  1426. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['account_page'] )); 
  1427.  
  1428. if(!empty($M_options['nocontent_page'])) { 
  1429. $exclude[] = get_permalink( (int) $M_options['nocontent_page'] ); 
  1430. $exclude[] = untrailingslashit(get_permalink( (int) $M_options['nocontent_page'] )); 
  1431.  
  1432. if(!empty($wp_query->query_vars['protectedfile']) && !$forceviewing) { 
  1433. $exclude[] = $host; 
  1434. $exclude[] = untrailingslashit($host); 
  1435.  
  1436. // we have the current page / url - get the groups selected 
  1437. foreach((array) $this->data as $group_id) { 
  1438. $group = new M_Urlgroup( $group_id ); 
  1439.  
  1440. if($group->url_matches( $host ) && !in_array(strtolower($host), $exclude)) { 
  1441. $redirect = true; 
  1442.  
  1443. if($redirect === true) { 
  1444. // we need to redirect 
  1445. $this->redirect(); 
  1446.  
  1447.  
  1448. function redirect() { 
  1449.  
  1450. membership_redirect_to_protected(); 
  1451.  
  1452.  
  1453.  
  1454. function M_setup_default_rules() { 
  1455.  
  1456. M_register_rule('downloads', 'M_Downloads', 'content'); 
  1457. M_register_rule('comments', 'M_Comments', 'main'); 
  1458. M_register_rule('more', 'M_More', 'main'); 
  1459. M_register_rule('categories', 'M_Categories', 'main'); 
  1460. M_register_rule('pages', 'M_Pages', 'main'); 
  1461. M_register_rule('posts', 'M_Posts', 'main'); 
  1462. M_register_rule('shortcodes', 'M_Shortcodes', 'content'); 
  1463. M_register_rule('menu', 'M_Menu', 'main'); 
  1464. M_register_rule('urlgroups', 'M_URLGroups', 'main'); 
  1465.  
  1466. if(is_multisite()) { 
  1467. M_register_rule('blogcreation', 'M_Blogcreation', 'admin'); 
  1468.  
  1469. add_action('plugins_loaded', 'M_setup_default_rules', 99); 
  1470.  
  1471. ?> 
.