PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ssellek/saywhatnation.bitbucket
PHP | 440 lines | 257 code | 84 blank | 99 comment | 45 complexity | 6bdb18801139c72579abc87e2f1006a7 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-2.0, GPL-3.0
  1. <?php
  2. /**
  3. * W3 PgCache flushing
  4. */
  5. require_once W3TC_LIB_W3_DIR . '/PgCache.php';
  6. /**
  7. * Class W3_PgCacheFlush
  8. */
  9. class W3_PgCacheFlush extends W3_PgCache {
  10. /**
  11. * PHP5 Constructor
  12. */
  13. function __construct() {
  14. parent::__construct();
  15. }
  16. /**
  17. * PHP4 Constructor
  18. */
  19. function W3_PgCacheFlush() {
  20. $this->__construct();
  21. }
  22. /**
  23. * Flushes all caches
  24. *
  25. * @return boolean
  26. */
  27. function flush() {
  28. $cache = & $this->_get_cache();
  29. return $cache->flush();
  30. }
  31. /**
  32. * Flushes post cache
  33. *
  34. * @param integer $post_id
  35. * @return boolean
  36. */
  37. function flush_post($post_id = null) {
  38. if (!$post_id) {
  39. $post_id = $this->_detect_post_id();
  40. }
  41. if ($post_id) {
  42. $uris = array();
  43. $domain_url = w3_get_domain_url();
  44. $feeds = $this->_config->get_array('pgcache.purge.feed.types');
  45. if ($this->_config->get_boolean('pgcache.purge.terms') || $this->_config->get_boolean('pgcache.purge.feed.terms')) {
  46. $taxonomies = get_post_taxonomies($post_id);
  47. $terms = wp_get_post_terms($post_id, $taxonomies);
  48. }
  49. switch (true) {
  50. case $this->_config->get_boolean('pgcache.purge.author'):
  51. case $this->_config->get_boolean('pgcache.purge.archive.daily'):
  52. case $this->_config->get_boolean('pgcache.purge.archive.monthly'):
  53. case $this->_config->get_boolean('pgcache.purge.archive.yearly'):
  54. case $this->_config->get_boolean('pgcache.purge.feed.author'):
  55. $post = get_post($post_id);
  56. }
  57. /**
  58. * Home URL
  59. */
  60. if ($this->_config->get_boolean('pgcache.purge.home')) {
  61. $home_path = w3_get_home_path();
  62. $site_path = w3_get_site_path();
  63. $uris[] = $home_path;
  64. if ($site_path != $home_path) {
  65. $uris[] = $site_path;
  66. }
  67. }
  68. /**
  69. * Post URL
  70. */
  71. if ($this->_config->get_boolean('pgcache.purge.post')) {
  72. $post_link = post_permalink($post_id);
  73. $post_uri = str_replace($domain_url, '', $post_link);
  74. $uris[] = $post_uri;
  75. }
  76. /**
  77. * Post comments URLs
  78. */
  79. if ($this->_config->get_boolean('pgcache.purge.comments') && function_exists('get_comments_pagenum_link')) {
  80. $comments_number = get_comments_number($post_id);
  81. $comments_per_page = get_option('comments_per_page');
  82. $comments_pages_number = @ceil($comments_number / $comments_per_page);
  83. for ($pagenum = 1; $pagenum <= $comments_pages_number; $pagenum++) {
  84. $comments_pagenum_link = $this->_get_comments_pagenum_link($post_id, $pagenum);
  85. $comments_pagenum_uri = str_replace($domain_url, '', $comments_pagenum_link);
  86. $uris[] = $comments_pagenum_uri;
  87. }
  88. }
  89. /**
  90. * Post author URLs
  91. */
  92. if ($this->_config->get_boolean('pgcache.purge.author') && $post) {
  93. $posts_number = count_user_posts($post->post_author);
  94. $posts_per_page = get_option('posts_per_page');
  95. $posts_pages_number = @ceil($posts_number / $posts_per_page);
  96. $author_link = get_author_link(false, $post->post_author);
  97. $author_uri = str_replace($domain_url, '', $author_link);
  98. for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) {
  99. $author_pagenum_link = $this->_get_pagenum_link($author_uri, $pagenum);
  100. $author_pagenum_uri = str_replace($domain_url, '', $author_pagenum_link);
  101. $uris[] = $author_pagenum_uri;
  102. }
  103. }
  104. /**
  105. * Post terms URLs
  106. */
  107. if ($this->_config->get_boolean('pgcache.purge.terms')) {
  108. $posts_per_page = get_option('posts_per_page');
  109. foreach ($terms as $term) {
  110. $term_link = get_term_link($term, $term->taxonomy);
  111. $term_uri = str_replace($domain_url, '', $term_link);
  112. $posts_pages_number = @ceil($term->count / $posts_per_page);
  113. for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) {
  114. $term_pagenum_link = $this->_get_pagenum_link($term_uri, $pagenum);
  115. $term_pagenum_uri = str_replace($domain_url, '', $term_pagenum_link);
  116. $uris[] = $term_pagenum_uri;
  117. }
  118. }
  119. }
  120. /**
  121. * Daily archive URLs
  122. */
  123. if ($this->_config->get_boolean('pgcache.purge.archive.daily') && $post) {
  124. $post_date = strtotime($post->post_date);
  125. $post_year = gmdate('Y', $post_date);
  126. $post_month = gmdate('m', $post_date);
  127. $post_day = gmdate('d', $post_date);
  128. $posts_per_page = get_option('posts_per_page');
  129. $posts_number = $this->_get_archive_posts_count($post_year, $post_month, $post_day);
  130. $posts_pages_number = @ceil($posts_number / $posts_per_page);
  131. $day_link = get_day_link($post_year, $post_month, $post_day);
  132. $day_uri = str_replace($domain_url, '', $day_link);
  133. for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) {
  134. $day_pagenum_link = $this->_get_pagenum_link($day_uri, $pagenum);
  135. $day_pagenum_uri = str_replace($domain_url, '', $day_pagenum_link);
  136. $uris[] = $day_pagenum_uri;
  137. }
  138. }
  139. /**
  140. * Monthly archive URLs
  141. */
  142. if ($this->_config->get_boolean('pgcache.purge.archive.monthly') && $post) {
  143. $post_date = strtotime($post->post_date);
  144. $post_year = gmdate('Y', $post_date);
  145. $post_month = gmdate('m', $post_date);
  146. $posts_per_page = get_option('posts_per_page');
  147. $posts_number = $this->_get_archive_posts_count($post_year, $post_month);
  148. $posts_pages_number = @ceil($posts_number / $posts_per_page);
  149. $month_link = get_month_link($post_year, $post_month);
  150. $month_uri = str_replace($domain_url, '', $month_link);
  151. for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) {
  152. $month_pagenum_link = $this->_get_pagenum_link($month_uri, $pagenum);
  153. $month_pagenum_uri = str_replace($domain_url, '', $month_pagenum_link);
  154. $uris[] = $month_pagenum_uri;
  155. }
  156. }
  157. /**
  158. * Yearly archive URLs
  159. */
  160. if ($this->_config->get_boolean('pgcache.purge.archive.yearly') && $post) {
  161. $post_date = strtotime($post->post_date);
  162. $post_year = gmdate('Y', $post_date);
  163. $posts_per_page = get_option('posts_per_page');
  164. $posts_number = $this->_get_archive_posts_count($post_year);
  165. $posts_pages_number = @ceil($posts_number / $posts_per_page);
  166. $year_link = get_year_link($post_year);
  167. $year_uri = str_replace($domain_url, '', $year_link);
  168. for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) {
  169. $year_pagenum_link = $this->_get_pagenum_link($year_uri, $pagenum);
  170. $year_pagenum_uri = str_replace($domain_url, '', $year_pagenum_link);
  171. $uris[] = $year_pagenum_uri;
  172. }
  173. }
  174. /**
  175. * Feed URLs
  176. */
  177. if ($this->_config->get_boolean('pgcache.purge.feed.blog')) {
  178. foreach ($feeds as $feed) {
  179. $feed_link = get_feed_link($feed);
  180. $feed_uri = str_replace($domain_url, '', $feed_link);
  181. $uris[] = $feed_uri;
  182. }
  183. }
  184. if ($this->_config->get_boolean('pgcache.purge.feed.comments')) {
  185. foreach ($feeds as $feed) {
  186. $post_comments_feed_link = get_post_comments_feed_link($post_id, $feed);
  187. $post_comments_feed_uri = str_replace($domain_url, '', $post_comments_feed_link);
  188. $uris[] = $post_comments_feed_uri;
  189. }
  190. }
  191. if ($this->_config->get_boolean('pgcache.purge.feed.author') && $post) {
  192. foreach ($feeds as $feed) {
  193. $author_feed_link = get_author_feed_link($post->post_author, $feed);
  194. $author_feed_uri = str_replace($domain_url, '', $author_feed_link);
  195. $uris[] = $author_feed_uri;
  196. }
  197. }
  198. if ($this->_config->get_boolean('pgcache.purge.feed.terms')) {
  199. foreach ($terms as $term) {
  200. foreach ($feeds as $feed) {
  201. $term_feed_link = get_term_feed_link($term->term_id, $term->taxonomy, $feed);
  202. $term_feed_uri = str_replace($domain_url, '', $term_feed_link);
  203. $uris[] = $term_feed_uri;
  204. }
  205. }
  206. }
  207. /**
  208. * Flush cache
  209. */
  210. if (count($uris)) {
  211. $cache = & $this->_get_cache();
  212. $mobile_groups = $this->_get_mobile_groups();
  213. $referrer_groups = $this->_get_referrer_groups();
  214. $encryptions = $this->_get_encryptions();
  215. $compressions = $this->_get_compressions();
  216. foreach ($uris as $uri) {
  217. foreach ($mobile_groups as $mobile_group) {
  218. foreach ($referrer_groups as $referrer_group) {
  219. foreach ($encryptions as $encryption) {
  220. foreach ($compressions as $compression) {
  221. $page_key = $this->_get_page_key($uri, $mobile_group, $referrer_group, $encryption, $compression);
  222. $cache->delete($page_key);
  223. }
  224. }
  225. }
  226. }
  227. }
  228. /**
  229. * Purge varnish servers
  230. */
  231. if ($this->_config->get_boolean('varnish.enabled')) {
  232. $varnish = & w3_instance('W3_Varnish');
  233. foreach ($uris as $uri) {
  234. $varnish->purge($uri);
  235. }
  236. }
  237. }
  238. return true;
  239. }
  240. return false;
  241. }
  242. /**
  243. * Returns array of mobile groups
  244. *
  245. * @return array
  246. */
  247. function _get_mobile_groups() {
  248. $mobile_groups = array('');
  249. if ($this->_mobile) {
  250. $mobile_groups = array_merge($mobile_groups, array_keys($this->_mobile->groups));
  251. }
  252. return $mobile_groups;
  253. }
  254. /**
  255. * Returns array of referrer groups
  256. *
  257. * @return array
  258. */
  259. function _get_referrer_groups() {
  260. $referrer_groups = array('');
  261. if ($this->_referrer) {
  262. $referrer_groups = array_merge($referrer_groups, array_keys($this->_referrer->groups));
  263. }
  264. return $referrer_groups;
  265. }
  266. /**
  267. * Returns array of encryptions
  268. *
  269. * @return array
  270. */
  271. function _get_encryptions() {
  272. return array(false, 'ssl');
  273. }
  274. /**
  275. * Detects post ID
  276. *
  277. * @return integer
  278. */
  279. function _detect_post_id() {
  280. global $posts, $comment_post_ID, $post_ID;
  281. if ($post_ID) {
  282. return $post_ID;
  283. } elseif ($comment_post_ID) {
  284. return $comment_post_ID;
  285. } elseif (is_single() || is_page() && count($posts)) {
  286. return $posts[0]->ID;
  287. } elseif (isset($_REQUEST['p'])) {
  288. return (integer) $_REQUEST['p'];
  289. }
  290. return 0;
  291. }
  292. /**
  293. * Workaround for get_pagenum_link function
  294. *
  295. * @param string $url
  296. * @param int $pagenum
  297. * @return string
  298. */
  299. function _get_pagenum_link($url, $pagenum = 1) {
  300. $request_uri = $_SERVER['REQUEST_URI'];
  301. $_SERVER['REQUEST_URI'] = $url;
  302. $link = get_pagenum_link($pagenum);
  303. $_SERVER['REQUEST_URI'] = $request_uri;
  304. return $link;
  305. }
  306. /**
  307. * Workaround for get_comments_pagenum_link function
  308. *
  309. * @param integer $post_id
  310. * @param integer $pagenum
  311. * @param integer $max_page
  312. * @return string
  313. */
  314. function _get_comments_pagenum_link($post_id, $pagenum = 1, $max_page = 0) {
  315. if (isset($GLOBALS['post']) && is_object($GLOBALS['post'])) {
  316. $old_post = &$GLOBALS['post'];
  317. } else {
  318. @$GLOBALS['post'] = & new stdClass();
  319. $old_post = null;
  320. }
  321. $GLOBALS['post']->ID = $post_id;
  322. $link = get_comments_pagenum_link($pagenum, $max_page);
  323. if ($old_post) {
  324. $GLOBALS['post'] = &$old_post;
  325. }
  326. return $link;
  327. }
  328. /**
  329. * Returns number of posts in the archive
  330. *
  331. * @param int $year
  332. * @param int $month
  333. * @param int $day
  334. * @return int
  335. */
  336. function _get_archive_posts_count($year = 0, $month = 0, $day = 0) {
  337. global $wpdb;
  338. $filters = array(
  339. 'post_type = "post"',
  340. 'post_status = "publish"'
  341. );
  342. if ($year) {
  343. $filters[] = sprintf('YEAR(post_date) = %d', $year);
  344. }
  345. if ($month) {
  346. $filters[] = sprintf('MONTH(post_date) = %d', $month);
  347. }
  348. if ($day) {
  349. $filters[] = sprintf('DAY(post_date) = %d', $day);
  350. }
  351. $where = implode(' AND ', $filters);
  352. $sql = sprintf('SELECT COUNT(*) FROM %s WHERE %s', $wpdb->posts, $where);
  353. $count = (int) $wpdb->get_var($sql);
  354. return $count;
  355. }
  356. }