Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

Commit 509f44f

Browse files
authored
v1.0.1 (#1)
2 parents 5b95047 + 52ff26f commit 509f44f

File tree

2 files changed

+62
-88
lines changed

2 files changed

+62
-88
lines changed

README.md

Lines changed: 35 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -20,88 +20,55 @@ composer require log1x/sage-password-protected
2020

2121
## Usage
2222

23-
Out of the box, this package does absolutely nothing as all values are defaulted to false. To get started, begin passing your values through the provided filters. When passing the password, you must either pass it through `password_hash()` or if using ACF, use my [acf-encrypted-password](https://github.com/log1x/acf-encrypted-password) field.
23+
Out of the box, this package does absolutely nothing as all values are defaulted to false. To get started, begin passing your values in an array through the provided filter. When passing the password, you must either pass it through `password_hash()` or if using ACF, use my [acf-encrypted-password](https://github.com/log1x/acf-encrypted-password) field.
2424

25-
Below are not only the provided filters, but a personal example of how I use them alongside ACF and [ACF Fluent](https://github.com/samrap/acf-fluent).
25+
### Configuration
2626

27-
### Filters
27+
#### Defaults
2828

29-
```php
30-
/**
31-
* Returns true if password protection is enabled.
32-
*
33-
* @return boolean
34-
*/
35-
add_filter('password-protected/isActive', function () {
36-
return Acf::option('password_protected')->get();
37-
});
38-
39-
/**
40-
* Returns the password set for password protection.
41-
*
42-
* @return string
43-
*/
44-
add_filter('password-protected/password', function () {
45-
return Acf::option('password')->get();
46-
});
47-
48-
/**
49-
* Returns true if feeds are allowed while password protection is enabled.
50-
*
51-
* @return boolean
52-
*/
53-
add_filter('password-protected/allowFeeds', function () {
54-
return Acf::option('password_show_feeds')->get();
55-
});
56-
57-
/**
58-
* Returns true if admins are allowed to bypass authentication while password protection is enabled.
59-
*
60-
* @return boolean
61-
*/
62-
add_filter('password-protected/allowAdmins', function () {
63-
return Acf::option('password_allow_administrators')->get();
64-
});
65-
66-
/**
67-
* Returns true if users are allowed to bypass authentication while password protection is enabled.
68-
*
69-
* @return boolean
70-
*/
71-
add_filter('password-protected/allowUsers', function () {
72-
return Acf::option('password_allow_users')->get();
73-
});
74-
75-
/**
76-
* Returns true if specific IP Addresses are allowed to bypass authentication while password protection is enabled.
77-
*
78-
* @return boolean
79-
*/
80-
add_filter('password-protected/allowIpAddresses', function () {
81-
return Acf::option('password_allow_by_ip_address')->get();
82-
});
29+
Below are the default / possible configuration values.
8330

31+
```php
8432
/**
85-
* Returns the IP addresses allowed to bypass authentication while password protection is enabled.
86-
*
33+
* Default configuration for Sage Password Protected
34+
*
8735
* @return array
8836
*/
89-
add_filter('password-protected/allowedIpAddresses', function () {
90-
return Acf::option('password_allowed_ip_addresses')->get();
37+
add_filter('password_protected', function () {
38+
return [
39+
'active' => false,
40+
'password' => false,
41+
'secret' => $this->secret,
42+
'allowFeeds' => false,
43+
'allowAdmins' => false,
44+
'allowUsers' => false,
45+
'allowIpAddresses' => false,
46+
'allowedIpAddresses' => [],
47+
'title' => $this->name()
48+
];
9149
});
9250
```
9351

94-
You can also filter the page title:
52+
#### Example
53+
54+
Below is a personal example of how I handle the configuration alongside ACF and [ACF Fluent](https://github.com/samrap/acf-fluent).
9555

9656
```php
9757
/**
98-
* Returns the page title used for the password protection page.
99-
*
100-
* @param string $name
101-
* @return string
58+
* Configuration for Sage Password Protected.
59+
*
60+
* @return array
10261
*/
103-
add_filter('password-protected/title', function ($name) {
104-
return $name . ' | Protected';
62+
add_filter('password_protected', function () {
63+
return [
64+
'active' => Acf::option('password_protected')->get(),
65+
'password' => Acf::option('password')->get(),
66+
'allowFeeds' => Acf::option('password_show_feeds')->get(),
67+
'allowAdmins' => Acf::option('password_allow_administrators')->get(),
68+
'allowUsers' => Acf::option('password_allow_users')->get(),
69+
'allowIpAddresses' => Acf::option('password_allow_by_ip_address')->get(),
70+
'allowedIpAddresses' => Acf::option('password_allowed_ip_addresses')->get(),
71+
];
10572
});
10673
```
10774

src/PasswordProtected.php

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,19 @@ public function __construct()
5959
add_action('template_redirect', [$this, 'showLogin'], -1);
6060

6161
/** Configuration */
62-
$this->isActive = apply_filters('password-protected/isActive', false);
63-
$this->password = apply_filters('password-protected/password', false);
64-
$this->secret = apply_filters('password-protected/passwordSecret', $this->secret);
65-
$this->allowFeeds = apply_filters('password-protected/allowFeeds', false);
66-
$this->allowAdmins = apply_filters('password-protected/allowAdmins', false);
67-
$this->allowUsers = apply_filters('password-protected/allowUsers', false);
68-
$this->allowIpAddresses = apply_filters('password-protected/allowIpAddresses', false);
69-
$this->allowedIpAddresses = apply_filters('password-protected/allowedIpAddresses', []);
62+
$this->defaults = [
63+
'active' => false,
64+
'password' => false,
65+
'secret' => $this->secret,
66+
'allowFeeds' => false,
67+
'allowAdmins' => false,
68+
'allowUsers' => false,
69+
'allowIpAddresses' => false,
70+
'allowedIpAddresses' => [],
71+
'title' => $this->name()
72+
];
73+
74+
$this->config = (object) wp_parse_args(apply_filters('password_protected', []), $this->defaults);
7075

7176
/** Initialize WP_Error */
7277
$this->errors = new \WP_Error();
@@ -126,7 +131,7 @@ public function processLogin()
126131
*/
127132
public function isActive()
128133
{
129-
if ($this->isActive) {
134+
if ($this->config->active) {
130135
return is_robots() ? false : true;
131136
}
132137

@@ -174,7 +179,7 @@ public function disableFeeds()
174179
*/
175180
protected function allowFeeds()
176181
{
177-
if ($this->allowFeeds && is_feed()) {
182+
if ($this->config->allowFeeds && is_feed()) {
178183
return true;
179184
}
180185

@@ -189,7 +194,7 @@ protected function allowFeeds()
189194
*/
190195
protected function allowAdmins()
191196
{
192-
if (!is_admin() && $this->allowAdmins && current_user_can('manage_options')) {
197+
if (!is_admin() && $this->config->allowAdmins && current_user_can('manage_options')) {
193198
return true;
194199
}
195200

@@ -204,7 +209,7 @@ protected function allowAdmins()
204209
*/
205210
protected function allowUsers()
206211
{
207-
if (!is_admin() && $this->allowUsers && is_user_logged_in()) {
212+
if (!is_admin() && $this->config->allowUsers && is_user_logged_in()) {
208213
return true;
209214
}
210215

@@ -218,7 +223,7 @@ protected function allowUsers()
218223
*/
219224
protected function allowedIpAddress()
220225
{
221-
if ($this->allowIpAddresses && $this->getAllowedIpAddresses()) {
226+
if ($this->config->allowIpAddresses && is_array($this->getAllowedIpAddresses())) {
222227
if (in_array($_SERVER['REMOTE_ADDR'], $this->getAllowedIpAddresses())) {
223228
return true;
224229
}
@@ -234,9 +239,11 @@ protected function allowedIpAddress()
234239
*/
235240
protected function getAllowedIpAddresses()
236241
{
237-
return collect($this->allowedIpAddresses)
242+
return collect($this->config->allowedIpAddresses)
238243
->map(function ($address) {
239-
return $address['ip_address'] ?? $address;
244+
return collect($address)
245+
->filter()
246+
->pop();
240247
})->filter()->toArray();
241248
}
242249

@@ -261,7 +268,7 @@ protected function isAllowed()
261268
*/
262269
protected function getPassword()
263270
{
264-
return $this->password;
271+
return $this->config->password;
265272
}
266273

267274
/**
@@ -346,7 +353,7 @@ public function url()
346353
*/
347354
public function title()
348355
{
349-
return apply_filters('password-protected/title', $this->name());
356+
return $this->config->title;
350357
}
351358

352359
/**
@@ -393,7 +400,7 @@ protected function setCookie()
393400
'agent' => $this->browserAgent() ?? false
394401
]), $this->cipher, $this->secret, false, $this->vector);
395402

396-
return setcookie($this->cookie, $cookie, $this->getCookieDuration(), '/', parse_url(get_home_url(), PHP_URL_HOST), is_ssl(), true);
403+
return setcookie($this->cookie, $cookie, $this->getCookieDuration(), COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
397404
}
398405

399406
/**
@@ -403,7 +410,7 @@ protected function setCookie()
403410
*/
404411
protected function unsetCookie()
405412
{
406-
return setcookie($this->cookie, '', -1, '/', parse_url(get_home_url(), PHP_URL_HOST));
413+
return setcookie($this->cookie, '', -1, COOKIEPATH, COOKIE_DOMAIN);
407414
}
408415

409416
/**
@@ -431,7 +438,7 @@ protected function verifyCookie()
431438
*/
432439
protected function parseCookie()
433440
{
434-
if (!$cookie = openssl_decrypt($this->getCookie(), $this->cipher, $this->secret, false, $this->vector)) {
441+
if (!$cookie = openssl_decrypt($this->getCookie(), $this->cipher, $this->config->secret, false, $this->vector)) {
435442
return false;
436443
}
437444

0 commit comments

Comments
 (0)