5
5
Plugin URI: https://wordpress.org/plugins/password-protected/
6
6
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.
7
7
Version: 2.2.5
8
+ Requires PHP: 5.6
8
9
Author: Ben Huson
9
10
Text Domain: password-protected
10
11
Author URI: http://github.com/benhuson/password-protected/
42
43
43
44
class Password_Protected {
44
45
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
+
45
58
var $ version = '2.2.5 ' ;
46
59
var $ admin = null ;
47
60
var $ errors = null ;
@@ -54,6 +67,7 @@ public function __construct() {
54
67
$ this ->errors = new WP_Error ();
55
68
56
69
register_activation_hook ( __FILE__ , array ( &$ this , 'install ' ) );
70
+ register_uninstall_hook ( __FILE__ , array ( __CLASS__ , 'uninstall ' ) );
57
71
58
72
add_action ( 'plugins_loaded ' , array ( $ this , 'load_plugin_textdomain ' ) );
59
73
@@ -122,7 +136,7 @@ public function is_active() {
122
136
return false ;
123
137
}
124
138
125
- if ( (bool ) get_option ( ' password_protected_status ' ) ) {
139
+ if ( (bool ) get_option ( self :: _OPTION_STATUS ) ) {
126
140
$ is_active = true ;
127
141
} else {
128
142
$ is_active = false ;
@@ -174,7 +188,7 @@ public function disable_feed() {
174
188
*/
175
189
public function allow_feeds ( $ bool ) {
176
190
177
- if ( is_feed () && (bool ) get_option ( ' password_protected_feeds ' ) ) {
191
+ if ( is_feed () && (bool ) get_option ( self :: _OPTION_FEEDS ) ) {
178
192
return 0 ;
179
193
}
180
194
@@ -190,7 +204,7 @@ public function allow_feeds( $bool ) {
190
204
*/
191
205
public function allow_administrators ( $ bool ) {
192
206
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 ) ) {
194
208
return 0 ;
195
209
}
196
210
@@ -206,7 +220,7 @@ public function allow_administrators( $bool ) {
206
220
*/
207
221
public function allow_users ( $ bool ) {
208
222
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 ) ) {
210
224
return 0 ;
211
225
}
212
226
@@ -241,7 +255,7 @@ public function allow_ip_addresses( $bool ) {
241
255
*/
242
256
public function get_allowed_ip_addresses () {
243
257
244
- return explode ( "\n" , get_option ( ' password_protected_allowed_ip_addresses ' ) );
258
+ return explode ( "\n" , get_option ( self :: _OPTION_ALLOWED_IP_ADDRESSES ) );
245
259
246
260
}
247
261
@@ -252,7 +266,7 @@ public function get_allowed_ip_addresses() {
252
266
*/
253
267
public function allow_remember_me () {
254
268
255
- return (bool ) get_option ( ' password_protected_remember_me ' );
269
+ return (bool ) get_option ( self :: _OPTION_REMEMBER_ME );
256
270
257
271
}
258
272
@@ -297,7 +311,7 @@ public function maybe_process_login() {
297
311
298
312
if ( $ this ->is_active () && isset ( $ _REQUEST ['password_protected_pwd ' ] ) ) {
299
313
$ password_protected_pwd = $ _REQUEST ['password_protected_pwd ' ];
300
- $ pwd = get_option ( ' password_protected_password ' );
314
+ $ pwd = get_option ( self :: _OPTION_PASSWORD_PROTECTED );
301
315
302
316
// If correct password...
303
317
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 ) {
494
508
*/
495
509
public function get_hashed_password () {
496
510
497
- return md5 ( get_option ( ' password_protected_password ' ) . wp_salt () );
511
+ return md5 ( get_option ( self :: _OPTION_PASSWORD_PROTECTED ) . wp_salt () );
498
512
499
513
}
500
514
@@ -604,7 +618,7 @@ public function parse_auth_cookie( $cookie = '', $scheme = '' ) {
604
618
public function set_auth_cookie ( $ remember = false , $ secure = '' ) {
605
619
606
620
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 );
608
622
$ expiration = $ expire = current_time ( 'timestamp ' ) + $ expiration_time ;
609
623
} else {
610
624
$ expiration_time = apply_filters ( 'password_protected_auth_cookie_expiration ' , DAY_IN_SECONDS * 20 , $ remember );
@@ -652,11 +666,11 @@ public function cookie_name() {
652
666
*/
653
667
public function install () {
654
668
655
- $ old_version = get_option ( ' password_protected_version ' );
669
+ $ old_version = get_option ( self :: _OPTION_VERSION );
656
670
657
671
// 1.1 - Upgrade to MD5
658
672
if ( empty ( $ old_version ) || version_compare ( '1.1 ' , $ old_version ) ) {
659
- $ pwd = get_option ( ' password_protected_password ' );
673
+ $ pwd = get_option ( self :: _OPTION_PASSWORD );
660
674
if ( ! empty ( $ pwd ) ) {
661
675
$ new_pwd = $ this ->encrypt_password ( $ pwd );
662
676
update_option ( 'password_protected_password ' , $ new_pwd );
@@ -667,6 +681,28 @@ public function install() {
667
681
668
682
}
669
683
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
+
670
706
/**
671
707
* Compat
672
708
*
@@ -804,7 +840,7 @@ static function is_plugin_supported() {
804
840
public function only_allow_logged_in_rest_access ( $ access ) {
805
841
806
842
// 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 ) ) {
808
844
return new WP_Error ( 'rest_cannot_access ' , __ ( 'Only authenticated users can access the REST API. ' , 'password-protected ' ), array ( 'status ' => rest_authorization_required_code () ) );
809
845
}
810
846
0 commit comments