/wp-content/plugins/w3-total-cache/lib/W3/Plugin/PgCache.php

https://gitlab.com/juanito.abelo/nlmobile · PHP · 348 lines · 217 code · 52 blank · 79 comment · 24 complexity · 6007bce097755e1d701b12df36e5cae9 MD5 · raw file

  1. <?php
  2. /**
  3. * W3 PgCache plugin
  4. */
  5. if (!defined('W3TC')) {
  6. die();
  7. }
  8. w3_require_once(W3TC_LIB_W3_DIR . '/Plugin.php');
  9. /**
  10. * Class W3_Plugin_PgCache
  11. */
  12. class W3_Plugin_PgCache extends W3_Plugin {
  13. /**
  14. * Runs plugin
  15. */
  16. function run() {
  17. add_filter('cron_schedules', array(
  18. &$this,
  19. 'cron_schedules'
  20. ));
  21. if ($this->_config->get_string('pgcache.engine') == 'file' ||
  22. $this->_config->get_string('pgcache.engine') == 'file_generic') {
  23. add_action('w3_pgcache_cleanup', array(
  24. &$this,
  25. 'cleanup'
  26. ));
  27. }
  28. add_action('w3_pgcache_prime', array(
  29. &$this,
  30. 'prime'
  31. ));
  32. add_action('publish_phone', array(
  33. &$this,
  34. 'on_post_edit'
  35. ), 0);
  36. add_action('wp_trash_post', array(
  37. &$this,
  38. 'on_post_change'
  39. ), 0);
  40. add_action('save_post', array(
  41. &$this,
  42. 'on_post_change'
  43. ), 0);
  44. global $wp_version;
  45. if (version_compare($wp_version,'3.5', '>=')) {
  46. add_action('clean_post_cache', array(
  47. &$this,
  48. 'on_post_change'
  49. ), 0, 2);
  50. }
  51. add_action('comment_post', array(
  52. &$this,
  53. 'on_comment_change'
  54. ), 0);
  55. add_action('edit_comment', array(
  56. &$this,
  57. 'on_comment_change'
  58. ), 0);
  59. add_action('delete_comment', array(
  60. &$this,
  61. 'on_comment_change'
  62. ), 0);
  63. add_action('wp_set_comment_status', array(
  64. &$this,
  65. 'on_comment_status'
  66. ), 0, 2);
  67. add_action('trackback_post', array(
  68. &$this,
  69. 'on_comment_change'
  70. ), 0);
  71. add_action('pingback_post', array(
  72. &$this,
  73. 'on_comment_change'
  74. ), 0);
  75. add_action('switch_theme', array(
  76. &$this,
  77. 'on_change'
  78. ), 0);
  79. add_action('wp_update_nav_menu', array(
  80. &$this,
  81. 'on_change'
  82. ), 0);
  83. add_action('edit_user_profile_update', array(
  84. &$this,
  85. 'on_change'
  86. ), 0);
  87. add_filter('comment_cookie_lifetime', array(
  88. &$this,
  89. 'comment_cookie_lifetime'
  90. ));
  91. add_action('w3tc_purge_from_pgcache', array(
  92. &$this,
  93. 'on_post_change'
  94. ), 0);
  95. if (w3_is_multisite()) {
  96. add_action('delete_blog', array(
  97. &$this,
  98. 'on_change'
  99. ), 0);
  100. }
  101. add_action('delete_post', array(
  102. &$this,
  103. 'on_post_edit'
  104. ), 0);
  105. if ($this->_config->get_string('pgcache.engine') == 'file_generic') {
  106. add_action('wp_logout', array (
  107. &$this,
  108. 'on_logout'
  109. ), 0);
  110. add_action('wp_login', array (
  111. &$this,
  112. 'on_login'
  113. ), 0);
  114. }
  115. add_filter('wp_redirect', array(
  116. &$this,
  117. 'on_redirect_cleanup'
  118. ), 0, 1);
  119. add_action('sns_actions_executed', array(
  120. &$this,
  121. 'flush_post_cleanup'
  122. ), 0);
  123. if ($this->_config->get_boolean('pgcache.prime.post.enabled', false)) {
  124. add_action('publish_post', array(
  125. &$this,
  126. 'prime_post'
  127. ), 30);
  128. }
  129. if ($this->_config->get_boolean('pgcache.late_init') && !is_admin()) {
  130. add_action('init', array($this,'delayed_cache_print'), 99999);
  131. }
  132. }
  133. /**
  134. * Does disk cache cleanup
  135. *
  136. * @return void
  137. */
  138. function cleanup() {
  139. $this->_get_admin()->cleanup();
  140. }
  141. /**
  142. * Prime cache
  143. *
  144. * @param integer $start
  145. * @return void
  146. */
  147. function prime($start = 0) {
  148. $this->_get_admin()->prime($start);
  149. }
  150. /**
  151. * Instantiates worker on demand
  152. *
  153. * @return W3_Plugin_PgCacheAdmin
  154. */
  155. private function _get_admin() {
  156. return w3_instance('W3_Plugin_PgCacheAdmin');
  157. }
  158. /**
  159. * Cron schedules filter
  160. *
  161. * @param array $schedules
  162. * @return array
  163. */
  164. function cron_schedules($schedules) {
  165. $gc_interval = $this->_config->get_integer('pgcache.file.gc');
  166. $prime_interval = $this->_config->get_integer('pgcache.prime.interval');
  167. return array_merge($schedules, array(
  168. 'w3_pgcache_cleanup' => array(
  169. 'interval' => $gc_interval,
  170. 'display' => sprintf('[W3TC] Page Cache file GC (every %d seconds)', $gc_interval)
  171. ),
  172. 'w3_pgcache_prime' => array(
  173. 'interval' => $prime_interval,
  174. 'display' => sprintf('[W3TC] Page Cache prime (every %d seconds)', $prime_interval)
  175. )
  176. ));
  177. }
  178. /**
  179. * Post edit action
  180. *
  181. * @param integer $post_id
  182. */
  183. function on_post_edit($post_id) {
  184. if ($this->_config->get_boolean('pgcache.cache.flush')) {
  185. $this->on_change();
  186. } else {
  187. $this->on_post_change($post_id);
  188. }
  189. }
  190. /**
  191. * Post change action
  192. *
  193. * @param integer $post_id
  194. * @param null $post
  195. * @return void
  196. */
  197. function on_post_change($post_id, $post = null) {
  198. static $flushed_posts = array();
  199. if (!in_array($post_id, $flushed_posts)) {
  200. if (is_null($post))
  201. $post = $post_id;
  202. if (!w3_is_flushable_post($post, 'pgcache', $this->_config)) {
  203. return;
  204. }
  205. $w3_cacheflush = w3_instance('W3_CacheFlush');
  206. $w3_cacheflush->pgcache_flush_post($post_id);
  207. $flushed_posts[] = $post_id;
  208. }
  209. }
  210. /**
  211. *
  212. * @param integer $lifetime
  213. * @return integer
  214. */
  215. function comment_cookie_lifetime($lifetime) {
  216. $l = $this->_config->get_integer('pgcache.comment_cookie_ttl');
  217. if ($l != -1)
  218. return $l;
  219. else
  220. return $lifetime;
  221. }
  222. /**
  223. * Comment change action
  224. *
  225. * @param integer $comment_id
  226. */
  227. function on_comment_change($comment_id) {
  228. $post_id = 0;
  229. if ($comment_id) {
  230. $comment = get_comment($comment_id, ARRAY_A);
  231. $post_id = !empty($comment['comment_post_ID']) ? (int) $comment['comment_post_ID'] : 0;
  232. }
  233. $this->on_post_change($post_id);
  234. }
  235. /**
  236. * Comment status action
  237. *
  238. * @param integer $comment_id
  239. * @param string $status
  240. */
  241. function on_comment_status($comment_id, $status) {
  242. if ($status === 'approve' || $status === '1') {
  243. $this->on_comment_change($comment_id);
  244. }
  245. }
  246. /**
  247. * Change action
  248. */
  249. function on_change() {
  250. static $flushed = false;
  251. if (!$flushed) {
  252. $w3_pgcache = w3_instance('W3_CacheFlush');
  253. $w3_pgcache->pgcache_flush();
  254. }
  255. }
  256. /**
  257. * Add cookie on logout to circumvent pagecache due to browser cache resulting in 304s
  258. */
  259. function on_logout() {
  260. setcookie('w3tc_logged_out');
  261. }
  262. /**
  263. * Remove logout cookie on logins
  264. */
  265. function on_login() {
  266. if (isset($_COOKIE['w3tc_logged_out']))
  267. setcookie('w3tc_logged_out', '', 1);
  268. }
  269. /**
  270. * @param $location
  271. * @return mixed
  272. */
  273. function on_redirect_cleanup($location) {
  274. $this->flush_post_cleanup();
  275. return $location;
  276. }
  277. /**
  278. * Runs after multiple post edits
  279. */
  280. function flush_post_cleanup() {
  281. $w3_pgcache = w3_instance('W3_PgCacheFlush');
  282. $w3_pgcache->flush_post_cleanup();
  283. }
  284. /**
  285. * @param $post_id
  286. * @return boolean
  287. */
  288. function prime_post($post_id) {
  289. $w3_pgcache = w3_instance('W3_CacheFlush');
  290. return $w3_pgcache->prime_post($post_id);
  291. }
  292. public function delayed_cache_print() {
  293. $w3_pgcache = w3_instance('W3_PgCache');
  294. $w3_pgcache->delayed_cache_print();
  295. }
  296. }