/content/plugins/w3-total-cache/PgCache_Flush.php

https://gitlab.com/karlen/ayo_wp · PHP · 384 lines · 227 code · 59 blank · 98 comment · 46 complexity · d1d8c205a8dd596aadb6f064d600035c MD5 · raw file

  1. <?php
  2. namespace W3TC;
  3. /**
  4. * W3 PgCache flushing
  5. */
  6. class PgCache_Flush extends PgCache_ContentGrabber {
  7. /**
  8. * Array of urls to flush
  9. *
  10. * @var array
  11. */
  12. private $queued_urls = array();
  13. private $flush_operation_requested = false;
  14. /**
  15. * PHP5 Constructor
  16. */
  17. function __construct() {
  18. parent::__construct();
  19. }
  20. /**
  21. * Flushes all caches
  22. *
  23. * @return boolean
  24. */
  25. function flush() {
  26. $this->flush_operation_requested = true;
  27. return true;
  28. }
  29. /**
  30. * Flushes post cache
  31. *
  32. * @param integer $post_id
  33. * @return boolean
  34. */
  35. function flush_post( $post_id = null ) {
  36. if ( !$post_id ) {
  37. $post_id = Util_Environment::detect_post_id();
  38. }
  39. if ( !$post_id )
  40. return false;
  41. $full_urls = array();
  42. $post = null;
  43. $terms = array();
  44. $feeds = $this->_config->get_array( 'pgcache.purge.feed.types' );
  45. $limit_post_pages = $this->_config->get_integer( 'pgcache.purge.postpages_limit' );
  46. if ( $this->_config->get_boolean( 'pgcache.purge.terms' ) ||
  47. $this->_config->get_boolean( 'pgcache.purge.feed.terms' ) ) {
  48. $taxonomies = get_post_taxonomies( $post_id );
  49. $terms = wp_get_post_terms( $post_id, $taxonomies );
  50. $terms = $this->_append_parent_terms( $terms, $terms );
  51. }
  52. $post = get_post( $post_id );
  53. $post_type = in_array( $post->post_type, array(
  54. 'post', 'page', 'attachment', 'revision' ) ) ? null : $post->post_type;
  55. $front_page = get_option( 'show_on_front' );
  56. /**
  57. * Home (Frontpage) URL
  58. */
  59. if ( ( $this->_config->get_boolean( 'pgcache.purge.home' ) &&
  60. $front_page == 'posts' ) ||
  61. $this->_config->get_boolean( 'pgcache.purge.front_page' ) ) {
  62. $full_urls = array_merge( $full_urls,
  63. Util_PageUrls::get_frontpage_urls( $limit_post_pages ) );
  64. }
  65. /**
  66. * Home (Post page) URL
  67. */
  68. if ( $this->_config->get_boolean( 'pgcache.purge.home' ) &&
  69. $front_page != 'posts' ) {
  70. $full_urls = array_merge( $full_urls,
  71. Util_PageUrls::get_postpage_urls( $limit_post_pages ) );
  72. }
  73. /**
  74. * Post URL
  75. */
  76. if ( $this->_config->get_boolean( 'pgcache.purge.post' ) ) {
  77. $full_urls = array_merge( $full_urls,
  78. Util_PageUrls::get_post_urls( $post_id ) );
  79. }
  80. /**
  81. * Post comments URLs
  82. */
  83. if ( $this->_config->get_boolean( 'pgcache.purge.comments' ) &&
  84. function_exists( 'get_comments_pagenum_link' ) ) {
  85. $full_urls = array_merge( $full_urls,
  86. Util_PageUrls::get_post_comments_urls( $post_id ) );
  87. }
  88. /**
  89. * Post author URLs
  90. */
  91. if ( $this->_config->get_boolean( 'pgcache.purge.author' ) ) {
  92. $full_urls = array_merge( $full_urls,
  93. Util_PageUrls::get_post_author_urls( $post->post_author,
  94. $limit_post_pages ) );
  95. }
  96. /**
  97. * Post terms URLs
  98. */
  99. if ( $this->_config->get_boolean( 'pgcache.purge.terms' ) ) {
  100. $full_urls = array_merge( $full_urls,
  101. Util_PageUrls::get_post_terms_urls( $terms, $limit_post_pages ) );
  102. }
  103. /**
  104. * Daily archive URLs
  105. */
  106. if ( $this->_config->get_boolean( 'pgcache.purge.archive.daily' ) && $post ) {
  107. $full_urls = array_merge( $full_urls,
  108. Util_PageUrls::get_daily_archive_urls( $post, $limit_post_pages ) );
  109. }
  110. /**
  111. * Monthly archive URLs
  112. */
  113. if ( $this->_config->get_boolean( 'pgcache.purge.archive.monthly' ) && $post ) {
  114. $full_urls = array_merge( $full_urls,
  115. Util_PageUrls::get_monthly_archive_urls( $post, $limit_post_pages ) );
  116. }
  117. /**
  118. * Yearly archive URLs
  119. */
  120. if ( $this->_config->get_boolean( 'pgcache.purge.archive.yearly' ) && $post ) {
  121. $full_urls = array_merge( $full_urls,
  122. Util_PageUrls::get_yearly_archive_urls( $post, $limit_post_pages ) );
  123. }
  124. /**
  125. * Feed URLs
  126. */
  127. if ( $this->_config->get_boolean( 'pgcache.purge.feed.blog' ) ) {
  128. $full_urls = array_merge( $full_urls,
  129. Util_PageUrls::get_feed_urls( $feeds, $post_type ) );
  130. }
  131. if ( $this->_config->get_boolean( 'pgcache.purge.feed.comments' ) ) {
  132. $full_urls = array_merge( $full_urls,
  133. Util_PageUrls::get_feed_comments_urls( $post_id, $feeds ) );
  134. }
  135. if ( $this->_config->get_boolean( 'pgcache.purge.feed.author' ) ) {
  136. $full_urls = array_merge( $full_urls,
  137. Util_PageUrls::get_feed_author_urls( $post->post_author, $feeds ) );
  138. }
  139. if ( $this->_config->get_boolean( 'pgcache.purge.feed.terms' ) ) {
  140. $full_urls = array_merge( $full_urls,
  141. Util_PageUrls::get_feed_terms_urls( $terms, $feeds ) );
  142. }
  143. /**
  144. * Purge selected pages
  145. */
  146. if ( $this->_config->get_array( 'pgcache.purge.pages' ) ) {
  147. $pages = $this->_config->get_array( 'pgcache.purge.pages' );
  148. $full_urls = array_merge( $full_urls,
  149. Util_PageUrls::get_pages_urls( $pages ) );
  150. }
  151. /**
  152. * Queue flush
  153. */
  154. if ( count( $full_urls ) ) {
  155. foreach ( $full_urls as $url )
  156. $this->queued_urls[$url] = '*';
  157. }
  158. // add mirror urls
  159. $this->queued_urls = Util_PageUrls::complement_with_mirror_urls(
  160. $this->queued_urls );
  161. return true;
  162. }
  163. /**
  164. * Flush a single url
  165. *
  166. * @param unknown $url
  167. * @param unknown $cache
  168. * @param unknown $mobile_groups
  169. * @param unknown $referrer_groups
  170. * @param unknown $encryptions
  171. * @param unknown $compressions
  172. */
  173. function _flush_url( $url, $cache, $mobile_groups, $referrer_groups,
  174. $encryptions, $compressions ) {
  175. foreach ( $mobile_groups as $mobile_group ) {
  176. foreach ( $referrer_groups as $referrer_group ) {
  177. foreach ( $encryptions as $encryption ) {
  178. foreach ( $compressions as $compression ) {
  179. $page_keys = array();
  180. $page_keys[] = $this->_get_page_key( $mobile_group,
  181. $referrer_group, $encryption, $compression, false,
  182. $url );
  183. $page_keys = apply_filters(
  184. 'w3tc_pagecache_flush_url_keys', $page_keys );
  185. foreach ( $page_keys as $page_key )
  186. $cache->delete( $page_key );
  187. }
  188. }
  189. }
  190. }
  191. }
  192. /**
  193. * Flush a single url
  194. *
  195. * @param unknown $url
  196. */
  197. function flush_url( $url ) {
  198. static $cache, $mobile_groups, $referrer_groups, $encryptions;
  199. static $compressions;
  200. if ( !isset( $cache ) )
  201. $cache = $this->_get_cache();
  202. if ( !isset( $mobile_groups ) )
  203. $mobile_groups = $this->_get_mobile_groups();
  204. if ( !isset( $referrer_groups ) )
  205. $referrer_groups = $this->_get_referrer_groups();
  206. if ( !isset( $encryptions ) )
  207. $encryptions = $this->_get_encryptions();
  208. if ( !isset( $compressions ) )
  209. $compressions = $this->_get_compressions();
  210. $this->_flush_url( $url, $cache, $mobile_groups, $referrer_groups,
  211. $encryptions, $compressions );
  212. }
  213. /**
  214. * Flushes global and repeated urls
  215. *
  216. * @return count of elements it has flushed
  217. */
  218. function flush_post_cleanup() {
  219. if ( $this->flush_operation_requested ) {
  220. $cache = $this->_get_cache();
  221. $cache->flush();
  222. $count = 999;
  223. $this->flush_operation_requested = false;
  224. $this->queued_urls = array();
  225. } else {
  226. $count = count( $this->queued_urls );
  227. if ( $count > 0 ) {
  228. $cache = $this->_get_cache();
  229. $mobile_groups = $this->_get_mobile_groups();
  230. $referrer_groups = $this->_get_referrer_groups();
  231. $encryptions = $this->_get_encryptions();
  232. $compressions = $this->_get_compressions();
  233. foreach ( $this->queued_urls as $url => $flag ) {
  234. $this->_flush_url( $url, $cache, $mobile_groups,
  235. $referrer_groups, $encryptions, $compressions );
  236. }
  237. // Purge sitemaps if a sitemap option has a regex
  238. if ( $this->_config->get_string( 'pgcache.purge.sitemap_regex' ) ) {
  239. $cache = $this->_get_cache();
  240. $cache->flush( 'sitemaps' );
  241. }
  242. $this->queued_urls = array();
  243. }
  244. }
  245. return $count;
  246. }
  247. /**
  248. * Returns array of mobile groups
  249. *
  250. * @return array
  251. */
  252. function _get_mobile_groups() {
  253. $mobile_groups = array( '' );
  254. if ( $this->_mobile ) {
  255. $mobile_groups = array_merge( $mobile_groups, array_keys(
  256. $this->_mobile->get_groups() ) );
  257. }
  258. return $mobile_groups;
  259. }
  260. /**
  261. * Returns array of referrer groups
  262. *
  263. * @return array
  264. */
  265. function _get_referrer_groups() {
  266. $referrer_groups = array( '' );
  267. if ( $this->_referrer ) {
  268. $referrer_groups = array_merge( $referrer_groups, array_keys(
  269. $this->_referrer->get_groups() ) );
  270. }
  271. return $referrer_groups;
  272. }
  273. /**
  274. * Returns array of encryptions
  275. *
  276. * @return array
  277. */
  278. function _get_encryptions() {
  279. $is_https = ( substr( get_home_url(), 0, 5 ) == 'https' );
  280. $encryptions = array();
  281. if ( ! $is_https || $this->_config->get_boolean( 'pgcache.cache.ssl' ) )
  282. $encryptions[] = '';
  283. if ( $is_https || $this->_config->get_boolean( 'pgcache.cache.ssl' ) )
  284. $encryptions[] = 'ssl';
  285. return $encryptions;
  286. }
  287. private function _append_parent_terms( $terms, $terms_to_check_parents ) {
  288. $terms_to_check_parents = $terms;
  289. $ids = null;
  290. for ( ;; ) {
  291. $parent_ids = array();
  292. $taxonomies = array();
  293. foreach ( $terms_to_check_parents as $term ) {
  294. if ( $term->parent ) {
  295. $parent_ids[$term->parent] = '*';
  296. $taxonomies[$term->taxonomy] = '*';
  297. }
  298. }
  299. if ( empty( $parent_ids ) )
  300. return $terms;
  301. if ( is_null( $ids ) ) {
  302. // build a map of ids for faster check
  303. $ids = array();
  304. foreach ( $terms as $term )
  305. $ids[$term->term_id] = '*';
  306. } else {
  307. // append last new items to ids map
  308. foreach ( $terms_to_check_parents as $term )
  309. $ids[$term->term_id] = '*';
  310. }
  311. // build list to extract
  312. $include_ids = array();
  313. foreach ( $parent_ids as $id => $v ) {
  314. if ( !isset( $ids[$id] ) )
  315. $include_ids[] = $id;
  316. }
  317. if ( empty( $include_ids ) )
  318. return $terms;
  319. $new_terms = get_terms( array_keys( $taxonomies ),
  320. array( 'include' => $include_ids ) );
  321. $terms = array_merge( $terms, $new_terms );
  322. $terms_to_check_parents = $new_terms;
  323. }
  324. }
  325. }