PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/sharpmachine/wakeupmedia.com
PHP | 332 lines | 178 code | 52 blank | 102 comment | 26 complexity | bf3220981cfe396dffe6b832917327c9 MD5 | raw file
  1. <?php
  2. /**
  3. * W3 ObjectCache plugin
  4. */
  5. if (!defined('W3TC')) {
  6. die();
  7. }
  8. require_once W3TC_LIB_W3_DIR . '/Plugin.php';
  9. /**
  10. * Class W3_Plugin_BrowserCache
  11. */
  12. class W3_Plugin_BrowserCache extends W3_Plugin {
  13. /**
  14. * Runs plugin
  15. */
  16. function run() {
  17. if ($this->_config->get_boolean('browsercache.html.w3tc')) {
  18. add_action('send_headers', array(
  19. &$this,
  20. 'send_headers'
  21. ));
  22. }
  23. if ($this->can_ob()) {
  24. ob_start(array(
  25. &$this,
  26. 'ob_callback'
  27. ));
  28. }
  29. }
  30. /**
  31. * Instantiates worker with admin functionality on demand
  32. *
  33. * @return W3_Plugin_BrowserCacheAdmin
  34. */
  35. function &get_admin() {
  36. return w3_instance('W3_Plugin_BrowserCacheAdmin');
  37. }
  38. /**
  39. * Activate plugin action (called by W3_PluginProxy)
  40. */
  41. function activate() {
  42. $this->get_admin()->activate();
  43. }
  44. /**
  45. * Deactivate plugin action (called by W3_PluginProxy)
  46. */
  47. function deactivate() {
  48. $this->get_admin()->deactivate();
  49. }
  50. /**
  51. * Check if we can start OB
  52. *
  53. * @return boolean
  54. */
  55. function can_ob() {
  56. /**
  57. * Replace feature should be enabled
  58. */
  59. if (!$this->_config->get_boolean('browsercache.cssjs.replace') && !$this->_config->get_boolean('browsercache.html.replace') && !$this->_config->get_boolean('browsercache.other.replace')) {
  60. return false;
  61. }
  62. /**
  63. * Skip if admin
  64. */
  65. if (defined('WP_ADMIN')) {
  66. return false;
  67. }
  68. /**
  69. * Skip if doing AJAX
  70. */
  71. if (defined('DOING_AJAX')) {
  72. return false;
  73. }
  74. /**
  75. * Skip if doing cron
  76. */
  77. if (defined('DOING_CRON')) {
  78. return false;
  79. }
  80. /**
  81. * Skip if APP request
  82. */
  83. if (defined('APP_REQUEST')) {
  84. return false;
  85. }
  86. /**
  87. * Skip if XMLRPC request
  88. */
  89. if (defined('XMLRPC_REQUEST')) {
  90. return false;
  91. }
  92. /**
  93. * Check for WPMU's and WP's 3.0 short init
  94. */
  95. if (defined('SHORTINIT') && SHORTINIT) {
  96. return false;
  97. }
  98. /**
  99. * Check User Agent
  100. */
  101. if (isset($_SERVER['HTTP_USER_AGENT']) && stristr($_SERVER['HTTP_USER_AGENT'], W3TC_POWERED_BY) !== false) {
  102. return false;
  103. }
  104. return true;
  105. }
  106. /**
  107. * Output buffer callback
  108. *
  109. * @param string $buffer
  110. * @return mixed
  111. */
  112. function ob_callback(&$buffer) {
  113. if ($buffer != '' && w3_is_xml($buffer)) {
  114. $domain_url_regexp = w3_get_domain_url_regexp();
  115. $buffer = preg_replace_callback('~(href|src|action)=[\'"]((' . $domain_url_regexp . ')?(/[^\'"]*\.([a-z-_]+)(\?[^\'"]*)?))[\'"]~Ui', array(
  116. &$this,
  117. 'link_replace_callback'
  118. ), $buffer);
  119. }
  120. return $buffer;
  121. }
  122. /**
  123. * Link replace callback
  124. *
  125. * @param string $matches
  126. * @return string
  127. */
  128. function link_replace_callback($matches) {
  129. static $id = null, $extensions = null;
  130. if ($id === null) {
  131. $id = $this->get_replace_id();
  132. }
  133. if ($extensions === null) {
  134. $extensions = $this->get_replace_extensions();
  135. }
  136. list ($match, $attr, $url, , , , , $extension) = $matches;
  137. if (in_array($extension, $extensions)) {
  138. $url = w3_remove_query($url);
  139. $url .= (strstr($url, '?') !== false ? '&amp;' : '?') . $id;
  140. return sprintf('%s="%s"', $attr, $url);
  141. }
  142. return $match;
  143. }
  144. /**
  145. * Returns replace ID
  146. *
  147. * @return string
  148. */
  149. function get_replace_id() {
  150. static $cache_id = null;
  151. if ($cache_id === null) {
  152. $keys = array(
  153. 'browsercache.cssjs.compression',
  154. 'browsercache.cssjs.expires',
  155. 'browsercache.cssjs.lifetime',
  156. 'browsercache.cssjs.cache.control',
  157. 'browsercache.cssjs.cache.policy',
  158. 'browsercache.cssjs.etag',
  159. 'browsercache.cssjs.w3tc',
  160. 'browsercache.html.compression',
  161. 'browsercache.html.expires',
  162. 'browsercache.html.lifetime',
  163. 'browsercache.html.cache.control',
  164. 'browsercache.html.cache.policy',
  165. 'browsercache.html.etag',
  166. 'browsercache.html.w3tc',
  167. 'browsercache.other.compression',
  168. 'browsercache.other.expires',
  169. 'browsercache.other.lifetime',
  170. 'browsercache.other.cache.control',
  171. 'browsercache.other.cache.policy',
  172. 'browsercache.other.etag',
  173. 'browsercache.other.w3tc'
  174. );
  175. $values = array();
  176. foreach ($keys as $key) {
  177. $values[] = $this->_config->get($key);
  178. }
  179. $cache_id = substr(md5(implode('', $values)), 0, 6);
  180. }
  181. return $cache_id;
  182. }
  183. /**
  184. * Returns replace extensions
  185. *
  186. * @return array
  187. */
  188. function get_replace_extensions() {
  189. static $extensions = null;
  190. if ($extensions === null) {
  191. $types = array();
  192. $extensions = array();
  193. if ($this->_config->get_boolean('browsercache.cssjs.replace')) {
  194. $types = array_merge($types, array_keys($this->_get_cssjs_types()));
  195. }
  196. if ($this->_config->get_boolean('browsercache.html.replace')) {
  197. $types = array_merge($types, array_keys($this->_get_html_types()));
  198. }
  199. if ($this->_config->get_boolean('browsercache.other.replace')) {
  200. $types = array_merge($types, array_keys($this->_get_other_types()));
  201. }
  202. foreach ($types as $type) {
  203. $extensions = array_merge($extensions, explode('|', $type));
  204. }
  205. }
  206. return $extensions;
  207. }
  208. /**
  209. * Send headers
  210. */
  211. function send_headers() {
  212. @header('X-Powered-By: ' . W3TC_POWERED_BY);
  213. }
  214. /**
  215. * Returns CSS/JS mime types
  216. *
  217. * @return array
  218. */
  219. function _get_cssjs_types() {
  220. $mime_types = include W3TC_INC_DIR . '/mime/cssjs.php';
  221. return $mime_types;
  222. }
  223. /**
  224. * Returns HTML mime types
  225. *
  226. * @return array
  227. */
  228. function _get_html_types() {
  229. $mime_types = include W3TC_INC_DIR . '/mime/html.php';
  230. return $mime_types;
  231. }
  232. /**
  233. * Returns other mime types
  234. *
  235. * @return array
  236. */
  237. function _get_other_types() {
  238. $mime_types = include W3TC_INC_DIR . '/mime/other.php';
  239. return $mime_types;
  240. }
  241. /**
  242. * Returns cache config for CDN
  243. *
  244. * @return array
  245. */
  246. function get_cache_config() {
  247. $config = array();
  248. $cssjs_types = $this->_get_cssjs_types();
  249. $html_types = $this->_get_html_types();
  250. $other_types = $this->_get_other_types();
  251. $this->_get_cache_config($config, $cssjs_types, 'cssjs');
  252. $this->_get_cache_config($config, $html_types, 'html');
  253. $this->_get_cache_config($config, $other_types, 'other');
  254. return $config;
  255. }
  256. /**
  257. * Writes cache config
  258. *
  259. * @param string $config
  260. * @param string $mime_types
  261. * @param array $section
  262. * @return void
  263. */
  264. function _get_cache_config(&$config, $mime_types, $section) {
  265. $expires = $this->_config->get_boolean('browsercache.' . $section . '.expires');
  266. $lifetime = $this->_config->get_integer('browsercache.' . $section . '.lifetime');
  267. $cache_control = $this->_config->get_boolean('browsercache.' . $section . '.cache.control');
  268. $cache_policy = $this->_config->get_string('browsercache.' . $section . '.cache.policy');
  269. $etag = $this->_config->get_boolean('browsercache.' . $section . '.etag');
  270. $w3tc = $this->_config->get_boolean('browsercache.' . $section . '.w3tc');
  271. foreach ($mime_types as $mime_type) {
  272. $config[$mime_type] = array(
  273. 'etag' => $etag,
  274. 'w3tc' => $w3tc,
  275. 'lifetime' => ($expires ? $lifetime : 0),
  276. 'cache_control' => ($cache_control ? $cache_policy : false)
  277. );
  278. }
  279. }
  280. }