Skip to content

Commit ad1d56b

Browse files
committed
Add uninstall routine
1 parent e4b630a commit ad1d56b

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

password-protected.php

+48-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Plugin URI: https://wordpress.org/plugins/password-protected/
66
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.
77
Version: 2.2.5
8+
Requires PHP: 5.6
89
Author: Ben Huson
910
Text Domain: password-protected
1011
Author URI: http://github.com/benhuson/password-protected/
@@ -42,6 +43,18 @@
4243

4344
class Password_Protected {
4445

46+
const _OPTION_ADMINISTRATORS = 'password_protected_administrators';
47+
const _OPTION_ALLOWED_IP_ADDRESSES = 'password_protected_allowed_ip_addresses';
48+
const _OPTION_FEEDS = 'password_protected_feeds';
49+
const _OPTION_PASSWORD = 'password_protected_password';
50+
const _OPTION_PASSWORD_PROTECTED = 'password_protected_password';
51+
const _OPTION_REMEMBER_ME = 'password_protected_remember_me';
52+
const _OPTION_REMEMBER_ME_LIFETIME = 'password_protected_remember_me_lifetime';
53+
const _OPTION_REST = 'password_protected_rest';
54+
const _OPTION_STATUS = 'password_protected_status';
55+
const _OPTION_USERS = 'password_protected_users';
56+
const _OPTION_VERSION = 'password_protected_version';
57+
4558
var $version = '2.2.5';
4659
var $admin = null;
4760
var $errors = null;
@@ -54,6 +67,7 @@ public function __construct() {
5467
$this->errors = new WP_Error();
5568

5669
register_activation_hook( __FILE__, array( &$this, 'install' ) );
70+
register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall' ) );
5771

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

@@ -122,7 +136,7 @@ public function is_active() {
122136
return false;
123137
}
124138

125-
if ( (bool) get_option( 'password_protected_status' ) ) {
139+
if ( (bool) get_option( self::_OPTION_STATUS ) ) {
126140
$is_active = true;
127141
} else {
128142
$is_active = false;
@@ -174,7 +188,7 @@ public function disable_feed() {
174188
*/
175189
public function allow_feeds( $bool ) {
176190

177-
if ( is_feed() && (bool) get_option( 'password_protected_feeds' ) ) {
191+
if ( is_feed() && (bool) get_option( self::_OPTION_FEEDS ) ) {
178192
return 0;
179193
}
180194

@@ -190,7 +204,7 @@ public function allow_feeds( $bool ) {
190204
*/
191205
public function allow_administrators( $bool ) {
192206

193-
if ( ! is_admin() && current_user_can( 'manage_options' ) && (bool) get_option( 'password_protected_administrators' ) ) {
207+
if ( ! is_admin() && current_user_can( 'manage_options' ) && (bool) get_option( self::_OPTION_ADMINISTRATORS ) ) {
194208
return 0;
195209
}
196210

@@ -206,7 +220,7 @@ public function allow_administrators( $bool ) {
206220
*/
207221
public function allow_users( $bool ) {
208222

209-
if ( ! is_admin() && is_user_logged_in() && (bool) get_option( 'password_protected_users' ) ) {
223+
if ( ! is_admin() && is_user_logged_in() && (bool) get_option( self::_OPTION_USERS ) ) {
210224
return 0;
211225
}
212226

@@ -241,7 +255,7 @@ public function allow_ip_addresses( $bool ) {
241255
*/
242256
public function get_allowed_ip_addresses() {
243257

244-
return explode( "\n", get_option( 'password_protected_allowed_ip_addresses' ) );
258+
return explode( "\n", get_option( self::_OPTION_ALLOWED_IP_ADDRESSES ) );
245259

246260
}
247261

@@ -252,7 +266,7 @@ public function get_allowed_ip_addresses() {
252266
*/
253267
public function allow_remember_me() {
254268

255-
return (bool) get_option( 'password_protected_remember_me' );
269+
return (bool) get_option( self::_OPTION_REMEMBER_ME );
256270

257271
}
258272

@@ -297,7 +311,7 @@ public function maybe_process_login() {
297311

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

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

497-
return md5( get_option( 'password_protected_password' ) . wp_salt() );
511+
return md5( get_option( self::_OPTION_PASSWORD_PROTECTED ) . wp_salt() );
498512

499513
}
500514

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

606620
if ( $remember ) {
607-
$expiration_time = apply_filters( 'password_protected_auth_cookie_expiration', get_option( 'password_protected_remember_me_lifetime', 14 ) * DAY_IN_SECONDS, $remember );
621+
$expiration_time = apply_filters( 'password_protected_auth_cookie_expiration', get_option( self::_OPTION_REMEMBER_ME_LIFETIME, 14 ) * DAY_IN_SECONDS, $remember );
608622
$expiration = $expire = current_time( 'timestamp' ) + $expiration_time;
609623
} else {
610624
$expiration_time = apply_filters( 'password_protected_auth_cookie_expiration', DAY_IN_SECONDS * 20, $remember );
@@ -652,11 +666,11 @@ public function cookie_name() {
652666
*/
653667
public function install() {
654668

655-
$old_version = get_option( 'password_protected_version' );
669+
$old_version = get_option( self::_OPTION_VERSION );
656670

657671
// 1.1 - Upgrade to MD5
658672
if ( empty( $old_version ) || version_compare( '1.1', $old_version ) ) {
659-
$pwd = get_option( 'password_protected_password' );
673+
$pwd = get_option( self::_OPTION_PASSWORD );
660674
if ( ! empty( $pwd ) ) {
661675
$new_pwd = $this->encrypt_password( $pwd );
662676
update_option( 'password_protected_password', $new_pwd );
@@ -667,6 +681,28 @@ public function install() {
667681

668682
}
669683

684+
/**
685+
* Uninstall
686+
*/
687+
public static function uninstall() {
688+
$options = array(
689+
self::_OPTION_ADMINISTRATORS,
690+
self::_OPTION_ALLOWED_IP_ADDRESSES,
691+
self::_OPTION_FEEDS,
692+
self::_OPTION_PASSWORD,
693+
self::_OPTION_PASSWORD_PROTECTED,
694+
self::_OPTION_REMEMBER_ME,
695+
self::_OPTION_REMEMBER_ME_LIFETIME,
696+
self::_OPTION_REST,
697+
self::_OPTION_STATUS,
698+
self::_OPTION_USERS,
699+
self::_OPTION_VERSION,
700+
);
701+
foreach ( $options as $option ) {
702+
delete_option($option);
703+
}
704+
}
705+
670706
/**
671707
* Compat
672708
*
@@ -804,7 +840,7 @@ static function is_plugin_supported() {
804840
public function only_allow_logged_in_rest_access( $access ) {
805841

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

0 commit comments

Comments
 (0)