PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/karlen/ayo_wp
PHP | 300 lines | 189 code | 45 blank | 66 comment | 30 complexity | 9a17f6c14ca0cf884bc7e58d833807e9 MD5 | raw file
  1. <?php
  2. namespace W3TC;
  3. /**
  4. * W3 PgCache plugin
  5. */
  6. class PgCache_Plugin {
  7. /**
  8. * Config
  9. */
  10. private $_config = null;
  11. function __construct() {
  12. $this->_config = Dispatcher::config();
  13. }
  14. /**
  15. * Runs plugin
  16. */
  17. function run() {
  18. add_action( 'w3tc_flush_all',
  19. array( $this, 'w3tc_flush_posts' ),
  20. 1100, 1 );
  21. add_action( 'w3tc_flush_post',
  22. array( $this, 'w3tc_flush_post' ),
  23. 1100, 1 );
  24. add_action( 'w3tc_flushable_posts',
  25. '__return_true',
  26. 1100 );
  27. add_action( 'w3tc_flush_posts',
  28. array( $this, 'w3tc_flush_posts' ),
  29. 1100 );
  30. add_action( 'w3tc_flush_url',
  31. array( $this, 'w3tc_flush_url' ),
  32. 1100, 1 );
  33. add_filter( 'w3tc_admin_bar_menu',
  34. array( $this, 'w3tc_admin_bar_menu' ) );
  35. add_filter( 'cron_schedules',
  36. array( $this, 'cron_schedules' ) );
  37. $o = Dispatcher::component( 'PgCache_ContentGrabber' );
  38. add_filter( 'w3tc_footer_comment',
  39. array( $o, 'w3tc_footer_comment' ) );
  40. add_action( 'w3tc_usage_statistics_of_request',
  41. array( $o, 'w3tc_usage_statistics_of_request' ),
  42. 10, 1 );
  43. add_filter( 'w3tc_usage_statistics_metrics',
  44. array( $this, 'w3tc_usage_statistics_metrics' ) );
  45. if ( $this->_config->get_string( 'pgcache.engine' ) == 'file' ||
  46. $this->_config->get_string( 'pgcache.engine' ) == 'file_generic' ) {
  47. add_action( 'w3_pgcache_cleanup',
  48. array( $this, 'cleanup' ) );
  49. }
  50. add_action( 'w3_pgcache_prime', array( $this, 'prime' ) );
  51. Util_AttachToActions::flush_posts_on_actions();
  52. add_filter( 'comment_cookie_lifetime',
  53. array( $this, 'comment_cookie_lifetime' ) );
  54. if ( $this->_config->get_string( 'pgcache.engine' ) == 'file_generic' ) {
  55. add_action( 'wp_logout',
  56. array( $this, 'on_logout' ),
  57. 0 );
  58. add_action( 'wp_login',
  59. array( $this, 'on_login' ),
  60. 0 );
  61. }
  62. if ( $this->_config->get_boolean( 'pgcache.prime.post.enabled', false ) ) {
  63. add_action( 'publish_post',
  64. array( $this, 'prime_post' ),
  65. 30 );
  66. }
  67. if ( ( $this->_config->get_boolean( 'pgcache.late_init' ) ||
  68. $this->_config->get_boolean( 'pgcache.late_caching' ) ) &&
  69. !is_admin() ) {
  70. $o = Dispatcher::component( 'PgCache_ContentGrabber' );
  71. add_action( 'init',
  72. array( $o, 'delayed_cache_print' ),
  73. 99999 );
  74. }
  75. if ( !$this->_config->get_boolean( 'pgcache.mirrors.enabled' ) &&
  76. !Util_Environment::is_wpmu_subdomain() ) {
  77. add_action( 'init',
  78. array( $this, 'redirect_on_foreign_domain' ) );
  79. }
  80. }
  81. /**
  82. * Does disk cache cleanup
  83. *
  84. * @return void
  85. */
  86. function cleanup() {
  87. $this->_get_admin()->cleanup();
  88. }
  89. /**
  90. * Prime cache
  91. *
  92. * @param integer $start
  93. * @return void
  94. */
  95. function prime( $start = 0 ) {
  96. $this->_get_admin()->prime( $start );
  97. }
  98. /**
  99. * Instantiates worker on demand
  100. */
  101. private function _get_admin() {
  102. return Dispatcher::component( 'PgCache_Plugin_Admin' );
  103. }
  104. /**
  105. * Cron schedules filter
  106. *
  107. * @param array $schedules
  108. * @return array
  109. */
  110. function cron_schedules( $schedules ) {
  111. $c = $this->_config;
  112. if ( $c->get_boolean( 'pgcache.enabled' ) &&
  113. ( $c->get_string( 'pgcache.engine' ) == 'file' ||
  114. $c->get_string( 'pgcache.engine' ) == 'file_generic' ) ) {
  115. $v = $c->get_integer( 'pgcache.file.gc' );
  116. $schedules['w3_pgcache_cleanup'] = array(
  117. 'interval' => $v,
  118. 'display' => sprintf( '[W3TC] Page Cache file GC (every %d seconds)',
  119. $v )
  120. );
  121. }
  122. if ( $c->get_boolean( 'pgcache.enabled' ) &&
  123. $c->get_boolean( 'pgcache.prime.enabled' ) ) {
  124. $v = $c->get_integer( 'pgcache.prime.interval' );
  125. $schedules['w3_pgcache_prime'] = array(
  126. 'interval' => $v,
  127. 'display' => sprintf( '[W3TC] Page Cache prime (every %d seconds)',
  128. $v )
  129. );
  130. }
  131. return $schedules;
  132. }
  133. public function redirect_on_foreign_domain() {
  134. $request_host = Util_Environment::host();
  135. // host not known, potentially we are in console mode not http request
  136. if ( empty( $request_host ) )
  137. return;
  138. $home_url = get_home_url();
  139. $parsed_url = @parse_url( $home_url );
  140. if ( isset( $parsed_url['host'] ) &&
  141. strtolower( $parsed_url['host'] ) != strtolower( $request_host ) ) {
  142. $redirect_url = $parsed_url['scheme'] . '://';
  143. if ( !empty( $parsed_url['user'] ) ) {
  144. $redirect_url .= $parsed_url['user'];
  145. if ( !empty( $parsed_url['pass'] ) )
  146. $redirect_url .= ':' . $parsed_url['pass'];
  147. }
  148. if ( !empty( $parsed_url['host'] ) )
  149. $redirect_url .= $parsed_url['host'];
  150. if ( !empty( $parsed_url['port'] ) && $parsed_url['port'] != 80 ) {
  151. $redirect_url .= ':' . (int)$parsed_url['port'];
  152. }
  153. $redirect_url .= $_SERVER['REQUEST_URI'];
  154. //echo $redirect_url;
  155. wp_redirect( $redirect_url, 301 );
  156. exit();
  157. }
  158. }
  159. /**
  160. *
  161. *
  162. * @param integer $lifetime
  163. * @return integer
  164. */
  165. function comment_cookie_lifetime( $lifetime ) {
  166. $l = $this->_config->get_integer( 'pgcache.comment_cookie_ttl' );
  167. if ( $l != -1 )
  168. return $l;
  169. else
  170. return $lifetime;
  171. }
  172. /**
  173. * Add cookie on logout to circumvent pagecache due to browser cache resulting in 304s
  174. */
  175. function on_logout() {
  176. setcookie( 'w3tc_logged_out' );
  177. }
  178. /**
  179. * Remove logout cookie on logins
  180. */
  181. function on_login() {
  182. if ( isset( $_COOKIE['w3tc_logged_out'] ) )
  183. setcookie( 'w3tc_logged_out', '', 1 );
  184. }
  185. /**
  186. *
  187. *
  188. * @param unknown $post_id
  189. * @return boolean
  190. */
  191. function prime_post( $post_id ) {
  192. $w3_pgcache = Dispatcher::component( 'CacheFlush' );
  193. return $w3_pgcache->prime_post( $post_id );
  194. }
  195. public function w3tc_usage_statistics_metrics( $metrics ) {
  196. return array_merge( $metrics, array(
  197. 'pagecache_requests_total', 'pagecache_requests_hits',
  198. 'pagecache_requests_time_10ms' ) );
  199. }
  200. public function w3tc_admin_bar_menu( $menu_items ) {
  201. $menu_items['20110.pagecache'] = array(
  202. 'id' => 'w3tc_flush_pgcache',
  203. 'parent' => 'w3tc_flush',
  204. 'title' => __( 'Page Cache: All', 'w3-total-cache' ),
  205. 'href' => wp_nonce_url( network_admin_url(
  206. 'admin.php?page=w3tc_dashboard&amp;w3tc_flush_pgcache' ),
  207. 'w3tc' )
  208. );
  209. if ( Util_Environment::detect_post_id() && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) ) {
  210. $menu_items['20120.pagecache'] = array(
  211. 'id' => 'w3tc_pgcache_flush_post',
  212. 'parent' => 'w3tc_flush',
  213. 'title' => __( 'Page Cache: Current Page', 'w3-total-cache' ),
  214. 'href' => wp_nonce_url( network_admin_url(
  215. 'admin.php?page=w3tc_dashboard&amp;w3tc_flush_post&amp;post_id=' .
  216. Util_Environment::detect_post_id() ), 'w3tc' )
  217. );
  218. }
  219. return $menu_items;
  220. }
  221. /**
  222. * Flushes all caches
  223. *
  224. * @return boolean
  225. */
  226. function w3tc_flush_posts( $extras = array() ) {
  227. if ( isset( $extras['only'] ) && $extras['only'] != 'pagecache' )
  228. return;
  229. $pgcacheflush = Dispatcher::component( 'PgCache_Flush' );
  230. $v = $pgcacheflush->flush();
  231. return $v;
  232. }
  233. /**
  234. * Flushes post cache
  235. *
  236. * @param integer $post_id
  237. * @return boolean
  238. */
  239. function w3tc_flush_post( $post_id ) {
  240. $pgcacheflush = Dispatcher::component( 'PgCache_Flush' );
  241. $v = $pgcacheflush->flush_post( $post_id );
  242. return $v;
  243. }
  244. /**
  245. * Flushes post cache
  246. *
  247. * @param string $url
  248. * @return boolean
  249. */
  250. function w3tc_flush_url( $url ) {
  251. $pgcacheflush = Dispatcher::component( 'PgCache_Flush' );
  252. $v = $pgcacheflush->flush_url( $url );
  253. return $v;
  254. }
  255. }