55Plugin URI: https://wordpress.org/plugins/password-protected/ 
66Description: 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. 
77Version: 2.3 
8+ Requires PHP: 5.6 
89Author: Ben Huson 
910Text Domain: password-protected 
1011Author URI: http://github.com/benhuson/password-protected/ 
4243
4344class  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.3 ' ;
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  );
@@ -655,11 +669,11 @@ public function cookie_name() {
655669	 */ 
656670	public  function  install () {
657671
658- 		$ old_version  = get_option ( ' password_protected_version '   );
672+ 		$ old_version  = get_option ( self :: _OPTION_VERSION  );
659673
660674		// 1.1 - Upgrade to MD5 
661675		if  ( empty ( $ old_version  ) || version_compare ( '1.1 ' , $ old_version  ) ) {
662- 			$ pwd  = get_option ( ' password_protected_password '   );
676+ 			$ pwd  = get_option ( self :: _OPTION_PASSWORD  );
663677			if  ( ! empty ( $ pwd  ) ) {
664678				$ new_pwd  = $ this  ->encrypt_password ( $ pwd  );
665679				update_option ( 'password_protected_password ' , $ new_pwd  );
@@ -670,6 +684,28 @@ public function install() {
670684
671685	}
672686
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+ 
673709	/** 
674710	 * Compat 
675711	 * 
@@ -807,7 +843,7 @@ static function is_plugin_supported() {
807843	public  function  only_allow_logged_in_rest_access ( $ access  ) {
808844
809845		// 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  ) ) {
811847			return  new  WP_Error ( 'rest_cannot_access ' , __ ( 'Only authenticated users can access the REST API. ' , 'password-protected '  ), array ( 'status '  => rest_authorization_required_code () ) );
812848		}
813849
0 commit comments