Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added page/post whitelisting #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
38 changes: 38 additions & 0 deletions admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,22 @@ 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' );
register_setting( $this->options_group, 'password_protected_administrators', 'intval' );
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' ) );

}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -249,6 +277,16 @@ public function password_protected_allowed_ip_addresses_field() {
echo '<p class="description">' . esc_html__( 'Enter one IP address per line.', 'password-protected' ) . ' ' . esc_html( sprintf( __( 'Your IP is address %s.', 'password-protected' ), $_SERVER['REMOTE_ADDR'] ) ) . '</p>';

}

/**
* Unprotected Pages Field
*/
public function password_protected_unprotected_pages_field() {

echo '<textarea name="password_protected_unprotected_pages" id="password_protected_unprotected_pages" rows="3" class="large-text" />' . get_option( 'password_protected_unprotected_pages' ) . '</textarea>';
echo '<p class="description">' . esc_html__( 'Enter one Page/Post ID per line.', 'password-protected' ) . '</p>';

}

/**
* Pre-update 'password_protected_password' Option
Expand Down
8 changes: 8 additions & 0 deletions password-protected.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) {
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down