-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfrontend.php
219 lines (187 loc) · 6.23 KB
/
frontend.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* Front-end
*/
class WC_Category_Locker_Frontend
{
/**
* Constructor
*/
public function __construct()
{
add_action('pre_get_posts', [$this, 'password'], 10);
add_action('pre_get_posts', [$this, 'update_shop_queries'], 10);
add_action('template_redirect', [$this, 'redirect_from_locked_product']);
}
/**
* Get cookies
*
* @since 1.0
* @return void
*/
public function get_cookies()
{
// loop thorugh the cookies and ones with our prefix put in to
// new array which we then return`
$wcl_cookies = [];
foreach ($_COOKIE as $ec => $ec_val) {
if (strpos($ec, 'wcl_') !== false) {
$wcl_cookies[$ec] = $ec_val;
}
}
return $wcl_cookies;
}
/**
* Front end password handling
*
* @param object $query
* @since 1.0
* @return void
*/
public function password($query)
{
// don't run if it's admin
if (is_admin()) {
return;
}
// make sure current category is "product_cat"
if (!isset(get_queried_object()->taxonomy) || (!isset(get_queried_object()->taxonomy) && (get_queried_object()->taxonomy !== 'product_cat'))) {
return;
}
// make sure temr id is set / that the page is actually a category
if (isset(get_queried_object()->term_id)) {
$is_password_protected = get_woocommerce_term_meta(get_queried_object()->term_id, 'wcl_cat_password_protected');
if ($is_password_protected) {
$cookie = 'wcl_' . md5(get_queried_object()->term_id);
$hash = isset($_COOKIE[wp_unslash($cookie)]) ? $_COOKIE[wp_unslash($cookie)] : false;
if (!$hash) {
add_filter('template_include', [$this, 'replace_template']);
} else {
// get current category id password
$cat_pass = get_woocommerce_term_meta(get_queried_object()->term_id, 'wcl_cat_password', true);
// decrypt cookie
require_once ABSPATH . WPINC . '/class-phpass.php';
$hasher = new PasswordHash(8, true);
$check = $hasher->CheckPassword($cat_pass, $hash);
if ($check) {
return;
} else {
add_filter('template_include', [$this, 'replace_template']);
}
}
}
}
}
/**
* Replace tempalte with password form
*
* @param string $template
* @since 1.0
* @return void
*/
public function replace_template($template)
{
// see if tempalte exists in the theme
$located = locate_template('woocommerce/password-form.php');
if (!empty($located)) {
// if yes, use theme template
$template = get_template_directory() . '/woocommerce/password-form.php';
} else {
// otherwise use default plugin template
$template = WCL_PLUGIN_DIR . '/templates/password-form.php';
}
return $template;
}
/**
* Update shop queries
*
* @param object $query
* @return void
*/
public function update_shop_queries($query)
{
// don't run if it's admin
if (is_admin()) {
return;
}
// make sure it's main query
if (!$query->is_main_query()) {
return;
}
// make sure its archive page
if (!$query->is_post_type_archive()) {
return;
}
// get locked categories / taxonomies
$locked = wcl_get_locked_categories();
// set query to exclude locked ones
$query->set('tax_query', [[
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $locked,
'operator' => 'NOT IN'
]]);
return $query;
}
/**
* Redirect from locked product
*
* @since 1.0
* @return void
*/
public function redirect_from_locked_product()
{
global $post;
// make sure we can access $post global to prevent errors
if (!isset($post)) {
return false;
}
// if it's not product page, we don't need to check further
if (!is_product()) {
return false;
}
// get terms of current "post" / "page"
$terms = get_the_terms($post->ID, 'product_cat');
// make sure this "post" has product categories
if (!empty($terms)) {
$product_cat_ids = [];
foreach ($terms as $term) {
$product_cat_ids[] = $term->term_id;
}
// see if product has locked category
$locked = wcl_get_locked_categories();
// intersect both arrays
$result_intersect = array_intersect($locked, $product_cat_ids);
// if it doesn't belong to locked category return
if (empty($result_intersect)) {
return;
}
// tidy up our array, make sure it starts form 0
$result = array_values($result_intersect);
// check for cookie hash
$cookie = 'wcl_' . md5($result[0]);
$hash = isset($_COOKIE[wp_unslash($cookie)]) ? $_COOKIE[wp_unslash($cookie)] : false;
if (!$hash) {
nocache_headers();
wp_safe_redirect(get_term_link($result[0]));
exit();
} else {
// get current category id password
$cat_pass = get_woocommerce_term_meta($result[0], 'wcl_cat_password', true);
// decrypt cookie
require_once ABSPATH . WPINC . '/class-phpass.php';
$hasher = new PasswordHash(8, true);
$check = $hasher->CheckPassword($cat_pass, $hash);
if ($check) {
return;
} else {
nocache_headers();
wp_safe_redirect(get_term_link($result[0]));
exit();
}
}
}
}
}
// init
$WC_Category_Locker_Frontend = new WC_Category_Locker_Frontend();