Guest User

Untitled

a guest
Sep 12th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin logic
  4. *
  5. * @link http://seventhqueen.com
  6. * @since 1.0.0
  7. *
  8. * @package Restrict_My_Site
  9. */
  10.  
  11. if ( ! class_exists( 'Kleo' ) ) {
  12. return;
  13. }
  14.  
  15. /***************************************************
  16. :: Theme settings
  17. ***************************************************/
  18.  
  19.  
  20. add_filter( 'kleo_theme_settings', 'kleo_private_settings' );
  21.  
  22. function kleo_private_settings( $kleo ) {
  23. //
  24. // Settings Sections
  25. //
  26.  
  27. $kleo['sec']['kleo_section_private'] = array(
  28. 'title' => __( 'Private site', 'buddyapp' ),
  29. 'priority' => 16
  30. );
  31.  
  32. $kleo['set'][] = array(
  33. 'id' => 'restricted_site',
  34. 'title' => __('Make my site private', 'buddyapp'),
  35. 'type' => 'select',
  36. 'default' => 'no',
  37. 'choices' => array( 'no' => 'No', 'yes' => 'Yes'),
  38. 'section' => 'kleo_section_private',
  39. 'description' => __('Restrict all pages from not logged in users.', 'buddyapp'),
  40. );
  41.  
  42.  
  43. $pages_array = array( '' => 'None' );
  44. $pages = get_pages();
  45. foreach ( $pages as $page ) {
  46. $pages_array[$page->ID] = $page->post_title;
  47. }
  48.  
  49. $kleo['set'][] = array(
  50. 'id' => 'restrict_login_page',
  51. 'title' => __('Redirect to login page', 'buddyapp'),
  52. 'type' => 'select',
  53. 'default' => '',
  54. 'choices' => $pages_array,
  55. 'section' => 'kleo_section_private',
  56. 'description' => __('Select a page to redirect restricted users.', 'buddyapp'),
  57. 'condition' => array( 'restricted_site', 'yes' )
  58. );
  59.  
  60. $kleo['set'][] = array(
  61. 'id' => 'restricted_site_excludes',
  62. 'title' => __('Pages exceptions', 'buddyapp'),
  63. 'type' => 'multi-select',
  64. 'default' => '',
  65. 'choices' => $pages_array,
  66. 'section' => 'kleo_section_private',
  67. 'description' => __('Page restrictions exceptions. Select pages to make public', 'buddyapp'),
  68. 'condition' => array( 'restricted_site', 'yes' )
  69. );
  70.  
  71. return $kleo;
  72.  
  73. }
  74.  
  75. //Check if some page has childrens
  76. function sq_get_children($excluded = array()) {
  77.  
  78. $childpages = [];
  79.  
  80. if (!empty($excluded)) {
  81. foreach ($excluded as $restriction_page ) {
  82. $children = get_pages( 'child_of=' . $restriction_page . '&parents=' . $restriction_page );
  83. // loop through the child page links
  84. foreach ( $children as $child ) {
  85. $childpages[] = $child->ID;
  86. }
  87. }
  88. }
  89.  
  90. return $childpages;
  91. }
  92.  
  93.  
  94. function kleo_restrict_site() {
  95.  
  96. if ( is_user_logged_in() || sq_option( 'restricted_site', 'no' ) != 'yes' ) {
  97. return false;
  98. }
  99.  
  100. if ( function_exists( 'bp_is_active' ) ) {
  101. if ( bp_is_activation_page() || bp_is_register_page() ) {
  102. return false;
  103. }
  104. }
  105.  
  106. $excluded = sq_option( 'restricted_site_excludes', '' );
  107.  
  108. $child = sq_get_children($excluded);
  109. $excluded = array_merge($excluded, $child);
  110.  
  111.  
  112. if ( ! empty( $excluded ) && $excluded != '' && is_page( $excluded ) ) {
  113. return false;
  114. }
  115.  
  116. /* Redirect to login */
  117.  
  118. $redirect_page = home_url( '/wp-login.php' );
  119.  
  120. //Custom redirect page
  121. if ( sq_option( 'restrict_login_page' ) ) {
  122. if (get_permalink( sq_option('restrict_login_page') )) {
  123. $redirect_page = get_permalink(sq_option('restrict_login_page'));
  124.  
  125. // If we are actually on the redirect page - bail out
  126. if ( is_page( sq_option( 'restrict_login_page' ) ) ) {
  127. return false;
  128. }
  129. }
  130. }
  131.  
  132. //Extra check so we don't enter a loop
  133. if( kleo_full_url() === $redirect_page ) {
  134. return false;
  135. }
  136.  
  137. $redirect = '?redirect_to=' . esc_url( kleo_full_url() );
  138. $return_url = esc_url( $redirect_page . $redirect );
  139.  
  140. wp_redirect( $return_url );
  141. exit;
  142.  
  143. }
  144. if ( ! is_admin() ) {
  145. add_action( 'template_redirect', 'kleo_restrict_site' );
  146. }
Advertisement
Add Comment
Please, Sign In to add comment