Advertisement
meetsos

PageRestrict WP Remove Hardecoded

Mar 7th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.43 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Page Restrict
  4. Plugin URI: http://theandystratton.com/pagerestrict
  5. Description: Restrict certain pages to logged in users
  6. Author: Matt Martz & Andy Stratton
  7. Author URI: http://theandystratton.com
  8. Version: 2.2.6
  9.  
  10.     Copyright (c) 2008 Matt Martz (http://sivel.net)
  11.         Page Restrict is released under the GNU Lesser General Public License (LGPL)
  12.     http://www.gnu.org/licenses/lgpl-3.0.txt
  13. */
  14.  
  15. // ff we are in the admin load the admin functionality
  16. if ( is_admin () )
  17.     require_once( dirname ( __FILE__ ) . '/inc/admin.php' );
  18.  
  19. // get specific option
  20. function pr_get_opt ( $option ) {
  21.     $pr_options = get_option ( 'pr_options' );
  22.     // clean up PHP warning for in_array() later when they have not been saved
  23.     if ( $option == 'posts' || $option == 'pages' ) {
  24.         if ( !is_array($pr_options[$option]) ) {
  25.             $pr_options[$option] = array();
  26.         }
  27.     }
  28.     return $pr_options[$option];
  29. }
  30.  
  31. // Add headers to keep browser from caching the pages when user not logged in
  32. // Resolves a problem where users see the login form after logging in and need
  33. // to refresh to see content
  34. function pr_no_cache_headers () {
  35.     if ( !is_user_logged_in() )
  36.         nocache_headers();
  37. }
  38.  
  39. // gets standard page content when page/post is restricted.
  40. function pr_get_page_content() {
  41.     $pr_page_content = '
  42.         <p>' . pr_get_opt ( 'message' )  . '</p>';
  43.     if ( pr_get_opt ( 'loginform' ) == true ) :
  44.  
  45.         $errors = '';
  46.         if ( isset( $_GET['wp-error'] ) )
  47.         {
  48.             $errors = strip_tags( $_GET['wp-error'] );
  49.             $errors = str_ireplace( 'Lost your password?', '<a href="' . wp_lostpassword_url() . '">Lost your password?></a>', $errors );
  50.             $errors = '<div class="pr-message pr-error"><p>' . $errors . '</p></div>';
  51.         }
  52.  
  53.         $user_login = '';
  54.  
  55.         if ( !isset( $user_login ) && isset( $_GET['pr-user-login'] ) )
  56.         {
  57.             $user_login = sanitize_user( $_GET['pr-user-login'] );
  58.         }
  59.  
  60.         $pr_page_content .= '
  61.         <form style="text-align: left;" action="' . wp_login_url() . '" method="post">
  62.         ' . $errors . '
  63.             <p>
  64.                 <label for="log"><input type="text" name="log" id="log" value="' . esc_html ( stripslashes ( $user_login ) , 1 ) . '" size="22" /> '
  65.                 . apply_filters( 'pr_username_label', 'Username' ) . '</label><br />
  66.                 <label for="pwd"><input type="password" name="pwd" id="pwd" size="22" /> '
  67.                 . apply_filters( 'pr_password_label' , 'Password' ) . '</label><br />
  68.                 <input type="submit" name="submit" value="Log In" class="button" />
  69.                 <label for="rememberme"><input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me</label><br />
  70.             </p>
  71.             <input type="hidden" name="redirect_to" value="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" />
  72.         </form>
  73.         <p>
  74.         ';
  75.        
  76.         if ( get_option('users_can_register') )
  77.             $pr_page_content .= '   <a href="' . wp_registration_url() . '">Register</a> | ';
  78.  
  79.         $pr_page_content .= '<a href="' . wp_lostpassword_url() . '">Lost your password??</a>
  80.         </p>
  81.         ';
  82.         global $post;
  83.         $post->comment_status = 'closed';
  84.     endif;
  85.     return apply_filters( 'pr_page_content', $pr_page_content );
  86. }
  87.  
  88. // Perform the restriction and if restricted replace the page content with a login form
  89. function pr_page_restrict ( $pr_page_content ) {
  90.     global $post;
  91.     $pr_check = pr_get_opt('method') == 'all';
  92.     $pr_check = $pr_check || (
  93.         ( is_array(pr_get_opt('pages')) || is_array(pr_get_opt('posts')) )
  94.         && ( count(pr_get_opt('pages')) + count(pr_get_opt('posts')) > 0 )
  95.     );
  96.     $pr_check = $pr_check || ( pr_get_opt('pr_restrict_home') && is_home() );
  97.     if ( !is_user_logged_in() && $pr_check ) :
  98.         // current post is in either page / post restriction array
  99.         $is_restricted = ( in_array($post->ID, pr_get_opt('pages')) || in_array($post->ID, pr_get_opt('posts')) ) && pr_get_opt ( 'method' ) != 'none';
  100.         // content is restricted OR everything is restricted
  101.         if ( (is_single() || is_page()) && ($is_restricted || pr_get_opt('method') == 'all') ):
  102.             $pr_page_content = pr_get_page_content();
  103.             $pr_page_content = '<div class="page-restrict-output">' . $pr_page_content . '</div>';
  104.         // home page, archives, search
  105.         elseif ( ( in_array($post->ID, pr_get_opt('pages')) || in_array($post->ID, pr_get_opt('posts')) || pr_get_opt('method') == 'all' )
  106.                 && ( is_archive() || is_search() || is_home() )
  107.         ) :
  108.             $pr_page_content = '<p>' . pr_get_opt ( 'message' )  . '</p>';
  109.             $pr_page_content = str_replace('login', '<a href="' . wp_login_url() . '">login</a>', $pr_page_content);
  110.             $pr_page_content = '<div class="page-restrict-output">' . apply_filters( 'pr_message_content', $pr_page_content ) . '</div>';
  111.         endif;
  112.     endif;
  113.     return $pr_page_content;
  114. }
  115.  
  116. function pr_comment_restrict ( $pr_comment_array ) {
  117.     global $post;
  118.     if ( !is_user_logged_in()  && is_array ( pr_get_opt ( 'pages' ) ) ) :
  119.         $is_restricted = ( in_array($post->ID, pr_get_opt('pages')) || in_array($post->ID, pr_get_opt('posts')) ) && pr_get_opt ( 'method' ) != 'none';
  120.         if ( $is_restricted || pr_get_opt('method') == 'all' ):
  121.             $pr_comment_array = array();
  122.         endif;
  123.     endif;
  124.     return $pr_comment_array;
  125. }
  126.  
  127. // Add Actions
  128. add_action( 'send_headers' , 'pr_no_cache_headers' );
  129.  
  130. // Add Filters
  131. add_filter ( 'the_content' , 'pr_page_restrict' , 50 );
  132. add_filter ( 'the_excerpt' , 'pr_page_restrict' , 50 );
  133. add_filter ( 'comments_array' , 'pr_comment_restrict' , 50 );
  134.  
  135. add_action( 'wp_login_failed', 'pr_login_failed', 50, 1 );
  136. function pr_login_failed( $username = null )
  137. {
  138.     $referrer = $_SERVER['HTTP_REFERER'];
  139.  
  140.     if ( !empty( $referrer ) && !strstr( $referrer, 'wp-login.php/' ) && !strstr( $referrer, 'wp-admin' ) )
  141.     {
  142.         $params = parse_url( $referrer );      
  143.         $redirect = $params['scheme'] . '://' . $params['host'] . $params['path'];
  144.  
  145.         $query = false;
  146.        
  147.         if ( isset( $params['query'] ) )
  148.             parse_str( $params['query'], $query );
  149.  
  150.         $query['login'] = 'failed';
  151.        
  152.         if ( is_wp_error( $username ) )
  153.             $query['wp-error'] = $username->get_error_message();
  154.  
  155.         if ( isset( $username->user_login ) )
  156.             $query['pr-user-login'] = $username->user_login;
  157.  
  158.         if ( count( $query ) > 0 )
  159.         {
  160.             $redirect .= '?' . http_build_query( $query );
  161.         }
  162.  
  163.         wp_redirect( $redirect, 303 );
  164.         die;
  165.     }
  166. }
  167.  
  168. if ( sizeof( $_POST ) )
  169.     add_filter( 'authenticate', 'pr_authenticate', 50, 3 );
  170.  
  171. function pr_authenticate( $error, $user, $pass )
  172. {
  173.     if ( !empty( $user ) )
  174.         $error->user_login = $_POST['log'];
  175.  
  176.     if ( is_wp_error( $error ) )
  177.         do_action( 'wp_login_failed', $error );
  178.  
  179.     return $error;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement