PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/membership/membershipincludes/includes/default.rules.php

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