PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/sokeara/ayana-journeys
PHP | 283 lines | 218 code | 30 blank | 35 comment | 25 complexity | 69717ec280a0c9e386e6ce820cb80b2a MD5 | raw file
  1. <?php
  2. /**
  3. * The W3 Total Cache plugin
  4. *
  5. * @package wp-cli
  6. * @subpackage commands/third-party
  7. * @maintainer Anthony Somerset
  8. */
  9. class W3TotalCache_Command extends WP_CLI_Command {
  10. /**
  11. * Clear something from the cache
  12. *
  13. * @param array $args
  14. * @param array $vars
  15. */
  16. function flush($args = array(), $vars = array()) {
  17. $args = array_unique($args);
  18. do {
  19. $cache_type = array_shift($args);
  20. switch($cache_type) {
  21. case 'db':
  22. case 'database':
  23. try {
  24. $w3_db = w3_instance('W3_CacheFlush');
  25. $w3_db->dbcache_flush();
  26. }
  27. catch (Exception $e) {
  28. WP_CLI::error(__('Flushing the DB cache failed.', 'w3-total-cache'));
  29. }
  30. WP_CLI::success(__('The DB cache is flushed successfully.', 'w3-total-cache'));
  31. break;
  32. case 'minify':
  33. try {
  34. $w3_minify = w3_instance('W3_CacheFlush');
  35. $w3_minify->minifycache_flush();
  36. }
  37. catch (Exception $e) {
  38. WP_CLI::error(__('Flushing the minify cache failed.', 'w3-total-cache'));
  39. }
  40. WP_CLI::success(__('The minify cache is flushed successfully.', 'w3-total-cache'));
  41. break;
  42. case 'object':
  43. try {
  44. $w3_objectcache = w3_instance('W3_CacheFlush');
  45. $w3_objectcache->objectcache_flush();
  46. }
  47. catch (Exception $e) {
  48. WP_CLI::error(__('Flushing the object cache failed.', 'w3-total-cache'));
  49. }
  50. WP_CLI::success(__('The object cache is flushed successfully.', 'w3-total-cache'));
  51. break;
  52. case 'post':
  53. default:
  54. if (isset($vars['post_id'])) {
  55. if (is_numeric($vars['post_id'])) {
  56. try {
  57. $w3_cacheflush = w3_instance('W3_CacheFlush');
  58. $w3_cacheflush->pgcache_flush_post($vars['post_id']);
  59. $w3_cacheflush->varnish_flush_post($vars['post_id']);
  60. }
  61. catch (Exception $e) {
  62. WP_CLI::error(__('Flushing the page from cache failed.', 'w3-total-cache'));
  63. }
  64. WP_CLI::success(__('The page is flushed from cache successfully.', 'w3-total-cache'));
  65. } else {
  66. WP_CLI::error(__('This is not a valid post id.', 'w3-total-cache'));
  67. }
  68. w3tc_pgcache_flush_post($vars['post_id']);
  69. }
  70. elseif (isset($vars['permalink'])) {
  71. $id = url_to_postid($vars['permalink']);
  72. if (is_numeric($id)) {
  73. try {
  74. $w3_cacheflush = w3_instance('W3_CacheFlush');
  75. $w3_cacheflush->pgcache_flush_post($id);
  76. $w3_cacheflush->varnish_flush_post($id);
  77. }
  78. catch (Exception $e) {
  79. WP_CLI::error(__('Flushing the page from cache failed.', 'w3-total-cache'));
  80. }
  81. WP_CLI::success(__('The page is flushed from cache successfully.', 'w3-total-cache'));
  82. } else {
  83. WP_CLI::error(__('There is no post with this permalink.', 'w3-total-cache'));
  84. }
  85. } else {
  86. if (isset($flushed_page_cache) && $flushed_page_cache)
  87. break;
  88. $flushed_page_cache = true;
  89. try {
  90. $w3_cacheflush = w3_instance('W3_CacheFlush');
  91. $w3_cacheflush->pgcache_flush();
  92. $w3_cacheflush->varnish_flush();
  93. }
  94. catch (Exception $e) {
  95. WP_CLI::error(__('Flushing the page cache failed.', 'w3-total-cache'));
  96. }
  97. WP_CLI::success(__('The page cache is flushed successfully.', 'w3-total-cache'));
  98. }
  99. }
  100. } while (!empty($args));
  101. }
  102. /**
  103. * Update query string function
  104. */
  105. function querystring() {
  106. try {
  107. $w3_querystring = w3_instance('W3_CacheFlush');
  108. $w3_querystring->browsercache_flush();
  109. }
  110. catch (Exception $e) {
  111. WP_CLI::error(__('updating the query string failed. with error %s', 'w3-total-cache'), $e);
  112. }
  113. WP_CLI::success(__('The query string was updated successfully.', 'w3-total-cache'));
  114. }
  115. /**
  116. * Purge URL's from cdn and varnish if enabled
  117. * @param array $args
  118. */
  119. function cdn_purge($args = array()) {
  120. $purgeitems = array();
  121. foreach ($args as $file) {
  122. $cdncommon = w3_instance('W3_Plugin_CdnCommon');
  123. $local_path = WP_ROOT . $file;
  124. $remote_path = $file;
  125. $purgeitems[] = $cdncommon->build_file_descriptor($local_path, $remote_path);
  126. }
  127. try {
  128. $w3_cdn_purge = w3_instance('W3_CacheFlush');
  129. $w3_cdn_purge->cdn_purge_files($purgeitems);
  130. }
  131. catch (Exception $e) {
  132. WP_CLI::error(__('Files did not successfully purge with error %s', 'w3-total-cache'), $e);
  133. }
  134. WP_CLI::success(__('Files purged successfully.', 'w3-total-cache'));
  135. }
  136. /**
  137. * Tell APC to reload PHP files
  138. * @param array $args
  139. */
  140. function apc_reload_files($args = array()) {
  141. try {
  142. $method = array_shift($args);
  143. if (!in_array($method, array('SNS', 'local')))
  144. WP_CLI::error($method . __(' is not supported. Change to SNS or local to reload APC files', 'w3-total-cache'));
  145. if ($method == 'SNS') {
  146. $w3_cache = w3_instance('W3_CacheFlush');
  147. $w3_cache->apc_reload_files($args);
  148. } else {
  149. $url = WP_PLUGIN_URL . '/' . dirname(W3TC_FILE) . '/pub/apc.php';
  150. $path = parse_url($url, PHP_URL_PATH);
  151. $post = array(
  152. 'method' => 'POST',
  153. 'timeout' => 45,
  154. 'redirection' => 5,
  155. 'httpversion' => '1.0',
  156. 'blocking' => true,
  157. 'body' => array( 'nonce' => wp_hash($path), 'command' => 'reload_files', 'files' => $args),
  158. );
  159. $result = wp_remote_post($url, $post);
  160. if (is_wp_error($result)) {
  161. WP_CLI::error(__('Files did not successfully reload with error %s', 'w3-total-cache'), $result);
  162. } elseif ($result['response']['code'] != '200') {
  163. WP_CLI::error(__('Files did not successfully reload with message: ', 'w3-total-cache') . $result['body']);
  164. }
  165. }
  166. }
  167. catch (Exception $e) {
  168. WP_CLI::error(__('Files did not successfully reload with error %s', 'w3-total-cache'), $e);
  169. }
  170. WP_CLI::success(__('Files reloaded successfully.', 'w3-total-cache'));
  171. }
  172. /**
  173. * Tell APC to reload PHP files
  174. * @param array $args
  175. */
  176. function apc_delete_based_on_regex($args = array()) {
  177. try {
  178. $method = array_shift($args);
  179. if (!in_array($method, array('SNS', 'local')))
  180. WP_CLI::error($method . __(' is not supported. Change to SNS or local to delete APC files', 'w3-total-cache'));
  181. if ($method == 'SNS') {
  182. $w3_cache = w3_instance('W3_CacheFlush');
  183. $w3_cache->apc_delete_files_based_on_regex($args[0]);
  184. } else {
  185. $url = WP_PLUGIN_URL . '/' . dirname(W3TC_FILE) . '/pub/apc.php';
  186. $path = parse_url($url, PHP_URL_PATH);
  187. $post = array(
  188. 'method' => 'POST',
  189. 'timeout' => 45,
  190. 'redirection' => 5,
  191. 'httpversion' => '1.0',
  192. 'blocking' => true,
  193. 'body' => array( 'nonce' => wp_hash($path), 'command' => 'delete_files', 'regex' => $args[0]),
  194. );
  195. $result = wp_remote_post($url, $post);
  196. if (is_wp_error($result)) {
  197. WP_CLI::error(__('Files did not successfully delete with error %s', 'w3-total-cache'), $result);
  198. } elseif ($result['response']['code'] != '200') {
  199. WP_CLI::error(__('Files did not successfully delete with message: ', 'w3-total-cache'). $result['body']);
  200. }
  201. }
  202. }
  203. catch (Exception $e) {
  204. WP_CLI::error(__('Files did not successfully delete with error %s', 'w3-total-cache'), $e);
  205. }
  206. WP_CLI::success(__('Files deleted successfully.', 'w3-total-cache'));
  207. }
  208. /**
  209. * triggers PgCache Garbage Cleanup
  210. */
  211. function pgcache_cleanup() {
  212. try {
  213. $pgcache_cleanup = w3_instance('W3_Plugin_PgCacheAdmin');
  214. $pgcache_cleanup->cleanup();
  215. }
  216. catch (Exception $e) {
  217. WP_CLI::error(__('PageCache Garbage cleanup did not start with error %s', 'w3-total-cache'), $e);
  218. }
  219. WP_CLI::success(__('PageCache Garbage cleanup triggered successfully.', 'w3-total-cache'));
  220. }
  221. /**
  222. * Help function for this command
  223. */
  224. public static function help() {
  225. WP_CLI::line( <<<EOB
  226. usage: wp w3-total-cache flush [post|database|minify|object] [--post_id=<post-id>] [--permalink=<post-permalink>]
  227. or : wp w3-total-cache querystring
  228. or : wp w3-total-cache cdn_purge <file> [<file2>]...
  229. or : wp w3-total-cache pgcache_cleanup
  230. flush flushes whole cache or specific items based on provided arguments
  231. querystring update query string for all static files
  232. cdn_purge Purges command line provided files from Varnish and the CDN
  233. pgcache_cleanup Generally triggered from a cronjob, allows for manual Garbage collection of page cache to be triggered
  234. apc_reload_files SNS/local file.php file2.php file3.php Tells apc to compile files
  235. apc_delete_based_on_regex SNS/local expression Tells apc to delete files that match a RegEx mask
  236. Available flush sub-commands:
  237. --post_id=<id> flush a specific post ID
  238. --permalink=<post-permalink> flush a specific permalink
  239. database flush the database cache
  240. object flush the object cache
  241. minify flush the minify cache
  242. EOB
  243. );
  244. }
  245. }
  246. if (method_exists('WP_CLI','add_command')) {
  247. WP_CLI::add_command('w3-total-cache', 'W3TotalCache_Command');
  248. WP_CLI::add_command('total-cache', 'W3TotalCache_Command');
  249. } else {
  250. // backward compatibility
  251. WP_CLI::addCommand('w3-total-cache', 'W3TotalCache_Command');
  252. WP_CLI::addCommand('total-cache', 'W3TotalCache_Command');
  253. }