Skip to content

Add uninstall routine #154

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
60 changes: 48 additions & 12 deletions password-protected.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Plugin URI: https://wordpress.org/plugins/password-protected/
Description: A very simple way to quickly password protect your WordPress site with a single password. Please note: This plugin does not restrict access to uploaded files and images and does not work with some caching setups.
Version: 2.3
Requires PHP: 5.6
Author: Ben Huson
Text Domain: password-protected
Author URI: http://github.com/benhuson/password-protected/
Expand Down Expand Up @@ -42,6 +43,18 @@

class Password_Protected {

const _OPTION_ADMINISTRATORS = 'password_protected_administrators';
const _OPTION_ALLOWED_IP_ADDRESSES = 'password_protected_allowed_ip_addresses';
const _OPTION_FEEDS = 'password_protected_feeds';
const _OPTION_PASSWORD = 'password_protected_password';
const _OPTION_PASSWORD_PROTECTED = 'password_protected_password';
const _OPTION_REMEMBER_ME = 'password_protected_remember_me';
const _OPTION_REMEMBER_ME_LIFETIME = 'password_protected_remember_me_lifetime';
const _OPTION_REST = 'password_protected_rest';
const _OPTION_STATUS = 'password_protected_status';
const _OPTION_USERS = 'password_protected_users';
const _OPTION_VERSION = 'password_protected_version';

var $version = '2.3';
var $admin = null;
var $errors = null;
Expand All @@ -54,6 +67,7 @@ public function __construct() {
$this->errors = new WP_Error();

register_activation_hook( __FILE__, array( &$this, 'install' ) );
register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall' ) );

add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );

Expand Down Expand Up @@ -122,7 +136,7 @@ public function is_active() {
return false;
}

if ( (bool) get_option( 'password_protected_status' ) ) {
if ( (bool) get_option( self::_OPTION_STATUS ) ) {
$is_active = true;
} else {
$is_active = false;
Expand Down Expand Up @@ -174,7 +188,7 @@ public function disable_feed() {
*/
public function allow_feeds( $bool ) {

if ( is_feed() && (bool) get_option( 'password_protected_feeds' ) ) {
if ( is_feed() && (bool) get_option( self::_OPTION_FEEDS ) ) {
return 0;
}

Expand All @@ -190,7 +204,7 @@ public function allow_feeds( $bool ) {
*/
public function allow_administrators( $bool ) {

if ( ! is_admin() && current_user_can( 'manage_options' ) && (bool) get_option( 'password_protected_administrators' ) ) {
if ( ! is_admin() && current_user_can( 'manage_options' ) && (bool) get_option( self::_OPTION_ADMINISTRATORS ) ) {
return 0;
}

Expand All @@ -206,7 +220,7 @@ public function allow_administrators( $bool ) {
*/
public function allow_users( $bool ) {

if ( ! is_admin() && is_user_logged_in() && (bool) get_option( 'password_protected_users' ) ) {
if ( ! is_admin() && is_user_logged_in() && (bool) get_option( self::_OPTION_USERS ) ) {
return 0;
}

Expand Down Expand Up @@ -241,7 +255,7 @@ public function allow_ip_addresses( $bool ) {
*/
public function get_allowed_ip_addresses() {

return explode( "\n", get_option( 'password_protected_allowed_ip_addresses' ) );
return explode( "\n", get_option( self::_OPTION_ALLOWED_IP_ADDRESSES ) );

}

Expand All @@ -252,7 +266,7 @@ public function get_allowed_ip_addresses() {
*/
public function allow_remember_me() {

return (bool) get_option( 'password_protected_remember_me' );
return (bool) get_option( self::_OPTION_REMEMBER_ME );

}

Expand Down Expand Up @@ -297,7 +311,7 @@ public function maybe_process_login() {

if ( $this->is_active() && isset( $_REQUEST['password_protected_pwd'] ) ) {
$password_protected_pwd = $_REQUEST['password_protected_pwd'];
$pwd = get_option( 'password_protected_password' );
$pwd = get_option( self::_OPTION_PASSWORD_PROTECTED );

// If correct password...
if ( ( hash_equals( $pwd, $this->encrypt_password( $password_protected_pwd ) ) && $pwd != '' ) || apply_filters( 'password_protected_process_login', false, $password_protected_pwd ) ) {
Expand Down Expand Up @@ -494,7 +508,7 @@ public function logout_link_shortcode( $atts, $content = null ) {
*/
public function get_hashed_password() {

return md5( get_option( 'password_protected_password' ) . wp_salt() );
return md5( get_option( self::_OPTION_PASSWORD_PROTECTED ) . wp_salt() );

}

Expand Down Expand Up @@ -604,7 +618,7 @@ public function parse_auth_cookie( $cookie = '', $scheme = '' ) {
public function set_auth_cookie( $remember = false, $secure = '') {

if ( $remember ) {
$expiration_time = apply_filters( 'password_protected_auth_cookie_expiration', get_option( 'password_protected_remember_me_lifetime', 14 ) * DAY_IN_SECONDS, $remember );
$expiration_time = apply_filters( 'password_protected_auth_cookie_expiration', get_option( self::_OPTION_REMEMBER_ME_LIFETIME, 14 ) * DAY_IN_SECONDS, $remember );
$expiration = $expire = current_time( 'timestamp' ) + $expiration_time;
} else {
$expiration_time = apply_filters( 'password_protected_auth_cookie_expiration', DAY_IN_SECONDS * 20, $remember );
Expand Down Expand Up @@ -655,11 +669,11 @@ public function cookie_name() {
*/
public function install() {

$old_version = get_option( 'password_protected_version' );
$old_version = get_option( self::_OPTION_VERSION );

// 1.1 - Upgrade to MD5
if ( empty( $old_version ) || version_compare( '1.1', $old_version ) ) {
$pwd = get_option( 'password_protected_password' );
$pwd = get_option( self::_OPTION_PASSWORD );
if ( ! empty( $pwd ) ) {
$new_pwd = $this->encrypt_password( $pwd );
update_option( 'password_protected_password', $new_pwd );
Expand All @@ -670,6 +684,28 @@ public function install() {

}

/**
* Uninstall
*/
public static function uninstall() {
$options = array(
self::_OPTION_ADMINISTRATORS,
self::_OPTION_ALLOWED_IP_ADDRESSES,
self::_OPTION_FEEDS,
self::_OPTION_PASSWORD,
self::_OPTION_PASSWORD_PROTECTED,
self::_OPTION_REMEMBER_ME,
self::_OPTION_REMEMBER_ME_LIFETIME,
self::_OPTION_REST,
self::_OPTION_STATUS,
self::_OPTION_USERS,
self::_OPTION_VERSION,
);
foreach ( $options as $option ) {
delete_option($option);
}
}

/**
* Compat
*
Expand Down Expand Up @@ -807,7 +843,7 @@ static function is_plugin_supported() {
public function only_allow_logged_in_rest_access( $access ) {

// If user is not logged in
if ( $this->is_active() && ! $this->is_user_logged_in() && ! is_user_logged_in() && ! (bool) get_option( 'password_protected_rest' ) ) {
if ( $this->is_active() && ! $this->is_user_logged_in() && ! is_user_logged_in() && ! (bool) get_option( self::_OPTION_REST ) ) {
return new WP_Error( 'rest_cannot_access', __( 'Only authenticated users can access the REST API.', 'password-protected' ), array( 'status' => rest_authorization_required_code() ) );
}

Expand Down