PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/blog/wp-content/plugins/w3-total-cache/inc/functions/plugin.php

https://bitbucket.org/bsnowman/classyblog
PHP | 413 lines | 172 code | 48 blank | 193 comment | 10 complexity | ae7ed5debad19aecca6bef8e483a40c1 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * W3 Total Cache plugins API
  4. */
  5. /**
  6. * Add W3TC action callback
  7. *
  8. * @param string $action
  9. * @param mixed $callback
  10. * @return void
  11. */
  12. function w3tc_add_action($action, $callback) {
  13. $GLOBALS['_w3tc_actions'][$action][] = $callback;
  14. }
  15. /**
  16. * Do W3TC action
  17. *
  18. * @param string $action
  19. * @param mixed $value
  20. * @return mixed
  21. */
  22. function w3tc_do_action($action, $value = null) {
  23. if (isset($GLOBALS['_w3tc_actions'][$action])) {
  24. foreach ((array) $GLOBALS['_w3tc_actions'][$action] as $callback) {
  25. if (is_callable($callback)) {
  26. $value = call_user_func($callback, $value);
  27. }
  28. }
  29. }
  30. return $value;
  31. }
  32. /**
  33. * Shortcut for page cache flush
  34. *
  35. * @return boolean
  36. */
  37. function w3tc_pgcache_flush() {
  38. $w3_pgcache = w3_instance('W3_PgCacheFlush');
  39. return $w3_pgcache->flush();
  40. }
  41. /**
  42. * Shortcut for page post cache flush
  43. *
  44. * @param integer $post_id
  45. * @return boolean
  46. */
  47. function w3tc_pgcache_flush_post($post_id) {
  48. $w3_cacheflush = w3_instance('W3_CacheFlush');
  49. return $w3_cacheflush->pgcache_flush_post($post_id);
  50. }
  51. /**
  52. * Shortcut for url page cache flush
  53. *
  54. * @param string $url
  55. * @return boolean
  56. */
  57. function w3tc_pgcache_flush_url($url) {
  58. $w3_cacheflush = w3_instance('W3_CacheFlush');
  59. return $w3_cacheflush->pgcache_flush_url($url);
  60. }
  61. /**
  62. * Shortcut for database cache flush
  63. *
  64. * @return boolean
  65. */
  66. function w3tc_dbcache_flush() {
  67. $w3_db = w3_instance('W3_DbCache');
  68. return $w3_db->flush_cache();
  69. }
  70. /**
  71. * Shortcut for minify cache flush
  72. *
  73. * @return boolean
  74. */
  75. function w3tc_minify_flush() {
  76. $w3_minify = w3_instance('W3_Minify');
  77. return $w3_minify->flush();
  78. }
  79. /**
  80. * Shortcut for objectcache cache flush
  81. *
  82. * @return boolean
  83. */
  84. function w3tc_objectcache_flush() {
  85. $w3_objectcache = w3_instance('W3_ObjectCache');
  86. return $w3_objectcache->flush();
  87. }
  88. /**
  89. * Shortcut for CDN cache post purge
  90. * @param $post_id
  91. * @return mixed
  92. */
  93. function w3tc_cdncache_purge_post($post_id) {
  94. $w3_cacheflush = w3_instance('W3_CacheFlush');
  95. return $w3_cacheflush->cdncache_purge_post($post_id);
  96. }
  97. /**
  98. * Shortcut for CDN cache url purge
  99. * @param string $url
  100. * @return mixed
  101. */
  102. function w3tc_cdncache_purge_url($url) {
  103. $w3_cacheflush = w3_instance('W3_CacheFlush');
  104. return $w3_cacheflush->cdncache_purge_url($url);
  105. }
  106. /**
  107. * Shortcut for CDN cache purge
  108. * @return mixed
  109. */
  110. function w3tc_cdncache_purge() {
  111. $w3_cacheflush = w3_instance('W3_CacheFlush');
  112. return $w3_cacheflush->cdncache_purge();
  113. }
  114. /**
  115. * Shortcut for CDN purge files
  116. * @param array $files Array consisting of uri paths (i.e wp-content/uploads/image.pnp)
  117. * @return mixed
  118. */
  119. function w3tc_cdn_purge_files($files) {
  120. $w3_cacheflush = w3_instance('W3_CacheFlush');
  121. return $w3_cacheflush->cdn_purge_files($files);
  122. }
  123. /**
  124. * Prints script tag for scripts group
  125. *
  126. * @param string $location
  127. * @retun void
  128. */
  129. function w3tc_minify_script_group($location) {
  130. $w3_plugin_minify = w3_instance('W3_Plugin_Minify');
  131. $w3_plugin_minify->printed_scripts[] = $location;
  132. echo $w3_plugin_minify->get_script_group($location);
  133. }
  134. /**
  135. * Prints style tag for styles group
  136. *
  137. * @param string $location
  138. * @retun void
  139. */
  140. function w3tc_minify_style_group($location) {
  141. $w3_plugin_minify = w3_instance('W3_Plugin_Minify');
  142. $w3_plugin_minify->printed_styles[] = $location;
  143. echo $w3_plugin_minify->get_style_group($location);
  144. }
  145. /**
  146. * Prints script tag for custom scripts
  147. *
  148. * @param string|array $files
  149. * @param boolean $blocking
  150. * @return void
  151. */
  152. function w3tc_minify_script_custom($files, $blocking = true) {
  153. $w3_plugin_minify = w3_instance('W3_Plugin_Minify');
  154. echo $w3_plugin_minify->get_script_custom($files, $blocking);
  155. }
  156. /**
  157. * Prints style tag for custom styles
  158. *
  159. * @param string|array $files
  160. * @param boolean $import
  161. * @return void
  162. */
  163. function w3tc_minify_style_custom($files, $import = false) {
  164. $w3_plugin_minify = w3_instance('W3_Plugin_Minify');
  165. echo $w3_plugin_minify->get_style_custom($files, $import);
  166. }
  167. /**
  168. * @param string $fragment_group
  169. * @param boolean $global If group is for whole network in MS install
  170. * @return mixed
  171. */
  172. function w3tc_fragmentcache_flush_group($fragment_group, $global = false) {
  173. $w3_fragmentcache = w3_instance('W3_CacheFlush');
  174. return $w3_fragmentcache->fragmentcache_flush_group($fragment_group, $global);
  175. }
  176. /**
  177. * Flush all fragment groups
  178. * @return mixed
  179. */
  180. function w3tc_fragmentcache_flush() {
  181. $w3_fragmentcache = w3_instance('W3_CacheFlush');
  182. return $w3_fragmentcache->fragmentcache_flush();
  183. }
  184. /**
  185. * Register a fragment group and connected actions for current blog
  186. * @param string $group
  187. * @param array $actions on which actions group should be flushed
  188. * @return mixed
  189. */
  190. function w3tc_register_fragment_group($group, $actions) {
  191. $w3_fragmentcache = w3_instance('W3_Pro_Plugin_FragmentCache');
  192. return $w3_fragmentcache->register_group($group, $actions);
  193. }
  194. /**
  195. * Register a fragment group for whole network in MS install
  196. * @param $group
  197. * @param $actions
  198. * @return mixed
  199. */
  200. function w3tc_register_fragment_global_group($group, $actions) {
  201. $w3_fragmentcache = w3_instance('W3_Pro_Plugin_FragmentCache');
  202. return $w3_fragmentcache->register_global_group($group, $actions);
  203. }
  204. /**
  205. * Shortcut for varnish flush
  206. *
  207. * @return boolean
  208. */
  209. function w3tc_varnish_flush() {
  210. $w3_pgcache = w3_instance('W3_CacheFlush');
  211. return $w3_pgcache->varnish_flush();
  212. }
  213. /**
  214. * Shortcut for post varnish flush
  215. *
  216. * @param integer $post_id
  217. * @return boolean
  218. */
  219. function w3tc_varnish_flush_post($post_id) {
  220. $w3_cacheflush = w3_instance('W3_CacheFlush');
  221. return $w3_cacheflush->varnish_flush_post($post_id);
  222. }
  223. /**
  224. * Shortcut for url varnish flush
  225. *
  226. * @param string $url
  227. * @return boolean
  228. */
  229. function w3tc_varnish_flush_url($url) {
  230. $w3_cacheflush = w3_instance('W3_CacheFlush');
  231. return $w3_cacheflush->varnish_flush_url($url);
  232. }
  233. /**
  234. * Deletes files.
  235. *
  236. * @param string $mask regular expression matching files to be deleted
  237. * @param bool $http if delete request should be made over http to current site. Default false.
  238. * @return mixed
  239. */
  240. function w3tc_apc_delete_files_based_on_regex($mask, $http = false) {
  241. if (!$http) {
  242. $w3_cacheflush = w3_instance('W3_CacheFlush');
  243. return $w3_cacheflush->apc_delete_files_based_on_regex($mask);
  244. } else {
  245. $url = WP_PLUGIN_URL . '/' . dirname(W3TC_FILE) . '/pub/apc.php';
  246. $path = parse_url($url, PHP_URL_PATH);
  247. $post = array(
  248. 'method' => 'POST',
  249. 'timeout' => 45,
  250. 'redirection' => 5,
  251. 'httpversion' => '1.0',
  252. 'blocking' => true,
  253. 'body' => array( 'nonce' => wp_hash($path), 'command' => 'delete_files', 'regex' => $mask),
  254. );
  255. $result = wp_remote_post($url, $post);
  256. if (is_wp_error($result)) {
  257. return $result;
  258. } elseif ($result['response']['code'] != '200') {
  259. return $result['response']['code'];
  260. }
  261. return true;
  262. }
  263. }
  264. /**
  265. * Reloads files.
  266. * @param string[] $files list of files supports, fullpath, from root, wp-content
  267. * @param bool $http if delete request should be made over http to current site. Default false.
  268. * @return mixed
  269. */
  270. function w3tc_apc_reload_files($files, $http = false) {
  271. if (!$http) {
  272. $w3_cacheflush = w3_instance('W3_CacheFlush');
  273. return $w3_cacheflush->apc_reload_files($files);
  274. } else {
  275. $url = WP_PLUGIN_URL . '/' . dirname(W3TC_FILE) . '/pub/apc.php';
  276. $path = parse_url($url, PHP_URL_PATH);
  277. $post = array(
  278. 'method' => 'POST',
  279. 'timeout' => 45,
  280. 'redirection' => 5,
  281. 'httpversion' => '1.0',
  282. 'blocking' => true,
  283. 'body' => array( 'nonce' => wp_hash($path), 'command' => 'reload_files', 'files' => $files),
  284. );
  285. $result = wp_remote_post($url, $post);
  286. if (is_wp_error($result)) {
  287. return $result;
  288. } elseif ($result['response']['code'] != '200') {
  289. return $result['response']['code'];
  290. }
  291. return true;
  292. }
  293. }
  294. /**
  295. * Use w3tc_get_themes() to get a list themenames to use with user agent groups
  296. * @param $group_name
  297. * @param string $theme the themename default is default theme. For childtheme it should be parentthemename/childthemename
  298. * @param string $redirect
  299. * @param array $agents Remember to escape special characters like spaces, dots or dashes with a backslash. Regular expressions are also supported.
  300. * @param bool $enabled
  301. */
  302. function w3tc_save_user_agent_group($group_name, $theme = 'default', $redirect = '', $agents = array(), $enabled = false) {
  303. /**
  304. * @var $w3_mobile W3_Mobile
  305. */
  306. $w3_mobile = w3_instance('W3_Mobile');
  307. $w3_mobile->save_group($group_name, $theme, $redirect, $agents, $enabled);
  308. }
  309. /**
  310. * @param $group
  311. */
  312. function w3tc_delete_user_agent_group($group) {
  313. /**
  314. * @var $w3_mobile W3_Mobile
  315. */
  316. $w3_mobile = w3_instance('W3_Mobile');
  317. $w3_mobile->delete_group($group);
  318. }
  319. /**
  320. * @param $group
  321. * @return mixed
  322. */
  323. function w3tc_get_user_agent_group($group) {
  324. /**
  325. * @var $w3_mobile W3_Mobile
  326. */
  327. $w3_mobile = w3_instance('W3_Mobile');
  328. return $w3_mobile->get_group_values($group);
  329. }
  330. /**
  331. * Use w3tc_get_themes() to get a list themenames to use with referrer groups
  332. * @param $group_name
  333. * @param string $theme the themename default is default theme. For childtheme it should be parentthemename/childthemename
  334. * @param string $redirect
  335. * @param array $referrers Remember to escape special characters like spaces, dots or dashes with a backslash. Regular expressions are also supported.
  336. * @param bool $enabled
  337. */
  338. function w3tc_save_referrer_group($group_name, $theme = 'default', $redirect = '', $referrers = array(), $enabled = false) {
  339. /**
  340. * @var $w3_referrer W3_Referrer
  341. */
  342. $w3_referrer = w3_instance('W3_Referrer');
  343. $w3_referrer->save_group($group_name, $theme, $redirect, $referrers, $enabled);
  344. }
  345. /**
  346. * @param $group
  347. */
  348. function w3tc_delete_referrer_group($group) {
  349. /**
  350. * @var $w3_referrer W3_Referrer
  351. */
  352. $w3_referrer = w3_instance('W3_Referrer');
  353. $w3_referrer->delete_group($group);
  354. }
  355. /**
  356. * @param $group
  357. * @return mixed
  358. */
  359. function w3tc_get_referrer_group($group) {
  360. /**
  361. * @var $w3_mobile W3_Referrer
  362. */
  363. $w3_referrer = w3_instance('W3_Referrer');
  364. return $w3_referrer->get_group_values($group);
  365. }