PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/juanito.abelo/nlmobile
PHP | 228 lines | 132 code | 27 blank | 69 comment | 14 complexity | 3d13befe1c79a41f0dd359fe54223571 MD5 | raw file
  1. <?php
  2. /**
  3. * W3 FragmentCache plugin
  4. */
  5. if (!defined('W3TC')) {
  6. die();
  7. }
  8. w3_require_once(W3TC_LIB_W3_DIR . '/Plugin.php');
  9. /**
  10. * Class W3_Plugin_FragmentCache
  11. */
  12. class W3_Pro_Plugin_FragmentCache extends W3_Plugin {
  13. private $_fragment_groups = array();
  14. private $_fragment_groups_global = array();
  15. private $_actions = array();
  16. private $_actions_global = array();
  17. /**
  18. * Runs plugin
  19. */
  20. function run() {
  21. add_action('init', array($this, 'on_init'),9999999);
  22. add_filter('cron_schedules', array(
  23. &$this,
  24. 'cron_schedules'
  25. ));
  26. if ($this->_config->get_string('fragmentcache.engine') == 'file') {
  27. add_action('w3_fragmentcache_cleanup', array(
  28. &$this,
  29. 'cleanup'
  30. ));
  31. }
  32. add_action('switch_blog', array(
  33. &$this,
  34. 'switch_blog'
  35. ), 0, 2);
  36. $groups = $this->_config->get_array('fragmentcache.groups');
  37. foreach ($groups as $group) {
  38. $split = explode(',', $group);
  39. $group = array_shift($split);
  40. $actions = $split;
  41. $this->register_group($group, $actions,
  42. $this->_config->get_integer('fragmentcache.lifetime'));
  43. }
  44. }
  45. /**
  46. * Does disk cache cleanup
  47. *
  48. * @return void
  49. */
  50. function cleanup() {
  51. $this->get_admin()->cleanup();
  52. }
  53. /**
  54. * Cron schedules filter
  55. *
  56. * @param array $schedules
  57. * @return array
  58. */
  59. function cron_schedules($schedules) {
  60. $gc_interval = $this->_config->get_integer('fragmentcache.file.gc');
  61. return array_merge($schedules, array(
  62. 'w3_fragmentcache_cleanup' => array(
  63. 'interval' => $gc_interval,
  64. 'display' => sprintf('[W3TC] Fragment Cache file GC (every %d seconds)', $gc_interval)
  65. ),
  66. ));
  67. }
  68. /**
  69. * Register actions on init
  70. */
  71. function on_init() {
  72. do_action('w3tc_register_fragment_groups');
  73. $actions = $this->get_registered_actions();
  74. foreach ($actions as $action => $groups) {
  75. add_action($action,array($this, 'on_action'), 0,0);
  76. }
  77. if (w3_is_network()) {
  78. $global_actions = $this->get_registered_global_actions();
  79. foreach ($global_actions as $action => $groups) {
  80. add_action($action,array($this, 'on_action_global'), 0,0);
  81. }
  82. }
  83. }
  84. /**
  85. * Flush action
  86. */
  87. function on_action() {
  88. $w3_fragmentcache = w3_instance('W3_Pro_FragmentCache');
  89. $actions = $this->get_registered_actions();
  90. $action = current_filter();
  91. $groups = $actions[$action];
  92. foreach($groups as $group) {
  93. $w3_fragmentcache->flush_group($group);
  94. }
  95. }
  96. /**
  97. * Flush action global
  98. */
  99. function on_action_global() {
  100. $w3_fragmentcache = w3_instance('W3_Pro_FragmentCache');
  101. $global_actions = $this->get_registered_global_actions();
  102. $action = current_filter();
  103. $global_groups = $global_actions[$action];
  104. foreach($global_groups as $group) {
  105. $w3_fragmentcache->flush_group($group, true);
  106. }
  107. }
  108. /**
  109. * Instantiates worker on demand
  110. *
  111. * @return W3_Plugin_FragmentCacheAdmin
  112. */
  113. function get_admin() {
  114. return w3_instance('W3_Pro_Plugin_FragmentCacheAdmin');
  115. }
  116. /**
  117. * Register transients group
  118. *
  119. * @param $group
  120. * @param $actions
  121. * @param $expiration
  122. */
  123. function register_group($group, $actions, $expiration) {
  124. if (empty($group) || empty($actions) || empty($expiration))
  125. return;
  126. if (!is_int($expiration)) {
  127. $expiration = (int) $expiration;
  128. trigger_error(__METHOD__ . ' needs expiration parameter to be an int.', E_USER_WARNING);
  129. }
  130. $this->_fragment_groups[$group] = array(
  131. 'actions' => $actions,
  132. 'expiration' => $expiration
  133. );
  134. foreach ($actions as $action) {
  135. if (!isset($this->_actions[$action]))
  136. $this->_actions[$action] = array();
  137. $this->_actions[$action][] = $group;
  138. }
  139. }
  140. /**
  141. * Register site-transients group
  142. *
  143. * @param string $group
  144. * @param array $actions
  145. * @param int $expiration
  146. */
  147. function register_global_group($group, $actions, $expiration) {
  148. if (empty($group) || empty($actions) || empty($expiration))
  149. return;
  150. if (!is_int($expiration)) {
  151. $expiration = (int) $expiration;
  152. trigger_error(__METHOD__ . ' needs expiration parameter to be an int.', E_USER_WARNING);
  153. }
  154. $this->_fragment_groups_global[$group] = array(
  155. 'actions' => $actions,
  156. 'expiration' => $expiration
  157. );
  158. foreach ($actions as $action) {
  159. if (!isset($this->_actions_global[$action]))
  160. $this->_actions_global[$action] = array();
  161. $this->_actions_global[$action][] = $group;
  162. }
  163. }
  164. /**
  165. * Returns registered fragment groups, ie transients.
  166. *
  167. * @return array array('group' => array('action1','action2'))
  168. */
  169. function get_registered_fragment_groups() {
  170. return $this->_fragment_groups;
  171. }
  172. /**
  173. * Returns registered actions and transient groups that should be purged per action
  174. * @return array array('action' => array('group1', 'group2'))
  175. */
  176. function get_registered_actions() {
  177. return $this->_actions;
  178. }
  179. /**
  180. * Returns registered global fragment groups, ie site-transients.
  181. *
  182. * @return array array('group' => array('action1','action2'))
  183. */
  184. function get_registered_global_fragment_groups() {
  185. return $this->_fragment_groups_global;
  186. }
  187. /**
  188. * Returns registered actions and site-transient groups that should be purged per action
  189. * @return array array('action' => array('group1', 'group2'))
  190. */
  191. function get_registered_global_actions() {
  192. return $this->_actions_global;
  193. }
  194. /**
  195. * Switch blog action
  196. */
  197. function switch_blog($blog_id, $previous_blog_id) {
  198. $o = w3_instance('W3_Pro_FragmentCache');
  199. $o->switch_blog($blog_id);
  200. }
  201. }