diff --git a/CHANGELOG.md b/CHANGELOG.md index 182c138..42bb34b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added +- Option to specify page/post IDs to always allow viewing (unprotect) + ## [2.0.3] - 2015-03-23 ### Added diff --git a/README.md b/README.md index 3c14ef0..55fb780 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Features include: - Password protect your WordPress site with a single password. - Option to allow access to feeds. - Option to allow administrators access without entering password. +- Option to unprotect pages and posts by ID (whitelisting) - Works with Mark Jaquith's [Login Logo](http://wordpress.org/extend/plugins/login-logo/) plugin. - Works with the [Uber Login Logo](http://wordpress.org/plugins/uber-login-logo/) plugin. diff --git a/admin/admin.php b/admin/admin.php index 0d5e245..8c6758a 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -128,6 +128,14 @@ public function password_protected_settings() { $this->options_group, 'password_protected' ); + + add_settings_field( + 'password_protected_unprotected_pages', + __( 'Unprotected Pages', 'password-protected' ), + array( $this, 'password_protected_unprotected_pages_field' ), + $this->options_group, + 'password_protected' + ); register_setting( $this->options_group, 'password_protected_status', 'intval' ); register_setting( $this->options_group, 'password_protected_feeds', 'intval' ); @@ -135,6 +143,7 @@ public function password_protected_settings() { register_setting( $this->options_group, 'password_protected_users', 'intval' ); register_setting( $this->options_group, 'password_protected_password', array( $this, 'sanitize_password_protected_password' ) ); register_setting( $this->options_group, 'password_protected_allowed_ip_addresses', array( $this, 'sanitize_ip_addresses' ) ); + register_setting( $this->options_group, 'password_protected_unprotected_pages', array( $this, 'sanitize_pages' ) ); } @@ -199,6 +208,25 @@ private function validate_ip_address( $ip_address ) { return filter_var( $ip_address, FILTER_VALIDATE_IP ); } + + /** + * Sanitize Pages + * + * @param string $val List of page IDs, one per line + * @return string Sanitized list of pages. + */ + public function sanitize_pages( $val ) { + + $page_ids = explode( "\n", $val ); + $page_ids = array_map( 'sanitize_text_field', $page_ids ); + $page_ids = array_map( 'trim', $page_ids ); + $page_ids = array_filter( $page_ids ); + + $val = implode( "\n", $page_ids ); + + return $val; + + } /** * Password Protected Section @@ -249,6 +277,16 @@ public function password_protected_allowed_ip_addresses_field() { echo '

' . esc_html__( 'Enter one IP address per line.', 'password-protected' ) . ' ' . esc_html( sprintf( __( 'Your IP is address %s.', 'password-protected' ), $_SERVER['REMOTE_ADDR'] ) ) . '

'; } + + /** + * Unprotected Pages Field + */ + public function password_protected_unprotected_pages_field() { + + echo ''; + echo '

' . esc_html__( 'Enter one Page/Post ID per line.', 'password-protected' ) . '

'; + + } /** * Pre-update 'password_protected_password' Option diff --git a/password-protected.php b/password-protected.php index e72ca70..b56502d 100644 --- a/password-protected.php +++ b/password-protected.php @@ -329,6 +329,14 @@ public function maybe_show_login() { if ( $this->is_user_logged_in() ) { return; } + + $ppup_val = get_option( 'password_protected_unprotected_pages' ); + if($ppup_val !== false && strlen(trim($ppup_val)) > 0) { + $unprotected_page_ids = explode( "\n", $ppup_val ); + if( is_array($unprotected_page_ids) && is_page( $unprotected_page_ids ) ) { + return; + } + } // Show login form if ( isset( $_REQUEST['password-protected'] ) && 'login' == $_REQUEST['password-protected'] ) { diff --git a/readme.txt b/readme.txt index dff51d6..a9c6c19 100644 --- a/readme.txt +++ b/readme.txt @@ -20,6 +20,7 @@ Features include: * Password protect your WordPress site with a single password. * Option to allow access to feeds. * Option to allow administrators access without entering password. +* Option to unprotect pages and posts by ID (whitelisting) * Works with Mark Jaquith's [Login Logo](http://wordpress.org/extend/plugins/login-logo/) plugin. * Works with the [Uber Login Logo](http://wordpress.org/plugins/uber-login-logo/) plugin.