class.WpdiscuzCache.php
8.2 KB
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
<?php
if (!defined("ABSPATH")) {
exit();
}
class WpdiscuzCache implements WpDiscuzConstants {
public $wpUploadsDir;
private $options;
private $helper;
public function __construct($options, $helper) {
$this->options = $options;
$this->helper = $helper;
$this->wpUploadsDir = wp_upload_dir();
add_action("admin_post_purgeAllCaches", [&$this, "purgeAllCaches"]);
add_action("admin_post_purgePostCaches", [&$this, "purgePostCaches"]);
add_action("wpdiscuz_reset_users_cache", [&$this, "resetUsersCache"]);
add_action("wpdiscuz_reset_comments_cache", [&$this, "resetCommentsCache"]);
add_action("wpdiscuz_reset_comments_extra_cache", [&$this, "resetExtraCache"]);
add_action("comment_post", [&$this, "commentPost"], 248, 3);
}
public function purgeAllCaches() {
if (current_user_can("manage_options") && isset($_GET["_wpnonce"]) && wp_verify_nonce(sanitize_text_field($_GET["_wpnonce"]), "purgeAllCaches")) {
$this->resetCommentsCache();
$this->resetUsersCache();
}
$referer = wp_get_referer();
if (strpos($referer, "page=" . self::PAGE_SETTINGS) === false) {
$redirect = $referer;
} else {
$redirect = admin_url("admin.php?page=" . self::PAGE_SETTINGS . "&wpd_tab=" . self::TAB_GENERAL);
}
wp_redirect($redirect);
exit();
}
public function purgePostCaches() {
if (current_user_can("manage_options") && isset($_GET["_wpnonce"]) && !empty($_GET["post_id"]) && wp_verify_nonce(sanitize_text_field($_GET["_wpnonce"]), "purgePostCaches")) {
$this->resetCommentsCache(sanitize_text_field($_GET["post_id"]));
$this->resetUsersCache();
}
wp_redirect(wp_get_referer());
exit();
}
public function deleteGravatarsFolder() {
if (!class_exists("WP_Filesystem_Direct")) {
require_once ABSPATH . "wp-admin/includes/class-wp-filesystem-base.php";
require_once ABSPATH . "wp-admin/includes/class-wp-filesystem-direct.php";
}
$fs = new WP_Filesystem_Direct([]);
$fs->rmdir($this->wpUploadsDir["basedir"] . "/wpdiscuz/cache/gravatars/", true);
}
/*
* User and Comment Cache
*/
public function getUserCache($userKey) {
return $this->getCache($this->getUserCacheFileinfo($userKey));
}
public function getCommentsCache($commentsArgs) {
return $this->getCache($this->getCommentsCacheFileinfo($commentsArgs));
}
public function getExtraCache($args) {
return $this->getCache($this->getExtraCacheFileinfo($args));
}
public function setUserCache($userKey, $user) {
unset($user["author_title"], $user["commentWrapRoleClass"]);
return $this->setCache($this->getUserCacheFileinfo($userKey), $user);
}
public function setCommentsCache($commentsArgs, $commentList, $commentData) {
if (!$this->helper->isUnapprovedInTree($commentList)) {
$data = ["commentList" => $commentList, "commentData" => $commentData];
return $this->setCache($this->getCommentsCacheFileinfo($commentsArgs), $data);
}
return false;
}
public function setExtraCache($commentsArgs, $extraData) {
return $this->setCache($this->getExtraCacheFileinfo($commentsArgs), $extraData);
}
public function resetUsersCache($userKey = "") {
if ($userKey) {
$dirs = $this->getUserCacheFileinfo($userKey);
$path = $dirs["path"];
} else {
$dirs = $this->getCacheDirectories();
$path = $dirs["users"];
}
$this->resetCache($path);
}
public function resetCommentsCache($postId = 0) {
$dirs = $this->getCacheDirectories();
$path = $dirs["comments"] . ($postId ? ($postId . "/") : "");
$this->resetCache($path);
}
public function resetExtraCache($postId) {
$dirs = $this->getCacheDirectories();
$path = $dirs["comments"] . $postId . "/" . $dirs["extra"];
$this->resetCache($path);
}
private function getCache($fileInfo) {
// removing stat caches to avoid unexpected results
clearstatcache();
if ($this->options->general["isCacheEnabled"] && file_exists($fileInfo["path"])) {
/**
* delete old cached file
* !!!IMPORTANT
* do not use current_time here as it returns WP time
*/
if (is_file($fileInfo["path"]) && time() > @filemtime($fileInfo["path"]) + ($this->options->general["cacheTimeout"] * DAY_IN_SECONDS)) {
@unlink($fileInfo["path"]);
return [];
}
if (is_readable($fileInfo["path"]) && ($cache = maybe_unserialize(file_get_contents($fileInfo["path"]))) && is_array($cache)) {
return $cache;
}
}
return [];
}
private function setCache($fileInfo, $data) {
if ($this->options->general["isCacheEnabled"]) {
// removing stat caches to avoid unexpected results
clearstatcache();
if (!is_dir($fileInfo["dir"])) {
wp_mkdir_p($fileInfo["dir"]);
}
$htaccces = ".htaccess";
if (is_dir($fileInfo["basedir"]) && !file_exists($fileInfo["basedir"] . $htaccces)) {
file_put_contents($fileInfo["basedir"] . $htaccces, "Deny from all");
}
if (is_writable($fileInfo["dir"]) && ($data = serialize($data))) {
return file_put_contents($fileInfo["path"], $data);
}
}
return false;
}
private function resetCache($path) {
if (!class_exists("WP_Filesystem_Direct")) {
require_once ABSPATH . "wp-admin/includes/class-wp-filesystem-base.php";
require_once ABSPATH . "wp-admin/includes/class-wp-filesystem-direct.php";
}
$fs = new WP_Filesystem_Direct([]);
$fs->rmdir($path, true);
}
private function getUserCacheFileinfo($userKey) {
$dirs = $this->getCacheDirectories();
$fileName = md5($userKey);
return [
"basedir" => $dirs["users"],
"dir" => $dirs["users"],
"name" => $fileName,
"path" => $dirs["users"] . $fileName,
];
}
private function getCommentsCacheFileinfo($commentsArgs) {
$dirs = $this->getCacheDirectories();
$fileDir = $dirs["comments"] . $commentsArgs["post_id"] . "/";
$fileName = md5(implode(",", $commentsArgs["user_roles"]) . $commentsArgs["wpdType"] . $commentsArgs["last_parent_id"] . $commentsArgs["page"] . $commentsArgs["order"] . $commentsArgs["orderby"]) . "_" . $commentsArgs["last_parent_id"];
return [
"basedir" => $dirs["comments"],
"name" => $fileName,
"path" => $fileDir . $fileName,
"dir" => $fileDir,
];
}
private function getExtraCacheFileinfo($commentsArgs) {
$dirs = $this->getCacheDirectories();
$fileDir = $dirs["comments"] . $commentsArgs["post_id"] . "/" . $dirs["extra"] . "/";
$fileName = md5(implode(",", $commentsArgs["user_roles"]) . $commentsArgs["wpdType"] . $commentsArgs["last_parent_id"] . $commentsArgs["page"] . $commentsArgs["order"] . $commentsArgs["orderby"]) . "_" . $commentsArgs["last_parent_id"];
return [
"basedir" => $dirs["extra"],
"name" => $fileName,
"path" => $fileDir . $fileName,
"dir" => $fileDir,
];
}
private function getCacheDirectories() {
return [
"comments" => $this->wpUploadsDir["basedir"] . self::COMMENTS_CACHE_DIR,
"users" => $this->wpUploadsDir["basedir"] . self::USERS_CACHE_DIR,
"extra" => self::EXTRA_CACHE_DIR,
];
}
public function commentPost($comment_ID, $approved, $commentdata) {
if (!empty($commentdata['comment_post_ID'])) {
$this->resetCommentsCache($commentdata['comment_post_ID']);
}
}
}