PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/w3-total-cache/inc/define.php

https://github.com/failstar/polycaro
PHP | 1626 lines | 893 code | 231 blank | 502 comment | 176 complexity | c1e7b95db0578d2f418b799866a1362c MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. if (!defined('ABSPATH')) {
  3. die();
  4. }
  5. define('W3TC', true);
  6. define('W3TC_VERSION', '0.9.4');
  7. define('W3TC_POWERED_BY', 'W3 Total Cache/' . W3TC_VERSION);
  8. define('W3TC_EMAIL', 'w3tc@w3-edge.com');
  9. define('W3TC_TEXT_DOMAIN', 'w3-total-cache');
  10. define('W3TC_PAYPAL_URL', 'https://www.paypal.com/cgi-bin/webscr');
  11. define('W3TC_PAYPAL_BUSINESS', 'w3tc-team@w3-edge.com');
  12. define('W3TC_LINK_URL', 'http://www.w3-edge.com/wordpress-plugins/');
  13. define('W3TC_LINK_NAME', 'W3 EDGE, Optimization Products for WordPress');
  14. define('W3TC_FEED_URL', 'http://feeds.feedburner.com/W3TOTALCACHE');
  15. define('W3TC_NEWS_FEED_URL', 'http://feeds.feedburner.com/W3EDGE');
  16. define('W3TC_README_URL', 'http://plugins.svn.wordpress.org/w3-total-cache/trunk/readme.txt');
  17. define('W3TC_SUPPORT_US_TIMEOUT', 2592000);
  18. define('W3TC_SUPPORT_REQUEST_URL', 'https://www.w3-edge.com/w3tc/support/');
  19. define('W3TC_TRACK_URL', 'https://www.w3-edge.com/w3tc/track/');
  20. define('W3TC_MAILLINGLIST_SIGNUP_URL', 'https://www.w3-edge.com/w3tc/emailsignup/');
  21. define('NEWRELIC_SIGNUP_URL', 'http://bit.ly/w3tc-partner-newrelic-signup');
  22. define('MAXCDN_SIGNUP_URL', 'http://bit.ly/w3tc-cdn-maxcdn-create-account');
  23. define('MAXCDN_AUTHORIZE_URL', 'http://bit.ly/w3tc-cdn-maxcdn-authorize');
  24. define('NETDNA_AUTHORIZE_URL', 'https://cp.netdna.com/i/w3tc');
  25. // this is the URL our updater / license checker pings. This should be the URL of the site with EDD installed
  26. if (!defined('EDD_W3EDGE_STORE_URL')) define('EDD_W3EDGE_STORE_URL', 'https://www.w3-edge.com/' );
  27. if (!defined('EDD_W3EDGE_STORE_URL_PLUGIN')) define('EDD_W3EDGE_STORE_URL_PLUGIN', 'https://www.w3-edge.com/?w3tc_buy_pro_plugin' );
  28. // the name of your product. This should match the download name in EDD exactly
  29. define('EDD_W3EDGE_W3TC_NAME', 'W3 Total Cache Pro: Annual Subscription');
  30. define('W3TC_WIN', (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'));
  31. defined('W3TC_DIR') || define('W3TC_DIR', realpath(dirname(__FILE__) . '/..'));
  32. define('W3TC_FILE', 'w3-total-cache/w3-total-cache.php');
  33. define('W3TC_INC_DIR', W3TC_DIR . '/inc');
  34. define('W3TC_INC_WIDGET_DIR', W3TC_INC_DIR. '/widget');
  35. define('W3TC_INC_FUNCTIONS_DIR', W3TC_INC_DIR . '/functions');
  36. define('W3TC_INC_OPTIONS_DIR', W3TC_INC_DIR . '/options');
  37. define('W3TC_INC_LIGHTBOX_DIR', W3TC_INC_DIR . '/lightbox');
  38. define('W3TC_INC_POPUP_DIR', W3TC_INC_DIR . '/popup');
  39. define('W3TC_LIB_DIR', W3TC_DIR . '/lib');
  40. define('W3TC_LIB_W3_DIR', W3TC_LIB_DIR . '/W3');
  41. define('W3TC_LIB_MINIFY_DIR', W3TC_LIB_DIR . '/Minify');
  42. define('W3TC_LIB_CF_DIR', W3TC_LIB_DIR . '/CF');
  43. define('W3TC_LIB_CSSTIDY_DIR', W3TC_LIB_DIR . '/CSSTidy');
  44. define('W3TC_LIB_MICROSOFT_DIR', W3TC_LIB_DIR . '/Microsoft');
  45. define('W3TC_LIB_NUSOAP_DIR', W3TC_LIB_DIR . '/Nusoap');
  46. define('W3TC_LIB_NETDNA_DIR', W3TC_LIB_DIR . '/NetDNA');
  47. define('W3TC_LIB_OAUTH_DIR', W3TC_LIB_DIR . '/OAuth');
  48. define('W3TC_LIB_NEWRELIC_DIR', W3TC_LIB_DIR . '/NewRelic');
  49. define('W3TC_PLUGINS_DIR', W3TC_DIR . '/plugins');
  50. define('W3TC_INSTALL_DIR', W3TC_DIR . '/wp-content');
  51. define('W3TC_INSTALL_MINIFY_DIR', W3TC_INSTALL_DIR . '/w3tc/min');
  52. define('W3TC_LANGUAGES_DIR', W3TC_DIR . '/languages');
  53. define('W3TC_CACHE_DIR', WP_CONTENT_DIR . '/cache');
  54. define('W3TC_CONFIG_DIR', WP_CONTENT_DIR . '/w3tc-config');
  55. define('W3TC_CACHE_CONFIG_DIR', W3TC_CACHE_DIR . '/config');
  56. define('W3TC_CACHE_MINIFY_DIR', W3TC_CACHE_DIR . '/minify');
  57. define('W3TC_CACHE_PAGE_ENHANCED_DIR', W3TC_CACHE_DIR . '/page_enhanced');
  58. define('W3TC_CACHE_TMP_DIR', W3TC_CACHE_DIR . '/tmp');
  59. define('W3TC_CACHE_BLOGMAP_FILENAME', W3TC_CACHE_DIR . '/blogs.php');
  60. defined('WP_CONTENT_DIR') || define('WP_CONTENT_DIR', realpath(W3TC_DIR . '/../..'));
  61. define('W3TC_CDN_COMMAND_UPLOAD', 1);
  62. define('W3TC_CDN_COMMAND_DELETE', 2);
  63. define('W3TC_CDN_COMMAND_PURGE', 3);
  64. define('W3TC_CDN_TABLE_QUEUE', 'w3tc_cdn_queue');
  65. define('W3TC_INSTALL_FILE_ADVANCED_CACHE', W3TC_INSTALL_DIR . '/advanced-cache.php');
  66. define('W3TC_INSTALL_FILE_DB', W3TC_INSTALL_DIR . '/db.php');
  67. define('W3TC_INSTALL_FILE_OBJECT_CACHE', W3TC_INSTALL_DIR . '/object-cache.php');
  68. define('W3TC_ADDIN_FILE_ADVANCED_CACHE', WP_CONTENT_DIR . '/advanced-cache.php');
  69. define('W3TC_ADDIN_FILE_DB', WP_CONTENT_DIR . '/db.php');
  70. define('W3TC_FILE_DB_CLUSTER_CONFIG', WP_CONTENT_DIR . '/db-cluster-config.php');
  71. define('W3TC_ADDIN_FILE_OBJECT_CACHE', WP_CONTENT_DIR . '/object-cache.php');
  72. define('W3TC_WP_LOADER', (defined('WP_PLUGIN_DIR') ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins') . '/w3tc-wp-loader.php');
  73. if (!defined('W3TC_EXTENSION_DIR'))
  74. define('W3TC_EXTENSION_DIR', (defined('WP_PLUGIN_DIR') ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins'));
  75. define('W3TC_CORE_EXTENSION_DIR', W3TC_DIR . '/extensions');
  76. w3_require_once(W3TC_INC_DIR . '/functions/compat.php');
  77. w3_require_once(W3TC_INC_DIR . '/functions/plugin.php');
  78. @ini_set('pcre.backtrack_limit', 4194304);
  79. @ini_set('pcre.recursion_limit', 4194304);
  80. global $w3_late_init;
  81. $w3_late_init = false;
  82. /**
  83. * Returns current microtime
  84. *
  85. * @return double
  86. */
  87. function w3_microtime() {
  88. list ($usec, $sec) = explode(' ', microtime());
  89. return ((double) $usec + (double) $sec);
  90. }
  91. /**
  92. * Check if content is HTML or XML
  93. *
  94. * @param string $content
  95. * @return boolean
  96. */
  97. function w3_is_xml($content) {
  98. if (strlen($content) > 1000) {
  99. $content = substr($content, 0, 1000);
  100. }
  101. if (strstr($content, '<!--') !== false) {
  102. $content = preg_replace('~<!--.*?-->~s', '', $content);
  103. }
  104. $content = ltrim($content, "\x00\x09\x0A\x0D\x20\xBB\xBF\xEF");
  105. return (stripos($content, '<?xml') === 0 || stripos($content, '<html') === 0 || stripos($content, '<!DOCTYPE') === 0);
  106. }
  107. /**
  108. * If content can handle HTML comments, can disable printout per request using filter 'w3tc_can_print_comment'
  109. * @param $buffer
  110. * @return bool
  111. */
  112. function w3_can_print_comment(&$buffer) {
  113. if (function_exists('apply_filters'))
  114. return apply_filters('w3tc_can_print_comment', w3_is_xml($buffer) && !defined('DOING_AJAX'));
  115. return w3_is_xml($buffer) && !defined('DOING_AJAX');
  116. }
  117. /*
  118. * Returns URI from filename/dirname
  119. *
  120. * @return string
  121. */
  122. function w3_filename_to_url($filename, $use_site_url = false) {
  123. // using wp-content instead of document_root as known dir since dirbased
  124. // multisite wp adds blogname to the path inside site_url
  125. if (substr($filename, 0, strlen(WP_CONTENT_DIR)) != WP_CONTENT_DIR)
  126. return '';
  127. $uri_from_wp_content = substr($filename, strlen(WP_CONTENT_DIR));
  128. if ($use_site_url)
  129. $site_url_ssl = w3_get_url_ssl(w3_get_site_url());
  130. else
  131. $site_url_ssl = w3_get_url_ssl(w3_get_home_url());
  132. $dir = '';
  133. if (substr(trailingslashit(WP_CONTENT_DIR), 0, strlen(trailingslashit(w3_get_site_root()))) == trailingslashit(w3_get_site_root())) {
  134. if ($use_site_url || w3_get_domain(w3_get_home_url()) == w3_get_domain(w3_get_site_url()))
  135. $dir = str_replace($site_url_ssl, '', w3_get_url_ssl(w3_get_site_url()));
  136. else
  137. $dir = str_replace($site_url_ssl, '', w3_get_url_ssl(w3_get_home_url()));
  138. $dir = trim($dir, '/\\');
  139. if ($dir)
  140. $dir = '/' . $dir;
  141. $content_path = trim(substr(trailingslashit(WP_CONTENT_DIR), strlen(trailingslashit(w3_get_site_root()))), '/\\');
  142. }
  143. else
  144. $content_path = trim(substr(trailingslashit(WP_CONTENT_DIR), strlen(trailingslashit(w3_get_document_root()))), '/\\');
  145. $url = $site_url_ssl . $dir . '/' . $content_path . $uri_from_wp_content;
  146. return $url;
  147. }
  148. /**
  149. * Returns true if database cluster is used
  150. *
  151. * @return boolean
  152. */
  153. function w3_is_dbcluster() {
  154. return defined('W3TC_FILE_DB_CLUSTER_CONFIG') && @file_exists(W3TC_FILE_DB_CLUSTER_CONFIG)
  155. && defined('W3TC_ENTERPRISE') && W3TC_ENTERPRISE;
  156. }
  157. /**
  158. * Returns true if it's WPMU
  159. *
  160. * @return boolean
  161. */
  162. function w3_is_wpmu() {
  163. static $wpmu = null;
  164. if ($wpmu === null) {
  165. $wpmu = file_exists(ABSPATH . 'wpmu-settings.php');
  166. }
  167. return $wpmu;
  168. }
  169. /**
  170. * Returns true if WPMU uses vhosts
  171. *
  172. * @return boolean
  173. */
  174. function w3_is_subdomain_install() {
  175. return ((defined('SUBDOMAIN_INSTALL') && SUBDOMAIN_INSTALL) || (defined('VHOST') && VHOST == 'yes'));
  176. }
  177. /**
  178. * Returns true if it's WP with enabled Network mode
  179. *
  180. * @return boolean
  181. */
  182. function w3_is_multisite() {
  183. static $multisite = null;
  184. if ($multisite === null) {
  185. $multisite = ((defined('MULTISITE') && MULTISITE) || defined('SUNRISE') || w3_is_subdomain_install());
  186. }
  187. return $multisite;
  188. }
  189. /**
  190. * Returns if there is multisite mode
  191. *
  192. * @return boolean
  193. */
  194. function w3_is_network() {
  195. return (w3_is_wpmu() || w3_is_multisite());
  196. }
  197. /**
  198. * Check if URL is valid
  199. *
  200. * @param string $url
  201. * @return boolean
  202. */
  203. function w3_is_url($url) {
  204. return preg_match('~^(https?:)?//~', $url);
  205. }
  206. /**
  207. * Returns true if current connection is secure
  208. *
  209. * @return boolean
  210. */
  211. function w3_is_https() {
  212. switch (true) {
  213. case (isset($_SERVER['HTTPS']) && w3_to_boolean($_SERVER['HTTPS'])):
  214. case (isset($_SERVER['SERVER_PORT']) && (int) $_SERVER['SERVER_PORT'] == 443):
  215. return true;
  216. }
  217. return false;
  218. }
  219. /**
  220. * Check if there was database error
  221. *
  222. * @param string $content
  223. * @return boolean
  224. */
  225. function w3_is_database_error(&$content) {
  226. return (stristr($content, '<title>Database Error</title>') !== false);
  227. }
  228. /**
  229. * Retuns true if preview settings active
  230. *
  231. * @return boolean
  232. */
  233. function w3_is_preview_mode() {
  234. return (isset($_COOKIE['w3tc_preview']) && $_COOKIE['w3tc_preview'] == true) || (isset($_REQUEST['w3tc_preview']) ||
  235. (isset($_SERVER['HTTP_REFERER']) &&
  236. strstr($_SERVER['HTTP_REFERER'], 'w3tc_preview') !== false));
  237. }
  238. /**
  239. * Returns a preview link with current state
  240. * @return string
  241. */
  242. function w3tc_get_preview_link() {
  243. w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/ui.php');
  244. return w3_is_preview_mode() ? w3_button_link(__('Stop Previewing', 'w3-total-cache'), wp_nonce_url(w3_admin_url('admin.php?page=w3tc_dashboard&w3tc_default_stop_previewing'), 'w3tc'), false) : w3_button_link(__('Preview', 'w3-total-cache'), wp_nonce_url(w3_admin_url('admin.php?page=w3tc_dashboard&w3tc_default_previewing'), 'w3tc'), true);
  245. }
  246. /**
  247. * Returns true if server is Apache
  248. *
  249. * @return boolean
  250. */
  251. function w3_is_apache() {
  252. return (isset($_SERVER['SERVER_SOFTWARE']) && stristr($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false);
  253. }
  254. /**
  255. * Check whether server is LiteSpeed
  256. *
  257. * @return bool
  258. */
  259. function w3_is_litespeed() {
  260. return (isset($_SERVER['SERVER_SOFTWARE']) && stristr($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false);
  261. }
  262. /**
  263. * Returns true if server is nginx
  264. *
  265. * @return boolean
  266. */
  267. function w3_is_nginx() {
  268. return (isset($_SERVER['SERVER_SOFTWARE']) && stristr($_SERVER['SERVER_SOFTWARE'], 'nginx') !== false);
  269. }
  270. /**
  271. * Check whether $engine is correct CDN engine
  272. *
  273. * @param string $engine
  274. * @return boolean
  275. */
  276. function w3_is_cdn_engine($engine) {
  277. return in_array($engine, array('ftp', 's3', 'cf', 'cf2', 'rscf', 'azure', 'mirror', 'netdna', 'maxcdn',
  278. 'cotendo', 'akamai', 'edgecast', 'att'));
  279. }
  280. /**
  281. * Returns true if CDN engine is mirror
  282. *
  283. * @param string $engine
  284. * @return bool
  285. */
  286. function w3_is_cdn_mirror($engine) {
  287. return in_array($engine, array('mirror', 'netdna', 'maxcdn', 'cotendo', 'cf2', 'akamai', 'edgecast', 'att'));
  288. }
  289. /**
  290. * Returns true if CDN has purge all support
  291. * @param $engine
  292. * @return bool
  293. */
  294. function w3_cdn_can_purge_all($engine) {
  295. return in_array($engine, array('cotendo', 'edgecast', 'att', 'netdna', 'maxcdn'));
  296. }
  297. /**
  298. * Returns domain from host
  299. *
  300. * @param string $host
  301. * @return string
  302. */
  303. function w3_get_domain($host) {
  304. $host = strtolower($host);
  305. if (($pos = strpos($host, ':')) !== false) {
  306. $host = substr($host, $pos+3);
  307. }
  308. if (($pos = strpos($host, '/')) !== false) {
  309. $host = substr($host, 0, $pos);
  310. }
  311. $host = rtrim($host, '.');
  312. return $host;
  313. }
  314. /**
  315. * Returns array of all available blognames
  316. *
  317. * @return array
  318. */
  319. function w3_get_blognames() {
  320. global $wpdb;
  321. $blognames = array();
  322. $sql = sprintf('SELECT domain, path FROM %s', $wpdb->blogs);
  323. $blogs = $wpdb->get_results($sql);
  324. if ($blogs) {
  325. $base_path = w3_get_base_path();
  326. foreach ($blogs as $blog) {
  327. $blogname = trim(str_replace($base_path, '', $blog->path), '/');
  328. if ($blogname) {
  329. $blognames[] = $blogname;
  330. }
  331. }
  332. }
  333. return $blognames;
  334. }
  335. /**
  336. * Returns current blog ID
  337. *
  338. * @return integer
  339. */
  340. function w3_get_blog_id() {
  341. global $w3_current_blog_id;
  342. if (!is_null($w3_current_blog_id))
  343. return $w3_current_blog_id;
  344. if (!w3_is_network() || is_network_admin()) {
  345. $w3_current_blog_id = 0;
  346. return $w3_current_blog_id;
  347. }
  348. $blog_data = w3_blogmap_get_blog_data();
  349. if (!is_null($blog_data))
  350. $w3_current_blog_id = substr($blog_data, 1);
  351. else
  352. $w3_current_blog_id = 0;
  353. return $w3_current_blog_id;
  354. }
  355. /**
  356. * Returns blogmap filename by home url
  357. *
  358. * @param string $blog_home_url
  359. * @return string
  360. */
  361. function w3_blogmap_filename($blog_home_url) {
  362. if (!defined('W3TC_BLOG_LEVELS'))
  363. return W3TC_CACHE_BLOGMAP_FILENAME;
  364. else {
  365. $filename = dirname(W3TC_CACHE_BLOGMAP_FILENAME) . '/' .
  366. basename(W3TC_CACHE_BLOGMAP_FILENAME, '.php') . '/';
  367. $s = md5($blog_home_url);
  368. for ($n = 0; $n < W3TC_BLOG_LEVELS; $n++)
  369. $filename .= substr($s, $n, 1) . '/';
  370. return $filename . basename(W3TC_CACHE_BLOGMAP_FILENAME);
  371. }
  372. }
  373. /**
  374. * Returns blog_id by home url
  375. * If database not initialized yet - returns 0
  376. *
  377. * @return integer
  378. */
  379. function w3_blogmap_get_blog_data() {
  380. $host = w3_get_host();
  381. // subdomain
  382. if (w3_is_subdomain_install()) {
  383. $blog_data = w3_blogmap_try_get_blog_data($host);
  384. if (is_null($blog_data))
  385. $GLOBALS['w3tc_blogmap_register_new_item'] = $host;
  386. return $blog_data;
  387. } else {
  388. // try subdir blog
  389. $url = $host . $_SERVER['REQUEST_URI'];
  390. $pos = strpos($url, '?');
  391. if ($pos !== false)
  392. $url = substr($url, 0, $pos);
  393. $url = rtrim($url, '/');
  394. $start_url = $url;
  395. for (;;) {
  396. $blog_data = w3_blogmap_try_get_blog_data($url);
  397. if (!is_null($blog_data))
  398. return $blog_data;
  399. $pos = strrpos($url, '/');
  400. if ($pos === false)
  401. break;
  402. $url = rtrim(substr($url, 0, $pos), '/');
  403. }
  404. $GLOBALS['w3tc_blogmap_register_new_item'] = $start_url;
  405. return null;
  406. }
  407. }
  408. function w3_blogmap_try_get_blog_data($url) {
  409. $filename = w3_blogmap_filename($url);
  410. if (file_exists($filename)) {
  411. $data = file_get_contents($filename);
  412. $blog_data = @eval($data);
  413. if (is_array($blog_data) && isset($blog_data[$url]))
  414. return $blog_data[$url];
  415. }
  416. return null;
  417. }
  418. /**
  419. * @return bool
  420. */
  421. function w3_force_master() {
  422. global $w3_force_master;
  423. if (!is_null($w3_force_master))
  424. return $w3_force_master;
  425. if (!w3_is_multisite())
  426. $w3_force_master = false;
  427. else {
  428. $blog_data = w3_blogmap_get_blog_data();
  429. if (is_null($blog_data) ||
  430. ($blog_data[0] != 'm' && $blog_data[0] != 'c'))
  431. $w3_force_master = true;
  432. else
  433. $w3_force_master = ($blog_data[0] == 'm');
  434. }
  435. return $w3_force_master;
  436. }
  437. /**
  438. * Returns path to section's cache dir
  439. *
  440. * @param string $section
  441. * @return string
  442. */
  443. function w3_cache_dir($section) {
  444. return W3TC_CACHE_DIR . '/' . $section;
  445. }
  446. /**
  447. * Returns path to blog's cache dir
  448. *
  449. * @param string $section
  450. * @param null|int $blog_id
  451. * @return string
  452. */
  453. function w3_cache_blog_dir($section, $blog_id = null) {
  454. if (is_null($blog_id))
  455. $blog_id = w3_get_blog_id();
  456. $postfix = sprintf('%06d', $blog_id);
  457. if (defined('W3TC_BLOG_LEVELS')) {
  458. for ($n = 0; $n < W3TC_BLOG_LEVELS; $n++)
  459. $postfix = substr($postfix, strlen($postfix) - 1 - $n, 1) . '/' .
  460. $postfix;
  461. }
  462. return w3_cache_dir($section) . '/' . $postfix;
  463. }
  464. /**
  465. * Return full path to log file for module
  466. * Path used in priority
  467. * 1) W3TC_DEBUG_DIR
  468. * 2) WP_DEBUG_LOG
  469. * 3) W3TC_CACHE_DIR
  470. *
  471. * @param $module
  472. * @param null $blog_id
  473. * @return string
  474. */
  475. function w3_debug_log($module, $blog_id = null) {
  476. if (is_null($blog_id))
  477. $blog_id = w3_get_blog_id();
  478. $postfix = sprintf('%06d', $blog_id);
  479. if (defined('W3TC_BLOG_LEVELS')) {
  480. for ($n = 0; $n < W3TC_BLOG_LEVELS; $n++)
  481. $postfix = substr($postfix, strlen($postfix) - 1 - $n, 1) . '/' .
  482. $postfix;
  483. }
  484. $from_dir = W3TC_CACHE_DIR;
  485. if (defined('W3TC_DEBUG_DIR') && W3TC_DEBUG_DIR) {
  486. $dir_path = W3TC_DEBUG_DIR;
  487. if (!is_dir(W3TC_DEBUG_DIR))
  488. $from_dir = dirname(W3TC_DEBUG_DIR);
  489. } else
  490. $dir_path = w3_cache_dir('log');
  491. $filename = $dir_path . '/' . $postfix . '/' . $module . '.log';
  492. if (!is_dir(dirname($filename))) {
  493. w3_require_once( W3TC_INC_DIR . '/functions/file.php');
  494. w3_mkdir_from(dirname($filename), $from_dir);
  495. }
  496. return $filename;
  497. }
  498. /**
  499. * Returns URL regexp from URL
  500. *
  501. * @param string $url
  502. * @return string
  503. */
  504. function w3_get_url_regexp($url) {
  505. $url = preg_replace('~(https?:)?//~i', '', $url);
  506. $url = preg_replace('~^www\.~i', '', $url);
  507. $regexp = '(https?:)?//(www\.)?' . w3_preg_quote($url);
  508. return $regexp;
  509. }
  510. /**
  511. * Returns SSL URL if current connection is https
  512. * @param string $url
  513. * @return string
  514. */
  515. function w3_get_url_ssl($url) {
  516. if (w3_is_https()) {
  517. $url = str_replace('http://', 'https://', $url);
  518. }
  519. return $url;
  520. }
  521. /**
  522. * Get domain URL
  523. *
  524. * @return string
  525. */
  526. function w3_get_domain_url() {
  527. $home_url = w3_get_home_url();
  528. $parse_url = @parse_url($home_url);
  529. if ($parse_url && isset($parse_url['scheme']) && isset($parse_url['host'])) {
  530. $scheme = $parse_url['scheme'];
  531. $host = $parse_url['host'];
  532. $port = (isset($parse_url['port']) && $parse_url['port'] != 80 ? ':' . (int) $parse_url['port'] : '');
  533. $domain_url = sprintf('%s://%s%s', $scheme, $host, $port);
  534. return $domain_url;
  535. }
  536. return false;
  537. }
  538. /**
  539. * Returns domain url regexp
  540. *
  541. * @return string
  542. */
  543. function w3_get_domain_url_regexp() {
  544. $domain_url = w3_get_domain_url();
  545. $regexp = w3_get_url_regexp($domain_url);
  546. return $regexp;
  547. }
  548. /**
  549. * Returns home URL
  550. *
  551. * No trailing slash!
  552. *
  553. * @return string
  554. */
  555. function w3_get_home_url() {
  556. static $home_url = null;
  557. if ($home_url === null) {
  558. $config = w3_instance('W3_Config');
  559. if (w3_is_multisite() && $config->get_boolean('common.force_master')) {
  560. $home_url = get_home_url();
  561. } else {
  562. // get_option is unusable here, it can cause problem when objCache isn't yet initialized
  563. // Which is why we save the 'home' option in our ConfigCache
  564. // We don't just use $config->get_string, because we want the cache to rebuild
  565. // when 'wordpress.home' is not (yet) present
  566. $home_url = $config->get_cache_option('wordpress.home');
  567. $home_url = rtrim($home_url, '/');
  568. }
  569. }
  570. return $home_url;
  571. }
  572. /**
  573. * Returns SSL home url
  574. *
  575. * @return string
  576. */
  577. function w3_get_home_url_ssl() {
  578. $home_url = w3_get_home_url();
  579. $ssl = w3_get_url_ssl($home_url);
  580. return $ssl;
  581. }
  582. /**
  583. * Returns home url regexp
  584. *
  585. * @return string
  586. */
  587. function w3_get_home_url_regexp() {
  588. $home_url = w3_get_home_url();
  589. $regexp = w3_get_url_regexp($home_url);
  590. return $regexp;
  591. }
  592. /**
  593. * Network installs returns wrong wp site path
  594. * @return string
  595. */
  596. function w3_get_wp_sitepath() {
  597. if (w3_is_network()) {
  598. global $current_site;
  599. return $current_site->path;
  600. } else {
  601. return w3_get_site_path();
  602. }
  603. }
  604. /**
  605. * Returns site URL
  606. *
  607. * No trailing slash!
  608. *
  609. * @return string
  610. */
  611. function w3_get_site_url() {
  612. static $site_url = null;
  613. if ($site_url === null) {
  614. $site_url = get_option('siteurl');
  615. $site_url = rtrim($site_url, '/');
  616. }
  617. return $site_url;
  618. }
  619. /**
  620. * Returns SSL site URL
  621. *
  622. * @return string
  623. */
  624. function w3_get_site_url_ssl() {
  625. $site_url = w3_get_site_url();
  626. $ssl = w3_get_url_ssl($site_url);
  627. return $ssl;
  628. }
  629. /**
  630. * Returns absolute path to document root
  631. *
  632. * No trailing slash!
  633. *
  634. * @return string
  635. */
  636. function w3_get_document_root() {
  637. static $document_root = null;
  638. if ($document_root === null) {
  639. if (!empty($_SERVER['SCRIPT_FILENAME']) && $_SERVER['SCRIPT_FILENAME'] == $_SERVER['PHP_SELF']) {
  640. $document_root = w3_get_site_root();
  641. } elseif (!empty($_SERVER['SCRIPT_FILENAME'])) {
  642. $document_root = substr(w3_path($_SERVER['SCRIPT_FILENAME']), 0, -strlen(w3_path($_SERVER['PHP_SELF'])));
  643. } elseif (!empty($_SERVER['PATH_TRANSLATED'])) {
  644. $document_root = substr(w3_path($_SERVER['PATH_TRANSLATED']), 0, -strlen(w3_path($_SERVER['PHP_SELF'])));
  645. } elseif (!empty($_SERVER['DOCUMENT_ROOT'])) {
  646. $document_root = w3_path($_SERVER['DOCUMENT_ROOT']);
  647. } else {
  648. $document_root = w3_get_site_root();
  649. }
  650. $document_root = realpath($document_root);
  651. $document_root = w3_path($document_root);
  652. }
  653. return $document_root;
  654. }
  655. /**
  656. * Returns absolute path to home directory
  657. *
  658. * Example:
  659. *
  660. * DOCUMENT_ROOT=/var/www/vhosts/domain.com
  661. * Install dir=/var/www/vhosts/domain.com/site/blog
  662. * home=http://domain.com/site
  663. * siteurl=http://domain.com/site/blog
  664. * return /var/www/vhosts/domain.com/site
  665. *
  666. * No trailing slash!
  667. *
  668. * @return string
  669. */
  670. function w3_get_home_root() {
  671. if (w3_is_network()) {
  672. $path = w3_get_base_path();
  673. } else {
  674. $path = w3_get_home_path();
  675. }
  676. $home_root = w3_get_document_root() . $path;
  677. $home_root = realpath($home_root);
  678. $home_root = w3_path($home_root);
  679. return $home_root;
  680. }
  681. /**
  682. * Returns absolute path to blog install dir
  683. *
  684. * Example:
  685. *
  686. * DOCUMENT_ROOT=/var/www/vhosts/domain.com
  687. * install dir=/var/www/vhosts/domain.com/site/blog
  688. * return /var/www/vhosts/domain.com/site/blog
  689. *
  690. * No trailing slash!
  691. *
  692. * @return string
  693. */
  694. function w3_get_site_root() {
  695. $site_root = ABSPATH;
  696. $site_root = realpath($site_root);
  697. $site_root = w3_path($site_root);
  698. return $site_root;
  699. }
  700. /**
  701. * Returns blog path
  702. *
  703. * Example:
  704. *
  705. * siteurl=http://domain.com/site/blog
  706. * return /site/blog/
  707. *
  708. * With trailing slash!
  709. *
  710. * @return string
  711. */
  712. function w3_get_site_path() {
  713. $site_url = w3_get_site_url();
  714. $parse_url = @parse_url($site_url);
  715. if ($parse_url && isset($parse_url['path'])) {
  716. $site_path = '/' . ltrim($parse_url['path'], '/');
  717. } else {
  718. $site_path = '/';
  719. }
  720. if (substr($site_path, -1) != '/') {
  721. $site_path .= '/';
  722. }
  723. return $site_path;
  724. }
  725. /**
  726. * Returns home domain
  727. *
  728. * @return string
  729. */
  730. function w3_get_home_domain() {
  731. $home_url = w3_get_home_url();
  732. $parse_url = @parse_url($home_url);
  733. if ($parse_url && isset($parse_url['host'])) {
  734. return $parse_url['host'];
  735. }
  736. return w3_get_host();
  737. }
  738. /**
  739. * Returns home path
  740. *
  741. * Example:
  742. *
  743. * home=http://domain.com/site/
  744. * siteurl=http://domain.com/site/blog
  745. * return /site/
  746. *
  747. * With trailing slash!
  748. *
  749. * @return string
  750. */
  751. function w3_get_home_path() {
  752. $home_url = w3_get_home_url();
  753. $parse_url = @parse_url($home_url);
  754. if ($parse_url && isset($parse_url['path'])) {
  755. $home_path = '/' . ltrim($parse_url['path'], '/');
  756. } else {
  757. $home_path = '/';
  758. }
  759. if (substr($home_path, -1) != '/') {
  760. $home_path .= '/';
  761. }
  762. return $home_path;
  763. }
  764. /**
  765. * Returns path to WP directory relative to document root
  766. *
  767. * Example:
  768. *
  769. * DOCUMENT_ROOT=/var/www/vhosts/domain.com/
  770. * Install dir=/var/www/vhosts/domain.com/site/blog/
  771. * return /site/blog/
  772. *
  773. * With trailing slash!
  774. *
  775. * @return string
  776. */
  777. function w3_get_base_path() {
  778. $document_root = w3_get_document_root();
  779. $site_root = w3_get_site_root();
  780. $base_path = str_replace($document_root, '', $site_root);
  781. $base_path = '/' . ltrim($base_path, '/');
  782. if (substr($base_path, -1) != '/') {
  783. $base_path .= '/';
  784. }
  785. return $base_path;
  786. }
  787. /**
  788. * Returns server hostname
  789. *
  790. * @return string
  791. */
  792. function w3_get_host() {
  793. static $host = null;
  794. if ($host === null) {
  795. if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
  796. $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
  797. } elseif (!empty($_SERVER['HTTP_HOST'])) {
  798. // HTTP_HOST sometimes is not set causing warning
  799. $host = $_SERVER['HTTP_HOST'];
  800. } else {
  801. $host = '';
  802. }
  803. }
  804. return $host;
  805. }
  806. /**
  807. * Returns host ID
  808. *
  809. * @return string
  810. */
  811. function w3_get_host_id() {
  812. static $host_id = null;
  813. if ($host_id === null) {
  814. $host = w3_get_host();
  815. $blog_id = w3_get_blog_id();
  816. $host_id = sprintf('%s_%d', $host, $blog_id);
  817. }
  818. return $host_id;
  819. }
  820. /**
  821. * Returns WP config file path
  822. *
  823. * @return string
  824. */
  825. function w3_get_wp_config_path() {
  826. $search = array(
  827. ABSPATH . 'wp-config.php',
  828. dirname(ABSPATH) . '/wp-config.php'
  829. );
  830. foreach ($search as $path) {
  831. if (file_exists($path)) {
  832. return $path;
  833. }
  834. }
  835. return false;
  836. }
  837. /**
  838. * Returns theme key
  839. *
  840. * @param string $theme_root
  841. * @param string $template
  842. * @param string $stylesheet
  843. * @return string
  844. */
  845. function w3_get_theme_key($theme_root, $template, $stylesheet) {
  846. $site_root = w3_get_site_root();
  847. $theme_path = ltrim(str_replace($site_root, '', w3_path($theme_root)), '/');
  848. return substr(md5($theme_path . $template . $stylesheet), 0, 5);
  849. }
  850. /**
  851. * Returns theme key (legacy support)
  852. *
  853. * @param string $theme_root
  854. * @param string $template
  855. * @param string $stylesheet
  856. * @return string
  857. */
  858. function w3_get_theme_key_legacy($theme_root, $template, $stylesheet) {
  859. return substr(md5($theme_root . $template . $stylesheet), 0, 6);
  860. }
  861. /**
  862. * Returns true if we can check rules
  863. *
  864. * @return bool
  865. */
  866. function w3_can_check_rules() {
  867. return (w3_is_apache() || w3_is_litespeed() || w3_is_nginx());
  868. }
  869. /**
  870. * Returns true if CDN engine is supporting purge
  871. *
  872. * @param string $engine
  873. * @return bool
  874. */
  875. function w3_can_cdn_purge($engine) {
  876. return in_array($engine, array('ftp', 's3', 'cf', 'cf2', 'rscf', 'azure', 'netdna', 'maxcdn', 'cotendo',
  877. 'edgecast', 'akamai', 'att'));
  878. }
  879. /**
  880. * Returns true if CDN supports realtime purge. That is purging on post changes, comments etc.
  881. * @param $engine
  882. * @return bool
  883. */
  884. function w3tc_cdn_supports_realtime_purge($engine) {
  885. return !in_array($engine, array('cf2'));
  886. }
  887. /**
  888. * Parses path
  889. *
  890. * @param string $path
  891. * @return mixed
  892. */
  893. function w3_parse_path($path) {
  894. $path = str_replace(array(
  895. '%BLOG_ID%',
  896. '%POST_ID%',
  897. '%BLOG_ID%',
  898. '%HOST%',
  899. '%DOMAIN%',
  900. '%BASE_PATH%'
  901. ), array(
  902. (isset($GLOBALS['blog_id']) ? (int) $GLOBALS['blog_id'] : 0),
  903. (isset($GLOBALS['post_id']) ? (int) $GLOBALS['post_id'] : 0),
  904. w3_get_blog_id(),
  905. w3_get_host(),
  906. w3_get_domain(w3_get_host()),
  907. trim(w3_get_base_path(), '/')
  908. ), $path);
  909. return $path;
  910. }
  911. /**
  912. * Normalizes file name
  913. *
  914. * Relative to site root!
  915. *
  916. * @param string $file
  917. * @return string
  918. */
  919. function w3_normalize_file($file) {
  920. if (w3_is_url($file)) {
  921. if (strstr($file, '?') === false) {
  922. $home_url_regexp = '~' . w3_get_home_url_regexp() . '~i';
  923. $file = preg_replace($home_url_regexp, '', $file);
  924. }
  925. }
  926. if (!w3_is_url($file)) {
  927. $file = w3_path($file);
  928. $file = str_replace(w3_get_site_root(), '', $file);
  929. $file = ltrim($file, '/');
  930. }
  931. return $file;
  932. }
  933. /**
  934. * Normalizes file name for minify
  935. *
  936. * Relative to document root!
  937. *
  938. * @param string $file
  939. * @return string
  940. */
  941. function w3_normalize_file_minify($file) {
  942. if (w3_is_url($file)) {
  943. if (strstr($file, '?') === false) {
  944. $domain_url_regexp = '~' . w3_get_domain_url_regexp() . '~i';
  945. $file = preg_replace($domain_url_regexp, '', $file);
  946. }
  947. }
  948. if (!w3_is_url($file)) {
  949. $file = w3_path($file);
  950. $file = str_replace(w3_get_document_root(), '', $file);
  951. $file = ltrim($file, '/');
  952. }
  953. return $file;
  954. }
  955. /**
  956. * Normalizes file name for minify
  957. *
  958. * Relative to document root!
  959. *
  960. * @param string $file
  961. * @return string
  962. */
  963. function w3_normalize_file_minify2($file) {
  964. $file = w3_remove_query($file);
  965. $file = w3_normalize_file_minify($file);
  966. $file = w3_translate_file($file);
  967. return $file;
  968. }
  969. /**
  970. * Translates remote file to local file
  971. *
  972. * @param string $file
  973. * @return string
  974. */
  975. function w3_translate_file($file) {
  976. if (!w3_is_url($file)) {
  977. $file = '/' . ltrim($file, '/');
  978. $regexp = '~^' . w3_preg_quote(w3_get_site_path()) . '~';
  979. $file = preg_replace($regexp, w3_get_base_path(), $file);
  980. $file = ltrim($file, '/');
  981. }
  982. return $file;
  983. }
  984. /**
  985. * Remove WP query string from URL
  986. *
  987. * @param string $url
  988. * @return string
  989. */
  990. function w3_remove_query($url) {
  991. $url = preg_replace('~[&\?]+(ver=([a-z0-9-_\.]+|[0-9-]+))~i', '', $url);
  992. return $url;
  993. }
  994. /**
  995. * Converts win path to unix
  996. *
  997. * @param string $path
  998. * @return string
  999. */
  1000. function w3_path($path) {
  1001. $path = preg_replace('~[/\\\]+~', '/', $path);
  1002. $path = rtrim($path, '/');
  1003. return $path;
  1004. }
  1005. /**
  1006. * Returns real path of given path
  1007. *
  1008. * @param string $path
  1009. * @return string
  1010. */
  1011. function w3_realpath($path) {
  1012. $path = w3_path($path);
  1013. $parts = explode('/', $path);
  1014. $absolutes = array();
  1015. foreach ($parts as $part) {
  1016. if ('.' == $part) {
  1017. continue;
  1018. }
  1019. if ('..' == $part) {
  1020. array_pop($absolutes);
  1021. } else {
  1022. $absolutes[] = $part;
  1023. }
  1024. }
  1025. return implode('/', $absolutes);
  1026. }
  1027. /**
  1028. * Returns GMT date
  1029. * @param integer $time
  1030. * @return string
  1031. */
  1032. function w3_http_date($time) {
  1033. return gmdate('D, d M Y H:i:s \G\M\T', $time);
  1034. }
  1035. /**
  1036. * Redirects to URL
  1037. *
  1038. * @param string $url
  1039. * @param array $params
  1040. * @return string
  1041. */
  1042. function w3_redirect($url = '', $params = array()) {
  1043. w3_require_once(W3TC_INC_DIR . '/functions/url.php');
  1044. $url = w3_url_format($url, $params);
  1045. if (function_exists('do_action'))
  1046. do_action('w3_redirect');
  1047. @header('Location: ' . $url);
  1048. exit();
  1049. }
  1050. /**
  1051. * Redirects to URL
  1052. *
  1053. * @param string $url
  1054. * @param array $params
  1055. *
  1056. * @return string
  1057. */
  1058. function w3_redirect_temp( $url = '', $params = array() ) {
  1059. w3_require_once( W3TC_INC_DIR . '/functions/url.php' );
  1060. $url = w3_url_format( $url, $params );
  1061. if (function_exists('do_action'))
  1062. do_action( 'w3_redirect' );
  1063. $status_code = 301;
  1064. $protocol = $_SERVER["SERVER_PROTOCOL"];
  1065. if ( 'HTTP/1.1' === $protocol ) {
  1066. $status_code = 307;
  1067. }
  1068. $text = get_status_header_desc( $status_code );
  1069. if ( !empty( $text ) ) {
  1070. $status_header = "$protocol $status_code $text";
  1071. @header( $status_header, true, $status_code );
  1072. }
  1073. @header( 'Location: ' . $url, true, $status_code );
  1074. exit();
  1075. }
  1076. /**
  1077. * Returns caching engine name
  1078. *
  1079. * @param $engine
  1080. * @param $module
  1081. *
  1082. * @return string
  1083. */
  1084. function w3_get_engine_name($engine, $module = '') {
  1085. switch ($engine) {
  1086. case 'memcached':
  1087. $engine_name = 'memcached';
  1088. break;
  1089. case 'apc':
  1090. $engine_name = 'apc';
  1091. break;
  1092. case 'eaccelerator':
  1093. $engine_name = 'eaccelerator';
  1094. break;
  1095. case 'xcache':
  1096. $engine_name = 'xcache';
  1097. break;
  1098. case 'wincache':
  1099. $engine_name = 'wincache';
  1100. break;
  1101. case 'file':
  1102. if ($module == 'pgcache')
  1103. $engine_name = 'disk: basic';
  1104. else
  1105. $engine_name = 'disk';
  1106. break;
  1107. case 'file_generic':
  1108. $engine_name = 'disk: enhanced';
  1109. break;
  1110. case 'ftp':
  1111. $engine_name = 'self-hosted / file transfer protocol upload';
  1112. break;
  1113. case 's3':
  1114. $engine_name = 'amazon simple storage service (s3)';
  1115. break;
  1116. case 'cf':
  1117. $engine_name = 'amazon cloudfront';
  1118. break;
  1119. case 'cf2':
  1120. $engine_name = 'amazon cloudfront';
  1121. break;
  1122. case 'rscf':
  1123. $engine_name = 'rackspace cloud files';
  1124. break;
  1125. case 'azure':
  1126. $engine_name = 'microsoft azure storage';
  1127. break;
  1128. case 'mirror':
  1129. $engine_name = 'mirror';
  1130. break;
  1131. case 'netdna':
  1132. $engine_name = 'netdna';
  1133. break;
  1134. case 'maxcdn':
  1135. $engine_name = 'maxcdn';
  1136. break;
  1137. case 'cotendo':
  1138. $engine_name = 'cotendo';
  1139. break;
  1140. case 'akamai':
  1141. $engine_name = 'akamai';
  1142. break;
  1143. case 'edgecast':
  1144. $engine_name = 'media template procdn / edgecast';
  1145. break;
  1146. case 'att':
  1147. $engine_name = 'at&amp;t';
  1148. break;
  1149. default:
  1150. $engine_name = 'n/a';
  1151. break;
  1152. }
  1153. return $engine_name;
  1154. }
  1155. /**
  1156. * Converts value to boolean
  1157. *
  1158. * @param mixed $value
  1159. * @return boolean
  1160. */
  1161. function w3_to_boolean($value) {
  1162. if (is_string($value)) {
  1163. switch (strtolower($value)) {
  1164. case '+':
  1165. case '1':
  1166. case 'y':
  1167. case 'on':
  1168. case 'yes':
  1169. case 'true':
  1170. case 'enabled':
  1171. return true;
  1172. case '-':
  1173. case '0':
  1174. case 'n':
  1175. case 'no':
  1176. case 'off':
  1177. case 'false':
  1178. case 'disabled':
  1179. return false;
  1180. }
  1181. }
  1182. return (boolean) $value;
  1183. }
  1184. /**
  1185. * Quotes regular expression string
  1186. *
  1187. * @param string $string
  1188. * @param string $delimiter
  1189. * @return string
  1190. */
  1191. function w3_preg_quote($string, $delimiter = null) {
  1192. $string = preg_quote($string, $delimiter);
  1193. $string = strtr($string, array(
  1194. ' ' => '\ '
  1195. ));
  1196. return $string;
  1197. }
  1198. /**
  1199. * Returns true if zlib output compression is enabled otherwise false
  1200. *
  1201. * @return boolean
  1202. */
  1203. function w3_zlib_output_compression() {
  1204. return w3_to_boolean(ini_get('zlib.output_compression'));
  1205. }
  1206. /**
  1207. * Recursive strips slahes from the var
  1208. *
  1209. * @param mixed $var
  1210. * @return mixed
  1211. */
  1212. function w3_stripslashes($var) {
  1213. if (is_string($var)) {
  1214. return stripslashes($var);
  1215. } elseif (is_array($var)) {
  1216. $var = array_map('w3_stripslashes', $var);
  1217. }
  1218. return $var;
  1219. }
  1220. /**
  1221. * Escapes HTML comment
  1222. *
  1223. * @param string $comment
  1224. * @return mixed
  1225. */
  1226. function w3_escape_comment($comment) {
  1227. while (strstr($comment, '--') !== false) {
  1228. $comment = str_replace('--', '- -', $comment);
  1229. }
  1230. return $comment;
  1231. }
  1232. /**
  1233. * Returns instance of singleton class
  1234. *
  1235. * @param string $class
  1236. * @return object
  1237. */
  1238. function w3_instance($class) {
  1239. static $instances = array();
  1240. if (!isset($instances[$class])) {
  1241. w3_require_once( W3TC_LIB_W3_DIR . '/' .
  1242. str_replace('_', '/', substr($class, 3)) . '.php');
  1243. $instances[$class] = new $class();
  1244. }
  1245. $v = $instances[$class]; // Don't return reference
  1246. return $v;
  1247. }
  1248. /**
  1249. * Requires and keeps track of which files has already been loaded.
  1250. *
  1251. * @param string $path Absolute path to the file
  1252. */
  1253. function w3_require_once($path) {
  1254. static $files = array();
  1255. if (!isset($files[$path])) {
  1256. $files[$path] = 1;
  1257. require_once $path;
  1258. }
  1259. }
  1260. /**
  1261. * Detects post ID
  1262. *
  1263. * @return integer
  1264. */
  1265. function w3_detect_post_id() {
  1266. global $posts, $comment_post_ID, $post_ID;
  1267. if ($post_ID) {
  1268. return $post_ID;
  1269. } elseif ($comment_post_ID) {
  1270. return $comment_post_ID;
  1271. } elseif (is_single() || is_page() && count($posts)) {
  1272. return $posts[0]->ID;
  1273. } elseif (isset($_REQUEST['p'])) {
  1274. return (integer) $_REQUEST['p'];
  1275. }
  1276. return 0;
  1277. }
  1278. function w3_get_instance_id() {
  1279. static $instance_id;
  1280. if(!isset($instance_id)) {
  1281. $config = w3_instance('W3_Config');
  1282. $instance_id = $config->get_integer('common.instance_id', 0);
  1283. }
  1284. return $instance_id;
  1285. }
  1286. /**
  1287. * Checks if post should be flushed or not. Returns true if it should not be flushed
  1288. * @param $post
  1289. * @param string $module which cache module to check against (pgcache, varnish, cdncache, dbcache or objectcache)
  1290. * @param W3_Config $config
  1291. * @return bool
  1292. */
  1293. function w3_is_flushable_post($post, $module, $config) {
  1294. if (is_numeric($post))
  1295. $post = get_post($post);
  1296. $post_status = array('publish');
  1297. // dont flush when we have post "attachment"
  1298. // its child of the post and is flushed always when post is published, while not changed in fact
  1299. $post_type = array('revision', 'attachment');
  1300. switch($module) {
  1301. case 'pgcache':
  1302. case 'varnish':
  1303. case 'cdncache':
  1304. if (!$config->get_boolean('pgcache.reject.logged'))
  1305. $post_status[] = 'private';
  1306. break;
  1307. case 'dbcache':
  1308. if (!$config->get_boolean('dbcache.reject.logged'))
  1309. $post_status[] = 'private';
  1310. break;
  1311. }
  1312. $flushable = !in_array($post->post_type, $post_type) && in_array($post->post_status, $post_status);
  1313. return apply_filters('w3tc_flushable_post', $flushable, $post, $module);
  1314. }
  1315. /**
  1316. * Takes seconds and converts to array('Nh ','Nm ', 'Ns ', 'Nms ') or "Nh Nm Ns Nms"
  1317. * @param $input
  1318. * @param bool $string
  1319. * @return array|string
  1320. */
  1321. function w3_convert_secs_to_time($input, $string = true) {
  1322. $input = (double)$input;
  1323. $time = array();
  1324. $msecs = floor($input*1000 % 1000);
  1325. $seconds = $input % 60;
  1326. $input = floor($input / 60);
  1327. $minutes = $input % 60;
  1328. $input = floor($input / 60);
  1329. $hours = $input % 60;
  1330. if ($hours)
  1331. $time[] = sprintf(__('%dh', 'w3-total-cache'), $hours);
  1332. if ($minutes)
  1333. $time[] = sprintf(__('%dm', 'w3-total-cache'), $minutes);
  1334. if ($seconds)
  1335. $time[] = sprintf(__('%ds', 'w3-total-cache'), $seconds);
  1336. if ($msecs)
  1337. $time[] = sprintf(__('%dms', 'w3-total-cache'), $msecs);
  1338. if(empty($time))
  1339. $time[] = sprintf(__('%dms', 'w3-total-cache'), 0);
  1340. if ($string)
  1341. return implode(' ', $time);
  1342. return $time;
  1343. }
  1344. /**
  1345. * @var W3_Config $config
  1346. * @return string
  1347. */
  1348. function w3_w3tc_release_version($config = null) {
  1349. if (w3_is_enterprise($config))
  1350. return 'enterprise';
  1351. if (w3_is_pro($config) && w3tc_is_pro_dev_mode())
  1352. return 'pro development';
  1353. if (w3_is_pro($config))
  1354. return 'pro';
  1355. return 'community';
  1356. }
  1357. /**
  1358. * @param W3_Config $config
  1359. * @return bool
  1360. */
  1361. function w3_is_pro($config = null) {
  1362. $result = false;
  1363. if ($config)
  1364. $result = $config->get_string('plugin.type') == 'pro' ||
  1365. ($config->get_string('plugin.type') == 'pro_dev' );
  1366. return $result || (defined('W3TC_PRO') && W3TC_PRO);
  1367. }
  1368. /**
  1369. * Enable Pro Dev mode support
  1370. * @return bool
  1371. */
  1372. function w3tc_is_pro_dev_mode() {
  1373. return defined('W3TC_PRO_DEV_MODE') && W3TC_PRO_DEV_MODE;
  1374. }
  1375. /**
  1376. * @param W3_Config $config
  1377. * @return bool
  1378. */
  1379. function w3_is_enterprise($config = null) {
  1380. $result = false;
  1381. if ($config)
  1382. $result = $config->get_string('plugin.type') == 'enterprise';
  1383. return $result || (defined('W3TC_ENTERPRISE') && W3TC_ENTERPRISE);
  1384. }
  1385. /**
  1386. * Checks if site is using edge mode.
  1387. * @return bool
  1388. */
  1389. function w3tc_edge_mode() {
  1390. return defined('W3TC_EDGE_MODE') && W3TC_EDGE_MODE;
  1391. }