PluginProbe
Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content / 2.5.3
Password Protected — Lock Entire Site, Pages, Posts, Categories, and Partial Content v2.5.3
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.5.3, at password-protected.php

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