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

Add ability to add pages/posts to the allowed list #174

Open
wants to merge 1 commit 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
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 (allowed list)
- Works with Mark Jaquith's [Login Logo](https://wordpress.org/plugins/login-logo/) plugin.
- Works with the [Uber Login Logo](https://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 @@ -163,6 +163,14 @@ public function password_protected_settings() {
'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_rest', 'intval' );
Expand All @@ -172,6 +180,7 @@ public function password_protected_settings() {
register_setting( $this->options_group, 'password_protected_allowed_ip_addresses', array( $this, 'sanitize_ip_addresses' ) );
register_setting( $this->options_group, 'password_protected_remember_me', 'boolval' );
register_setting( $this->options_group, 'password_protected_remember_me_lifetime', 'intval' );
register_setting( $this->options_group, 'password_protected_unprotected_pages', array( $this, 'sanitize_pages' ) );

}

Expand Down Expand Up @@ -237,6 +246,25 @@ private function validate_ip_address( $ip_address ) {

}

/**
* 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 @@ -311,6 +339,16 @@ public function password_protected_remember_me_lifetime_field() {

}

/**
* 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
28 changes: 28 additions & 0 deletions password-protected.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function __construct() {
add_filter( 'pre_option_password_protected_status', array( $this, 'allow_feeds' ) );
add_filter( 'pre_option_password_protected_status', array( $this, 'allow_administrators' ) );
add_filter( 'pre_option_password_protected_status', array( $this, 'allow_users' ) );
add_filter( 'pre_option_password_protected_status', array( $this, 'allow_pages' ) );
add_filter( 'rest_authentication_errors', array( $this, 'only_allow_logged_in_rest_access' ) );
add_action( 'init', array( $this, 'compat' ) );
add_action( 'password_protected_login_messages', array( $this, 'login_messages' ) );
Expand Down Expand Up @@ -214,6 +215,33 @@ public function allow_users( $bool ) {

}

/**
* Allow Pages
*
* @param boolean $bool Allow administrators.
* @return boolean True/false.
*/
public function allow_pages( $bool ) {

if (is_admin() && (bool) get_option( 'password_protected_administrators' ) ) {
return 0;
}

if (is_user_logged_in() && (bool) get_option( 'password_protected_users' ) ) {
return 0;
}

$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 0;
}
}

return $bool;
}

/**
* Allow IP Addresses
*
Expand Down