PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/quick-cache/includes/classes/clearing-routines.inc.php

https://bitbucket.org/sweetperceptions/nyey-everyday
PHP | 277 lines | 203 code | 0 blank | 74 comment | 83 complexity | 358ff370aec1875741b7de44498c1d57 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /*
  3. Copyright: Š 2009 WebSharks, Inc. ( coded in the USA )
  4. <mailto:support@websharks-inc.com> <http://www.websharks-inc.com/>
  5. Released under the terms of the GNU General Public License.
  6. You should have received a copy of the GNU General Public License,
  7. along with this software. In the main directory, see: /licensing/
  8. If not, see: <http://www.gnu.org/licenses/>.
  9. */
  10. if (realpath (__FILE__) === realpath ($_SERVER["SCRIPT_FILENAME"]))
  11. exit ("Do not access this file directly.");
  12. /**/
  13. if (!class_exists ("c_ws_plugin__qcache_clearing_routines"))
  14. {
  15. class c_ws_plugin__qcache_clearing_routines
  16. {
  17. /*
  18. Function clears cache on creations/deletions.
  19. Attach to: add_action("delete_post");
  20. Attach to: add_action("save_post");
  21. Attach to: add_action("edit_post");
  22. */
  23. public static function clear_on_post_page_creations_deletions ($id = FALSE)
  24. {
  25. static $once = false; /* Only clear "all" once. */
  26. global $pagenow; /* This variable holds the current page filename. */
  27. global $current_site, $current_blog; /* Need these for Multisite details. */
  28. /**/
  29. eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
  30. do_action ("ws_plugin__qcache_before_clear_on_post_page_creations_deletions", get_defined_vars ());
  31. unset ($__refs, $__v); /* Unset defined __refs, __v. */
  32. /**/
  33. if (is_blog_admin () && in_array ($pagenow, ($pages = array ("edit.php", "post.php", "post-new.php"))))
  34. {
  35. if ($id && preg_match ("/^single/", $GLOBALS["WS_PLUGIN__"]["qcache"]["o"]["clear_on_update"]))
  36. {
  37. if (($url = get_permalink ($id)) && is_array ($parse = c_ws_plugin__qcache_utils_urls::parse_url ($url)) && ($host_uri = preg_replace ("/^http(s?)\:\/\//i", "", $url)))
  38. {
  39. $host_uri = preg_replace ("/^(" . preg_quote ($parse["host"], "/") . ")(\:[0-9]+)(\/)/i", "$1$3", $host_uri);
  40. /**/
  41. list ($cache) = (array)glob (WP_CONTENT_DIR . "/cache/qc-c-*-" . md5 ($host_uri) . "-*"); /* Match md5_2. */
  42. /**/
  43. if ($cache) /* If a cache file exists for this $host_uri. */
  44. {
  45. if (is_writable ($cache) && unlink ($cache))
  46. {
  47. if (!is_multisite () || !c_ws_plugin__qcache_utils_conds::is_multisite_farm () || is_main_site ())
  48. {
  49. $notice = 'Quick Cache updated: <code>' . esc_html ($host_uri) . '</code> automatically :-)';
  50. c_ws_plugin__qcache_admin_notices::enqueue_admin_notice ($notice, $pages);
  51. }
  52. }
  53. else /* Notify site owners when their /cache directory files are NOT writable. */
  54. {
  55. if (!is_multisite () || !c_ws_plugin__qcache_utils_conds::is_multisite_farm () || is_main_site ())
  56. {
  57. $notice = 'Quick Cache was unable to update: <code>' . esc_html ($host_uri) . '</code>. File not writable.';
  58. c_ws_plugin__qcache_admin_notices::enqueue_admin_notice ($notice, $pages, true);
  59. }
  60. }
  61. }
  62. }
  63. /**/
  64. if (preg_match ("/^single-fp$/", $GLOBALS["WS_PLUGIN__"]["qcache"]["o"]["clear_on_update"]))
  65. {
  66. if (($url = site_url ("/")) && is_array ($parse = c_ws_plugin__qcache_utils_urls::parse_url ($url)) && ($host_uri = preg_replace ("/^http(s?)\:\/\//i", "", $url)))
  67. {
  68. $host_uri = preg_replace ("/^(" . preg_quote ($parse["host"], "/") . ")(\:[0-9]+)(\/)/i", "$1$3", $host_uri);
  69. /**/
  70. list ($cache) = (array)glob (WP_CONTENT_DIR . "/cache/qc-c-*-" . md5 ($host_uri) . "-*"); /* Match md5_2. */
  71. /**/
  72. if ($cache) /* If a cache file exists for this $host_uri. */
  73. {
  74. if (is_writable ($cache) && unlink ($cache))
  75. {
  76. if (!is_multisite () || !c_ws_plugin__qcache_utils_conds::is_multisite_farm () || is_main_site ())
  77. {
  78. $notice = 'Quick Cache updated: <code>' . esc_html ($host_uri) . '</code> automatically :-)';
  79. c_ws_plugin__qcache_admin_notices::enqueue_admin_notice ($notice, $pages);
  80. }
  81. }
  82. else /* Notify site owners when their /cache directory files are NOT writable. */
  83. {
  84. if (!is_multisite () || !c_ws_plugin__qcache_utils_conds::is_multisite_farm () || is_main_site ())
  85. {
  86. $notice = 'Quick Cache was unable to update: <code>' . esc_html ($host_uri) . '</code>. File not writable.';
  87. c_ws_plugin__qcache_admin_notices::enqueue_admin_notice ($notice, $pages, true);
  88. }
  89. }
  90. }
  91. }
  92. }
  93. /**/
  94. do_action ("ws_plugin__qcache_during_clear_on_post_page_creations_deletions", get_defined_vars ());
  95. }
  96. else if (!$once && preg_match ("/^all$/", $GLOBALS["WS_PLUGIN__"]["qcache"]["o"]["clear_on_update"]) && ($once = true))
  97. {
  98. if (is_multisite () && is_object ($current_blog) && $current_blog->blog_id)
  99. {
  100. c_ws_plugin__qcache_purging_routines::schedule_cache_dir_purge ($current_blog);
  101. /**/
  102. if (!is_multisite () || !c_ws_plugin__qcache_utils_conds::is_multisite_farm () || is_main_site ())
  103. {
  104. $notice = 'Blog# <code>' . esc_html ($current_blog->blog_id) . '</code> : Quick Cache reset automatically to avoid conflicts :-)';
  105. c_ws_plugin__qcache_admin_notices::enqueue_admin_notice ($notice, $pages);
  106. }
  107. }
  108. else /* Otherwise, handle this normally. We are NOT in Multisite Mode. */
  109. {
  110. c_ws_plugin__qcache_purging_routines::schedule_cache_dir_purge ();
  111. /**/
  112. if (!is_multisite () || !c_ws_plugin__qcache_utils_conds::is_multisite_farm () || is_main_site ())
  113. {
  114. $notice = 'Quick Cache reset automatically to avoid conflicts :-)';
  115. c_ws_plugin__qcache_admin_notices::enqueue_admin_notice ($notice, $pages);
  116. }
  117. }
  118. /**/
  119. do_action ("ws_plugin__qcache_during_clear_on_post_page_creations_deletions", get_defined_vars ());
  120. }
  121. }
  122. /**/
  123. do_action ("ws_plugin__qcache_after_clear_on_post_page_creations_deletions", get_defined_vars ());
  124. /**/
  125. return; /* Return for uniformity. */
  126. }
  127. /*
  128. Function clears cache on various creations/deletions.
  129. Attach to: add_action("create_term");
  130. Attach to: add_action("edit_terms");
  131. Attach to: add_action("delete_term");
  132. Attach to: add_action("add_link");
  133. Attach to: add_action("edit_link");
  134. Attach to: add_action("delete_link");
  135. */
  136. public static function clear_on_creations_deletions ($term_or_link_id = FALSE)
  137. {
  138. static $once = false; /* Only clear once. */
  139. global $pagenow; /* This variable holds the current page filename. */
  140. global $current_site, $current_blog; /* Need these for Multisite details. */
  141. /**/
  142. eval ('foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v;');
  143. do_action ("ws_plugin__qcache_before_clear_on_creations_deletions", get_defined_vars ());
  144. unset ($__refs, $__v); /* Unset defined __refs, __v. */
  145. /**/
  146. if (is_blog_admin () && in_array ($pagenow, ($pages = array ("edit-tags.php", "link-manager.php", "link.php", "link-add.php", "edit-link-categories.php", "link-category.php"))))
  147. {
  148. if (!$once && preg_match ("/^all$/", $GLOBALS["WS_PLUGIN__"]["qcache"]["o"]["clear_on_update"]) && ($once = true))
  149. {
  150. if (is_multisite () && is_object ($current_blog) && $current_blog->blog_id)
  151. {
  152. c_ws_plugin__qcache_purging_routines::schedule_cache_dir_purge ($current_blog);
  153. /**/
  154. if (!is_multisite () || !c_ws_plugin__qcache_utils_conds::is_multisite_farm () || is_main_site ())
  155. {
  156. $notice = 'Blog# <code>' . esc_html ($current_blog->blog_id) . '</code> : Quick Cache reset automatically to avoid conflicts :-)';
  157. c_ws_plugin__qcache_admin_notices::enqueue_admin_notice ($notice, $pages);
  158. }
  159. }
  160. else /* Otherwise, handle this normally. We are NOT in Multisite Mode. */
  161. {
  162. c_ws_plugin__qcache_purging_routines::schedule_cache_dir_purge ();
  163. /**/
  164. if (!is_multisite () || !c_ws_plugin__qcache_utils_conds::is_multisite_farm () || is_main_site ())
  165. {
  166. $notice = 'Quick Cache reset automatically to avoid conflicts :-)';
  167. c_ws_plugin__qcache_admin_notices::enqueue_admin_notice ($notice, $pages);
  168. }
  169. }
  170. /**/
  171. do_action ("ws_plugin__qcache_during_clear_on_creations_deletions", get_defined_vars ());
  172. }
  173. }
  174. /**/
  175. do_action ("ws_plugin__qcache_after_clear_on_creations_deletions", get_defined_vars ());
  176. /**/
  177. return; /* Return for uniformity. */
  178. }
  179. /*
  180. Function for clearing cache on theme changes.
  181. Attach to: add_action("switch_theme");
  182. The cache is always reset after a theme change,
  183. no matter what setting has been configured. A theme
  184. being changed will always require a cache reset!
  185. */
  186. public static function clear_on_theme_changes ()
  187. {
  188. static $once = false; /* Only clear once. */
  189. global $pagenow; /* Holds the current page filename. */
  190. global $current_site, $current_blog; /* Need these for Multisite details. */
  191. /**/
  192. do_action ("ws_plugin__qcache_before_clear_on_theme_changes", get_defined_vars ());
  193. /**/
  194. if (is_blog_admin () && in_array ($pagenow, ($pages = array ("themes.php"))))
  195. {
  196. if (!$once && ($once = true)) /* Only clear once. */
  197. {
  198. if (is_multisite () && is_object ($current_blog) && $current_blog->blog_id)
  199. {
  200. c_ws_plugin__qcache_purging_routines::schedule_cache_dir_purge ($current_blog);
  201. /**/
  202. if (!is_multisite () || !c_ws_plugin__qcache_utils_conds::is_multisite_farm () || is_main_site ())
  203. {
  204. $notice = 'Blog# <code>' . esc_html ($current_blog->blog_id) . '</code> : Quick Cache reset automatically to avoid conflicts :-)';
  205. c_ws_plugin__qcache_admin_notices::enqueue_admin_notice ($notice, $pages);
  206. }
  207. }
  208. else /* Otherwise, handle this normally. We are NOT in Multisite Mode. */
  209. {
  210. c_ws_plugin__qcache_purging_routines::schedule_cache_dir_purge ();
  211. /**/
  212. if (!is_multisite () || !c_ws_plugin__qcache_utils_conds::is_multisite_farm () || is_main_site ())
  213. {
  214. $notice = 'Quick Cache reset automatically to avoid conflicts :-)';
  215. c_ws_plugin__qcache_admin_notices::enqueue_admin_notice ($notice, $pages);
  216. }
  217. }
  218. /**/
  219. do_action ("ws_plugin__qcache_during_clear_on_theme_changes", get_defined_vars ());
  220. }
  221. }
  222. /**/
  223. do_action ("ws_plugin__qcache_after_clear_on_theme_changes", get_defined_vars ());
  224. /**/
  225. return; /* Return for uniformity. */
  226. }
  227. /*
  228. Function for clearing cache via ajax.
  229. Attach to: add_action("wp_ajax_ws_plugin__qcache_ajax_clear");
  230. */
  231. public static function ajax_clear ()
  232. {
  233. global $current_site, $current_blog; /* Multisite details. */
  234. /**/
  235. do_action ("ws_plugin__qcache_before_ajax_clear", get_defined_vars ());
  236. /**/
  237. status_header (200); /* Send a 200 OK status header. */
  238. header ("Content-Type: text/plain; charset=utf-8"); /* Content-Type text/plain with UTF-8. */
  239. eval ('while (@ob_end_clean ());'); /* End/clean all output buffers that may or may not exist. */
  240. /**/
  241. if (($nonce = $_POST["ws_plugin__qcache_ajax_clear"]) && wp_verify_nonce ($nonce, "ws-plugin--qcache-ajax-clear"))
  242. {
  243. if (is_multisite () && !is_main_site () && is_object ($current_blog) && $current_blog->blog_id)
  244. {
  245. c_ws_plugin__qcache_purging_routines::purge_cache_dir ($current_blog);
  246. /**/
  247. $status = 'Cleared ( this blog )'; /* Indicate "this blog" to the Super Admin. */
  248. /**/
  249. echo "jQuery ('input#ws-plugin--qcache-ajax-clear').css ('background-image', 'url(\'" . c_ws_plugin__qcache_utils_strings::esc_js_sq ($GLOBALS["WS_PLUGIN__"]["qcache"]["c"]["dir_url"], 3) . "/images/ajax-clear.png\')');";
  250. echo "setTimeout (function (){ jQuery ('input#ws-plugin--qcache-ajax-clear').val ('Clear Cache'); }, 2000);";
  251. echo "jQuery ('input#ws-plugin--qcache-ajax-clear').val ('" . c_ws_plugin__qcache_utils_strings::esc_js_sq ($status) . "');";
  252. echo "jQuery ('input#ws-plugin--qcache-ajax-clear').each (function (){ this.blur(); });";
  253. }
  254. else /* Otherwise, handle this normally. We are NOT in Multisite Mode. */
  255. {
  256. c_ws_plugin__qcache_purging_routines::purge_cache_dir ();
  257. /**/
  258. $status = (is_multisite ()) ? 'Cleared ( all blogs )' : '( Cleared )';
  259. /**/
  260. echo "jQuery ('input#ws-plugin--qcache-ajax-clear').css ('background-image', 'url(\'" . c_ws_plugin__qcache_utils_strings::esc_js_sq ($GLOBALS["WS_PLUGIN__"]["qcache"]["c"]["dir_url"], 3) . "/images/ajax-clear.png\')');";
  261. echo "setTimeout (function (){ jQuery ('input#ws-plugin--qcache-ajax-clear').val ('Clear Cache'); }, 2000);";
  262. echo "jQuery ('input#ws-plugin--qcache-ajax-clear').val ('" . c_ws_plugin__qcache_utils_strings::esc_js_sq ($status) . "');";
  263. echo "jQuery ('input#ws-plugin--qcache-ajax-clear').each (function (){ this.blur(); });";
  264. }
  265. /**/
  266. do_action ("ws_plugin__qcache_during_ajax_clear", get_defined_vars ());
  267. }
  268. /**/
  269. do_action ("ws_plugin__qcache_after_ajax_clear", get_defined_vars ());
  270. /**/
  271. exit (); /* Clean exit. */
  272. }
  273. }
  274. }
  275. ?>