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.3
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.3 ' ;
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 );
@@ -655,11 +669,11 @@ public function cookie_name() {
655
669
*/
656
670
public function install () {
657
671
658
- $ old_version = get_option ( ' password_protected_version ' );
672
+ $ old_version = get_option ( self :: _OPTION_VERSION );
659
673
660
674
// 1.1 - Upgrade to MD5
661
675
if ( empty ( $ old_version ) || version_compare ( '1.1 ' , $ old_version ) ) {
662
- $ pwd = get_option ( ' password_protected_password ' );
676
+ $ pwd = get_option ( self :: _OPTION_PASSWORD );
663
677
if ( ! empty ( $ pwd ) ) {
664
678
$ new_pwd = $ this ->encrypt_password ( $ pwd );
665
679
update_option ( 'password_protected_password ' , $ new_pwd );
@@ -670,6 +684,28 @@ public function install() {
670
684
671
685
}
672
686
687
+ /**
688
+ * Uninstall
689
+ */
690
+ public static function uninstall () {
691
+ $ options = array (
692
+ self ::_OPTION_ADMINISTRATORS ,
693
+ self ::_OPTION_ALLOWED_IP_ADDRESSES ,
694
+ self ::_OPTION_FEEDS ,
695
+ self ::_OPTION_PASSWORD ,
696
+ self ::_OPTION_PASSWORD_PROTECTED ,
697
+ self ::_OPTION_REMEMBER_ME ,
698
+ self ::_OPTION_REMEMBER_ME_LIFETIME ,
699
+ self ::_OPTION_REST ,
700
+ self ::_OPTION_STATUS ,
701
+ self ::_OPTION_USERS ,
702
+ self ::_OPTION_VERSION ,
703
+ );
704
+ foreach ( $ options as $ option ) {
705
+ delete_option ($ option );
706
+ }
707
+ }
708
+
673
709
/**
674
710
* Compat
675
711
*
@@ -807,7 +843,7 @@ static function is_plugin_supported() {
807
843
public function only_allow_logged_in_rest_access ( $ access ) {
808
844
809
845
// If user is not logged in
810
- if ( $ this ->is_active () && ! $ this ->is_user_logged_in () && ! is_user_logged_in () && ! (bool ) get_option ( ' password_protected_rest ' ) ) {
846
+ if ( $ this ->is_active () && ! $ this ->is_user_logged_in () && ! is_user_logged_in () && ! (bool ) get_option ( self :: _OPTION_REST ) ) {
811
847
return new WP_Error ( 'rest_cannot_access ' , __ ( 'Only authenticated users can access the REST API. ' , 'password-protected ' ), array ( 'status ' => rest_authorization_required_code () ) );
812
848
}
813
849
0 commit comments