PageRenderTime 30ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/sharpmachine/wakeupmedia.com
PHP | 727 lines | 471 code | 135 blank | 121 comment | 99 complexity | 4870c64a23524386176b64791b1028bb MD5 | raw file
  1. <?php
  2. /**
  3. * W3 Minify 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_MinifyAdmin
  12. */
  13. class W3_Plugin_MinifyAdmin extends W3_Plugin {
  14. /**
  15. * Activate plugin action
  16. */
  17. function activate() {
  18. require_once W3TC_INC_DIR . '/functions/activation.php';
  19. if (!@is_dir(W3TC_CONTENT_MINIFY_DIR) && !@mkdir(W3TC_CONTENT_MINIFY_DIR)) {
  20. w3_writable_error(W3TC_CONTENT_MINIFY_DIR);
  21. }
  22. $file_index = W3TC_CONTENT_MINIFY_DIR . '/index.php';
  23. if (!@copy(W3TC_INSTALL_MINIFY_DIR . '/index.php', $file_index)) {
  24. w3_writable_error($file_index);
  25. }
  26. if ($this->_config->get_boolean('minify.enabled') && $this->_config->get_boolean('minify.rewrite')) {
  27. if (w3_can_modify_rules(w3_get_minify_rules_core_path())) {
  28. $this->write_rules_core();
  29. }
  30. if ($this->_config->get_string('minify.engine') == 'file' && w3_can_modify_rules(w3_get_minify_rules_cache_path())) {
  31. $this->write_rules_cache();
  32. }
  33. }
  34. $this->schedule();
  35. }
  36. /**
  37. * Deactivate plugin action
  38. */
  39. function deactivate() {
  40. $this->unschedule();
  41. if (w3_can_modify_rules(w3_get_minify_rules_cache_path())) {
  42. $this->remove_rules_cache();
  43. }
  44. if (w3_can_modify_rules(w3_get_minify_rules_core_path())) {
  45. $this->remove_rules_core();
  46. }
  47. @unlink(W3TC_CONTENT_MINIFY_DIR . '/index.php');
  48. }
  49. /**
  50. * Schedules events
  51. */
  52. function schedule() {
  53. if ($this->_config->get_boolean('minify.enabled') && $this->_config->get_string('minify.engine') == 'file') {
  54. if (!wp_next_scheduled('w3_minify_cleanup')) {
  55. wp_schedule_event(time(), 'w3_minify_cleanup', 'w3_minify_cleanup');
  56. }
  57. } else {
  58. $this->unschedule();
  59. }
  60. }
  61. /**
  62. * Unschedules events
  63. */
  64. function unschedule() {
  65. if (wp_next_scheduled('w3_minify_cleanup')) {
  66. wp_clear_scheduled_hook('w3_minify_cleanup');
  67. }
  68. }
  69. /**
  70. * Does disk cache cleanup
  71. *
  72. * @return void
  73. */
  74. function cleanup() {
  75. require_once W3TC_LIB_W3_DIR . '/Cache/File/Cleaner/Generic.php';
  76. @$w3_cache_file_cleaner_generic = & new W3_Cache_File_Cleaner_Generic(array(
  77. 'exclude' => array(
  78. '*.files',
  79. '.htaccess',
  80. 'index.php'
  81. ),
  82. 'cache_dir' => W3TC_CACHE_FILE_MINIFY_DIR,
  83. 'expire' => $this->_config->get_integer('minify.file.gc'),
  84. 'clean_timelimit' => $this->_config->get_integer('timelimit.cache_gc')
  85. ));
  86. $w3_cache_file_cleaner_generic->clean();
  87. }
  88. /**
  89. * Called from admin interface before configuration is changed
  90. *
  91. * @param object $old_config
  92. * @param object $new_config
  93. * @return void
  94. */
  95. function before_config_change(&$old_config, &$new_config) {
  96. if ($old_config->get_integer('minify.file.gc') !=
  97. $new_config->get_integer('minify.file.gc')) {
  98. $this->unschedule();
  99. }
  100. }
  101. /**
  102. * Called from admin interface after configuration is changed
  103. */
  104. function after_config_change() {
  105. $this->schedule();
  106. if ($this->_config->get_boolean('minify.enabled') &&
  107. $this->_config->get_boolean('minify.rewrite')) {
  108. if (w3_can_modify_rules(w3_get_minify_rules_core_path())) {
  109. $this->write_rules_core();
  110. }
  111. if ($this->_config->get_string('minify.engine') == 'file') {
  112. if (w3_can_modify_rules(w3_get_minify_rules_cache_path())) {
  113. $this->write_rules_cache();
  114. }
  115. } else {
  116. if (w3_can_modify_rules(w3_get_minify_rules_cache_path())) {
  117. $this->remove_rules_cache();
  118. }
  119. }
  120. } else {
  121. if (w3_can_modify_rules(w3_get_minify_rules_core_path())) {
  122. $this->remove_rules_core();
  123. }
  124. if (w3_can_modify_rules(w3_get_minify_rules_cache_path())) {
  125. $this->remove_rules_cache();
  126. }
  127. }
  128. }
  129. /**
  130. * Generates rules
  131. *
  132. * @return string
  133. */
  134. function generate_rules_core() {
  135. switch (true) {
  136. case w3_is_apache():
  137. case w3_is_litespeed():
  138. return $this->generate_rules_core_apache();
  139. case w3_is_nginx():
  140. return $this->generate_rules_core_nginx();
  141. }
  142. return false;
  143. }
  144. /**
  145. * Generates rules
  146. *
  147. * @return string
  148. */
  149. function generate_rules_core_apache() {
  150. $cache_dir = str_replace(w3_get_document_root(), '', w3_path(W3TC_CACHE_FILE_MINIFY_DIR));
  151. $engine = $this->_config->get_string('minify.engine');
  152. $browsercache = $this->_config->get_integer('browsercache.enabled');
  153. $compression = ($browsercache && $this->_config->get_boolean('browsercache.cssjs.compression'));
  154. $rules = '';
  155. $rules .= W3TC_MARKER_BEGIN_MINIFY_CORE . "\n";
  156. $rules .= "<IfModule mod_rewrite.c>\n";
  157. $rules .= " RewriteEngine On\n";
  158. $rules .= " RewriteBase " . $cache_dir . "/\n";
  159. $rules .= " RewriteRule ^w3tc_rewrite_test$ index.php?w3tc_rewrite_test=1 [L]\n";
  160. if ($engine == 'file') {
  161. if ($compression) {
  162. $rules .= " RewriteCond %{HTTP:Accept-Encoding} gzip\n";
  163. $rules .= " RewriteRule .* - [E=APPEND_EXT:.gzip]\n";
  164. }
  165. $rules .= " RewriteCond %{REQUEST_FILENAME}%{ENV:APPEND_EXT} -" . ($this->_config->get_boolean('minify.file.nfs') ? 'F' : 'f') . "\n";
  166. $rules .= " RewriteRule (.*) $1%{ENV:APPEND_EXT} [L]\n";
  167. }
  168. $rules .= " RewriteRule ^(.+\\.(css|js))$ index.php?file=$1 [L]\n";
  169. $rules .= "</IfModule>\n";
  170. $rules .= W3TC_MARKER_END_MINIFY_CORE . "\n";
  171. return $rules;
  172. }
  173. /**
  174. * Generates rules
  175. *
  176. * @return string
  177. */
  178. function generate_rules_core_nginx() {
  179. $is_network = w3_is_network();
  180. $cache_root = w3_path(W3TC_CACHE_FILE_MINIFY_DIR);
  181. $cache_dir_condition = $cache_dir_rewrite = rtrim(str_replace(w3_get_document_root(), '', $cache_root), '/');
  182. $offset = 1;
  183. if ($is_network) {
  184. $cache_dir_condition = preg_replace('~/w3tc.*?/~', '/w3tc(.*?)/', $cache_dir_condition, 1);
  185. $cache_dir_rewrite = preg_replace('~/w3tc.*?/~', '/w3tc\$' . $offset++ . '/', $cache_dir_rewrite, 1);
  186. }
  187. $engine = $this->_config->get_string('minify.engine');
  188. $browsercache = $this->_config->get_integer('browsercache.enabled');
  189. $compression = ($browsercache && $this->_config->get_boolean('browsercache.cssjs.compression'));
  190. $rules = '';
  191. $rules .= W3TC_MARKER_BEGIN_MINIFY_CORE . "\n";
  192. $rules .= "rewrite ^" . $cache_dir_condition . "/w3tc_rewrite_test$ " . $cache_dir_rewrite . "/index.php?w3tc_rewrite_test=1 last;\n";
  193. if ($engine == 'file') {
  194. $rules .= "set \$w3tc_enc \"\";\n";
  195. if ($compression) {
  196. $rules .= "if (\$http_accept_encoding ~ gzip) {\n";
  197. $rules .= " set \$w3tc_enc .gzip;\n";
  198. $rules .= "}\n";
  199. }
  200. $rules .= "if (-f \$request_filename\$w3tc_enc) {\n";
  201. $rules .= " rewrite (.*) $1\$w3tc_enc break;\n";
  202. $rules .= "}\n";
  203. }
  204. $rules .= "rewrite ^" . $cache_dir_condition . "/(.+\\.(css|js))$ " . $cache_dir_rewrite . "/index.php?file=$" . $offset . " last;\n";
  205. $rules .= W3TC_MARKER_END_MINIFY_CORE . "\n";
  206. return $rules;
  207. }
  208. /**
  209. * Generates rules
  210. *
  211. * @return string
  212. */
  213. function generate_rules_cache() {
  214. switch (true) {
  215. case w3_is_apache():
  216. case w3_is_litespeed():
  217. return $this->generate_rules_cache_apache();
  218. case w3_is_nginx():
  219. return $this->generate_rules_cache_nginx();
  220. }
  221. return false;
  222. }
  223. /**
  224. * Generates rules
  225. *
  226. * @return string
  227. */
  228. function generate_rules_cache_apache() {
  229. $browsercache = $this->_config->get_integer('browsercache.enabled');
  230. $compression = ($browsercache && $this->_config->get_boolean('browsercache.cssjs.compression'));
  231. $expires = ($browsercache && $this->_config->get_boolean('browsercache.cssjs.expires'));
  232. $lifetime = ($browsercache ? $this->_config->get_integer('browsercache.cssjs.lifetime') : 0);
  233. $cache_control = ($browsercache && $this->_config->get_boolean('browsercache.cssjs.cache.control'));
  234. $etag = ($browsercache && $this->_config->get_integer('browsercache.html.etag'));
  235. $w3tc = ($browsercache && $this->_config->get_integer('browsercache.cssjs.w3tc'));
  236. $rules = '';
  237. $rules .= W3TC_MARKER_BEGIN_MINIFY_CACHE . "\n";
  238. if ($etag) {
  239. $rules .= "FileETag MTime Size\n";
  240. }
  241. if ($compression) {
  242. $rules .= "<IfModule mod_mime.c>\n";
  243. $rules .= " AddEncoding gzip .gzip\n";
  244. $rules .= " <Files *.css.gzip>\n";
  245. $rules .= " ForceType text/css\n";
  246. $rules .= " </Files>\n";
  247. $rules .= " <Files *.js.gzip>\n";
  248. $rules .= " ForceType application/x-javascript\n";
  249. $rules .= " </Files>\n";
  250. $rules .= "</IfModule>\n";
  251. $rules .= "<IfModule mod_deflate.c>\n";
  252. $rules .= " <IfModule mod_setenvif.c>\n";
  253. $rules .= " SetEnvIfNoCase Request_URI \\.gzip$ no-gzip\n";
  254. $rules .= " </IfModule>\n";
  255. $rules .= "</IfModule>\n";
  256. }
  257. if ($expires) {
  258. $rules .= "<IfModule mod_expires.c>\n";
  259. $rules .= " ExpiresActive On\n";
  260. $rules .= " ExpiresByType text/css M" . $lifetime . "\n";
  261. $rules .= " ExpiresByType application/x-javascript M" . $lifetime . "\n";
  262. $rules .= "</IfModule>\n";
  263. }
  264. if ($w3tc || $compression || $cache_control) {
  265. $rules .= "<IfModule mod_headers.c>\n";
  266. if ($w3tc) {
  267. $rules .= " Header set X-Powered-By \"" . W3TC_POWERED_BY . "\"\n";
  268. }
  269. if ($compression) {
  270. $rules .= " Header set Vary \"Accept-Encoding\"\n";
  271. }
  272. if ($cache_control) {
  273. $cache_policy = $this->_config->get_string('browsercache.cssjs.cache.policy');
  274. switch ($cache_policy) {
  275. case 'cache':
  276. $rules .= " Header set Pragma \"public\"\n";
  277. $rules .= " Header set Cache-Control \"public\"\n";
  278. break;
  279. case 'cache_validation':
  280. $rules .= " Header set Pragma \"public\"\n";
  281. $rules .= " Header set Cache-Control \"public, must-revalidate, proxy-revalidate\"\n";
  282. break;
  283. case 'cache_noproxy':
  284. $rules .= " Header set Pragma \"public\"\n";
  285. $rules .= " Header set Cache-Control \"public, must-revalidate\"\n";
  286. break;
  287. case 'cache_maxage':
  288. $rules .= " Header set Pragma \"public\"\n";
  289. if ($expires) {
  290. $rules .= " Header append Cache-Control \"public, must-revalidate, proxy-revalidate\"\n";
  291. } else {
  292. $rules .= " Header set Cache-Control \"max-age=" . $lifetime . ", public, must-revalidate, proxy-revalidate\"\n";
  293. }
  294. break;
  295. case 'no_cache':
  296. $rules .= " Header set Pragma \"no-cache\"\n";
  297. $rules .= " Header set Cache-Control \"max-age=0, private, no-store, no-cache, must-revalidate\"\n";
  298. break;
  299. }
  300. }
  301. $rules .= "</IfModule>\n";
  302. }
  303. $rules .= W3TC_MARKER_END_MINIFY_CACHE . "\n";
  304. return $rules;
  305. }
  306. /**
  307. * Generates rules
  308. *
  309. * @return string
  310. */
  311. function generate_rules_cache_nginx() {
  312. $cache_root = w3_path(W3TC_CACHE_FILE_MINIFY_DIR);
  313. $cache_dir = rtrim(str_replace(w3_get_document_root(), '', $cache_root), '/');
  314. if (w3_is_network()) {
  315. $cache_dir = preg_replace('~/w3tc.*?/~', '/w3tc.*?/', $cache_dir, 1);
  316. }
  317. $browsercache = $this->_config->get_integer('browsercache.enabled');
  318. $compression = ($browsercache && $this->_config->get_boolean('browsercache.cssjs.compression'));
  319. $expires = ($browsercache && $this->_config->get_boolean('browsercache.cssjs.expires'));
  320. $lifetime = ($browsercache ? $this->_config->get_integer('browsercache.cssjs.lifetime') : 0);
  321. $cache_control = ($browsercache && $this->_config->get_boolean('browsercache.cssjs.cache.control'));
  322. $w3tc = ($browsercache && $this->_config->get_integer('browsercache.cssjs.w3tc'));
  323. $rules = '';
  324. $rules .= W3TC_MARKER_BEGIN_MINIFY_CACHE . "\n";
  325. $common_rules = '';
  326. if ($expires) {
  327. $common_rules .= " expires modified " . $lifetime . "s;\n";
  328. }
  329. if ($w3tc) {
  330. $common_rules .= " add_header X-Powered-By \"" . W3TC_POWERED_BY . "\";\n";
  331. }
  332. if ($compression) {
  333. $common_rules .= " add_header Vary \"Accept-Encoding\";\n";
  334. }
  335. if ($cache_control) {
  336. $cache_policy = $this->_config->get_string('browsercache.cssjs.cache.policy');
  337. switch ($cache_policy) {
  338. case 'cache':
  339. $common_rules .= " add_header Pragma \"public\";\n";
  340. $common_rules .= " add_header Cache-Control \"public\";\n";
  341. break;
  342. case 'cache_validation':
  343. $common_rules .= " add_header Pragma \"public\";\n";
  344. $common_rules .= " add_header Cache-Control \"public, must-revalidate, proxy-revalidate\";\n";
  345. break;
  346. case 'cache_noproxy':
  347. $common_rules .= " add_header Pragma \"public\";\n";
  348. $common_rules .= " add_header Cache-Control \"public, must-revalidate\";\n";
  349. break;
  350. case 'cache_maxage':
  351. $common_rules .= " add_header Pragma \"public\";\n";
  352. $common_rules .= " add_header Cache-Control \"max-age=" . $lifetime . ", public, must-revalidate, proxy-revalidate\";\n";
  353. break;
  354. case 'no_cache':
  355. $common_rules .= " add_header Pragma \"no-cache\";\n";
  356. $common_rules .= " add_header Cache-Control \"max-age=0, private, no-store, no-cache, must-revalidate\";\n";
  357. break;
  358. }
  359. }
  360. $rules .= "location ~ " . $cache_dir . ".*\\.js$ {\n";
  361. $rules .= " types {}\n";
  362. $rules .= " default_type application/x-javascript;\n";
  363. $rules .= $common_rules;
  364. $rules .= "}\n";
  365. $rules .= "location ~ " . $cache_dir . ".*\\.css$ {\n";
  366. $rules .= " types {}\n";
  367. $rules .= " default_type text/css;\n";
  368. $rules .= $common_rules;
  369. $rules .= "}\n";
  370. if ($compression) {
  371. $rules .= "location ~ " . $cache_dir . ".*js\\.gzip$ {\n";
  372. $rules .= " gzip off;\n";
  373. $rules .= " types {}\n";
  374. $rules .= " default_type application/x-javascript;\n";
  375. $rules .= $common_rules;
  376. $rules .= " add_header Content-Encoding gzip;\n";
  377. $rules .= "}\n";
  378. $rules .= "location ~ " . $cache_dir . ".*css\\.gzip$ {\n";
  379. $rules .= " gzip off;\n";
  380. $rules .= " types {}\n";
  381. $rules .= " default_type text/css;\n";
  382. $rules .= $common_rules;
  383. $rules .= " add_header Content-Encoding gzip;\n";
  384. $rules .= "}\n";
  385. }
  386. $rules .= W3TC_MARKER_END_MINIFY_CACHE . "\n";
  387. return $rules;
  388. }
  389. /**
  390. * Writes rules to file cache .htaccess
  391. *
  392. * @return boolean
  393. */
  394. function write_rules_core() {
  395. $path = w3_get_minify_rules_core_path();
  396. if (file_exists($path)) {
  397. $data = @file_get_contents($path);
  398. if ($data !== false) {
  399. $data = $this->erase_rules_legacy($data);
  400. } else {
  401. return false;
  402. }
  403. } else {
  404. $data = '';
  405. }
  406. $replace_start = strpos($data, W3TC_MARKER_BEGIN_MINIFY_CORE);
  407. $replace_end = strpos($data, W3TC_MARKER_END_MINIFY_CORE);
  408. if ($replace_start !== false && $replace_end !== false && $replace_start < $replace_end) {
  409. $replace_length = $replace_end - $replace_start + strlen(W3TC_MARKER_END_MINIFY_CORE) + 1;
  410. } else {
  411. $replace_start = false;
  412. $replace_length = 0;
  413. $search = array(
  414. W3TC_MARKER_BEGIN_PGCACHE_CORE => 0,
  415. W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP => 0,
  416. W3TC_MARKER_BEGIN_WORDPRESS => 0,
  417. W3TC_MARKER_END_BROWSERCACHE_CACHE => strlen(W3TC_MARKER_END_BROWSERCACHE_CACHE) + 1,
  418. W3TC_MARKER_END_PGCACHE_CACHE => strlen(W3TC_MARKER_END_PGCACHE_CACHE) + 1,
  419. W3TC_MARKER_END_MINIFY_CACHE => strlen(W3TC_MARKER_END_MINIFY_CACHE) + 1
  420. );
  421. foreach ($search as $string => $length) {
  422. $replace_start = strpos($data, $string);
  423. if ($replace_start !== false) {
  424. $replace_start += $length;
  425. break;
  426. }
  427. }
  428. }
  429. $rules = $this->generate_rules_core();
  430. if ($replace_start !== false) {
  431. $data = w3_trim_rules(substr_replace($data, $rules, $replace_start, $replace_length));
  432. } else {
  433. $data = w3_trim_rules($data . $rules);
  434. }
  435. return @file_put_contents($path, $data);
  436. }
  437. /**
  438. * Writes rules to file cache .htaccess
  439. *
  440. * @return boolean
  441. */
  442. function write_rules_cache() {
  443. $path = w3_get_minify_rules_cache_path();
  444. if (file_exists($path)) {
  445. $data = @file_get_contents($path);
  446. if ($data !== false) {
  447. $data = $this->erase_rules_legacy($data);
  448. } else {
  449. return false;
  450. }
  451. } else {
  452. $data = '';
  453. }
  454. $replace_start = strpos($data, W3TC_MARKER_BEGIN_MINIFY_CACHE);
  455. $replace_end = strpos($data, W3TC_MARKER_END_MINIFY_CACHE);
  456. if ($replace_start !== false && $replace_end !== false && $replace_start < $replace_end) {
  457. $replace_length = $replace_end - $replace_start + strlen(W3TC_MARKER_END_MINIFY_CACHE) + 1;
  458. } else {
  459. $replace_start = false;
  460. $replace_length = 0;
  461. $search = array(
  462. W3TC_MARKER_BEGIN_PGCACHE_CACHE => 0,
  463. W3TC_MARKER_BEGIN_BROWSERCACHE_CACHE => 0,
  464. W3TC_MARKER_BEGIN_MINIFY_CORE => 0,
  465. W3TC_MARKER_BEGIN_PGCACHE_CORE => 0,
  466. W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP => 0,
  467. W3TC_MARKER_BEGIN_WORDPRESS => 0
  468. );
  469. foreach ($search as $string => $length) {
  470. $replace_start = strpos($data, $string);
  471. if ($replace_start !== false) {
  472. $replace_start += $length;
  473. break;
  474. }
  475. }
  476. }
  477. $rules = $this->generate_rules_cache();
  478. if ($replace_start !== false) {
  479. $data = w3_trim_rules(substr_replace($data, $rules, $replace_start, $replace_length));
  480. } else {
  481. $data = w3_trim_rules($data . $rules);
  482. }
  483. return @file_put_contents($path, $data);
  484. }
  485. /**
  486. * Erases Minify core directives
  487. *
  488. * @param string $data
  489. * @return string
  490. */
  491. function erase_rules_core($data) {
  492. $data = w3_erase_rules($data, W3TC_MARKER_BEGIN_MINIFY_CORE, W3TC_MARKER_END_MINIFY_CORE);
  493. return $data;
  494. }
  495. /**
  496. * Erases Minify cache directives
  497. *
  498. * @param string $data
  499. * @return string
  500. */
  501. function erase_rules_cache($data) {
  502. $data = w3_erase_rules($data, W3TC_MARKER_BEGIN_MINIFY_CACHE, W3TC_MARKER_END_MINIFY_CACHE);
  503. return $data;
  504. }
  505. /**
  506. * Erases Minify legacy directives
  507. *
  508. * @param string $data
  509. * @return string
  510. */
  511. function erase_rules_legacy($data) {
  512. $data = w3_erase_rules($data, W3TC_MARKER_BEGIN_MINIFY_LEGACY, W3TC_MARKER_END_MINIFY_LEGACY);
  513. return $data;
  514. }
  515. /**
  516. * Removes Minify core directives
  517. *
  518. * @return boolean
  519. */
  520. function remove_rules_core() {
  521. $path = w3_get_minify_rules_core_path();
  522. if (file_exists($path)) {
  523. if (($data = @file_get_contents($path)) !== false) {
  524. $data = $this->erase_rules_core($data);
  525. return @file_put_contents($path, $data);
  526. }
  527. return false;
  528. }
  529. return true;
  530. }
  531. /**
  532. * Removes Minify cache directives
  533. *
  534. * @return boolean
  535. */
  536. function remove_rules_cache() {
  537. $path = w3_get_minify_rules_cache_path();
  538. if (file_exists($path)) {
  539. if (($data = @file_get_contents($path)) !== false) {
  540. $data = $this->erase_rules_cache($data);
  541. return @file_put_contents($path, $data);
  542. }
  543. return false;
  544. }
  545. return true;
  546. }
  547. /**
  548. * Removes Minify legacy directives
  549. *
  550. * @return boolean
  551. */
  552. function remove_rules_legacy() {
  553. $path = w3_get_minify_rules_cache_path();
  554. if (file_exists($path)) {
  555. if (($data = @file_get_contents($path)) !== false) {
  556. $data = $this->erase_rules_legacy($data);
  557. return @file_put_contents($path, $data);
  558. }
  559. return false;
  560. }
  561. return true;
  562. }
  563. /**
  564. * Checks if core rules exists
  565. *
  566. * @return boolean
  567. */
  568. function check_rules_core() {
  569. $path = w3_get_minify_rules_core_path();
  570. $search = $this->generate_rules_core();
  571. return (($data = @file_get_contents($path)) && strstr(w3_clean_rules($data), w3_clean_rules($search)) !== false);
  572. }
  573. /**
  574. * Checks if cache rules exists
  575. *
  576. * @return boolean
  577. */
  578. function check_rules_cache() {
  579. $path = w3_get_minify_rules_cache_path();
  580. $search = $this->generate_rules_cache();
  581. return (($data = @file_get_contents($path)) && strstr(w3_clean_rules($data), w3_clean_rules($search)) !== false);
  582. }
  583. /**
  584. * Check if legacy rules exists
  585. *
  586. * @return boolean
  587. */
  588. function check_rules_legacy() {
  589. $path = w3_get_minify_rules_core_path();
  590. return (($data = @file_get_contents($path)) && w3_has_rules(w3_clean_rules($data), W3TC_MARKER_BEGIN_MINIFY_LEGACY, W3TC_MARKER_END_MINIFY_LEGACY));
  591. }
  592. }