/wp-content/plugins/wpdiscuz/utils/class.WpdiscuzCache.php

https://github.com/livinglab/openlab · PHP · 212 lines · 174 code · 28 blank · 10 comment · 28 complexity · 34da8bfb3f75eb1fb700c4a8ecac6533 MD5 · raw file

  1. <?php
  2. if (!defined("ABSPATH")) {
  3. exit();
  4. }
  5. class WpdiscuzCache implements WpDiscuzConstants {
  6. public $wpUploadsDir;
  7. private $options;
  8. private $helper;
  9. public function __construct($options, $helper) {
  10. $this->options = $options;
  11. $this->helper = $helper;
  12. $this->wpUploadsDir = wp_upload_dir();
  13. add_action("admin_post_purgeAllCaches", [&$this, "purgeAllCaches"]);
  14. add_action("admin_post_purgePostCaches", [&$this, "purgePostCaches"]);
  15. add_action("wpdiscuz_reset_users_cache", [&$this, "resetUsersCache"]);
  16. add_action("wpdiscuz_reset_comments_cache", [&$this, "resetCommentsCache"]);
  17. add_action("wpdiscuz_reset_comments_extra_cache", [&$this, "resetExtraCache"]);
  18. add_action("comment_post", [&$this, "commentPost"], 248, 3);
  19. }
  20. public function purgeAllCaches() {
  21. if (current_user_can("manage_options") && isset($_GET["_wpnonce"]) && wp_verify_nonce(sanitize_text_field($_GET["_wpnonce"]), "purgeAllCaches")) {
  22. $this->resetCommentsCache();
  23. $this->resetUsersCache();
  24. }
  25. $referer = wp_get_referer();
  26. if (strpos($referer, "page=" . self::PAGE_SETTINGS) === false) {
  27. $redirect = $referer;
  28. } else {
  29. $redirect = admin_url("admin.php?page=" . self::PAGE_SETTINGS . "&wpd_tab=" . self::TAB_GENERAL);
  30. }
  31. wp_redirect($redirect);
  32. exit();
  33. }
  34. public function purgePostCaches() {
  35. if (current_user_can("manage_options") && isset($_GET["_wpnonce"]) && !empty($_GET["post_id"]) && wp_verify_nonce(sanitize_text_field($_GET["_wpnonce"]), "purgePostCaches")) {
  36. $this->resetCommentsCache(sanitize_text_field($_GET["post_id"]));
  37. $this->resetUsersCache();
  38. }
  39. wp_redirect(wp_get_referer());
  40. exit();
  41. }
  42. public function deleteGravatarsFolder() {
  43. if (!class_exists("WP_Filesystem_Direct")) {
  44. require_once ABSPATH . "wp-admin/includes/class-wp-filesystem-base.php";
  45. require_once ABSPATH . "wp-admin/includes/class-wp-filesystem-direct.php";
  46. }
  47. $fs = new WP_Filesystem_Direct([]);
  48. $fs->rmdir($this->wpUploadsDir["basedir"] . "/wpdiscuz/cache/gravatars/", true);
  49. }
  50. /*
  51. * User and Comment Cache
  52. */
  53. public function getUserCache($userKey) {
  54. return $this->getCache($this->getUserCacheFileinfo($userKey));
  55. }
  56. public function getCommentsCache($commentsArgs) {
  57. return $this->getCache($this->getCommentsCacheFileinfo($commentsArgs));
  58. }
  59. public function getExtraCache($args) {
  60. return $this->getCache($this->getExtraCacheFileinfo($args));
  61. }
  62. public function setUserCache($userKey, $user) {
  63. unset($user["author_title"], $user["commentWrapRoleClass"]);
  64. return $this->setCache($this->getUserCacheFileinfo($userKey), $user);
  65. }
  66. public function setCommentsCache($commentsArgs, $commentList, $commentData) {
  67. if (!$this->helper->isUnapprovedInTree($commentList)) {
  68. $data = ["commentList" => $commentList, "commentData" => $commentData];
  69. return $this->setCache($this->getCommentsCacheFileinfo($commentsArgs), $data);
  70. }
  71. return false;
  72. }
  73. public function setExtraCache($commentsArgs, $extraData) {
  74. return $this->setCache($this->getExtraCacheFileinfo($commentsArgs), $extraData);
  75. }
  76. public function resetUsersCache($userKey = "") {
  77. if ($userKey) {
  78. $dirs = $this->getUserCacheFileinfo($userKey);
  79. $path = $dirs["path"];
  80. } else {
  81. $dirs = $this->getCacheDirectories();
  82. $path = $dirs["users"];
  83. }
  84. $this->resetCache($path);
  85. }
  86. public function resetCommentsCache($postId = 0) {
  87. $dirs = $this->getCacheDirectories();
  88. $path = $dirs["comments"] . ($postId ? ($postId . "/") : "");
  89. $this->resetCache($path);
  90. }
  91. public function resetExtraCache($postId) {
  92. $dirs = $this->getCacheDirectories();
  93. $path = $dirs["comments"] . $postId . "/" . $dirs["extra"];
  94. $this->resetCache($path);
  95. }
  96. private function getCache($fileInfo) {
  97. // removing stat caches to avoid unexpected results
  98. clearstatcache();
  99. if ($this->options->general["isCacheEnabled"] && file_exists($fileInfo["path"])) {
  100. /**
  101. * delete old cached file
  102. * !!!IMPORTANT
  103. * do not use current_time here as it returns WP time
  104. */
  105. if (is_file($fileInfo["path"]) && time() > @filemtime($fileInfo["path"]) + ($this->options->general["cacheTimeout"] * DAY_IN_SECONDS)) {
  106. @unlink($fileInfo["path"]);
  107. return [];
  108. }
  109. if (is_readable($fileInfo["path"]) && ($cache = maybe_unserialize(file_get_contents($fileInfo["path"])))) {
  110. return $cache;
  111. }
  112. }
  113. return [];
  114. }
  115. private function setCache($fileInfo, $data) {
  116. if ($this->options->general["isCacheEnabled"]) {
  117. // removing stat caches to avoid unexpected results
  118. clearstatcache();
  119. if (!is_dir($fileInfo["dir"])) {
  120. wp_mkdir_p($fileInfo["dir"]);
  121. }
  122. $htaccces = ".htaccess";
  123. if (is_dir($fileInfo["basedir"]) && !file_exists($fileInfo["basedir"] . $htaccces)) {
  124. file_put_contents($fileInfo["basedir"] . $htaccces, "Deny from all");
  125. }
  126. if (is_writable($fileInfo["dir"]) && ($data = serialize($data))) {
  127. return file_put_contents($fileInfo["path"], $data);
  128. }
  129. }
  130. return false;
  131. }
  132. private function resetCache($path) {
  133. if (!class_exists("WP_Filesystem_Direct")) {
  134. require_once ABSPATH . "wp-admin/includes/class-wp-filesystem-base.php";
  135. require_once ABSPATH . "wp-admin/includes/class-wp-filesystem-direct.php";
  136. }
  137. $fs = new WP_Filesystem_Direct([]);
  138. $fs->rmdir($path, true);
  139. }
  140. private function getUserCacheFileinfo($userKey) {
  141. $dirs = $this->getCacheDirectories();
  142. $fileName = md5($userKey);
  143. return [
  144. "basedir" => $dirs["users"],
  145. "dir" => $dirs["users"],
  146. "name" => $fileName,
  147. "path" => $dirs["users"] . $fileName,
  148. ];
  149. }
  150. private function getCommentsCacheFileinfo($commentsArgs) {
  151. $dirs = $this->getCacheDirectories();
  152. $fileDir = $dirs["comments"] . $commentsArgs["post_id"] . "/";
  153. $fileName = md5(implode(",", $commentsArgs["user_roles"]) . $commentsArgs["wpdType"] . $commentsArgs["last_parent_id"] . $commentsArgs["page"] . $commentsArgs["order"] . $commentsArgs["orderby"]) . "_" . $commentsArgs["last_parent_id"];
  154. return [
  155. "basedir" => $dirs["comments"],
  156. "name" => $fileName,
  157. "path" => $fileDir . $fileName,
  158. "dir" => $fileDir,
  159. ];
  160. }
  161. private function getExtraCacheFileinfo($commentsArgs) {
  162. $dirs = $this->getCacheDirectories();
  163. $fileDir = $dirs["comments"] . $commentsArgs["post_id"] . "/" . $dirs["extra"] . "/";
  164. $fileName = md5(implode(",", $commentsArgs["user_roles"]) . $commentsArgs["wpdType"] . $commentsArgs["last_parent_id"] . $commentsArgs["page"] . $commentsArgs["order"] . $commentsArgs["orderby"]) . "_" . $commentsArgs["last_parent_id"];
  165. return [
  166. "basedir" => $dirs["extra"],
  167. "name" => $fileName,
  168. "path" => $fileDir . $fileName,
  169. "dir" => $fileDir,
  170. ];
  171. }
  172. private function getCacheDirectories() {
  173. return [
  174. "comments" => $this->wpUploadsDir["basedir"] . self::COMMENTS_CACHE_DIR,
  175. "users" => $this->wpUploadsDir["basedir"] . self::USERS_CACHE_DIR,
  176. "extra" => self::EXTRA_CACHE_DIR,
  177. ];
  178. }
  179. public function commentPost($comment_ID, $approved, $commentdata) {
  180. if (!empty($commentdata['comment_post_ID'])) {
  181. $this->resetCommentsCache($commentdata['comment_post_ID']);
  182. }
  183. }
  184. }