PageRenderTime 66ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/kennethreitz-archive/wordpress-skeleton
PHP | 2875 lines | 1864 code | 486 blank | 525 comment | 439 complexity | 5104b3a9260136b4cc2a289c2b6b5a7b MD5 | raw file
  1. <?php
  2. /**
  3. * W3 Total Cache plugin
  4. */
  5. require_once W3TC_LIB_W3_DIR . '/Plugin.php';
  6. /**
  7. * Class W3_Plugin_TotalCache
  8. */
  9. class W3_Plugin_TotalCache extends W3_Plugin
  10. {
  11. /**
  12. * Page tab
  13. * @var string
  14. */
  15. var $_tab = '';
  16. /**
  17. * Notes
  18. * @var array
  19. */
  20. var $_notes = array();
  21. /**
  22. * Errors
  23. * @var array
  24. */
  25. var $_errors = array();
  26. /**
  27. * Show support reminder flag
  28. * @var boolean
  29. */
  30. var $_support_reminder = false;
  31. /**
  32. * Used in PHPMailer init function
  33. * @var string
  34. */
  35. var $_phpmailer_sender = '';
  36. /**
  37. * Runs plugin
  38. */
  39. function run()
  40. {
  41. register_activation_hook(W3TC_FILE, array(
  42. &$this,
  43. 'activate'
  44. ));
  45. register_deactivation_hook(W3TC_FILE, array(
  46. &$this,
  47. 'deactivate'
  48. ));
  49. add_action('admin_menu', array(
  50. &$this,
  51. 'admin_menu'
  52. ));
  53. add_filter('plugin_action_links_' . W3TC_FILE, array(
  54. &$this,
  55. 'plugin_action_links'
  56. ));
  57. add_filter('favorite_actions', array(
  58. &$this,
  59. 'favorite_actions'
  60. ));
  61. add_action('init', array(
  62. &$this,
  63. 'init'
  64. ));
  65. add_action('in_plugin_update_message-' . W3TC_FILE, array(
  66. &$this,
  67. 'in_plugin_update_message'
  68. ));
  69. if ($this->_config->get_boolean('widget.latest.enabled')) {
  70. add_action('wp_dashboard_setup', array(
  71. &$this,
  72. 'wp_dashboard_setup'
  73. ));
  74. }
  75. if ($this->_config->get_boolean('cdn.enabled')) {
  76. add_action('switch_theme', array(
  77. &$this,
  78. 'switch_theme'
  79. ));
  80. add_filter('update_feedback', array(
  81. &$this,
  82. 'update_feedback'
  83. ));
  84. }
  85. if ($this->_config->get_boolean('pgcache.enabled') || $this->_config->get_boolean('minify.enabled')) {
  86. add_filter('pre_update_option_active_plugins', array(
  87. &$this,
  88. 'pre_update_option_active_plugins'
  89. ));
  90. }
  91. if ($this->_config->get_string('common.support') == 'footer') {
  92. add_action('wp_footer', array(
  93. &$this,
  94. 'footer'
  95. ));
  96. }
  97. if ($this->can_modify_contents()) {
  98. ob_start(array(
  99. &$this,
  100. 'ob_callback'
  101. ));
  102. }
  103. /**
  104. * Run DbCache plugin
  105. */
  106. require_once W3TC_DIR . '/lib/W3/Plugin/DbCache.php';
  107. $w3_plugin_dbcache = & W3_Plugin_DbCache::instance();
  108. $w3_plugin_dbcache->run();
  109. /**
  110. * Run PgCache plugin
  111. */
  112. require_once W3TC_DIR . '/lib/W3/Plugin/PgCache.php';
  113. $w3_plugin_pgcache = & W3_Plugin_PgCache::instance();
  114. $w3_plugin_pgcache->run();
  115. /**
  116. * Run CDN plugin
  117. */
  118. require_once W3TC_DIR . '/lib/W3/Plugin/Cdn.php';
  119. $w3_plugin_cdn = & W3_Plugin_Cdn::instance();
  120. $w3_plugin_cdn->run();
  121. /**
  122. * Run Minify plugin
  123. */
  124. if (W3TC_PHP5) {
  125. require_once W3TC_DIR . '/lib/W3/Plugin/Minify.php';
  126. $w3_plugin_minify = & W3_Plugin_Minify::instance();
  127. $w3_plugin_minify->run();
  128. }
  129. }
  130. /**
  131. * Returns plugin instance
  132. *
  133. * @return W3_Plugin_TotalCache
  134. */
  135. function &instance()
  136. {
  137. static $instances = array();
  138. if (!isset($instances[0])) {
  139. $class = __CLASS__;
  140. $instances[0] = & new $class();
  141. }
  142. return $instances[0];
  143. }
  144. /**
  145. * Check for buggy site-wide activation
  146. */
  147. function wpmu_check()
  148. {
  149. $sitewide_plugins = (array) @unserialize(get_site_option('active_sitewide_plugins'));
  150. $plugins = (array) @unserialize(get_option('active_plugins'));
  151. }
  152. /**
  153. * Activate plugin action
  154. */
  155. function activate()
  156. {
  157. if (!is_dir(W3TC_CONTENT_DIR)) {
  158. if (@mkdir(W3TC_CONTENT_DIR, 0755)) {
  159. @chmod(W3TC_CONTENT_DIR, 0755);
  160. } else {
  161. w3_writable_error(W3TC_CONTENT_DIR);
  162. }
  163. }
  164. if (!is_dir(W3TC_CACHE_FILE_DBCACHE_DIR)) {
  165. if (@mkdir(W3TC_CACHE_FILE_DBCACHE_DIR, 0755)) {
  166. @chmod(W3TC_CACHE_FILE_DBCACHE_DIR, 0755);
  167. } else {
  168. w3_writable_error(W3TC_CACHE_FILE_DBCACHE_DIR);
  169. }
  170. }
  171. if (!is_dir(W3TC_CACHE_FILE_PGCACHE_DIR)) {
  172. if (@mkdir(W3TC_CACHE_FILE_PGCACHE_DIR, 0755)) {
  173. @chmod(W3TC_CACHE_FILE_PGCACHE_DIR, 0755);
  174. } else {
  175. w3_writable_error(W3TC_CACHE_FILE_PGCACHE_DIR);
  176. }
  177. }
  178. if (!is_dir(W3TC_CACHE_FILE_MINIFY_DIR)) {
  179. if (@mkdir(W3TC_CACHE_FILE_MINIFY_DIR, 0755)) {
  180. @chmod(W3TC_CACHE_FILE_MINIFY_DIR, 0755);
  181. } else {
  182. w3_writable_error(W3TC_CACHE_FILE_MINIFY_DIR);
  183. }
  184. }
  185. if (!is_dir(W3TC_LOG_DIR)) {
  186. if (@mkdir(W3TC_LOG_DIR, 0755)) {
  187. @chmod(W3TC_LOG_DIR, 0755);
  188. } else {
  189. w3_writable_error(W3TC_LOG_DIR);
  190. }
  191. }
  192. if (!is_dir(W3TC_TMP_DIR)) {
  193. if (@mkdir(W3TC_TMP_DIR, 0755)) {
  194. @chmod(W3TC_TMP_DIR, 0755);
  195. } else {
  196. w3_writable_error(W3TC_TMP_DIR);
  197. }
  198. }
  199. if (!$this->_config->get_integer('common.install')) {
  200. $this->_config->set('common.install', time());
  201. }
  202. if (w3_is_wpmu()) {
  203. $this->_config->load_master();
  204. }
  205. if (!$this->_config->save()) {
  206. w3_writable_error(W3TC_CONFIG_PATH);
  207. }
  208. delete_option('w3tc_request_data');
  209. add_option('w3tc_request_data', '', null, 'no');
  210. $this->link_update();
  211. }
  212. /**
  213. * Deactivate plugin action
  214. * @todo Complete plugin uninstall on site wide activation
  215. */
  216. function deactivate()
  217. {
  218. $this->link_delete();
  219. delete_option('w3tc_request_data');
  220. w3_rmdir(W3TC_TMP_DIR);
  221. w3_rmdir(W3TC_LOG_DIR);
  222. w3_rmdir(W3TC_CACHE_FILE_MINIFY_DIR);
  223. w3_rmdir(W3TC_CACHE_FILE_PGCACHE_DIR);
  224. w3_rmdir(W3TC_CACHE_FILE_DBCACHE_DIR);
  225. w3_rmdir(W3TC_CONTENT_DIR);
  226. }
  227. /**
  228. * Init action
  229. */
  230. function init()
  231. {
  232. $this->check_request();
  233. }
  234. /**
  235. * Load action
  236. */
  237. function load()
  238. {
  239. require_once W3TC_LIB_W3_DIR . '/Request.php';
  240. $this->_tab = W3_Request::get_string('tab');
  241. switch (true) {
  242. case ($this->_tab == 'general'):
  243. case ($this->_tab == 'pgcache'):
  244. case ($this->_tab == 'dbcache'):
  245. case ($this->_tab == 'minify' && W3TC_PHP5):
  246. case ($this->_tab == 'cdn'):
  247. case ($this->_tab == 'install'):
  248. case ($this->_tab == 'faq'):
  249. case ($this->_tab == 'about'):
  250. case ($this->_tab == 'support'):
  251. break;
  252. default:
  253. $this->_tab = 'general';
  254. }
  255. /**
  256. * Flush all caches
  257. */
  258. if (isset($_REQUEST['flush_all'])) {
  259. $this->flush_memcached();
  260. $this->flush_opcode();
  261. $this->flush_file();
  262. $this->redirect(array(
  263. 'note' => 'flush_all'
  264. ), true);
  265. }
  266. /**
  267. * Flush memcached cache
  268. */
  269. if (isset($_REQUEST['flush_memcached'])) {
  270. $this->flush_memcached();
  271. $this->redirect(array(
  272. 'note' => 'flush_memcached'
  273. ), true);
  274. }
  275. /**
  276. * Flush APC cache
  277. */
  278. if (isset($_REQUEST['flush_opcode'])) {
  279. $this->flush_opcode();
  280. $this->redirect(array(
  281. 'note' => 'flush_opcode'
  282. ), true);
  283. }
  284. /**
  285. * Flush disk cache
  286. */
  287. if (isset($_REQUEST['flush_file'])) {
  288. $this->flush_file();
  289. $this->redirect(array(
  290. 'note' => 'flush_file'
  291. ), true);
  292. }
  293. /**
  294. * Flush page cache
  295. */
  296. if (isset($_REQUEST['flush_pgcache'])) {
  297. $this->flush_pgcache();
  298. $this->_config->set('notes.need_empty_pgcache', false);
  299. $this->_config->set('notes.plugins_updated', false);
  300. if (!$this->_config->save()) {
  301. $this->redirect(array(
  302. 'error' => 'config_save'
  303. ), true);
  304. }
  305. $this->redirect(array(
  306. 'note' => 'flush_pgcache'
  307. ), true);
  308. }
  309. /**
  310. * Flush db cache
  311. */
  312. if (isset($_REQUEST['flush_dbcache'])) {
  313. $this->flush_dbcache();
  314. $this->redirect(array(
  315. 'note' => 'flush_dbcache'
  316. ), true);
  317. }
  318. /**
  319. * Flush minify cache
  320. */
  321. if (isset($_REQUEST['flush_minify'])) {
  322. $this->flush_minify();
  323. $this->_config->set('notes.need_empty_minify', false);
  324. if (!$this->_config->save()) {
  325. $this->redirect(array(
  326. 'error' => 'config_save'
  327. ), true);
  328. }
  329. $this->redirect(array(
  330. 'note' => 'flush_minify'
  331. ), true);
  332. }
  333. /**
  334. * Hide notes
  335. */
  336. if (isset($_REQUEST['hide_note'])) {
  337. $setting = sprintf('notes.%s', W3_Request::get_string('hide_note'));
  338. $this->_config->set($setting, false);
  339. if (!$this->_config->save()) {
  340. $this->redirect(array(
  341. 'error' => 'config_save'
  342. ), true);
  343. }
  344. $this->redirect(array(), true);
  345. }
  346. /**
  347. * Save config
  348. */
  349. if (isset($_REQUEST['save_config'])) {
  350. if ($this->_config->save()) {
  351. $this->redirect(array(
  352. 'note' => 'config_save'
  353. ), true);
  354. } else {
  355. $this->redirect(array(
  356. 'error' => 'config_save'
  357. ), true);
  358. }
  359. }
  360. /**
  361. * Write page cache rules
  362. */
  363. if (isset($_REQUEST['pgcache_write_rules_core'])) {
  364. require_once W3TC_LIB_W3_DIR . '/Plugin/PgCache.php';
  365. $w3_plugin_pgcache = & W3_Plugin_PgCache::instance();
  366. if ($w3_plugin_pgcache->write_rules_core()) {
  367. $this->redirect(array(
  368. 'note' => 'pgcache_write_rules_core'
  369. ));
  370. } else {
  371. $this->redirect(array(
  372. 'error' => 'pgcache_write_rules_core'
  373. ));
  374. }
  375. }
  376. if (isset($_REQUEST['pgcache_write_rules_cache'])) {
  377. require_once W3TC_LIB_W3_DIR . '/Plugin/PgCache.php';
  378. $w3_plugin_pgcache = & W3_Plugin_PgCache::instance();
  379. if ($w3_plugin_pgcache->write_rules_cache()) {
  380. $this->redirect(array(
  381. 'note' => 'pgcache_write_rules_cache'
  382. ));
  383. } else {
  384. $this->redirect(array(
  385. 'error' => 'pgcache_write_rules_cache'
  386. ));
  387. }
  388. }
  389. /**
  390. * Write minify rules
  391. */
  392. if (W3TC_PHP5 && isset($_REQUEST['minify_write_rules'])) {
  393. require_once W3TC_LIB_W3_DIR . '/Plugin/Minify.php';
  394. $w3_plugin_minify = & W3_Plugin_Minify::instance();
  395. if ($w3_plugin_minify->write_rules()) {
  396. $this->redirect(array(
  397. 'note' => 'minify_write_rules'
  398. ));
  399. } else {
  400. $this->redirect(array(
  401. 'error' => 'minify_write_rules'
  402. ));
  403. }
  404. }
  405. /**
  406. * Save support us options
  407. */
  408. if (isset($_REQUEST['save_support_us'])) {
  409. $support = W3_Request::get_string('support');
  410. $this->_config->set('common.support', $support);
  411. if (!$this->_config->save()) {
  412. $this->redirect(array(
  413. 'error' => 'config_save'
  414. ));
  415. }
  416. $this->link_update();
  417. $this->redirect(array(
  418. 'note' => 'config_save'
  419. ));
  420. }
  421. /**
  422. * Run plugin action
  423. */
  424. if (isset($_REQUEST['w3tc_action'])) {
  425. $action = trim($_REQUEST['w3tc_action']);
  426. if (method_exists($this, $action)) {
  427. call_user_func(array(
  428. &$this,
  429. $action
  430. ));
  431. exit();
  432. }
  433. }
  434. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  435. if ($this->_tab == 'support') {
  436. $this->support_request();
  437. } else {
  438. $this->options_save();
  439. }
  440. exit();
  441. }
  442. $this->_support_reminder = ($this->_config->get_boolean('notes.support_us') && $this->_config->get_integer('common.install') < (time() - W3TC_SUPPORT_US_TIMEOUT) && !$this->is_supported());
  443. wp_enqueue_style('w3tc-options', WP_PLUGIN_URL . '/w3-total-cache/inc/css/options.css');
  444. wp_enqueue_style('w3tc-lightbox', WP_PLUGIN_URL . '/w3-total-cache/inc/css/lightbox.css');
  445. }
  446. /**
  447. * Dashboard setup action
  448. */
  449. function wp_dashboard_setup()
  450. {
  451. wp_add_dashboard_widget('w3tc_latest', 'The Latest from W3 EDGE', array(
  452. &$this,
  453. 'widget_latest'
  454. ), array(
  455. &$this,
  456. 'widget_latest_control'
  457. ));
  458. }
  459. /**
  460. * Prints latest widget contents
  461. */
  462. function widget_latest()
  463. {
  464. global $wp_version;
  465. $items = array();
  466. $items_count = $this->_config->get_integer('widget.latest.items');
  467. if ($wp_version >= 2.8) {
  468. include_once (ABSPATH . WPINC . '/feed.php');
  469. $feed = fetch_feed(W3TC_FEED_URL);
  470. if (!is_wp_error($feed)) {
  471. $feed_items = $feed->get_items(0, $items_count);
  472. foreach ($feed_items as $feed_item) {
  473. $items[] = array(
  474. 'link' => $feed_item->get_link(),
  475. 'title' => $feed_item->get_title(),
  476. 'description' => $feed_item->get_description()
  477. );
  478. }
  479. }
  480. } else {
  481. include_once (ABSPATH . WPINC . '/rss.php');
  482. $rss = fetch_rss(W3TC_FEED_URL);
  483. if (is_object($rss)) {
  484. $items = array_slice($rss->items, 0, $items_count);
  485. }
  486. }
  487. include W3TC_DIR . '/inc/widget/latest.phtml';
  488. }
  489. /**
  490. * Latest widget control
  491. */
  492. function widget_latest_control($widget_id, $form_inputs = array())
  493. {
  494. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  495. require_once W3TC_LIB_W3_DIR . '/Request.php';
  496. $this->_config->set('widget.latest.items', W3_Request::get_integer('w3tc_latest_items', 3));
  497. $this->_config->save();
  498. } else {
  499. include W3TC_DIR . '/inc/widget/latest_control.phtml';
  500. }
  501. }
  502. /**
  503. * Admin menu
  504. */
  505. function admin_menu()
  506. {
  507. $page = add_options_page('W3 Total Cache', 'W3 Total Cache', 'manage_options', W3TC_FILE, array(
  508. &$this,
  509. 'options'
  510. ));
  511. if (current_user_can('manage_options')) {
  512. /**
  513. * Only admin can modify W3TC settings
  514. */
  515. add_action('load-' . $page, array(
  516. &$this,
  517. 'load'
  518. ));
  519. /**
  520. * Only admin can see W3TC notices and errors
  521. */
  522. add_action('admin_notices', array(
  523. &$this,
  524. 'admin_notices'
  525. ));
  526. }
  527. }
  528. /**
  529. * Plugin action links filter
  530. *
  531. * @return array
  532. */
  533. function plugin_action_links($links)
  534. {
  535. array_unshift($links, '<a class="edit" href="options-general.php?page=' . W3TC_FILE . '">Settings</a>');
  536. return $links;
  537. }
  538. /**
  539. * favorite_actions filter
  540. */
  541. function favorite_actions($actions)
  542. {
  543. $actions['options-general.php?page=' . W3TC_FILE . '&amp;flush_all'] = array(
  544. 'Empty Caches',
  545. 'manage_options'
  546. );
  547. return $actions;
  548. }
  549. /**
  550. * Check request and handle w3tc_request_data requests
  551. */
  552. function check_request()
  553. {
  554. $pos = strpos($_SERVER['REQUEST_URI'], '/w3tc_request_data/');
  555. if ($pos !== false) {
  556. $hash = substr($_SERVER['REQUEST_URI'], $pos + 19, 32);
  557. if (strlen($hash) == 32) {
  558. $request_data = (array) get_option('w3tc_request_data');
  559. if (isset($request_data[$hash])) {
  560. echo '<pre>';
  561. foreach ($request_data[$hash] as $key => $value) {
  562. printf("%s: %s\n", $key, $value);
  563. }
  564. echo '</pre>';
  565. unset($request_data[$hash]);
  566. update_option('w3tc_request_data', $request_data);
  567. } else {
  568. echo 'Hash is expired or invalid';
  569. }
  570. exit();
  571. }
  572. }
  573. }
  574. /**
  575. * Admin notices action
  576. */
  577. function admin_notices()
  578. {
  579. $error_messages = array(
  580. 'config_save' => sprintf('The settings could not be saved because the config file is not write-able. Please run <strong>chmod 777 %s</strong> to resolve this issue.', file_exists(W3TC_CONFIG_PATH) ? W3TC_CONFIG_PATH : WP_CONTENT_DIR),
  581. 'fancy_permalinks_disabled' => sprintf('Fancy permalinks are disabled. Please %s it first, then re-attempt to enabling the enhanced disk mode.', $this->button_link('enable', 'options-permalink.php')),
  582. 'pgcache_write_rules_core' => sprintf('Either your .htaccess file does not exist or cannot be modified (%s.htaccess). Please run <strong>chmod 777 %s.htaccess</strong> to resolve this issue.', ABSPATH, ABSPATH),
  583. 'pgcache_write_rules_cache' => sprintf('The page cache rules (%s/.htaccess) could not be modified. Please run <strong>chmod 777 %s/.htaccess</strong> to resolve this issue.', W3TC_CACHE_FILE_PGCACHE_DIR, W3TC_CACHE_FILE_PGCACHE_DIR),
  584. 'minify_write_rules' => sprintf('The minify cache rules (%s/.htaccess) could not be modified. Please run <strong>chmod 777 %s/.htaccess</strong> to resolve this issue.', W3TC_CACHE_FILE_MINIFY_DIR, W3TC_CACHE_FILE_MINIFY_DIR),
  585. 'support_request_url' => 'Please enter the address of your blog in the Blog <acronym title="Uniform Resource Locator">URL</acronym> field.',
  586. 'support_request_name' => 'Please enter your name in the Name field',
  587. 'support_request_email' => 'Please enter valid email address in the E-Mail field.',
  588. 'support_request_type' => 'Please select request type.',
  589. 'support_request_description' => 'Please describe the issue in the issue description field.',
  590. 'support_request_wp_login' => 'Please enter an administrator login. Remember you can create a temporary one just for this support case.',
  591. 'support_request_wp_password' => 'Please enter WP Admin password, be sure it\'s spelled correctly.',
  592. 'support_request_ftp_host' => 'Please enter <acronym title="Secure Shell">SSH</acronym> or <acronym title="File Transfer Protocol">FTP</acronym> host for your site.',
  593. 'support_request_ftp_login' => 'Please enter <acronym title="Secure Shell">SSH</acronym> or <acronym title="File Transfer Protocol">FTP</acronym> login for your server. Remember you can create a temporary one just for this support case.',
  594. 'support_request_ftp_password' => 'Please enter <acronym title="Secure Shell">SSH</acronym> or <acronym title="File Transfer Protocol">FTP</acronym> password for your <acronym title="File Transfer Protocol">FTP</acronym> account.',
  595. 'support_request' => 'Unable to send your support request.'
  596. );
  597. $note_messages = array(
  598. 'config_save' => 'Plugin configuration successfully updated.',
  599. 'flush_all' => 'All caches successfully emptied.',
  600. 'flush_memcached' => 'Memcached cache(s) successfully emptied.',
  601. 'flush_opcode' => 'Opcode cache(s) successfully emptied.',
  602. 'flush_file' => 'Disk cache successfully emptied.',
  603. 'flush_pgcache' => 'Page cache successfully emptied.',
  604. 'flush_dbcache' => 'Database cache successfully emptied.',
  605. 'flush_minify' => 'Minify cache successfully emptied.',
  606. 'pgcache_write_rules_core' => 'Page cache rewrite rules have been successfully written.',
  607. 'pgcache_write_rules_cache' => 'Page cache rewrite rules have been successfully written.',
  608. 'minify_write_rules' => 'Minify rewrite rules have been successfully written.',
  609. 'support_request' => 'Your support request has been successfully sent.'
  610. );
  611. $errors = array();
  612. $notes = array();
  613. require_once W3TC_LIB_W3_DIR . '/Request.php';
  614. $error = W3_Request::get_string('error');
  615. $note = W3_Request::get_string('note');
  616. /**
  617. * Handle messages from reqeust
  618. */
  619. if (isset($error_messages[$error])) {
  620. $errors[] = $error_messages[$error];
  621. }
  622. if (isset($note_messages[$note])) {
  623. $notes[] = $note_messages[$note];
  624. }
  625. /**
  626. * Check config file
  627. */
  628. if (!file_exists(W3TC_CONFIG_PATH)) {
  629. $errors[] = sprintf('<strong>W3 Total Cache Error:</strong> Default settings are in use. The configuration file could not be read or doesn\'t exist. Please %s to create the file.', $this->button_link('save your settings', sprintf('options-general.php?page=%s&tab=%s&save_config', W3TC_FILE, $this->_tab)));
  630. }
  631. /**
  632. * CDN notifications
  633. */
  634. if ($this->_config->get_boolean('cdn.enabled') && $this->_config->get_string('cdn.engine') != 'mirror') {
  635. /**
  636. * Show notification after theme change
  637. */
  638. if ($this->_config->get_boolean('notes.theme_changed')) {
  639. $notes[] = sprintf('Your active theme has changed, please %s now to ensure proper operation. %s', $this->button_popup('upload active theme files', 'cdn_export', 'cdn_export_type=theme'), $this->button_hide_note('Hide this message', 'theme_changed'));
  640. }
  641. /**
  642. * Show notification after WP upgrade
  643. */
  644. if ($this->_config->get_boolean('notes.wp_upgraded')) {
  645. $notes[] = sprintf('Have you upgraded WordPress? Please %s files now to ensure proper operation. %s', $this->button_popup('upload wp-includes', 'cdn_export', 'cdn_export_type=includes'), $this->button_hide_note('Hide this message', 'wp_upgraded'));
  646. }
  647. /**
  648. * Show notification after CDN enable
  649. */
  650. if ($this->_config->get_boolean('notes.cdn_upload')) {
  651. $cdn_upload_buttons = array();
  652. if ($this->_config->get_boolean('cdn.includes.enable')) {
  653. $cdn_upload_buttons[] = $this->button_popup('wp-includes', 'cdn_export', 'cdn_export_type=includes');
  654. }
  655. if ($this->_config->get_boolean('cdn.theme.enable')) {
  656. $cdn_upload_buttons[] = $this->button_popup('theme files', 'cdn_export', 'cdn_export_type=theme');
  657. }
  658. if ($this->_config->get_boolean('minify.enabled') && $this->_config->get_boolean('cdn.minify.enable')) {
  659. $cdn_upload_buttons[] = $this->button_popup('minify files', 'cdn_export', 'cdn_export_type=minify');
  660. }
  661. if ($this->_config->get_boolean('cdn.custom.enable')) {
  662. $cdn_upload_buttons[] = $this->button_popup('custom files', 'cdn_export', 'cdn_export_type=custom');
  663. }
  664. $notes[] = sprintf('Make sure to %s and upload your %s, files to the CDN to ensure proper operation. %s', $this->button_popup('export your media library', 'cdn_export_library'), implode(', ', $cdn_upload_buttons), $this->button_hide_note('Hide this message', 'cdn_upload'));
  665. }
  666. }
  667. /**
  668. * Show notification after plugin activate/deactivate
  669. */
  670. if ($this->_config->get_boolean('notes.plugins_updated')) {
  671. $texts = array();
  672. if ($this->_config->get_boolean('pgcache.enabled')) {
  673. $texts[] = $this->button_link('empty the page cache', sprintf('options-general.php?page=%s&tab=%s&flush_pgcache', W3TC_FILE, $this->_tab));
  674. }
  675. if ($this->_config->get_boolean('minify.enabled')) {
  676. $texts[] = sprintf('check your %s to maintain the desired user experience', $this->button_hide_note('minify settings', 'plugins_updated', sprintf('options-general.php?page=%s&tab=minify', W3TC_FILE)));
  677. }
  678. if (count($texts)) {
  679. $notes[] = sprintf('One or more plugins have been activated or deactivated, please %s. %s', implode(' and ', $texts), $this->button_hide_note('Hide this message', 'plugins_updated'));
  680. }
  681. }
  682. /**
  683. * Show notification when page cache needs to be emptied
  684. */
  685. if ($this->_config->get_boolean('pgcache.enabled') && $this->_config->get('notes.need_empty_pgcache')) {
  686. $notes[] = sprintf('The setting change(s) made either invalidate your cached data or modify the behavior of your site. %s now to provide a consistent user experience.', $this->button_link('Empty the page cache', sprintf('options-general.php?page=%s&tab=%s&flush_pgcache', W3TC_FILE, $this->_tab)));
  687. }
  688. /**
  689. * Show notification when minify needs to be emptied
  690. */
  691. if ($this->_config->get_boolean('minify.enabled') && $this->_config->get('notes.need_empty_minify')) {
  692. $notes[] = sprintf('The setting change(s) made either invalidate your cached data or modify the behavior of your site. %s now to provide a consistent user experience.', $this->button_link('Empty the minify cache', sprintf('options-general.php?page=%s&tab=%s&flush_minify', W3TC_FILE, $this->_tab)));
  693. }
  694. /**
  695. * Show messages
  696. */
  697. foreach ($errors as $error) {
  698. echo sprintf('<div class="error"><p>%s</p></div>', $error);
  699. }
  700. foreach ($notes as $note) {
  701. echo sprintf('<div class="updated fade"><p>%s</p></div>', $note);
  702. }
  703. }
  704. /**
  705. * Switch theme action
  706. */
  707. function switch_theme()
  708. {
  709. $this->_config->set('notes.theme_changed', true);
  710. $this->_config->save();
  711. }
  712. /**
  713. * WP Upgrade action hack
  714. *
  715. * @param string $message
  716. */
  717. function update_feedback($message)
  718. {
  719. if ($message == __('Upgrading database')) {
  720. $this->_config->set('notes.wp_upgraded', true);
  721. $this->_config->save();
  722. }
  723. }
  724. /**
  725. * Active plugins pre update option filter
  726. */
  727. function pre_update_option_active_plugins($new_value)
  728. {
  729. $old_value = (array) get_option('active_plugins');
  730. if ($new_value !== $old_value && in_array(W3TC_FILE, (array) $new_value) && in_array(W3TC_FILE, (array) $old_value)) {
  731. $this->_config->set('notes.plugins_updated', true);
  732. $this->_config->save();
  733. }
  734. return $new_value;
  735. }
  736. /**
  737. * Show plugin changes
  738. */
  739. function in_plugin_update_message()
  740. {
  741. $data = w3_url_get(W3TC_README_URL);
  742. if ($data) {
  743. $matches = null;
  744. if (preg_match('~==\s*Changelog\s*==\s*=\s*[0-9.]+\s*=(.*)(=\s*[0-9.]+\s*=|$)~Uis', $data, $matches)) {
  745. $changelog = (array) preg_split('~[\r\n]+~', trim($matches[1]));
  746. echo '<div style="color: #f00;">Take a minute to update, here\'s why:</div><div style="font-weight: normal;">';
  747. $ul = false;
  748. foreach ($changelog as $index => $line) {
  749. if (preg_match('~^\s*\*\s*~', $line)) {
  750. if (!$ul) {
  751. echo '<ul style="list-style: disc; margin-left: 20px;">';
  752. $ul = true;
  753. }
  754. $line = preg_replace('~^\s*\*\s*~', '', htmlspecialchars($line));
  755. echo '<li style="width: 50%; margin: 0; float: left; ' . ($index % 2 == 0 ? 'clear: left;' : '') . '">' . $line . '</li>';
  756. } else {
  757. if ($ul) {
  758. echo '</ul><div style="clear: left;"></div>';
  759. $ul = false;
  760. }
  761. echo '<p style="margin: 5px 0;">' . htmlspecialchars($line) . '</p>';
  762. }
  763. }
  764. if ($ul) {
  765. echo '</ul><div style="clear: left;"></div>';
  766. }
  767. echo '</div>';
  768. }
  769. }
  770. }
  771. /**
  772. * Footer plugin action
  773. */
  774. function footer()
  775. {
  776. echo '<div style="text-align: center;">Performance Optimization <a href="http://www.w3-edge.com/wordpress-plugins/" rel="external">WordPress Plugins</a> by W3 EDGE</div>';
  777. }
  778. /**
  779. * Options page
  780. */
  781. function options()
  782. {
  783. /**
  784. * Check for page cache availability
  785. */
  786. if ($this->_config->get_boolean('pgcache.enabled')) {
  787. if (!$this->check_advanced_cache()) {
  788. $this->_errors[] = sprintf('Page caching is not available: advanced-cache.php is not installed. Either the <strong>%s</strong> directory is not write-able or you have another caching plugin installed. This error message will automatically disappear once the change is successfully made.', WP_CONTENT_DIR);
  789. } elseif (!defined('WP_CACHE')) {
  790. $this->_errors[] = sprintf('Page caching is not available: please add: <strong>define(\'WP_CACHE\', true);</strong> to <strong>%swp-config.php</strong>. This error message will automatically disappear once the change is successfully made.', ABSPATH);
  791. } else {
  792. switch ($this->_config->get_string('pgcache.engine')) {
  793. case 'file_pgcache':
  794. require_once W3TC_LIB_W3_DIR . '/Plugin/PgCache.php';
  795. $w3_plugin_pgcache = & W3_Plugin_PgCache::instance();
  796. if ($this->_config->get_boolean('notes.pgcache_rules_core') && !$w3_plugin_pgcache->check_rules_core()) {
  797. if (w3_is_wpmu()) {
  798. $this->_errors[] = sprintf('Enhanced mode page cache is not operational. Your .htaccess rules could not be modified. Please verify <strong>%s.htaccess</strong> has the following rules: <pre>%s</pre> %s', ABSPATH, htmlspecialchars($w3_plugin_pgcache->generate_rules_core()), $this->button_hide_note('Hide this message', 'pgcache_rules_core'));
  799. } else {
  800. $this->_errors[] = sprintf('You\'ve selected disk caching with enhanced mode however the .htaccess file is not properly configured. Please run <strong>chmod 777 %s.htaccess</strong>, then %s. To manually modify your server configuration for enhanced mode append the following code: <pre>%s</pre> and %s.', ABSPATH, $this->button_link('try again', sprintf('options-general.php?page=%s&tab=%s&pgcache_write_rules_core', W3TC_FILE, $this->_tab)), htmlspecialchars($w3_plugin_pgcache->generate_rules_core()), $this->button_hide_note('hide this message', 'pgcache_rules_core'));
  801. }
  802. }
  803. if ($this->_config->get_boolean('notes.pgcache_rules_cache') && !$w3_plugin_pgcache->check_rules_cache()) {
  804. $this->_errors[] = sprintf('You\'ve selected disk caching with enhanced mode however the .htaccess file is not properly configured. Please run <strong>chmod 777 %s/.htaccess</strong>, then %s. To manually modify your server configuration for enhanced mode append the following code: <pre>%s</pre> and %s.', W3TC_CACHE_FILE_PGCACHE_DIR, $this->button_link('try again', sprintf('options-general.php?page=%s&tab=%s&pgcache_write_rules_cache', W3TC_FILE, $this->_tab)), htmlspecialchars($w3_plugin_pgcache->generate_rules_cache()), $this->button_hide_note('hide this message', 'pgcache_rules_cache'));
  805. }
  806. break;
  807. case 'memcached':
  808. $pgcache_memcached_servers = $this->_config->get_array('pgcache.memcached.servers');
  809. if (!$this->is_memcache_available($pgcache_memcached_servers)) {
  810. $this->_errors[] = sprintf('Page caching is not working properly. Memcached server(s): <strong>%s</strong> may not running or not responding. This error message will automatically disappear once the issue is resolved.', implode(', ', $pgcache_memcached_servers));
  811. }
  812. break;
  813. }
  814. }
  815. }
  816. /**
  817. * Check for minify availability
  818. */
  819. if ($this->_config->get_boolean('minify.enabled')) {
  820. switch ($this->_config->get_string('minify.engine')) {
  821. case 'file':
  822. if (W3TC_PHP5) {
  823. require_once W3TC_LIB_W3_DIR . '/Plugin/Minify.php';
  824. $w3_plugin_minify = & W3_Plugin_Minify::instance();
  825. if ($this->_config->get_boolean('notes.minify_rules') && !$w3_plugin_minify->check_rules()) {
  826. $this->_errors[] = sprintf('The "Rewrite URL Structure" feature, requires rewrite rules be present. Please run <strong>chmod 777 %s/.htaccess</strong>, then %s. To manually modify your server configuration for minify append the following code: <pre>%s</pre> and %s.', W3TC_CACHE_FILE_MINIFY_DIR, $this->button_link('try again', sprintf('options-general.php?page=%s&tab=%s&minify_write_rules', W3TC_FILE, $this->_tab)), htmlspecialchars($w3_plugin_minify->generate_rules()), $this->button_hide_note('hide this message', 'minify_rules'));
  827. }
  828. }
  829. break;
  830. case 'memcached':
  831. $minify_memcached_servers = $this->_config->get_array('minify.memcached.servers');
  832. if (!$this->is_memcache_available($minify_memcached_servers)) {
  833. $this->_errors[] = sprintf('Minify is not working properly. Memcached server(s): <strong>%s</strong> may not running or not responding. This error message will automatically disappear once the issue is resolved.', implode(', ', $minify_memcached_servers));
  834. }
  835. break;
  836. }
  837. }
  838. /**
  839. * Check for database cache availability
  840. */
  841. if ($this->_config->get_boolean('dbcache.enabled')) {
  842. if (!$this->check_db()) {
  843. $this->_errors[] = sprintf('Database caching is not available: db.php is not installed. Either the <strong>%s</strong> directory is not write-able or you have another caching plugin installed. This error message will automatically disappear once the change is successfully made.', WP_CONTENT_DIR);
  844. } elseif ($this->_config->get_string('pgcache.engine') == 'memcached') {
  845. $dbcache_memcached_servers = $this->_config->get_array('dbcache.memcached.servers');
  846. if (!$this->is_memcache_available($dbcache_memcached_servers)) {
  847. $this->_errors[] = sprintf('Database caching is not working properly. Memcached server(s): <strong>%s</strong> may not running or not responding. This error message will automatically disappear once the issue is successfully resolved.', implode(', ', $dbcache_memcached_servers));
  848. }
  849. }
  850. }
  851. /**
  852. * Check PHP version
  853. */
  854. if (!W3TC_PHP5 && $this->_config->get_boolean('notes.php_is_old')) {
  855. $this->_notes[] = sprintf('Unfortunately, <strong>PHP5</strong> is required for full functionality of this plugin; incompatible features are automatically disabled. Please upgrade if possible. %s', $this->button_hide_note('Hide this message', 'php_is_old'));
  856. }
  857. /**
  858. * Check CURL extension
  859. */
  860. if ($this->_config->get_boolean('notes.no_curl') && $this->_config->get_boolean('cdn.enabled') && !function_exists('curl_init')) {
  861. $this->_notes[] = sprintf('The <strong>CURL PHP</strong> extension is not available. Please install it to enable S3 or CloudFront functionality. %s', $this->button_hide_note('Hide this message', 'no_curl'));
  862. }
  863. /**
  864. * Check Zlib extension
  865. */
  866. if ($this->_config->get_boolean('notes.no_zlib') && (!function_exists('gzencode') || !function_exists('gzdeflate'))) {
  867. $this->_notes[] = sprintf('Unfortunately the PHP installation is incomplete, the <strong>zlib module is missing</strong>. This is a core PHP module. Please notify your server administrator and ask for it to be installed. %s', $this->button_hide_note('Hide this message', 'no_zlib'));
  868. }
  869. /**
  870. * Check if Zlib output compression is enabled
  871. */
  872. if ($this->_config->get_boolean('notes.zlib_output_compression') && w3_zlib_output_compression()) {
  873. $this->_notes[] = sprintf('Either the PHP configuration, Web Server configuration or a script somewhere in your WordPress installation is has set <strong>zlib.output_compression</strong> to enabled.<br />Please locate and disable this setting to ensure proper HTTP compression management. %s', $this->button_hide_note('Hide this message', 'zlib_output_compression'));
  874. }
  875. /**
  876. * Show message when defaults are set
  877. */
  878. if ($this->_config->get_boolean('notes.defaults')) {
  879. $this->_notes[] = sprintf('The plugin is in quick setup mode, most recommended defaults are set. Satisfy any warnings customizing any settings. %s', $this->button_hide_note('Hide this message', 'defaults'));
  880. }
  881. /**
  882. * Check wp-content permissions
  883. */
  884. if (!W3TC_WIN && $this->_config->get_boolean('notes.wp_content_perms')) {
  885. $wp_content_stat = stat(WP_CONTENT_DIR);
  886. $wp_content_mode = ($wp_content_stat['mode'] & 0777);
  887. if ($wp_content_mode != 0755) {
  888. $this->_notes[] = sprintf('<strong>%s</strong> is write-able. If you\'ve finished installing the plugin, change the permissions back to the default: <strong>chmod 755 %s</strong>. %s', WP_CONTENT_DIR, WP_CONTENT_DIR, $this->button_hide_note('Hide this message', 'wp_content_perms'));
  889. }
  890. }
  891. /**
  892. * Check CDN settings
  893. */
  894. if ($this->_config->get_boolean('cdn.enabled')) {
  895. $cdn_engine = $this->_config->get_string('cdn.engine');
  896. switch (true) {
  897. case ($cdn_engine == 'mirror' && $this->_config->get_string('cdn.mirror.domain') == ''):
  898. $this->_errors[] = 'The <strong>"Replace default hostname with"</strong> field must be populated.';
  899. break;
  900. case ($cdn_engine == 'ftp' && $this->_config->get_string('cdn.ftp.domain') == ''):
  901. $this->_errors[] = 'The <strong>"Replace default hostname with"</strong> field must be populated. Enter the hostname of your <acronym title="Content Delivery Network">CDN</acronym> provider. <em>This is the hostname you would enter into your address bar in order to view objects in your browser.</em>';
  902. break;
  903. case ($cdn_engine == 's3' && ($this->_config->get_string('cdn.s3.key') == '' || $this->_config->get_string('cdn.s3.bucket') == '' || $this->_config->get_string('cdn.s3.bucket') == '')):
  904. $this->_errors[] = 'The <strong>"Access key", "Secret key" and "Bucket"</strong> fields must be populated.';
  905. break;
  906. case ($cdn_engine == 'cf' && ($this->_config->get_string('cdn.cf.key') == '' || $this->_config->get_string('cdn.cf.secret') == '' || $this->_config->get_string('cdn.cf.bucket') == '' || ($this->_config->get_string('cdn.cf.id') == '' && $this->_config->get_string('cdn.cf.cname') == ''))):
  907. $this->_errors[] = 'The <strong>"Access key", "Secret key", "Bucket" and "Replace default hostname with"</strong> fields must be populated.';
  908. break;
  909. }
  910. }
  911. /**
  912. * Show tab
  913. */
  914. switch ($this->_tab) {
  915. case 'general':
  916. $this->options_general();
  917. break;
  918. case 'pgcache':
  919. $this->options_pgcache();
  920. break;
  921. case 'dbcache':
  922. $this->options_dbcache();
  923. break;
  924. case 'minify':
  925. $this->options_minify();
  926. break;
  927. case 'cdn':
  928. $this->options_cdn();
  929. break;
  930. case 'faq':
  931. $this->options_faq();
  932. break;
  933. case 'support':
  934. $this->options_support();
  935. break;
  936. case 'install':
  937. $this->options_install();
  938. break;
  939. case 'about':
  940. $this->options_about();
  941. break;
  942. }
  943. }
  944. /**
  945. * General tab
  946. */
  947. function options_general()
  948. {
  949. $pgcache_enabled = $this->_config->get_boolean('pgcache.enabled');
  950. $dbcache_enabled = $this->_config->get_boolean('dbcache.enabled');
  951. $minify_enabled = $this->_config->get_boolean('minify.enabled');
  952. $cdn_enabled = $this->_config->get_boolean('cdn.enabled');
  953. $enabled = ($pgcache_enabled || $dbcache_enabled || $minify_enabled || $cdn_enabled);
  954. $check_apc = function_exists('apc_store');
  955. $check_eaccelerator = function_exists('eaccelerator_put');
  956. $check_xcache = function_exists('xcache_set');
  957. $check_curl = function_exists('curl_init');
  958. $check_memcached = class_exists('Memcache');
  959. $pgcache_engine = $this->_config->get_string('pgcache.engine');
  960. $dbcache_engine = $this->_config->get_string('dbcache.engine');
  961. $minify_engine = $this->_config->get_string('minify.engine');
  962. $can_empty_memcache = ($pgcache_engine == 'memcached' || $dbcache_engine == 'memcached' || $minify_engine == 'memcached');
  963. $can_empty_opcode = ($pgcache_engine == 'apc' || $pgcache_engine == 'eaccelerator' || $pgcache_engine == 'xcache');
  964. $can_empty_opcode = $can_empty_opcode || ($dbcache_engine == 'apc' || $dbcache_engine == 'eaccelerator' || $dbcache_engine == 'xcache');
  965. $can_empty_opcode = $can_empty_opcode || ($minify_engine == 'apc' || $minify_engine == 'eaccelerator' || $minify_engine == 'xcache');
  966. $can_empty_file = ($pgcache_engine == 'file' || $pgcache_engine == 'file_pgcache' || $dbcache_engine == 'file' || $minify_engine == 'file');
  967. $debug = ($this->_config->get_boolean('dbcache.debug') || $this->_config->get_boolean('pgcache.debug') || $this->_config->get_boolean('minify.debug') || $this->_config->get_boolean('cdn.debug'));
  968. $support = $this->_config->get_string('common.support');
  969. $supports = $this->get_supports();
  970. include W3TC_DIR . '/inc/options/general.phtml';
  971. }
  972. /**
  973. * Page cache tab
  974. */
  975. function options_pgcache()
  976. {
  977. $pgcache_enabled = $this->_config->get_boolean('pgcache.enabled');
  978. $pgcache_gzip = function_exists('gzencode');
  979. $pgcache_deflate = function_exists('gzdeflate');
  980. include W3TC_DIR . '/inc/options/pgcache.phtml';
  981. }
  982. /**
  983. * Minify tab
  984. */
  985. function options_minify()
  986. {
  987. $minify_enabled = $this->_config->get_boolean('minify.enabled');
  988. $minify_gzip = function_exists('gzencode');
  989. $minify_deflate = function_exists('gzdeflate');
  990. $groups = $this->minify_get_groups();
  991. $js_group = W3_Request::get_string('js_group', 'default');
  992. $js_groups = $this->_config->get_array('minify.js.groups');
  993. $css_group = W3_Request::get_string('css_group', 'default');
  994. $css_groups = $this->_config->get_array('minify.css.groups');
  995. include W3TC_DIR . '/inc/options/minify.phtml';
  996. }
  997. /**
  998. * Database cache tab
  999. */
  1000. function options_dbcache()
  1001. {
  1002. $dbcache_enabled = $this->_config->get_boolean('dbcache.enabled');
  1003. include W3TC_DIR . '/inc/options/dbcache.phtml';
  1004. }
  1005. /**
  1006. * CDN tab
  1007. */
  1008. function options_cdn()
  1009. {
  1010. $cdn_enabled = $this->_config->get_boolean('cdn.enabled');
  1011. $cdn_engine = $this->_config->get_string('cdn.engine');
  1012. $cdn_mirror = ($cdn_engine == 'mirror');
  1013. $minify_enabled = $this->_config->get_boolean('minify.enabled');
  1014. include W3TC_DIR . '/inc/options/cdn.phtml';
  1015. }
  1016. /**
  1017. * FAQ tab
  1018. */
  1019. function options_faq()
  1020. {
  1021. include W3TC_DIR . '/inc/options/faq.phtml';
  1022. }
  1023. /**
  1024. * Support tab
  1025. */
  1026. function options_support()
  1027. {
  1028. $theme = get_theme(get_current_theme());
  1029. $template_files = (isset($theme['Template Files']) ? (array) $theme['Template Files'] : array());
  1030. $request_types = array(
  1031. 'Bug Submission',
  1032. 'Plugin (add-on) Request'
  1033. );
  1034. require_once W3TC_LIB_W3_DIR . '/Request.php';
  1035. $user = get_user_by('login', 'admin');
  1036. $url = W3_Request::get_string('url', w3_get_domain_url());
  1037. $name = W3_Request::get_string('name', ($user ? $user->display_name : ''));
  1038. $email = W3_Request::get_string('email', ($user ? $user->user_email : ''));
  1039. $request_type = W3_Request::get_string('request_type');
  1040. $description = W3_Request::get_string('description');
  1041. $templates = W3_Request::get_array('templates');
  1042. $wp_login = W3_Request::get_string('wp_login');
  1043. $wp_password = W3_Request::get_string('wp_password');
  1044. $ftp_host = W3_Request::get_string('ftp_host');
  1045. $ftp_login = W3_Request::get_string('ftp_login');
  1046. $ftp_password = W3_Request::get_string('ftp_password');
  1047. include W3TC_DIR . '/inc/options/support.phtml';
  1048. }
  1049. /**
  1050. * Install tab
  1051. */
  1052. function options_install()
  1053. {
  1054. include W3TC_DIR . '/inc/options/install.phtml';
  1055. }
  1056. /**
  1057. * About tab
  1058. */
  1059. function options_about()
  1060. {
  1061. include W3TC_DIR . '/inc/options/about.phtml';
  1062. }
  1063. /**
  1064. * Options save action
  1065. */
  1066. function options_save()
  1067. {
  1068. require_once W3TC_LIB_W3_DIR . '/Request.php';
  1069. /**
  1070. * Redirect params
  1071. */
  1072. $params = array();
  1073. /**
  1074. * Read config
  1075. */
  1076. $config = & new W3_Config();
  1077. $config->read_request();
  1078. /**
  1079. * General tab
  1080. */
  1081. if ($this->_tab == 'general') {
  1082. $debug = W3_Request::get_array('debug');
  1083. $config->set('dbcache.debug', in_array('dbcache', $debug));
  1084. $config->set('pgcache.debug', in_array('pgcache', $debug));
  1085. $config->set('minify.debug', in_array('minify', $debug));
  1086. $config->set('cdn.debug', in_array('cdn', $debug));
  1087. /**
  1088. * Page cache tab
  1089. */
  1090. if ($config->get_boolean('pgcache.enabled') && $config->get_string('pgcache.engine') == 'file_pgcache' && get_option('permalink_structure') == '') {
  1091. $this->redirect(array(
  1092. 'error' => 'fancy_permalinks_disabled'
  1093. ));
  1094. }
  1095. /**
  1096. * Show notification when CDN enabled
  1097. */
  1098. if ($this->_config->get_boolean('cdn.enabled') == false && $config->get_boolean('cdn.enabled') == true && $config->get_string('cdn.engine') != 'mirror') {
  1099. $config->set('notes.cdn_upload', true);
  1100. }
  1101. }
  1102. /**
  1103. * Minify tab
  1104. */
  1105. if ($this->_tab == 'minify') {
  1106. $groups = $this->minify_get_groups();
  1107. $js_group = W3_Request::get_string('js_group');
  1108. $js_files = W3_Request::get_array('js_files');
  1109. $css_group = W3_Request::get_string('css_group');
  1110. $css_files = W3_Request::get_array('css_files');
  1111. $js_groups = array();
  1112. $css_groups = array();
  1113. foreach ($js_files as $group => $locations) {
  1114. if (!array_key_exists($group, $groups)) {
  1115. continue;
  1116. }
  1117. foreach ((array) $locations as $location => $files) {
  1118. switch ($location) {
  1119. case 'include':
  1120. $js_groups[$group][$location]['blocking'] = true;
  1121. break;
  1122. case 'include-nb':
  1123. $js_groups[$group][$location]['blocking'] = false;
  1124. break;
  1125. case 'include-footer':
  1126. $js_groups[$group][$location]['blocking'] = true;
  1127. break;
  1128. case 'include-footer-nb':
  1129. $js_groups[$group][$location]['blocking'] = false;
  1130. break;
  1131. }
  1132. foreach ((array) $files as $file) {
  1133. if (!empty($file)) {
  1134. $js_groups[$group][$location]['files'][] = w3_normalize_file($file);
  1135. }
  1136. }
  1137. }
  1138. }
  1139. foreach ($css_files as $group => $locations) {
  1140. if (!array_key_exists($group, $groups)) {
  1141. continue;
  1142. }
  1143. foreach ((array) $locations as $location => $files) {
  1144. foreach ((array) $files as $file) {
  1145. if (!empty($file)) {
  1146. $css_groups[$group][$location]['files'][] = w3_normalize_file($file);
  1147. }
  1148. }
  1149. }
  1150. }
  1151. $config->set('minify.js.groups', $js_groups);
  1152. $config->set('minify.css.groups', $css_groups);
  1153. $params = array_merge($params, array(
  1154. 'js_group' => $js_group,
  1155. 'css_group' => $css_group
  1156. ));
  1157. }
  1158. /**
  1159. * Handle settings change that require pgcache and minify empty
  1160. */
  1161. $pgcache_dependencies = array(
  1162. 'pgcache.debug',
  1163. 'dbcache.enabled',
  1164. 'minify.enabled',
  1165. 'cdn.enabled'
  1166. );
  1167. if ($config->get_boolean('dbcache.enabled')) {
  1168. $pgcache_dependencies = array_merge($pgcache_dependencies, array(
  1169. 'dbcache.debug'
  1170. ));
  1171. }
  1172. if ($config->get_boolean('minify.enabled')) {
  1173. $pgcache_dependencies = array_merge($pgcache_dependencies, array(
  1174. 'minify.debug',
  1175. 'minify.rewrite',
  1176. 'minify.options',
  1177. 'minify.html.enable',
  1178. 'minify.html.reject.admin',
  1179. 'minify.html.inline.css',
  1180. 'minify.html.inline.js',
  1181. 'minify.html.strip.crlf',
  1182. 'minify.css.enable',
  1183. 'minify.css.groups',
  1184. 'minify.js.enable',
  1185. 'minify.js.groups'
  1186. ));
  1187. }
  1188. if ($config->get_boolean('cdn.enabled')) {
  1189. $pgcache_dependencies = array_merge($pgcache_dependencies, array(
  1190. 'cdn.debug',
  1191. 'cdn.engine',
  1192. 'cdn.includes.enable',
  1193. 'cdn.includes.files',
  1194. 'cdn.theme.enable',
  1195. 'cdn.theme.files',
  1196. 'cdn.minify.enable',
  1197. 'cdn.custom.enable',
  1198. 'cdn.custom.files',
  1199. 'cdn.ftp.domain',
  1200. 'cdn.s3.bucket',
  1201. 'cdn.cf.id',
  1202. 'cdn.cf.cname'
  1203. ));
  1204. }
  1205. $minify_dependencies = array(
  1206. 'minify.debug',
  1207. 'minify.css.combine',
  1208. 'minify.css.strip.comments',
  1209. 'minify.css.strip.crlf',
  1210. 'minify.css.groups',
  1211. 'minify.js.combine.header',
  1212. 'minify.js.combine.footer',
  1213. 'minify.js.strip.comments',
  1214. 'minify.js.strip.crlf',
  1215. 'minify.js.groups'
  1216. );
  1217. $old_pgcache_dependencies_values = array();
  1218. $new_pgcache_dependencies_values = array();
  1219. $old_minify_dependencies_values = array();
  1220. $new_minify_dependencies_values = array();
  1221. foreach ($pgcache_dependencies as $pgcache_dependency) {
  1222. $old_pgcache_dependencies_values[] = $this->_config->get($pgcache_dependency);
  1223. $new_pgcache_dependencies_values[] = $config->get($pgcache_dependency);
  1224. }
  1225. foreach ($minify_dependencies as $minify_dependency) {
  1226. $old_minify_dependencies_values[] = $this->_config->get($minify_dependency);
  1227. $new_minify_dependencies_values[] = $config->get($minify_dependency);
  1228. }
  1229. if ($this->_config->get_boolean('pgcache.enabled') && serialize($old_pgcache_dependencies_values) != serialize($new_pgcache_dependencies_values)) {
  1230. $config->set('notes.need_empty_pgcache', true);
  1231. }
  1232. if ($this->_config->get_boolean('minify.enabled') && serialize($old_minify_dependencies_values) != serialize($new_minify_dependencies_values)) {
  1233. $config->set('notes.need_empty_minify', true);
  1234. }
  1235. /**
  1236. * Save config
  1237. */
  1238. if ($config->save()) {
  1239. require_once W3TC_LIB_W3_DIR . '/Plugin/PgCache.php';
  1240. require_once W3TC_LIB_W3_DIR . '/Plugin/DbCache.php';
  1241. require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php';
  1242. $w3_plugin_pgcache = & W3_Plugin_PgCache::instance();
  1243. $w3_plugin_dbcache = & W3_Plugin_DbCache::instance();
  1244. $w3_plugin_cdn = & W3_Plugin_Cdn::instance();
  1245. if (W3TC_PHP5) {
  1246. require_once W3TC_LIB_W3_DIR . '/Plugin/Minify.php';
  1247. $w3_plugin_minify = & W3_Plugin_Minify::instance();
  1248. }
  1249. /**
  1250. * Empty caches on engine change or cache enable/disable
  1251. */
  1252. if ($this->_config->get_string('pgcache.engine') != $config->get_string('pgcache.engine') || $this->_config->get_string('pgcache.enabled') != $config->get_string('pgcache.enabled')) {
  1253. $this->flush_pgcache();
  1254. }
  1255. if ($this->_config->get_string('dbcache.engine') != $config->get_string('dbcache.engine') || $this->_config->get_string('dbcache.enabled') != $config->get_string('dbcache.enabled')) {
  1256. $this->flush_dbcache();
  1257. }
  1258. if ($this->_config->get_string('minify.engine') != $config->get_string('minify.engine') || $this->_config->get_string('minify.enabled') != $config->get_string('minify.enabled')) {
  1259. $this->flush_minify();
  1260. }
  1261. /**
  1262. * Unschedule events if changed file gc interval
  1263. */
  1264. if ($this->_config->get_boolean('pgcache.file.gc') != $config->get_boolean('pgcache.file.gc')) {
  1265. $w3_plugin_pgcache->unschedule();
  1266. }
  1267. if ($this->_config->get_boolean('dbcache.file.gc') != $config->get_boolean('dbcache.file.gc')) {
  1268. $w3_plugin_dbcache->unschedule();
  1269. }
  1270. if (W3TC_PHP5 && $this->_config->get_boolean('minify.file.gc') != $config->get_boolean('minify.file.gc')) {
  1271. $w3_plugin_minify->unschedule();
  1272. }
  1273. $this->_config->load();
  1274. /**
  1275. * Schedule events
  1276. */
  1277. $w3_plugin_pgcache->schedule();
  1278. $w3_plugin_dbcache->schedule();
  1279. $w3_plugin_cdn->schedule();
  1280. if (W3TC_PHP5) {
  1281. $w3_plugin_minify->schedule();
  1282. }
  1283. /**
  1284. * Update support us option
  1285. */
  1286. $this->link_update();
  1287. /**
  1288. * Auto upload minify files to CDN
  1289. */
  1290. if ($this->_tab == 'minify' && $this->_config->get_boolean('minify.upload') && $this->_config->get_boolean('cdn.enabled') && $this->_config->get_string('cdn.engine') != 'mirror') {
  1291. $this->cdn_upload_minify();
  1292. }
  1293. /**
  1294. * Write page cache rewrite rules
  1295. */
  1296. if ($this->_tab == 'general' || $this->_tab == 'pgcache') {
  1297. $is_wpmu = w3_is_wpmu();
  1298. if ($this->_config->get_boolean('pgcache.enabled') && $this->_config->get_string('pgcache.engine') == 'file_pgcache') {
  1299. if (!$is_wpmu) {
  1300. $w3_plugin_pgcache->write_rules_core();
  1301. }
  1302. $w3_plugin_pgcache->write_rules_cache();
  1303. } else {
  1304. if (!$is_wpmu) {
  1305. $w3_plugin_pgcache->remove_rules_core();
  1306. }
  1307. $w3_plugin_pgcache->remove_rules_cache();
  1308. }
  1309. }
  1310. /**
  1311. * Write minify rewrite rules
  1312. */
  1313. if (W3TC_PHP5 && ($this->_tab == 'general' || $this->_tab == 'minify')) {
  1314. if ($this->_config->get_boolean('minify.enabled') && $this->_config->get_boolean('minify.rewrite')) {
  1315. $w3_plugin_minify->write_rules();
  1316. } else {
  1317. require_once W3TC_DIR . '/lib/W3/Plugin/Minify.php';
  1318. $w3_plugin_minify->remove_rules();
  1319. }
  1320. }
  1321. $this->redirect(array_merge($params, array(
  1322. 'note' => 'config_save'
  1323. )));
  1324. } else {
  1325. $this->redirect(array_merge($params, array(
  1326. 'error' => 'config_save'
  1327. )));
  1328. }
  1329. }
  1330. /**
  1331. * Send support request
  1332. */
  1333. function support_request()
  1334. {
  1335. require_once W3TC_LIB_W3_DIR . '/Request.php';
  1336. $url = W3_Request::get_string('url');
  1337. $name = W3_Request::get_string('name');
  1338. $email = W3_Request::get_string('email');
  1339. $request_type = W3_Request::get_string('request_type');
  1340. $description = W3_Request::get_string('description');
  1341. $templates = W3_Request::get_array('templates');
  1342. $wp_login = W3_Request::get_string('wp_login');
  1343. $wp_password = W3_Request::get_string('wp_password');
  1344. $ftp_host = W3_Request::get_string('ftp_host');
  1345. $ftp_login = W3_Request::get_string('ftp_login');
  1346. $ftp_password = W3_Request::get_string('ftp_password');
  1347. $params = array(
  1348. 'url' => $url,
  1349. 'name' => $name,
  1350. 'email' => $email,
  1351. 'request_type' => $request_type,
  1352. 'description' => $description,
  1353. 'wp_login' => $wp_login,
  1354. 'wp_password' => $wp_password,
  1355. 'ftp_host' => $ftp_host,
  1356. 'ftp_login' => $ftp_login,
  1357. 'ftp_password' => $ftp_password
  1358. );
  1359. foreach ($templates as $template_index => $template) {
  1360. $template_key = sprintf('templates[%d]', $template_index);
  1361. $params[$template_key] = $template;
  1362. }
  1363. if ($url == '') {
  1364. $this->redirect(array_merge($params, array(
  1365. 'error' => 'support_request_url'
  1366. )));
  1367. }
  1368. if ($name == '') {
  1369. $this->redirect(array_merge($params, array(
  1370. 'error' => 'support_request_name'
  1371. )));
  1372. }
  1373. if (!preg_match('~^[a-z0-9_\-\.]+@[a-z0-9-\.]+\.[a-z]{2,5}$~', $email)) {
  1374. $this->redirect(array_merge($params, array(
  1375. 'error' => 'support_request_email'
  1376. )));
  1377. }
  1378. if ($request_type == '') {
  1379. $this->redirect(array_merge($params, array(
  1380. 'error' => 'support_request_type'
  1381. )));
  1382. }
  1383. if ($description == '') {
  1384. $this->redirect(array_merge($params, array(
  1385. 'error' => 'support_request_description'
  1386. )));
  1387. }
  1388. if ($wp_login != '' || $wp_password != '') {
  1389. if ($wp_login == '') {
  1390. $this->redirect(array_merge($params, array(
  1391. 'error' => 'support_request_wp_login'
  1392. )));
  1393. }
  1394. if ($wp_password == '') {
  1395. $this->redirect(array_merge($params, array(
  1396. 'error' => 'support_request_wp_password'
  1397. )));
  1398. }
  1399. }
  1400. if ($ftp_host != '' || $ftp_login != '' || $ftp_password != '') {
  1401. if ($ftp_host == '') {
  1402. $this->redirect(array_merge($params, array(
  1403. 'error' => 'support_request_ftp_host'
  1404. )));
  1405. }
  1406. if ($ftp_login == '') {
  1407. $this->redirect(array_merge($params, array(
  1408. 'error' => 'support_request_ftp_login'
  1409. )));
  1410. }
  1411. if ($ftp_password == '') {
  1412. $this->redirect(array_merge($params, array(
  1413. 'error' => 'support_request_ftp_password'
  1414. )));
  1415. }
  1416. }
  1417. /**
  1418. * Add attachments
  1419. */
  1420. $attachments = array(
  1421. W3TC_CONFIG_PATH
  1422. );
  1423. /**
  1424. * Attach server info
  1425. */
  1426. $server_info = print_r($this->get_server_info(), true);
  1427. $server_info = str_replace("\n", "\r\n", $server_info);
  1428. $server_info_path = W3TC_TMP_DIR . '/server_info.txt';
  1429. if (@file_put_contents($server_info_path, $server_info)) {
  1430. $attachments[] = $server_info_path;
  1431. }
  1432. /**
  1433. * Attach phpinfo
  1434. */
  1435. ob_start();
  1436. phpinfo();
  1437. $php_info = ob_get_contents();
  1438. ob_end_clean();
  1439. $php_info_path = W3TC_TMP_DIR . '/php_info.html';
  1440. if (@file_put_contents($php_info_path, $php_info)) {
  1441. $attachments[] = $php_info_path;
  1442. }
  1443. /**
  1444. * Attach minify log
  1445. */
  1446. if (file_exists(W3TC_MINIFY_LOG_FILE)) {
  1447. $attachments[] = W3TC_MINIFY_LOG_FILE;
  1448. }
  1449. /**
  1450. * Attach templates
  1451. */
  1452. foreach ($templates as $template) {
  1453. if (!empty($template)) {
  1454. $attachments[] = $template;
  1455. }
  1456. }
  1457. /**
  1458. * Attach other files
  1459. */
  1460. if (!empty($_FILES['files'])) {
  1461. $files = (array) $_FILES['files'];
  1462. for ($i = 0, $l = count($files); $i < $l; $i++) {
  1463. if (isset($files['tmp_name'][$i]) && isset($files['name'][$i]) && isset($files['error'][$i]) && $files['error'][$i] == UPLOAD_ERR_OK) {
  1464. $path = W3TC_TMP_DIR . '/' . $files['name'][$i];
  1465. if (@move_uploaded_file($files['tmp_name'][$i], $path)) {
  1466. $attachments[] = $path;
  1467. }
  1468. }
  1469. }
  1470. }
  1471. $data = array();
  1472. if (!empty($wp_login) && !empty($wp_password)) {
  1473. $data['WP Admin login'] = $wp_login;
  1474. $data['WP Admin password'] = $wp_password;
  1475. }
  1476. if (!empty($ftp_host) && !empty($ftp_login) && !empty($ftp_password)) {
  1477. $data['SSH / FTP host'] = $ftp_host;
  1478. $data['SSH / FTP login'] = $ftp_login;
  1479. $data['SSH / FTP password'] = $ftp_password;
  1480. }
  1481. /**
  1482. * Store request data for future access
  1483. */
  1484. if (count($data)) {
  1485. $hash = md5(microtime());
  1486. $request_data = get_option('w3tc_request_data', array());
  1487. $request_data[$hash] = $data;
  1488. update_option('w3tc_request_data', $request_data);
  1489. $request_data_url = sprintf('%sw3tc_request_data/%s', w3_get_site_url(), $hash);
  1490. } else {
  1491. $request_data_url = null;
  1492. }
  1493. /**
  1494. * Get body contents
  1495. */
  1496. ob_start();
  1497. include W3TC_DIR . '/inc/options/support_email.phtml';
  1498. $body = ob_get_contents();
  1499. ob_end_clean();
  1500. /**
  1501. * Send email
  1502. */
  1503. $subject = sprintf('W3TC Support Request: %s', $request_type);
  1504. $headers = array(
  1505. sprintf('From: "%s" <%s>', addslashes($name), $email),
  1506. sprintf('Reply-To: "%s" <%s>', addslashes($name), $email),
  1507. 'Content-Type: text/html; charset=UTF-8'
  1508. );
  1509. $this->_phpmailer_sender = $email;
  1510. add_action('phpmailer_init', array(
  1511. &$this,
  1512. 'phpmailer_init'
  1513. ));
  1514. set_time_limit(600);
  1515. $result = @wp_mail(W3TC_EMAIL, $subject, $body, implode("\n", $headers), $attachments);
  1516. /**
  1517. * Remove temporary files
  1518. */
  1519. foreach ($attachments as $attachment) {
  1520. if (strstr($attachment, W3TC_TMP_DIR) !== false) {
  1521. @unlink($attachment);
  1522. }
  1523. }
  1524. if ($result) {
  1525. $this->redirect(array(
  1526. 'note' => 'support_request'
  1527. ));
  1528. } else {
  1529. $this->redirect(array_merge($params, array(
  1530. 'error' => 'support_request'
  1531. )));
  1532. }
  1533. }
  1534. /**
  1535. * PHPMailer init function
  1536. *
  1537. * @param PHPMailer $phpmailer
  1538. * @return void
  1539. */
  1540. function phpmailer_init(&$phpmailer)
  1541. {
  1542. $phpmailer->Sender = $this->_phpmailer_sender;
  1543. }
  1544. /**
  1545. * Returns button html
  1546. *
  1547. * @param string $text
  1548. * @param string $onclick
  1549. * @return string
  1550. */
  1551. function button($text, $onclick = '')
  1552. {
  1553. return sprintf('<input type="button" class="button" value="%s" onclick="%s" />', htmlspecialchars($text), htmlspecialchars($onclick));
  1554. }
  1555. /**
  1556. * Returns button link html
  1557. *
  1558. * @param string $text
  1559. * @param string $url
  1560. * @return string
  1561. */
  1562. function button_link($text, $url)
  1563. {
  1564. $onclick = sprintf('document.location.href = \'%s\';', addslashes($url));
  1565. return $this->button($text, $onclick);
  1566. }
  1567. /**
  1568. * Returns hide note button html
  1569. *
  1570. * @param string $text
  1571. * @param string $note
  1572. * @param string $redirect
  1573. * @return string
  1574. */
  1575. function button_hide_note($text, $note, $redirect = '')
  1576. {
  1577. $url = sprintf('options-general.php?page=%s&tab=%s&hide_note=%s', W3TC_FILE, $this->_tab, $note);
  1578. if ($redirect != '') {
  1579. $url .= '&redirect=' . urlencode($redirect);
  1580. }
  1581. return $this->button_link($text, $url);
  1582. }
  1583. /**
  1584. * Returns popup button html
  1585. *
  1586. * @param string $text
  1587. * @param string $w3tc_action
  1588. * @param string $params
  1589. * @param integer $width
  1590. * @param integer $height
  1591. * @return string
  1592. */
  1593. function button_popup($text, $w3tc_action, $params = '', $width = 800, $height = 600)
  1594. {
  1595. $onclick = sprintf('window.open(\'options-general.php?page=%s&w3tc_action=%s%s\', \'%s\', \'width=%d,height=%d,status=no,toolbar=no,menubar=no,scrollbars=yes\');', W3TC_FILE, $w3tc_action, ($params != '' ? '&' . $params : ''), $w3tc_action, $width, $height);
  1596. return $this->button($text, $onclick);
  1597. }
  1598. /**
  1599. * CDN queue action
  1600. */
  1601. function cdn_queue()
  1602. {
  1603. require_once W3TC_LIB_W3_DIR . '/Request.php';
  1604. require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php';
  1605. $w3_plugin_cdn = & W3_Plugin_Cdn::instance();
  1606. $cdn_queue_action = W3_Request::get_string('cdn_queue_action');
  1607. $cdn_queue_tab = W3_Request::get_string('cdn_queue_tab');
  1608. $notes = array();
  1609. switch ($cdn_queue_tab) {
  1610. case 'upload':
  1611. case 'delete':
  1612. break;
  1613. default:
  1614. $cdn_queue_tab = 'upload';
  1615. }
  1616. switch ($cdn_queue_action) {
  1617. case 'delete':
  1618. $cdn_queue_id = W3_Request::get_integer('cdn_queue_id');
  1619. if (!empty($cdn_queue_id)) {
  1620. $w3_plugin_cdn->queue_delete($cdn_queue_id);
  1621. $notes[] = 'File successfully deleted from the queue.';
  1622. }
  1623. break;
  1624. case 'empty':
  1625. $cdn_queue_type = W3_Request::get_integer('cdn_queue_type');
  1626. if (!empty($cdn_queue_type)) {
  1627. $w3_plugin_cdn->queue_empty($cdn_queue_type);
  1628. $notes[] = 'Queue successfully emptied.';
  1629. }
  1630. break;
  1631. }
  1632. $queue = $w3_plugin_cdn->queue_get();
  1633. $title = 'Unsuccessful file transfer queue.';
  1634. include W3TC_DIR . '/inc/popup/cdn_queue.phtml';
  1635. }
  1636. /**
  1637. * CDN export library action
  1638. */
  1639. function cdn_export_library()
  1640. {
  1641. require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php';
  1642. $w3_plugin_cdn = & W3_Plugin_Cdn::instance();
  1643. $total = $w3_plugin_cdn->get_attachments_count();
  1644. $title = 'Media Library export';
  1645. include W3TC_DIR . '/inc/popup/cdn_export_library.phtml';
  1646. }
  1647. /**
  1648. * CDN export library process
  1649. */
  1650. function cdn_export_library_process()
  1651. {
  1652. set_time_limit(1000);
  1653. require_once W3TC_LIB_W3_DIR . '/Request.php';
  1654. require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php';
  1655. $w3_plugin_cdn = & W3_Plugin_Cdn::instance();
  1656. $limit = W3_Request::get_integer('limit');
  1657. $offset = W3_Request::get_integer('offset');
  1658. $count = null;
  1659. $total = null;
  1660. $results = array();
  1661. @$w3_plugin_cdn->export_library($limit, $offset, $count, $total, $results);
  1662. echo sprintf("{limit: %d, offset: %d, count: %d, total: %s, results: [\r\n", $limit, $offset, $count, $total);
  1663. $results_count = count($results);
  1664. foreach ($results as $index => $result) {
  1665. echo sprintf("\t{local_path: '%s', remote_path: '%s', result: %d, error: '%s'}", addslashes($result['local_path']), addslashes($result['remote_path']), addslashes($result['result']), addslashes($result['error']));
  1666. if ($index < $results_count - 1) {
  1667. echo ',';
  1668. }
  1669. echo "\r\n";
  1670. }
  1671. echo ']}';
  1672. }
  1673. /**
  1674. * CDN import library action
  1675. */
  1676. function cdn_import_library()
  1677. {
  1678. require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php';
  1679. $w3_plugin_cdn = & W3_Plugin_Cdn::instance();
  1680. $cdn = & $w3_plugin_cdn->get_cdn();
  1681. $total = $w3_plugin_cdn->get_import_posts_count();
  1682. $cdn_host = $cdn->get_domain();
  1683. $title = 'Media Library import';
  1684. include W3TC_DIR . '/inc/popup/cdn_import_library.phtml';
  1685. }
  1686. /**
  1687. * CDN import library process
  1688. */
  1689. function cdn_import_library_process()
  1690. {
  1691. set_time_limit(1000);
  1692. require_once W3TC_LIB_W3_DIR . '/Request.php';
  1693. require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php';
  1694. $w3_plugin_cdn = & W3_Plugin_Cdn::instance();
  1695. $limit = W3_Request::get_integer('limit');
  1696. $offset = W3_Request::get_integer('offset');
  1697. $count = null;
  1698. $total = null;
  1699. $results = array();
  1700. @$w3_plugin_cdn->import_library($limit, $offset, $count, $total, $results);
  1701. echo sprintf("{limit: %d, offset: %d, count: %d, total: %s, results: [\r\n", $limit, $offset, $count, $total);
  1702. $results_count = count($results);
  1703. foreach ($results as $index => $result) {
  1704. echo sprintf("\t{src: '%s', dst: '%s', result: %d, error: '%s'}", addslashes($result['src']), addslashes($result['dst']), addslashes($result['result']), addslashes($result['error']));
  1705. if ($index < $results_count - 1) {
  1706. echo ',';
  1707. }
  1708. echo "\r\n";
  1709. }
  1710. echo ']}';
  1711. }
  1712. /**
  1713. * CDN rename domain action
  1714. */
  1715. function cdn_rename_domain()
  1716. {
  1717. require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php';
  1718. $w3_plugin_cdn = & W3_Plugin_Cdn::instance();
  1719. $total = $w3_plugin_cdn->get_rename_posts_count();
  1720. $title = 'Modify attachment URLs';
  1721. include W3TC_DIR . '/inc/popup/cdn_rename_domain.phtml';
  1722. }
  1723. /**
  1724. * CDN rename domain process
  1725. */
  1726. function cdn_rename_domain_process()
  1727. {
  1728. set_time_limit(1000);
  1729. require_once W3TC_LIB_W3_DIR . '/Request.php';
  1730. require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php';
  1731. $w3_plugin_cdn = & W3_Plugin_Cdn::instance();
  1732. $limit = W3_Request::get_integer('limit');
  1733. $offset = W3_Request::get_integer('offset');
  1734. $names = W3_Request::get_array('names');
  1735. $count = null;
  1736. $total = null;
  1737. $results = array();
  1738. @$w3_plugin_cdn->rename_domain($names, $limit, $offset, $count, $total, $results);
  1739. echo sprintf("{limit: %d, offset: %d, count: %d, total: %s, results: [\r\n", $limit, $offset, $count, $total);
  1740. $results_count = count($results);
  1741. foreach ($results as $index => $result) {
  1742. echo sprintf("\t{old: '%s', new: '%s', result: %d, error: '%s'}", addslashes($result['old']), addslashes($result['new']), addslashes($result['result']), addslashes($result['error']));
  1743. if ($index < $results_count - 1) {
  1744. echo ',';
  1745. }
  1746. echo "\r\n";
  1747. }
  1748. echo ']}';
  1749. }
  1750. /**
  1751. * CDN export action
  1752. */
  1753. function cdn_export()
  1754. {
  1755. require_once W3TC_LIB_W3_DIR . '/Request.php';
  1756. require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php';
  1757. $w3_plugin_cdn = & W3_Plugin_Cdn::instance();
  1758. $cdn_export_type = W3_Request::get_string('cdn_export_type', 'custom');
  1759. switch ($cdn_export_type) {
  1760. case 'includes':
  1761. $title = 'Includes files export';
  1762. $files = $w3_plugin_cdn->get_files_includes();
  1763. break;
  1764. case 'theme':
  1765. $title = 'Theme files export';
  1766. $files = $w3_plugin_cdn->get_files_theme();
  1767. break;
  1768. case 'minify':
  1769. $title = 'Minify files export';
  1770. $files = $w3_plugin_cdn->get_files_minify();
  1771. break;
  1772. default:
  1773. case 'custom':
  1774. $title = 'Custom files export';
  1775. $files = $w3_plugin_cdn->get_files_custom();
  1776. break;
  1777. }
  1778. include W3TC_DIR . '/inc/popup/cdn_export_file.phtml';
  1779. }
  1780. /**
  1781. * CDN export process
  1782. */
  1783. function cdn_export_process()
  1784. {
  1785. set_time_limit(1000);
  1786. require_once W3TC_LIB_W3_DIR . '/Request.php';
  1787. require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php';
  1788. $w3_plugin_cdn = & W3_Plugin_Cdn::instance();
  1789. $files = W3_Request::get_array('files');
  1790. $upload = array();
  1791. $results = array();
  1792. foreach ($files as $file) {
  1793. $upload[$file] = $file;
  1794. }
  1795. $w3_plugin_cdn->upload($upload, false, $results);
  1796. echo "{results: [\r\n";
  1797. $results_count = count($results);
  1798. foreach ($results as $index => $result) {
  1799. echo sprintf("\t{local_path: '%s', remote_path: '%s', result: %d, error: '%s'}", addslashes($result['local_path']), addslashes($result['remote_path']), addslashes($result['result']), addslashes($result['error']));
  1800. if ($index < $results_count - 1) {
  1801. echo ',';
  1802. }
  1803. echo "\r\n";
  1804. }
  1805. echo ']}';
  1806. }
  1807. /**
  1808. * Uploads minify files to CDN
  1809. */
  1810. function cdn_upload_minify()
  1811. {
  1812. require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php';
  1813. $w3_plugin_cdn = & W3_Plugin_Cdn::instance();
  1814. $files = $w3_plugin_cdn->get_files_minify();
  1815. $upload = array();
  1816. $results = array();
  1817. foreach ($files as $file) {
  1818. $upload[$file] = $file;
  1819. }
  1820. return $w3_plugin_cdn->upload($upload, false, $results);
  1821. }
  1822. /**
  1823. * CDN Test FTP
  1824. */
  1825. function cdn_test_ftp()
  1826. {
  1827. require_once W3TC_LIB_W3_DIR . '/Request.php';
  1828. require_once W3TC_LIB_W3_DIR . '/Cdn.php';
  1829. $host = W3_Request::get_string('host');
  1830. $user = W3_Request::get_string('user');
  1831. $pass = W3_Request::get_string('pass');
  1832. $path = W3_Request::get_string('path');
  1833. $pasv = W3_Request::get_boolean('pasv');
  1834. $w3_cdn_ftp = & W3_Cdn::instance('ftp', array(
  1835. 'host' => $host,
  1836. 'user' => $user,
  1837. 'pass' => $pass,
  1838. 'path' => $path,
  1839. 'pasv' => $pasv
  1840. ));
  1841. $error = null;
  1842. if ($w3_cdn_ftp->test($error)) {
  1843. $result = true;
  1844. $error = 'Test passed';
  1845. } else {
  1846. $result = false;
  1847. $error = sprintf('Test failed. Error: %s', $error);
  1848. }
  1849. echo sprintf('{result: %d, error: "%s"}', $result, addslashes($error));
  1850. }
  1851. /**
  1852. * CDN Test S3
  1853. */
  1854. function cdn_test_s3()
  1855. {
  1856. require_once W3TC_LIB_W3_DIR . '/Request.php';
  1857. require_once W3TC_LIB_W3_DIR . '/Cdn.php';
  1858. $key = W3_Request::get_string('key');
  1859. $secret = W3_Request::get_string('secret');
  1860. $bucket = W3_Request::get_string('bucket');
  1861. $w3_cdn_s3 = & W3_Cdn::instance('s3', array(
  1862. 'key' => $key,
  1863. 'secret' => $secret,
  1864. 'bucket' => $bucket
  1865. ));
  1866. $error = null;
  1867. if ($w3_cdn_s3->test($error)) {
  1868. $result = true;
  1869. $error = 'Test passed';
  1870. } else {
  1871. $result = false;
  1872. $error = sprintf('Test failed. Error: %s', $error);
  1873. }
  1874. echo sprintf('{result: %d, error: "%s"}', $result, addslashes($error));
  1875. }
  1876. /**
  1877. * CDN Test CloudFront
  1878. */
  1879. function cdn_test_cf()
  1880. {
  1881. require_once W3TC_LIB_W3_DIR . '/Request.php';
  1882. require_once W3TC_LIB_W3_DIR . '/Cdn.php';
  1883. $key = W3_Request::get_string('key');
  1884. $secret = W3_Request::get_string('secret');
  1885. $bucket = W3_Request::get_string('bucket');
  1886. $id = W3_Request::get_string('id');
  1887. $cname = W3_Request::get_string('cname');
  1888. $w3_cdn_s3 = & W3_Cdn::instance('cf', array(
  1889. 'key' => $key,
  1890. 'secret' => $secret,
  1891. 'bucket' => $bucket,
  1892. 'id' => $id,
  1893. 'cname' => $cname
  1894. ));
  1895. $error = null;
  1896. if ($w3_cdn_s3->test($error)) {
  1897. $result = true;
  1898. $error = 'Test passed';
  1899. } else {
  1900. $result = false;
  1901. $error = sprintf('Test failed. Error: %s', $error);
  1902. }
  1903. echo sprintf('{result: %d, error: "%s"}', $result, addslashes($error));
  1904. }
  1905. /**
  1906. * Create bucket action
  1907. */
  1908. function cdn_create_bucket()
  1909. {
  1910. require_once W3TC_LIB_W3_DIR . '/Request.php';
  1911. require_once W3TC_LIB_W3_DIR . '/Cdn.php';
  1912. $type = W3_Request::get_string('type');
  1913. $key = W3_Request::get_string('key');
  1914. $secret = W3_Request::get_string('secret');
  1915. $bucket = W3_Request::get_string('bucket');
  1916. switch ($type) {
  1917. case 's3':
  1918. case 'cf':
  1919. $result = true;
  1920. break;
  1921. default:
  1922. $result = false;
  1923. $error = 'Incorrect type.';
  1924. break;
  1925. }
  1926. if ($result) {
  1927. $w3_cdn_s3 = & W3_Cdn::instance($type, array(
  1928. 'key' => $key,
  1929. 'secret' => $secret,
  1930. 'bucket' => $bucket
  1931. ));
  1932. $error = null;
  1933. if ($w3_cdn_s3->create_bucket($error)) {
  1934. $result = true;
  1935. $error = 'Bucket has been successfully created.';
  1936. } else {
  1937. $result = false;
  1938. $error = sprintf('Error: %s', $error);
  1939. }
  1940. echo sprintf('{result: %d, error: "%s"}', $result, addslashes($error));
  1941. }
  1942. }
  1943. /**
  1944. * Check if memcache is available
  1945. *
  1946. * @param array $servers
  1947. * @return boolean
  1948. */
  1949. function is_memcache_available($servers)
  1950. {
  1951. static $results = array();
  1952. $key = md5(serialize($servers));
  1953. if (!isset($results[$key])) {
  1954. require_once W3TC_LIB_W3_DIR . '/Cache/Memcached.php';
  1955. $memcached = & new W3_Cache_Memcached(array(
  1956. 'servers' => $servers,
  1957. 'persistant' => false
  1958. ));
  1959. $test_string = sprintf('test_' . md5(time()));
  1960. $memcached->set($test_string, $test_string, 60);
  1961. $results[$key] = ($memcached->get($test_string) == $test_string);
  1962. }
  1963. return $results[$key];
  1964. }
  1965. /**
  1966. * Test memcached
  1967. */
  1968. function test_memcached()
  1969. {
  1970. require_once W3TC_LIB_W3_DIR . '/Request.php';
  1971. $servers = W3_Request::get_array('servers');
  1972. if ($this->is_memcache_available($servers)) {
  1973. $result = true;
  1974. $error = 'Test passed';
  1975. } else {
  1976. $result = false;
  1977. $error = 'Test failed';
  1978. }
  1979. echo sprintf('{result: %d, error: "%s"}', $result, addslashes($error));
  1980. }
  1981. /**
  1982. * Insert plugin link into Blogroll
  1983. */
  1984. function link_insert()
  1985. {
  1986. $support = $this->_config->get_string('common.support');
  1987. $matches = null;
  1988. if ($support != '' && preg_match('~^link_category_(\d+)$~', $support, $matches)) {
  1989. require_once ABSPATH . 'wp-admin/includes/bookmark.php';
  1990. wp_insert_link(array(
  1991. 'link_url' => W3TC_LINK_URL,
  1992. 'link_name' => W3TC_LINK_NAME,
  1993. 'link_category' => array(
  1994. (int) $matches[1]
  1995. )
  1996. ));
  1997. }
  1998. }
  1999. /**
  2000. * Deletes plugin link from Blogroll
  2001. */
  2002. function link_delete()
  2003. {
  2004. $bookmarks = get_bookmarks();
  2005. $link_id = 0;
  2006. foreach ($bookmarks as $bookmark) {
  2007. if ($bookmark->link_url == W3TC_LINK_URL) {
  2008. $link_id = $bookmark->link_id;
  2009. break;
  2010. }
  2011. }
  2012. if ($link_id) {
  2013. require_once ABSPATH . 'wp-admin/includes/bookmark.php';
  2014. wp_delete_link($link_id);
  2015. }
  2016. }
  2017. /**
  2018. * Updates link
  2019. */
  2020. function link_update()
  2021. {
  2022. $this->link_delete();
  2023. $this->link_insert();
  2024. }
  2025. /**
  2026. * Flush specified cache
  2027. *
  2028. * @param string $type
  2029. */
  2030. function flush($type)
  2031. {
  2032. if ($this->_config->get_string('pgcache.engine') == $type && $this->_config->get_boolean('pgcache.enabled')) {
  2033. $this->_config->set('notes.need_empty_pgcache', false);
  2034. $this->_config->set('notes.plugins_updated', false);
  2035. if (!$this->_config->save()) {
  2036. $this->redirect(array(
  2037. 'error' => 'config_save'
  2038. ));
  2039. }
  2040. $this->flush_pgcache();
  2041. }
  2042. if ($this->_config->get_string('dbcache.engine') == $type && $this->_config->get_boolean('dbcache.enabled')) {
  2043. $this->flush_dbcache();
  2044. }
  2045. if ($this->_config->get_string('minify.engine') == $type && $this->_config->get_boolean('minify.enabled')) {
  2046. $this->_config->set('notes.need_empty_minify', false);
  2047. if (!$this->_config->save()) {
  2048. $this->redirect(array(
  2049. 'error' => 'config_save'
  2050. ));
  2051. }
  2052. $this->flush_minify();
  2053. }
  2054. }
  2055. /**
  2056. * Flush memcached cache
  2057. *
  2058. * @return void
  2059. */
  2060. function flush_memcached()
  2061. {
  2062. $this->flush('memcached');
  2063. }
  2064. /**
  2065. * Flush APC cache
  2066. * @return void
  2067. */
  2068. function flush_opcode()
  2069. {
  2070. $this->flush('apc');
  2071. $this->flush('eaccelerator');
  2072. $this->flush('xcache');
  2073. }
  2074. /**
  2075. * Flush file cache
  2076. *
  2077. * @return void
  2078. */
  2079. function flush_file()
  2080. {
  2081. $this->flush('file');
  2082. $this->flush('file_pgcache');
  2083. }
  2084. /**
  2085. * Flush page cache
  2086. */
  2087. function flush_pgcache()
  2088. {
  2089. require_once W3TC_DIR . '/lib/W3/PgCache.php';
  2090. $w3_pgcache = & W3_PgCache::instance();
  2091. $w3_pgcache->flush();
  2092. }
  2093. /**
  2094. * Flush page cache
  2095. */
  2096. function flush_dbcache()
  2097. {
  2098. require_once W3TC_DIR . '/lib/W3/Db.php';
  2099. $w3_db = & W3_Db::instance();
  2100. $w3_db->flush_cache();
  2101. }
  2102. /**
  2103. * Flush minify cache
  2104. */
  2105. function flush_minify()
  2106. {
  2107. if (W3TC_PHP5) {
  2108. require_once W3TC_DIR . '/lib/W3/Minify.php';
  2109. $w3_minify = & W3_Minify::instance();
  2110. $w3_minify->flush();
  2111. }
  2112. }
  2113. /**
  2114. * Checks if advanced-cache.php exists
  2115. *
  2116. * @return boolean
  2117. */
  2118. function check_advanced_cache()
  2119. {
  2120. return (file_exists(WP_CONTENT_DIR . '/advanced-cache.php') && ($script_data = @file_get_contents(WP_CONTENT_DIR . '/advanced-cache.php')) && strstr($script_data, 'W3_PgCache') !== false);
  2121. }
  2122. /**
  2123. * Checks if db.php exists
  2124. *
  2125. * @return boolean
  2126. */
  2127. function check_db()
  2128. {
  2129. return (file_exists(WP_CONTENT_DIR . '/db.php') && ($script_data = @file_get_contents(WP_CONTENT_DIR . '/db.php')) && strstr($script_data, 'W3_Db') !== false);
  2130. }
  2131. /**
  2132. * Output buffering callback
  2133. *
  2134. * @param string $buffer
  2135. * @return string
  2136. */
  2137. function ob_callback($buffer)
  2138. {
  2139. global $wpdb;
  2140. if ($buffer != '' && w3_is_xml($buffer)) {
  2141. $date = date('Y-m-d H:i:s');
  2142. $host = (!empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost');
  2143. if ($this->is_supported()) {
  2144. $buffer .= sprintf("\r\n<!-- Served from: %s @ %s by W3 Total Cache -->", $host, $date);
  2145. } else {
  2146. $buffer .= "\r\n<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/\r\n\r\n";
  2147. if ($this->_config->get_boolean('minify.enabled')) {
  2148. require_once W3TC_LIB_W3_DIR . '/Plugin/Minify.php';
  2149. $w3_plugin_minify = & W3_Plugin_Minify::instance();
  2150. $buffer .= sprintf("Minified using %s%s\r\n", w3_get_engine_name($this->_config->get_string('minify.engine')), ($w3_plugin_minify->minify_reject_reason != '' ? sprintf(' (%s)', $w3_plugin_minify->minify_reject_reason) : ''));
  2151. }
  2152. if ($this->_config->get_boolean('pgcache.enabled')) {
  2153. require_once W3TC_LIB_W3_DIR . '/PgCache.php';
  2154. $w3_pgcache = & W3_PgCache::instance();
  2155. $buffer .= sprintf("Page Caching using %s%s\r\n", w3_get_engine_name($this->_config->get_string('pgcache.engine')), ($w3_pgcache->cache_reject_reason != '' ? sprintf(' (%s)', $w3_pgcache->cache_reject_reason) : ''));
  2156. }
  2157. if ($this->_config->get_boolean('dbcache.enabled') && is_a($wpdb, 'W3_Db')) {
  2158. $append = (is_user_logged_in() ? ' (user is logged in)' : '');
  2159. if ($wpdb->query_hits) {
  2160. $buffer .= sprintf("Database Caching %d/%d queries in %.3f seconds using %s%s\r\n", $wpdb->query_hits, $wpdb->query_total, $wpdb->time_total, w3_get_engine_name($this->_config->get_string('dbcache.engine')), $append);
  2161. } else {
  2162. $buffer .= sprintf("Database Caching using %s%s\r\n", w3_get_engine_name($this->_config->get_string('dbcache.engine')), $append);
  2163. }
  2164. }
  2165. if ($this->_config->get_boolean('cdn.enabled')) {
  2166. require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php';
  2167. $w3_plugin_cdn = & W3_Plugin_Cdn::instance();
  2168. $cdn = & $w3_plugin_cdn->get_cdn();
  2169. $via = $cdn->get_via();
  2170. $buffer .= sprintf("Content Delivery Network via %s%s\r\n", ($via ? $via : 'N/A'), ($w3_plugin_cdn->cdn_reject_reason != '' ? sprintf(' (%s)', $w3_plugin_cdn->cdn_reject_reason) : ''));
  2171. }
  2172. $buffer .= sprintf("\r\nServed from: %s @ %s -->", $host, $date);
  2173. }
  2174. }
  2175. return $buffer;
  2176. }
  2177. /**
  2178. * Check if we can do modify contents
  2179. * @return boolean
  2180. */
  2181. function can_modify_contents()
  2182. {
  2183. /**
  2184. * Skip if admin
  2185. */
  2186. if (defined('WP_ADMIN')) {
  2187. return false;
  2188. }
  2189. /**
  2190. * Skip if doint AJAX
  2191. */
  2192. if (defined('DOING_AJAX')) {
  2193. return false;
  2194. }
  2195. /**
  2196. * Skip if doing cron
  2197. */
  2198. if (defined('DOING_CRON')) {
  2199. return false;
  2200. }
  2201. /**
  2202. * Skip if APP request
  2203. */
  2204. if (defined('APP_REQUEST')) {
  2205. return false;
  2206. }
  2207. /**
  2208. * Skip if XMLRPC request
  2209. */
  2210. if (defined('XMLRPC_REQUEST')) {
  2211. return false;
  2212. }
  2213. /**
  2214. * Check request URI
  2215. */
  2216. if (!$this->check_request_uri()) {
  2217. return false;
  2218. }
  2219. /**
  2220. * Skip if debug mode is enabled
  2221. */
  2222. if ($this->_config->get_boolean('pgcache.debug') || $this->_config->get_boolean('dbcache.debug') || $this->_config->get_boolean('minify.debug') || $this->_config->get_boolean('cdn.debug')) {
  2223. return false;
  2224. }
  2225. return true;
  2226. }
  2227. /**
  2228. * Checks request URI
  2229. *
  2230. * @return boolean
  2231. */
  2232. function check_request_uri()
  2233. {
  2234. $reject_uri = array(
  2235. 'wp-login',
  2236. 'wp-register'
  2237. );
  2238. foreach ($reject_uri as $uri) {
  2239. if (strstr($_SERVER['REQUEST_URI'], $uri) !== false) {
  2240. return false;
  2241. }
  2242. }
  2243. return true;
  2244. }
  2245. /**
  2246. * Returns server info
  2247. */
  2248. function get_server_info()
  2249. {
  2250. global $wp_version, $wp_db_version, $wpdb;
  2251. $wordpress_plugins = get_plugins();
  2252. $wordpress_plugins_active = array();
  2253. foreach ($wordpress_plugins as $wordpress_plugin_file => $wordpress_plugin) {
  2254. if (is_plugin_active($wordpress_plugin_file)) {
  2255. $wordpress_plugins_active[$wordpress_plugin_file] = $wordpress_plugin;
  2256. }
  2257. }
  2258. $w3tc_config = (array) @include W3TC_CONFIG_PATH;
  2259. $mysql_version = (array) $wpdb->get_var('SELECT VERSION()');
  2260. $mysql_variables_result = (array) $wpdb->get_results('SHOW VARIABLES', ARRAY_N);
  2261. $mysql_variables = array();
  2262. foreach ($mysql_variables_result as $mysql_variables_row) {
  2263. $mysql_variables[$mysql_variables_row[0]] = $mysql_variables_row[1];
  2264. }
  2265. return array(
  2266. 'wp' => array(
  2267. 'version' => $wp_version,
  2268. 'db_version' => $wp_db_version,
  2269. 'w3tc_version' => W3TC_VERSION,
  2270. 'url' => w3_get_domain_url(),
  2271. 'path' => ABSPATH,
  2272. 'email' => get_option('admin_email'),
  2273. 'upload_info' => (array) w3_upload_info(),
  2274. 'theme' => get_theme(get_current_theme()),
  2275. 'plugins' => $wordpress_plugins_active,
  2276. 'wp_cache' => (defined('WP_CACHE') ? 'true' : 'false')
  2277. ),
  2278. 'mysql' => array(
  2279. 'version' => $mysql_version,
  2280. 'variables' => $mysql_variables
  2281. )
  2282. );
  2283. }
  2284. /**
  2285. * Support Us action
  2286. */
  2287. function support_us()
  2288. {
  2289. $supports = $this->get_supports();
  2290. include W3TC_DIR . '/inc/lightbox/support_us.phtml';
  2291. }
  2292. /**
  2293. * Tweet action
  2294. */
  2295. function tweet()
  2296. {
  2297. include W3TC_DIR . '/inc/lightbox/tweet.phtml';
  2298. }
  2299. /**
  2300. * Update twitter status
  2301. */
  2302. function twitter_status_update()
  2303. {
  2304. require_once W3TC_LIB_W3_DIR . '/Request.php';
  2305. $username = W3_Request::get_string('username');
  2306. $password = W3_Request::get_string('password');
  2307. $error = 'OK';
  2308. if (w3_twitter_status_update($username, $password, W3TC_TWITTER_STATUS, $error)) {
  2309. $this->_config->set('common.tweeted', time());
  2310. if ($this->_config->save()) {
  2311. $result = true;
  2312. } else {
  2313. $error = 'Unable to save config.';
  2314. $result = false;
  2315. }
  2316. } else {
  2317. $result = false;
  2318. }
  2319. echo sprintf('{result: %d, error: "%s"}', $result, addslashes($error));
  2320. }
  2321. /**
  2322. * Returns list of support types
  2323. * @return array
  2324. */
  2325. function get_supports()
  2326. {
  2327. $supports = array(
  2328. 'footer' => 'page footer'
  2329. );
  2330. $link_categories = get_terms('link_category', array(
  2331. 'hide_empty' => 0
  2332. ));
  2333. foreach ($link_categories as $link_category) {
  2334. $supports['link_category_' . $link_category->term_id] = strtolower($link_category->name);
  2335. }
  2336. return $supports;
  2337. }
  2338. /**
  2339. * Returns true if is supported
  2340. * @return boolean
  2341. */
  2342. function is_supported()
  2343. {
  2344. return ($this->_config->get_string('common.support') != '' || $this->_config->get_string('common.tweeted'));
  2345. }
  2346. /**
  2347. * Returns minify groups
  2348. * @return array
  2349. */
  2350. function minify_get_groups()
  2351. {
  2352. $groups = array(
  2353. 'default' => 'Default'
  2354. );
  2355. $current_theme = get_current_theme();
  2356. if ($current_theme) {
  2357. $theme = get_theme($current_theme);
  2358. if ($theme && isset($theme['Template Files'])) {
  2359. foreach ((array) $theme['Template Files'] as $template_file) {
  2360. $group = basename($template_file, '.php');
  2361. $groups[$group] = ucfirst($group);
  2362. }
  2363. }
  2364. }
  2365. return $groups;
  2366. }
  2367. /**
  2368. * Redirect function
  2369. *
  2370. * @param boolean $check_referer
  2371. */
  2372. function redirect($params = array(), $check_referer = false)
  2373. {
  2374. require_once W3TC_LIB_W3_DIR . '/Request.php';
  2375. $url = W3_Request::get_string('redirect');
  2376. if ($url == '') {
  2377. if ($check_referer && !empty($_SERVER['HTTP_REFERER'])) {
  2378. $url = $_SERVER['HTTP_REFERER'];
  2379. } else {
  2380. $url = 'options-general.php';
  2381. $params = array_merge(array(
  2382. 'page' => W3TC_FILE,
  2383. 'tab' => $this->_tab
  2384. ), $params);
  2385. }
  2386. }
  2387. w3_redirect($url, $params);
  2388. }
  2389. }