-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwc-category-locker.php
113 lines (98 loc) · 2.69 KB
/
wc-category-locker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* Woocommerce Category Locker
*/
class WC_Category_Locker
{
/**
* Constructor
*/
public function __construct()
{
add_action('login_form_postpass', [$this, 'password']);
}
/**
* Triggered password entry
*
* @since 1.0
* @return void
*/
public function password()
{
// extract request
extract($_POST);
// validate fileds
if (empty($wcl_cat_id) || empty($wcl_cat_password)) {
$this->error();
} // TODO: add error message
// get current category id password
$cat_pass = get_woocommerce_term_meta($wcl_cat_id, 'wcl_cat_password', true);
// if password is not valid
if ($cat_pass !== $wcl_cat_password) {
// redirect back
// TODO add error message
$this->error();
} else {
$handle_cookies = $this->handle_cookies($wcl_cat_id);
if ($handle_cookies) {
wp_safe_redirect(wp_get_referer());
exit();
}
}
}
/**
* Handle error error
*
* @param boolean $message
* @since 1.0
* @return void
*/
protected function error($message = false)
{
// redirect back
// TODO: probably add some error message - added attribute already
wp_safe_redirect(wp_get_referer());
exit();
}
/**
* Handle cookies
*
* @param Integer $cat_id
* @since 1.0
* @return void
*/
protected function handle_cookies($cat_id)
{
$cookie = 'wcl_' . md5($cat_id);
$hash = isset($_COOKIE[wp_unslash($cookie)]) ? $_COOKIE[wp_unslash($cookie)] : false;
if (!$hash) {
$this->generate_cat_pass_cookie($cat_id);
}
return true;
}
/**
* Generate enctypted category password cookie
*
* @param Integer $cat_id
* @since 1.0
* @return void
*/
protected function generate_cat_pass_cookie($cat_id)
{
// encrypted cookie
$cat_pass = get_woocommerce_term_meta($cat_id, 'wcl_cat_password', true);
require_once ABSPATH . WPINC . '/class-phpass.php';
$hasher = new PasswordHash(8, true);
$cookie = 'wcl_' . md5($cat_id);
if (!isset($_COOKIE[$cookie])) {
// create cookie for 30min by default
$expire = apply_filters('wcl_password_expires', time() + 30 * 60, COOKIEPATH);
// set cookie
setcookie($cookie, $hasher->HashPassword(wp_unslash($cat_pass)), $expire, COOKIE_DOMAIN, false);
return $cookie;
}
return false;
}
}
// init
$WC_Category_Locker = new WC_Category_Locker();