PluginProbe
Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content / 2.6.4
Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content v2.6.4
2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.5 1.6 1.6.1 1.6.2 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.0.1 2.0.2 2.0.3 All 63 releases
password-protected / password-protected.php

password-protected.php in Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content 2.6.4, at password-protected.php

966 lines 25.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Password Protected
4 Plugin URI: https://wordpress.org/plugins/password-protected/
5 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.
6 Version: 2.6.4
7 Author: WPExperts
8 Text Domain: password-protected
9 Author URI: https://wpexperts.io/
10 License: GPLv2
11 */
12 /*
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License, version 2, as
15 published by the Free Software Foundation.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 /**
28 * @todo Use wp_hash_password() ?
29 * @todo Remember me
30 */
31
32 define( 'PASSWORD_PROTECTED_SUBDIR', '/' . str_replace( basename( __FILE__ ), '', plugin_basename( __FILE__ ) ) );
33 define( 'PASSWORD_PROTECTED_URL', plugins_url( PASSWORD_PROTECTED_SUBDIR ) );
34 define( 'PASSWORD_PROTECTED_DIR', plugin_dir_path( __FILE__ ) );
35
36 require_once PASSWORD_PROTECTED_DIR . 'includes/freemius.php';
37
38 global $Password_Protected;
39 $Password_Protected = new Password_Protected();
40
41 class Password_Protected {
42
43 var $version = '2.6.4';
44 var $admin = null;
45 var $errors = null;
46 var $admin_caching = null;
47
48 /**
49 * Constructor
50 */
51 public function __construct() {
52
53 $this->errors = new WP_Error();
54
55 register_activation_hook( __FILE__, array( &$this, 'install' ) );
56
57 add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
58
59 add_filter( 'password_protected_is_active', array( $this, 'allow_ip_addresses' ) );
60
61 add_action( 'init', array( $this, 'disable_caching' ), 1 );
62 add_action( 'init', array( $this, 'maybe_process_logout' ), 1 );
63 add_action( 'init', array( $this, 'maybe_process_login' ), 1 );
64 add_action( 'wp', array( $this, 'disable_feeds' ) );
65 add_action( 'template_redirect', array( $this, 'maybe_show_login' ), -1 );
66 add_filter( 'pre_option_password_protected_status', array( $this, 'allow_feeds' ) );
67 add_filter( 'pre_option_password_protected_status', array( $this, 'allow_administrators' ) );
68 add_filter( 'pre_option_password_protected_status', array( $this, 'allow_users' ) );
69 add_filter( 'rest_authentication_errors', array( $this, 'only_allow_logged_in_rest_access' ) );
70 add_action( 'init', array( $this, 'compat' ) );
71 add_action( 'password_protected_login_messages', array( $this, 'login_messages' ) );
72 add_action( 'login_enqueue_scripts', array( $this, 'load_theme_stylesheet' ), 5 );
73
74 add_action('password_protected_above_password_field', array( $this, 'password_protected_above_password_field' ));
75 add_action('password_protected_below_password_field', array( $this, 'password_protected_below_password_field' ));
76
77
78 // Available from WordPress 4.3+
79 if ( function_exists( 'wp_site_icon' ) ) {
80 add_action( 'password_protected_login_head', 'wp_site_icon' );
81 }
82
83 add_shortcode( 'password_protected_logout_link', array( $this, 'logout_link_shortcode' ) );
84
85 include_once dirname( __FILE__ ) . '/admin/admin-bar.php';
86
87 if ( is_admin() ) {
88
89
90 include_once dirname( __FILE__ ) . '/admin/admin-caching.php';
91 include_once dirname( __FILE__ ) . '/admin/admin.php';
92
93 $this->admin_caching = new Password_Protected_Admin_Caching( $this );
94 $this->admin = new Password_Protected_Admin();
95
96
97 }
98 include_once dirname( __FILE__ ) . '/admin/class-recaptcha.php';
99 new Password_Protected_reCAPTCHA();
100
101 include_once dirname( __FILE__ ) . '/includes/transient-functions.php';
102 add_filter( 'gettext', array( $this, 'change_addon_to_pro' ), 1, 3 );
103 }
104
105 /**
106 * I18n
107 */
108 public function load_plugin_textdomain() {
109
110 load_plugin_textdomain( 'password-protected', false, basename( dirname( __FILE__ ) ) . '/languages' );
111
112 }
113
114 /**
115 * Disable Page Caching
116 */
117 public function disable_caching() {
118
119 if ( $this->is_active() && ! defined( 'DONOTCACHEPAGE' ) ) {
120 define( 'DONOTCACHEPAGE', true );
121 }
122
123 }
124
125 /**
126 * Is Active?
127 *
128 * @return boolean Is password protection active?
129 */
130 public function is_active() {
131
132 global $wp_query;
133
134 // Always allow access to robots.txt
135 if ( isset( $wp_query ) && is_robots() ) {
136 return false;
137 }
138
139 if ( (bool) get_option( 'password_protected_status' ) ) {
140 $is_active = true;
141 } else {
142 $is_active = false;
143 }
144
145 $is_active = apply_filters( 'password_protected_is_active', $is_active );
146
147 if ( isset( $_GET['password-protected'] ) ) {
148 $is_active = true;
149 }
150
151 return $is_active;
152
153 }
154
155 /**
156 * Disable Feeds
157 *
158 * @todo An option/filter to prevent disabling of feeds.
159 */
160 public function disable_feeds() {
161
162 if ( $this->is_active() ) {
163 add_action( 'do_feed', array( $this, 'disable_feed' ), 1 );
164 add_action( 'do_feed_rdf', array( $this, 'disable_feed' ), 1 );
165 add_action( 'do_feed_rss', array( $this, 'disable_feed' ), 1 );
166 add_action( 'do_feed_rss2', array( $this, 'disable_feed' ), 1 );
167 add_action( 'do_feed_atom', array( $this, 'disable_feed' ), 1 );
168 }
169
170 }
171
172 /**
173 * Disable Feed
174 *
175 * @todo Make Translatable
176 */
177 public function disable_feed() {
178
179 wp_die( sprintf( __( 'Feeds are not available for this site. Please visit the <a href="%s">website</a>.', 'password-protected' ), get_bloginfo( 'url' ) ) );
180
181 }
182
183 /**
184 * Allow Feeds
185 *
186 * @param boolean $bool Allow feeds.
187 * @return boolean True/false.
188 */
189 public function allow_feeds( $bool ) {
190
191 if ( is_feed() && (bool) get_option( 'password_protected_feeds' ) ) {
192 return 0;
193 }
194
195 return $bool;
196
197 }
198
199 /**
200 * Allow Administrators
201 *
202 * @param boolean $bool Allow administrators.
203 * @return boolean True/false.
204 */
205 public function allow_administrators( $bool ) {
206
207 if ( ! is_admin() && current_user_can( 'manage_options' ) && (bool) get_option( 'password_protected_administrators' ) ) {
208 return 0;
209 }
210
211 return $bool;
212
213 }
214
215 /**
216 * Allow Users
217 *
218 * @param boolean $bool Allow administrators.
219 * @return boolean True/false.
220 */
221 public function allow_users( $bool ) {
222
223 if ( ! is_admin() && is_user_logged_in() && (bool) get_option( 'password_protected_users' ) ) {
224 return 0;
225 }
226
227 return $bool;
228
229 }
230
231 /**
232 * Allow IP Addresses
233 *
234 * If user has a valid email address, return false to disable password protection.
235 *
236 * @param boolean $bool Allow IP addresses.
237 * @return boolean True/false.
238 */
239 public function allow_ip_addresses( $bool ) {
240
241 $ip_addresses = $this->get_allowed_ip_addresses();
242
243 if ( isset( $_SERVER['REMOTE_ADDR'] ) && in_array( $_SERVER['REMOTE_ADDR'], $ip_addresses ) ) {
244 $bool = false;
245 }
246
247 return $bool;
248
249 }
250
251 /**
252 * Get Allowed IP Addresses
253 *
254 * @return array IP addresses.
255 */
256 public function get_allowed_ip_addresses() {
257
258 return explode( "\n", get_option( 'password_protected_allowed_ip_addresses' ) );
259
260 }
261
262 /**
263 * Allow the remember me function
264 *
265 * @return. boolean
266 */
267 public function allow_remember_me() {
268
269 return (bool) get_option( 'password_protected_remember_me' );
270
271 }
272
273 /**
274 * Encrypt Password
275 *
276 * @param string $password Password.
277 * @return string Encrypted password.
278 */
279 public function encrypt_password( $password ) {
280
281 return md5( $password );
282
283 }
284
285 /**
286 * Maybe Process Logout
287 */
288 public function maybe_process_logout() {
289
290 if ( isset( $_REQUEST['password-protected'] ) && sanitize_text_field( $_REQUEST['password-protected'] ) == 'logout' ) {
291
292 $this->logout();
293
294 if ( isset( $_REQUEST['redirect_to'] ) ) {
295 $redirect_to = remove_query_arg( 'password-protected', esc_url_raw( $_REQUEST['redirect_to'], array( 'http', 'https' ) ) );
296 } else {
297 $redirect_to = home_url( '/' );
298 }
299
300 $this->safe_redirect( $redirect_to );
301 exit();
302
303 }
304
305 }
306
307 /**
308 * Maybe Process Login
309 */
310 public function maybe_process_login() {
311
312 if ( $this->is_active() && isset( $_REQUEST['password_protected_pwd'] ) ) {
313
314 $password_protected_pwd = sanitize_text_field( $_REQUEST['password_protected_pwd'] );
315 $default_password = get_option( 'password_protected_password' );
316
317 $auth = false;
318
319 if ( empty( $default_password ) ) {
320
321 $auth = $this->password_protected_check_pro_password( $password_protected_pwd );
322
323 } else {
324
325 if ( ( hash_equals( $default_password, $this->encrypt_password( $password_protected_pwd ) ) && $default_password != '' ) || apply_filters( 'password_protected_process_login', false, $password_protected_pwd ) ) {
326 $auth = true;
327 }
328
329 if ( ! $auth ) {
330
331 $auth = $this->password_protected_check_pro_password( $password_protected_pwd );
332 }
333
334 }
335
336 $this->errors = apply_filters( 'password_protected_verify_recaptcha', $this->errors );
337
338 if( count( @$this->errors->errors ) > 0 ) return;
339
340 $this->password_protected_process_login( $auth );
341
342 }
343
344 }
345
346 private function password_protected_process_login( bool $auth ) {
347
348 if( $auth )
349 $throttle = apply_filters( 'password_protected_check_for_throttling', true );
350
351
352 if( $auth && $throttle ) {
353
354 do_action( 'password_protected_success_login_attempt' );
355 $remember = isset( $_REQUEST['password_protected_rememberme'] ) ? boolval( $_REQUEST['password_protected_rememberme'] ) : false;
356
357 if ( ! $this->allow_remember_me() ) {
358 $remember = false;
359 }
360 $this->set_auth_cookie( $remember );
361
362 $redirect_to = isset( $_REQUEST['redirect_to'] ) ? esc_url($_REQUEST['redirect_to']) : '';
363 $redirect_to = apply_filters( 'password_protected_login_redirect', $redirect_to );
364
365 if ( ! empty( $redirect_to ) ) {
366 $this->safe_redirect( remove_query_arg( 'password-protected', $redirect_to ) );
367 exit;
368 } elseif ( isset( $_GET['password_protected_pwd'] ) ) {
369 $this->safe_redirect( remove_query_arg( 'password-protected' ) );
370 exit;
371 } else {
372 $this->safe_redirect( site_url() );
373 exit;
374 }
375 } else {
376 do_action( 'password_protected_failure_login_attempt' );
377
378 // ... otherwise incorrect password
379 $this->clear_auth_cookie();
380
381 $show_default_error = apply_filters( 'password_protected_throttling_error_messages', true );
382
383 if( $show_default_error )
384 $this->errors->add( 'incorrect_password', __( 'Incorrect Password', 'password-protected' ) );
385 }
386 }
387
388 /**
389 * password_protected_check_pro_password
390 *
391 * @param mixed $requested_password
392 * @return void
393 */
394 private function password_protected_check_pro_password( $requested_password ) {
395
396 $pro_passwords = apply_filters( 'password_protected_passwords', array() );
397 $pro_passwords = array_filter( $pro_passwords );
398
399 $auth = false;
400
401 if( is_array( $pro_passwords ) && count( $pro_passwords ) > 0 ) {
402
403 foreach( $pro_passwords as $i => $p ) {
404
405 if ( ( hash_equals( $p, $this->encrypt_password( $requested_password ) ) && $pro_passwords != '' ) || apply_filters( 'password_protected_process_login', false, $requested_password ) ) {
406
407 $auth = apply_filters( 'password_protected_login_password_matched', $p, $this->errors );
408
409 break;
410
411 }
412
413 }
414
415 } else {
416
417 $auth = false;
418
419 }
420
421 return $auth;
422 }
423
424 /**
425 * Is User Logged In?
426 *
427 * @return boolean
428 */
429 public function is_user_logged_in() {
430
431 return $this->is_active() && $this->validate_auth_cookie();
432
433 }
434
435 /**
436 * Maybe Show Login
437 */
438 public function maybe_show_login() {
439
440 if ( class_exists( 'Login_designer' ) ) {
441 if ( is_customize_preview() ) {
442 return 1;
443 }
444 }
445
446 // Filter for adding exceptions.
447 $show_login = apply_filters( 'password_protected_show_login', $this->is_active() );
448
449 // Logged in
450 if ( $this->is_user_logged_in() ) {
451 $show_login = false;
452 }
453
454 if ( ! $show_login ) {
455 return 1;
456 }
457
458 // Show login form
459 if ( isset( $_REQUEST['password-protected'] ) && 'login' == sanitize_text_field( $_REQUEST['password-protected'] ) ) {
460
461 $default_theme_file = locate_template( array( 'password-protected-login.php' ) );
462
463 if ( empty( $default_theme_file ) ) {
464 $default_theme_file = dirname( __FILE__ ) . '/theme/password-protected-login.php';
465 }
466
467 $theme_file = apply_filters( 'password_protected_theme_file', $default_theme_file );
468 if ( ! file_exists( $theme_file ) ) {
469 $theme_file = $default_theme_file;
470 }
471
472 load_template( $theme_file );
473 exit();
474
475 } else {
476 global $wp;
477
478 $redirect_to = add_query_arg( 'password-protected', 'login', home_url( $wp->request . '?' . $_SERVER['QUERY_STRING'] ) );
479
480 // URL to redirect back to after login
481 $redirect_to_url = apply_filters( 'password_protected_login_redirect_url', ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
482 if ( ! empty( $redirect_to_url ) ) {
483 $redirect_to = add_query_arg( 'redirect_to', urlencode( $redirect_to_url ), $redirect_to );
484 }
485
486 nocache_headers();
487 wp_redirect( $redirect_to );
488 exit();
489
490 }
491 }
492
493 /**
494 * Get Site ID
495 *
496 * @return string Site ID.
497 */
498 public function get_site_id() {
499
500 global $blog_id;
501 return 'bid_' . apply_filters( 'password_protected_blog_id', $blog_id );
502
503 }
504
505 /**
506 * Login URL
507 *
508 * @return string Login URL.
509 */
510 public function login_url() {
511 global $wp;
512 return add_query_arg( 'password-protected', 'login', home_url( $wp->request . '?' . $_SERVER['QUERY_STRING'] ) );
513
514 }
515
516 /**
517 * Logout
518 */
519 public function logout() {
520
521 $this->clear_auth_cookie();
522 do_action( 'password_protected_logout' );
523
524 }
525
526 /**
527 * Logout URL
528 *
529 * @param string $redirect_to Optional. Redirect URL.
530 * @return string Logout URL.
531 */
532 public function logout_url( $redirect_to = '' ) {
533
534 $query = array(
535 'password-protected' => 'logout',
536 'redirect_to' => esc_url_raw( $redirect_to ),
537 );
538
539 if ( empty( $query['redirect_to'] ) ) {
540 unset( $query['redirect_to'] );
541 }
542
543 return add_query_arg( $query, home_url() );
544
545 }
546
547 /**
548 * Logout Link
549 *
550 * @param array $args Link args.
551 * @return string HTML link tag.
552 */
553 public function logout_link( $args = null ) {
554
555 // Only show if user is logged in
556 if ( ! $this->is_user_logged_in() ) {
557 return '';
558 }
559
560 $args = wp_parse_args(
561 $args,
562 array(
563 'redirect_to' => '',
564 'text' => __( 'Logout', 'password-protected' ),
565 )
566 );
567
568 if ( empty( $args['text'] ) ) {
569 $args['text'] = __( 'Logout', 'password-protected' );
570 }
571
572 return sprintf( '<a href="%s">%s</a>', esc_url( $this->logout_url( $args['redirect_to'] ) ), esc_html( $args['text'] ) );
573
574 }
575
576 /**
577 * Logout Link Shortcode
578 *
579 * @param array $args Link args.
580 * @return string HTML link tag.
581 */
582 public function logout_link_shortcode( $atts, $content = null ) {
583
584 $atts = shortcode_atts(
585 array(
586 'redirect_to' => '',
587 'text' => $content,
588 ),
589 $atts,
590 'logout_link_shortcode'
591 );
592
593 return $this->logout_link( $atts );
594
595 }
596
597 /**
598 * Get Hashed Password
599 *
600 * @return string Hashed password.
601 */
602 public function get_hashed_password() {
603
604 return md5( get_option( 'password_protected_password' ) . wp_salt() );
605
606 }
607
608 /**
609 * Validate Auth Cookie
610 *
611 * @param string $cookie Cookie string.
612 * @param string $scheme Cookie scheme.
613 * @return boolean Validation successful?
614 */
615 public function validate_auth_cookie( $cookie = '', $scheme = '' ) {
616
617 if ( ! $cookie_elements = $this->parse_auth_cookie( $cookie, $scheme ) ) {
618 do_action( 'password_protected_auth_cookie_malformed', $cookie, $scheme );
619 return false;
620 }
621
622 extract( $cookie_elements, EXTR_OVERWRITE );
623
624 $expired = $expiration;
625
626 // Allow a grace period for POST and AJAX requests
627 if ( defined( 'DOING_AJAX' ) || 'POST' == $_SERVER['REQUEST_METHOD'] ) {
628 $expired += 3600;
629 }
630
631 // Quick check to see if an honest cookie has expired
632 if ( $expired < current_time( 'timestamp' ) ) {
633 do_action( 'password_protected_auth_cookie_expired', $cookie_elements );
634 return false;
635 }
636
637 $key = md5( $this->get_site_id() . $this->get_hashed_password() . '|' . $expiration );
638 $hash = hash_hmac( 'md5', $this->get_site_id() . '|' . $expiration, $key );
639
640 if ( $hmac != $hash ) {
641 do_action( 'password_protected_auth_cookie_bad_hash', $cookie_elements );
642 return false;
643 }
644
645 if ( $expiration < current_time( 'timestamp' ) ) { // AJAX/POST grace period set above
646 $GLOBALS['login_grace_period'] = 1;
647 }
648
649 return true;
650
651 }
652
653 /**
654 * Generate Auth Cookie
655 *
656 * @param int $expiration Expiration time in seconds.
657 * @param string $scheme Cookie scheme.
658 * @return string Cookie.
659 */
660 public function generate_auth_cookie( $expiration, $scheme = 'auth' ) {
661
662 $key = md5( $this->get_site_id() . $this->get_hashed_password() . '|' . $expiration );
663 $hash = hash_hmac( 'md5', $this->get_site_id() . '|' . $expiration, $key );
664 $cookie = $this->get_site_id() . '|' . $expiration . '|' . $hash;
665
666 return $cookie;
667
668 }
669
670 /**
671 * Parse Auth Cookie
672 *
673 * @param string $cookie Cookie string.
674 * @param string $scheme Cookie scheme.
675 * @return string Cookie string.
676 */
677 public function parse_auth_cookie( $cookie = '', $scheme = '' ) {
678 if ( empty( $cookie ) ) {
679
680 $cookie_name = $this->cookie_name();
681 $use_transient = get_option( 'password_protected_use_transient', '' );
682
683 if ( empty( $use_transient ) ) {
684 if ( empty( $_COOKIE[ $cookie_name ] ) ) {
685 return false;
686 }
687
688 $cookie = $_COOKIE[ $cookie_name ];
689 } else {
690 $cookie = pp_get_transient( $cookie_name );
691 }
692 }
693
694 $cookie_elements = explode( '|', $cookie );
695
696 if ( count( $cookie_elements ) != 3 ) {
697 return false;
698 }
699
700 list( $site_id, $expiration, $hmac ) = $cookie_elements;
701
702 return compact( 'site_id', 'expiration', 'hmac', 'scheme' );
703
704 }
705
706 /**
707 * Set Auth Cookie
708 *
709 * @todo
710 *
711 * @param boolean $remember Remember logged in.
712 * @param string $secure Secure cookie.
713 */
714 public function set_auth_cookie( $remember = false, $secure = '' ) {
715
716 if ( $remember ) {
717 $expiration_time = apply_filters( 'password_protected_auth_cookie_expiration', get_option( 'password_protected_remember_me_lifetime', 14 ) * DAY_IN_SECONDS, $remember );
718 $expiration = $expire = current_time( 'timestamp' ) + $expiration_time;
719 } else {
720 $expiration_time = apply_filters( 'password_protected_auth_cookie_expiration', DAY_IN_SECONDS * 20, $remember );
721 $expiration = current_time( 'timestamp' ) + $expiration_time;
722 $expire = 0;
723 }
724
725 if ( '' === $secure ) {
726 $secure = is_ssl();
727 }
728
729 $secure_password_protected_cookie = apply_filters( 'password_protected_secure_password_protected_cookie', false, $secure );
730 $password_protected_cookie = $this->generate_auth_cookie( $expiration, 'password_protected' );
731
732 $use_transient = get_option( 'password_protected_use_transient', '' );
733 if ( empty( $use_transient ) ) {
734 setcookie( $this->cookie_name(), $password_protected_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_password_protected_cookie, true );
735 if ( COOKIEPATH != SITECOOKIEPATH ) {
736 setcookie( $this->cookie_name(), $password_protected_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, $secure_password_protected_cookie, true );
737 }
738 } else {
739 pp_set_transient( $this->cookie_name(), $password_protected_cookie, $expiration_time );
740 }
741
742 }
743
744 /**
745 * Clear Auth Cookie
746 */
747 public function clear_auth_cookie() {
748 $use_transient = get_option( 'password_protected_use_transient', '' );
749 if ( empty( $use_transient ) ) {
750 setcookie( $this->cookie_name(), ' ', current_time( 'timestamp' ) - 31536000, COOKIEPATH, COOKIE_DOMAIN );
751 setcookie( $this->cookie_name(), ' ', current_time( 'timestamp' ) - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN );
752 } else {
753 pp_delete_transient( $this->cookie_name() );
754 }
755
756 }
757
758 /**
759 * Cookie Name
760 *
761 * @return string Cookie name.
762 */
763 public function cookie_name() {
764
765 /**
766 * Filters the cookie name
767 */
768 return apply_filters( 'password_protected_cookie_name', $this->get_site_id() . '_password_protected_auth', $this );
769
770 }
771
772 /**
773 * Install
774 */
775 public function install() {
776
777 $old_version = get_option( 'password_protected_version' );
778
779 // 1.1 - Upgrade to MD5
780 if ( empty( $old_version ) || $old_version == '1.1' ) {
781 $pwd = get_option( 'password_protected_password' );
782 if ( ! empty( $pwd ) ) {
783 $new_pwd = $this->encrypt_password( $pwd );
784 update_option( 'password_protected_password', $new_pwd );
785 }
786 }
787
788 update_option( 'password_protected_version', $this->version );
789
790 }
791
792 /**
793 * Compat
794 *
795 * Support for 3rd party plugins:
796 *
797 * - Login Logo https://wordpress.org/plugins/login-logo/
798 * - Uber Login Logo https://wordpress.org/plugins/uber-login-logo/
799 */
800 public function compat() {
801
802 if ( class_exists( 'CWS_Login_Logo_Plugin' ) ) {
803
804 // Add support for Mark Jaquith's Login Logo plugin
805 add_action( 'password_protected_login_head', array( new CWS_Login_Logo_Plugin(), 'login_head' ) );
806
807 } elseif ( class_exists( 'UberLoginLogo' ) ) {
808
809 // Add support for Uber Login Logo plugin
810 add_action( 'password_protected_login_head', array( 'UberLoginLogo', 'replaceLoginLogo' ) );
811
812 }
813
814 }
815
816 /**
817 * Login Messages
818 * Outputs messages and errors in the login template.
819 */
820 public function login_messages() {
821
822 // Add message
823 $message = apply_filters( 'password_protected_login_message', '' );
824 if ( ! empty( $message ) ) {
825 echo $message . "\n";
826 }
827
828 if ( $this->errors->get_error_code() ) {
829
830 $errors = '';
831 $messages = '';
832
833 foreach ( $this->errors->get_error_codes() as $code ) {
834 $severity = $this->errors->get_error_data( $code );
835 foreach ( $this->errors->get_error_messages( $code ) as $error ) {
836 if ( 'message' == $severity ) {
837 $messages .= $error . '<br />';
838 } else {
839 $errors .= $error . '<br />';
840 }
841 }
842 }
843
844 if ( ! empty( $errors ) ) {
845 echo '<div id="login_error">' . apply_filters( 'password_protected_login_errors', $errors ) . "</div>\n";
846 }
847 if ( ! empty( $messages ) ) {
848 echo '<p class="message">' . apply_filters( 'password_protected_login_messages', $messages ) . "</p>\n";
849 }
850 }
851
852 }
853
854 /**
855 * Load Theme Stylesheet
856 *
857 * Check wether a 'password-protected-login.css' stylesheet exists in your theme
858 * and if so loads it.
859 *
860 * Works with child themes.
861 *
862 * Possible to specify a different file in the theme folder via the
863 * 'password_protected_stylesheet_file' filter (allows for theme subfolders).
864 */
865 public function load_theme_stylesheet() {
866
867 $filename = apply_filters( 'password_protected_stylesheet_file', 'password-protected-login.css' );
868
869 $located = locate_template( $filename );
870
871 if ( ! empty( $located ) ) {
872
873 $stylesheet_directory = trailingslashit( get_stylesheet_directory() );
874 $template_directory = trailingslashit( get_template_directory() );
875
876 if ( $stylesheet_directory == substr( $located, 0, strlen( $stylesheet_directory ) ) ) {
877 wp_enqueue_style( 'password-protected-login', get_stylesheet_directory_uri() . '/' . $filename );
878 } elseif ( $template_directory == substr( $located, 0, strlen( $template_directory ) ) ) {
879 wp_enqueue_style( 'password-protected-login', get_template_directory_uri() . '/' . $filename );
880 }
881 }
882
883 }
884
885 /**
886 * Safe Redirect
887 *
888 * Ensure the redirect is to the same site or pluggable list of allowed domains.
889 * If invalid will redirect to ...
890 * Based on the WordPress wp_safe_redirect() function.
891 */
892 public function safe_redirect( $location, $status = 302 ) {
893
894 $location = wp_sanitize_redirect( $location );
895 $location = wp_validate_redirect( $location, home_url() );
896
897 wp_redirect( $location, $status );
898
899 }
900
901 /**
902 * Is Plugin Supported?
903 *
904 * Check to see if there are any known reasons why this plugin may not work in
905 * the user's hosting environment.
906 *
907 * @return boolean
908 */
909 static function is_plugin_supported() {
910
911 return true;
912
913 }
914
915 /**
916 * Check whether a given request has permissions
917 *
918 * Always allow logged in users who require REST API for Gutenberg
919 * and other admin/plugin compatibility.
920 *
921 * @param WP_REST_Request $access Full details about the request.
922 * @return WP_Error|boolean
923 */
924 public function only_allow_logged_in_rest_access( $access ) {
925
926 // If user is not logged in
927 if ( $this->is_active() && ! $this->is_user_logged_in() && ! is_user_logged_in() && ! (bool) get_option( 'password_protected_rest' ) ) {
928 return new WP_Error( 'rest_cannot_access', __( 'Only authenticated users can access the REST API.', 'password-protected' ), array( 'status' => rest_authorization_required_code() ) );
929 }
930
931 return $access;
932
933 }
934
935 /**
936 * Print text above password field
937 * @return void.
938 */
939 public function password_protected_above_password_field() {
940 $text = get_option('password_protected_text_above_password');
941 if( !empty($text) )
942 echo '<div class="password-protected-text-above">' . esc_attr( $text ) . '</div>';
943 }
944
945 /**
946 * Print text below password field
947 * @return void.
948 */
949 public function password_protected_below_password_field() {
950 $text = get_option('password_protected_text_below_password');
951 if( !empty($text) )
952 echo '<div class="password-protected-text-below">' . esc_attr( $text ) . '</div>';
953 }
954
955 public function change_addon_to_pro( $translated_text, $text, $domain ) {
956 if ( 'freemius' === $domain ) {
957 if ( 'Add-Ons' == $text ) {
958 return __( '⭐ Get Pro' );
959 }
960 }
961
962 return $translated_text;
963 }
964
965 }
966