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

https://github.com/sharpmachine/wakeupmedia.com · PHP · 740 lines · 476 code · 146 blank · 118 comment · 84 complexity · 6db80319d5c29af997fef91a72d9960d MD5 · raw file

  1. <?php
  2. /**
  3. * W3 ObjectCache plugin
  4. */
  5. if (!defined('W3TC')) {
  6. die();
  7. }
  8. require_once W3TC_INC_DIR . '/functions/rule.php';
  9. require_once W3TC_LIB_W3_DIR . '/Plugin.php';
  10. /**
  11. * Class W3_Plugin_BrowserCacheAdmin
  12. */
  13. class W3_Plugin_BrowserCacheAdmin extends W3_Plugin {
  14. /**
  15. * Activate plugin action
  16. */
  17. function activate() {
  18. if ($this->_config->get_boolean('browsercache.enabled')) {
  19. if (w3_can_modify_rules(w3_get_browsercache_rules_cache_path())) {
  20. $this->write_rules_cache();
  21. }
  22. if ($this->_config->get_boolean('browsercache.no404wp') && w3_can_modify_rules(w3_get_browsercache_rules_no404wp_path())) {
  23. $this->write_rules_no404wp();
  24. }
  25. }
  26. }
  27. /**
  28. * Deactivate plugin action
  29. */
  30. function deactivate() {
  31. if (w3_can_modify_rules(w3_get_browsercache_rules_no404wp_path())) {
  32. $this->remove_rules_no404wp();
  33. }
  34. if (w3_can_modify_rules(w3_get_browsercache_rules_cache_path())) {
  35. $this->remove_rules_cache();
  36. }
  37. }
  38. /**
  39. * Returns CSS/JS mime types
  40. *
  41. * @return array
  42. */
  43. function _get_cssjs_types() {
  44. $mime_types = include W3TC_INC_DIR . '/mime/cssjs.php';
  45. return $mime_types;
  46. }
  47. /**
  48. * Returns HTML mime types
  49. *
  50. * @return array
  51. */
  52. function _get_html_types() {
  53. $mime_types = include W3TC_INC_DIR . '/mime/html.php';
  54. return $mime_types;
  55. }
  56. /**
  57. * Returns other mime types
  58. *
  59. * @return array
  60. */
  61. function _get_other_types() {
  62. $mime_types = include W3TC_INC_DIR . '/mime/other.php';
  63. return $mime_types;
  64. }
  65. /**
  66. * Returns cache rules
  67. *
  68. * @param bool $cdn
  69. * @return string
  70. */
  71. function generate_rules_cache($cdn = false) {
  72. switch (true) {
  73. case w3_is_apache():
  74. case w3_is_litespeed():
  75. return $this->generate_rules_cache_apache($cdn);
  76. case w3_is_nginx():
  77. return $this->generate_rules_cache_nginx($cdn);
  78. }
  79. return false;
  80. }
  81. /**
  82. * Returns cache rules
  83. *
  84. * @param bool $cdn
  85. * @return string
  86. */
  87. function generate_rules_cache_apache($cdn = false) {
  88. $cssjs_types = $this->_get_cssjs_types();
  89. $html_types = $this->_get_html_types();
  90. $other_types = $this->_get_other_types();
  91. $cssjs_expires = $this->_config->get_boolean('browsercache.cssjs.expires');
  92. $html_expires = $this->_config->get_boolean('browsercache.html.expires');
  93. $other_expires = $this->_config->get_boolean('browsercache.other.expires');
  94. $cssjs_lifetime = $this->_config->get_integer('browsercache.cssjs.lifetime');
  95. $html_lifetime = $this->_config->get_integer('browsercache.html.lifetime');
  96. $other_lifetime = $this->_config->get_integer('browsercache.other.lifetime');
  97. $mime_types = array();
  98. if ($cssjs_expires && $cssjs_lifetime) {
  99. $mime_types = array_merge($mime_types, $cssjs_types);
  100. }
  101. if ($html_expires && $html_lifetime) {
  102. $mime_types = array_merge($mime_types, $html_types);
  103. }
  104. if ($other_expires && $other_lifetime) {
  105. $mime_types = array_merge($mime_types, $other_types);
  106. }
  107. $rules = '';
  108. $rules .= W3TC_MARKER_BEGIN_BROWSERCACHE_CACHE . "\n";
  109. if ($cdn) {
  110. $rules .= "<IfModule mod_headers.c>\n";
  111. $rules .= " Header set Access-Control-Allow-Origin *\n";
  112. $rules .= "</IfModule>\n";
  113. }
  114. if (count($mime_types)) {
  115. $rules .= "<IfModule mod_mime.c>\n";
  116. foreach ($mime_types as $ext => $mime_type) {
  117. $extensions = explode('|', $ext);
  118. $rules .= " AddType " . $mime_type;
  119. foreach ($extensions as $extension) {
  120. $rules .= " ." . $extension;
  121. }
  122. $rules .= "\n";
  123. }
  124. $rules .= "</IfModule>\n";
  125. $rules .= "<IfModule mod_expires.c>\n";
  126. $rules .= " ExpiresActive On\n";
  127. if ($cssjs_expires && $cssjs_lifetime) {
  128. foreach ($cssjs_types as $mime_type) {
  129. $rules .= " ExpiresByType " . $mime_type . " A" . $cssjs_lifetime . "\n";
  130. }
  131. }
  132. if ($html_expires && $html_lifetime) {
  133. foreach ($html_types as $mime_type) {
  134. $rules .= " ExpiresByType " . $mime_type . " A" . $html_lifetime . "\n";
  135. }
  136. }
  137. if ($other_expires && $other_lifetime) {
  138. foreach ($other_types as $mime_type) {
  139. $rules .= " ExpiresByType " . $mime_type . " A" . $other_lifetime . "\n";
  140. }
  141. }
  142. $rules .= "</IfModule>\n";
  143. }
  144. $cssjs_compression = $this->_config->get_boolean('browsercache.cssjs.compression');
  145. $html_compression = $this->_config->get_boolean('browsercache.html.compression');
  146. $other_compression = $this->_config->get_boolean('browsercache.other.compression');
  147. if ($cssjs_compression || $html_compression || $other_compression) {
  148. $compression_types = array();
  149. if ($cssjs_compression) {
  150. $compression_types = array_merge($compression_types, $cssjs_types);
  151. }
  152. if ($html_compression) {
  153. $compression_types = array_merge($compression_types, $html_types);
  154. }
  155. if ($other_compression) {
  156. $compression_types = array_merge($compression_types, array(
  157. 'ico' => 'image/x-icon'
  158. ));
  159. }
  160. $rules .= "<IfModule mod_deflate.c>\n";
  161. $rules .= " <IfModule mod_setenvif.c>\n";
  162. $rules .= " BrowserMatch ^Mozilla/4 gzip-only-text/html\n";
  163. $rules .= " BrowserMatch ^Mozilla/4\\.0[678] no-gzip\n";
  164. $rules .= " BrowserMatch \\bMSIE !no-gzip !gzip-only-text/html\n";
  165. $rules .= " BrowserMatch \\bMSI[E] !no-gzip !gzip-only-text/html\n";
  166. $rules .= " </IfModule>\n";
  167. $rules .= " <IfModule mod_headers.c>\n";
  168. $rules .= " Header append Vary User-Agent env=!dont-vary\n";
  169. $rules .= " </IfModule>\n";
  170. $rules .= " <IfModule mod_filter.c>\n";
  171. $rules .= " AddOutputFilterByType DEFLATE " . implode(' ', $compression_types) . "\n";
  172. $rules .= " </IfModule>\n";
  173. $rules .= "</IfModule>\n";
  174. }
  175. $this->_generate_rules_cache_apache($rules, $cssjs_types, 'cssjs');
  176. $this->_generate_rules_cache_apache($rules, $html_types, 'html');
  177. $this->_generate_rules_cache_apache($rules, $other_types, 'other');
  178. $rules .= W3TC_MARKER_END_BROWSERCACHE_CACHE . "\n";
  179. return $rules;
  180. }
  181. /**
  182. * Writes cache rules
  183. *
  184. * @param string $rules
  185. * @param array $mime_types
  186. * @param string $section
  187. * @return void
  188. */
  189. function _generate_rules_cache_apache(&$rules, $mime_types, $section) {
  190. $cache_control = $this->_config->get_boolean('browsercache.' . $section . '.cache.control');
  191. $etag = $this->_config->get_boolean('browsercache.' . $section . '.etag');
  192. $w3tc = $this->_config->get_boolean('browsercache.' . $section . '.w3tc');
  193. $extensions = array_keys($mime_types);
  194. $extensions_lowercase = array_map('strtolower', $extensions);
  195. $extensions_uppercase = array_map('strtoupper', $extensions);
  196. $rules .= "<FilesMatch \"\\.(" . implode('|', array_merge($extensions_lowercase, $extensions_uppercase)) . ")$\">\n";
  197. if ($cache_control) {
  198. $cache_policy = $this->_config->get_string('browsercache.' . $section . '.cache.policy');
  199. switch ($cache_policy) {
  200. case 'cache':
  201. $rules .= " <IfModule mod_headers.c>\n";
  202. $rules .= " Header set Pragma \"public\"\n";
  203. $rules .= " Header set Cache-Control \"public\"\n";
  204. $rules .= " </IfModule>\n";
  205. break;
  206. case 'cache_validation':
  207. $rules .= " <IfModule mod_headers.c>\n";
  208. $rules .= " Header set Pragma \"public\"\n";
  209. $rules .= " Header set Cache-Control \"public, must-revalidate, proxy-revalidate\"\n";
  210. $rules .= " </IfModule>\n";
  211. break;
  212. case 'cache_noproxy':
  213. $rules .= " <IfModule mod_headers.c>\n";
  214. $rules .= " Header set Pragma \"public\"\n";
  215. $rules .= " Header set Cache-Control \"public, must-revalidate\"\n";
  216. $rules .= " </IfModule>\n";
  217. break;
  218. case 'cache_maxage':
  219. $expires = $this->_config->get_boolean('browsercache.' . $section . '.expires');
  220. $lifetime = $this->_config->get_integer('browsercache.' . $section . '.lifetime');
  221. $rules .= " <IfModule mod_headers.c>\n";
  222. $rules .= " Header set Pragma \"public\"\n";
  223. if ($expires) {
  224. $rules .= " Header append Cache-Control \"public, must-revalidate, proxy-revalidate\"\n";
  225. } else {
  226. $rules .= " Header set Cache-Control \"max-age=" . $lifetime . ", public, must-revalidate, proxy-revalidate\"\n";
  227. }
  228. $rules .= " </IfModule>\n";
  229. break;
  230. case 'no_cache':
  231. $rules .= " <IfModule mod_headers.c>\n";
  232. $rules .= " Header set Pragma \"no-cache\"\n";
  233. $rules .= " Header set Cache-Control \"max-age=0, private, no-store, no-cache, must-revalidate\"\n";
  234. $rules .= " </IfModule>\n";
  235. break;
  236. }
  237. }
  238. if ($etag) {
  239. $rules .= " FileETag MTime Size\n";
  240. } else {
  241. $rules .= " FileETag None\n";
  242. }
  243. if ($w3tc) {
  244. $rules .= " <IfModule mod_headers.c>\n";
  245. $rules .= " Header set X-Powered-By \"" . W3TC_POWERED_BY . "\"\n";
  246. $rules .= " </IfModule>\n";
  247. }
  248. $rules .= "</FilesMatch>\n";
  249. return $rules;
  250. }
  251. /**
  252. * Returns cache rules
  253. *
  254. * @param bool $cdn
  255. * @return string
  256. */
  257. function generate_rules_cache_nginx($cdn = false) {
  258. $cssjs_types = $this->_get_cssjs_types();
  259. $html_types = $this->_get_html_types();
  260. $other_types = $this->_get_other_types();
  261. $rules = '';
  262. $rules .= W3TC_MARKER_BEGIN_BROWSERCACHE_CACHE . "\n";
  263. if ($cdn) {
  264. $rules .= "add_header Access-Control-Allow-Origin \"*\"\n";
  265. }
  266. $cssjs_compression = $this->_config->get_boolean('browsercache.cssjs.compression');
  267. $html_compression = $this->_config->get_boolean('browsercache.html.compression');
  268. $other_compression = $this->_config->get_boolean('browsercache.other.compression');
  269. if ($cssjs_compression || $html_compression || $other_compression) {
  270. $compression_types = array();
  271. if ($cssjs_compression) {
  272. $compression_types = array_merge($compression_types, $cssjs_types);
  273. }
  274. if ($html_compression) {
  275. $compression_types = array_merge($compression_types, $html_types);
  276. }
  277. if ($other_compression) {
  278. $compression_types = array_merge($compression_types, array(
  279. 'ico' => 'image/x-icon'
  280. ));
  281. }
  282. unset($compression_types['html|htm']);
  283. $rules .= "gzip on;\n";
  284. $rules .= "gzip_types " . implode(' ', $compression_types) . ";\n";
  285. }
  286. $this->_generate_rules_cache_nginx($rules, $cssjs_types, 'cssjs');
  287. $this->_generate_rules_cache_nginx($rules, $html_types, 'html');
  288. $this->_generate_rules_cache_nginx($rules, $other_types, 'other');
  289. $rules .= W3TC_MARKER_END_BROWSERCACHE_CACHE . "\n";
  290. return $rules;
  291. }
  292. /**
  293. * Writes cache rules
  294. *
  295. * @param string $rules
  296. * @param array $mime_types
  297. * @param string $section
  298. * @return void
  299. */
  300. function _generate_rules_cache_nginx(&$rules, $mime_types, $section) {
  301. $expires = $this->_config->get_boolean('browsercache.' . $section . '.expires');
  302. $cache_control = $this->_config->get_boolean('browsercache.' . $section . '.cache.control');
  303. $w3tc = $this->_config->get_boolean('browsercache.' . $section . '.w3tc');
  304. if ($expires || $cache_control || $w3tc) {
  305. $lifetime = $this->_config->get_integer('browsercache.' . $section . '.lifetime');
  306. $rules .= "location ~ \\.(" . implode('|', array_keys($mime_types)) . ")$ {\n";
  307. if ($expires) {
  308. $rules .= " expires " . $lifetime . "s;\n";
  309. }
  310. if ($cache_control) {
  311. $cache_policy = $this->_config->get_string('browsercache.cssjs.cache.policy');
  312. switch ($cache_policy) {
  313. case 'cache':
  314. $rules .= " add_header Pragma \"public\";\n";
  315. $rules .= " add_header Cache-Control \"public\";\n";
  316. break;
  317. case 'cache_validation':
  318. $rules .= " add_header Pragma \"public\";\n";
  319. $rules .= " add_header Cache-Control \"public, must-revalidate, proxy-revalidate\";\n";
  320. break;
  321. case 'cache_noproxy':
  322. $rules .= " add_header Pragma \"public\";\n";
  323. $rules .= " add_header Cache-Control \"public, must-revalidate\";\n";
  324. break;
  325. case 'cache_maxage':
  326. $rules .= " add_header Pragma \"public\";\n";
  327. $rules .= " add_header Cache-Control \"max-age=" . $lifetime . ", public, must-revalidate, proxy-revalidate\";\n";
  328. break;
  329. case 'no_cache':
  330. $rules .= " add_header Pragma \"no-cache\";\n";
  331. $rules .= " add_header Cache-Control \"max-age=0, private, no-store, no-cache, must-revalidate\";\n";
  332. break;
  333. }
  334. }
  335. if ($w3tc) {
  336. $rules .= " add_header X-Powered-By \"" . W3TC_POWERED_BY . "\";\n";
  337. }
  338. $rules .= "}\n";
  339. }
  340. }
  341. /**
  342. * Generate rules related to prevent for media 404 error by WP
  343. *
  344. * @return string
  345. */
  346. function generate_rules_no404wp() {
  347. switch (true) {
  348. case w3_is_apache():
  349. case w3_is_litespeed():
  350. return $this->generate_rules_no404wp_apache();
  351. case w3_is_nginx():
  352. return $this->generate_rules_no404wp_nginx();
  353. }
  354. return false;
  355. }
  356. /**
  357. * Generate rules related to prevent for media 404 error by WP
  358. *
  359. * @return string
  360. */
  361. function generate_rules_no404wp_apache() {
  362. $cssjs_types = $this->_get_cssjs_types();
  363. $html_types = $this->_get_html_types();
  364. $other_types = $this->_get_other_types();
  365. $extensions = array_merge(array_keys($cssjs_types), array_keys($html_types), array_keys($other_types));
  366. $permalink_structure = get_option('permalink_structure');
  367. $permalink_structure_ext = ltrim(strrchr($permalink_structure, '.'), '.');
  368. if ($permalink_structure_ext != '') {
  369. foreach ($extensions as $index => $extension) {
  370. if (strstr($extension, $permalink_structure_ext) !== false) {
  371. $extensions[$index] = preg_replace('~\|?' . w3_preg_quote($permalink_structure_ext) . '\|?~', '', $extension);
  372. }
  373. }
  374. }
  375. $exceptions = $this->_config->get_array('browsercache.no404wp.exceptions');
  376. $rules = '';
  377. $rules .= W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP . "\n";
  378. $rules .= "<IfModule mod_rewrite.c>\n";
  379. $rules .= " RewriteEngine On\n";
  380. $rules .= " RewriteCond %{REQUEST_FILENAME} !-f\n";
  381. $rules .= " RewriteCond %{REQUEST_FILENAME} !-d\n";
  382. if (count($exceptions)) {
  383. $rules .= " RewriteCond %{REQUEST_URI} !(" . implode('|', $exceptions) . ")\n";
  384. }
  385. $rules .= " RewriteCond %{REQUEST_FILENAME} \\.(" . implode('|', $extensions) . ")$ [NC]\n";
  386. $rules .= " RewriteRule .* - [L]\n";
  387. $rules .= "</IfModule>\n";
  388. $rules .= W3TC_MARKER_END_BROWSERCACHE_NO404WP . "\n";
  389. return $rules;
  390. }
  391. /**
  392. * Generate rules related to prevent for media 404 error by WP
  393. *
  394. * @return string
  395. */
  396. function generate_rules_no404wp_nginx() {
  397. $cssjs_types = $this->_get_cssjs_types();
  398. $html_types = $this->_get_html_types();
  399. $other_types = $this->_get_other_types();
  400. $extensions = array_merge(array_keys($cssjs_types), array_keys($html_types), array_keys($other_types));
  401. $permalink_structure = get_option('permalink_structure');
  402. $permalink_structure_ext = ltrim(strrchr($permalink_structure, '.'), '.');
  403. if ($permalink_structure_ext != '') {
  404. foreach ($extensions as $index => $extension) {
  405. if (strstr($extension, $permalink_structure_ext) !== false) {
  406. $extensions[$index] = preg_replace('~\|?' . w3_preg_quote($permalink_structure_ext) . '\|?~', '', $extension);
  407. }
  408. }
  409. }
  410. $exceptions = $this->_config->get_array('browsercache.no404wp.exceptions');
  411. $rules = '';
  412. $rules .= W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP . "\n";
  413. $rules .= "if (-f \$request_filename) {\n";
  414. $rules .= " break;\n";
  415. $rules .= "}\n";
  416. $rules .= "if (-d \$request_filename) {\n";
  417. $rules .= " break;\n";
  418. $rules .= "}\n";
  419. if (count($exceptions)) {
  420. $rules .= "if (\$request_uri ~ \"(" . implode('|', $exceptions) . ")\") {\n";
  421. $rules .= " break;\n";
  422. $rules .= "}\n";
  423. }
  424. $rules .= "if (\$request_uri ~* \\.(" . implode('|', $extensions) . ")$) {\n";
  425. $rules .= " return 404;\n";
  426. $rules .= "}\n";
  427. $rules .= W3TC_MARKER_END_BROWSERCACHE_NO404WP . "\n";
  428. return $rules;
  429. }
  430. /**
  431. * Writes cache rules
  432. *
  433. * @return boolean
  434. */
  435. function write_rules_cache() {
  436. $path = w3_get_browsercache_rules_cache_path();
  437. if (file_exists($path)) {
  438. $data = @file_get_contents($path);
  439. if ($data === false) {
  440. return false;
  441. }
  442. } else {
  443. $data = '';
  444. }
  445. $replace_start = strpos($data, W3TC_MARKER_BEGIN_BROWSERCACHE_CACHE);
  446. $replace_end = strpos($data, W3TC_MARKER_END_BROWSERCACHE_CACHE);
  447. if ($replace_start !== false && $replace_end !== false && $replace_start < $replace_end) {
  448. $replace_length = $replace_end - $replace_start + strlen(W3TC_MARKER_END_BROWSERCACHE_CACHE) + 1;
  449. } else {
  450. $replace_start = false;
  451. $replace_length = 0;
  452. $search = array(
  453. W3TC_MARKER_BEGIN_MINIFY_CORE => 0,
  454. W3TC_MARKER_BEGIN_PGCACHE_CORE => 0,
  455. W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP => 0,
  456. W3TC_MARKER_BEGIN_WORDPRESS => 0,
  457. W3TC_MARKER_END_PGCACHE_CACHE => strlen(W3TC_MARKER_END_PGCACHE_CACHE) + 1,
  458. W3TC_MARKER_END_MINIFY_CACHE => strlen(W3TC_MARKER_END_MINIFY_CACHE) + 1
  459. );
  460. foreach ($search as $string => $length) {
  461. $replace_start = strpos($data, $string);
  462. if ($replace_start !== false) {
  463. $replace_start += $length;
  464. break;
  465. }
  466. }
  467. }
  468. $rules = $this->generate_rules_cache();
  469. if ($replace_start !== false) {
  470. $data = w3_trim_rules(substr_replace($data, $rules, $replace_start, $replace_length));
  471. } else {
  472. $data = w3_trim_rules($data . $rules);
  473. }
  474. return @file_put_contents($path, $data);
  475. }
  476. /**
  477. * Writes no 404 by WP rules
  478. *
  479. * @return boolean
  480. */
  481. function write_rules_no404wp() {
  482. $path = w3_get_browsercache_rules_no404wp_path();
  483. if (file_exists($path)) {
  484. $data = @file_get_contents($path);
  485. if ($data === false) {
  486. return false;
  487. }
  488. } else {
  489. $data = '';
  490. }
  491. $replace_start = strpos($data, W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP);
  492. $replace_end = strpos($data, W3TC_MARKER_END_BROWSERCACHE_NO404WP);
  493. if ($replace_start !== false && $replace_end !== false && $replace_start < $replace_end) {
  494. $replace_length = $replace_end - $replace_start + strlen(W3TC_MARKER_END_BROWSERCACHE_NO404WP) + 1;
  495. } else {
  496. $replace_start = false;
  497. $replace_length = 0;
  498. $search = array(
  499. W3TC_MARKER_BEGIN_WORDPRESS => 0,
  500. W3TC_MARKER_END_PGCACHE_CORE => strlen(W3TC_MARKER_END_PGCACHE_CORE) + 1,
  501. W3TC_MARKER_END_MINIFY_CORE => strlen(W3TC_MARKER_END_MINIFY_CORE) + 1,
  502. W3TC_MARKER_END_BROWSERCACHE_CACHE => strlen(W3TC_MARKER_END_BROWSERCACHE_CACHE) + 1,
  503. W3TC_MARKER_END_PGCACHE_CACHE => strlen(W3TC_MARKER_END_PGCACHE_CACHE) + 1,
  504. W3TC_MARKER_END_MINIFY_CACHE => strlen(W3TC_MARKER_END_MINIFY_CACHE) + 1
  505. );
  506. foreach ($search as $string => $length) {
  507. $replace_start = strpos($data, $string);
  508. if ($replace_start !== false) {
  509. $replace_start += $length;
  510. break;
  511. }
  512. }
  513. }
  514. $rules = $this->generate_rules_no404wp();
  515. if ($replace_start !== false) {
  516. $data = w3_trim_rules(substr_replace($data, $rules, $replace_start, $replace_length));
  517. } else {
  518. $data = w3_trim_rules($data . $rules);
  519. }
  520. return @file_put_contents($path, $data);
  521. }
  522. /**
  523. * Erases cache rules
  524. *
  525. * @param string $data
  526. * @return string
  527. */
  528. function erase_rules_cache($data) {
  529. $data = w3_erase_rules($data, W3TC_MARKER_BEGIN_BROWSERCACHE_CACHE, W3TC_MARKER_END_BROWSERCACHE_CACHE);
  530. return $data;
  531. }
  532. /**
  533. * Erases no404wp rules
  534. *
  535. * @param string $data
  536. * @return string
  537. */
  538. function erase_rules_no404wp($data) {
  539. $data = w3_erase_rules($data, W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP, W3TC_MARKER_END_BROWSERCACHE_NO404WP);
  540. return $data;
  541. }
  542. /**
  543. * Removes cache rules
  544. *
  545. * @return boolean
  546. */
  547. function remove_rules_cache() {
  548. $path = w3_get_browsercache_rules_cache_path();
  549. if (file_exists($path)) {
  550. if (($data = @file_get_contents($path)) !== false) {
  551. $data = $this->erase_rules_cache($data);
  552. return @file_put_contents($path, $data);
  553. }
  554. return false;
  555. }
  556. return true;
  557. }
  558. /**
  559. * Removes no404wp rules
  560. *
  561. * @return boolean
  562. */
  563. function remove_rules_no404wp() {
  564. $path = w3_get_browsercache_rules_no404wp_path();
  565. if (file_exists($path)) {
  566. if (($data = @file_get_contents($path)) !== false) {
  567. $data = $this->erase_rules_no404wp($data);
  568. return @file_put_contents($path, $data);
  569. }
  570. return false;
  571. }
  572. return true;
  573. }
  574. /**
  575. * Check cache rules
  576. *
  577. * @return boolean
  578. */
  579. function check_rules_cache() {
  580. $path = w3_get_browsercache_rules_cache_path();
  581. $search = $this->generate_rules_cache();
  582. return (($data = @file_get_contents($path)) && strstr(w3_clean_rules($data), w3_clean_rules($search)) !== false);
  583. }
  584. /**
  585. * Check no404wp rules
  586. *
  587. * @return boolean
  588. */
  589. function check_rules_no404wp() {
  590. $path = w3_get_browsercache_rules_no404wp_path();
  591. $search = $this->generate_rules_no404wp();
  592. return (($data = @file_get_contents($path)) && strstr(w3_clean_rules($data), w3_clean_rules($search)) !== false);
  593. }
  594. }