|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +if (PHP_SAPI !== 'cli') { |
| 6 | + fwrite(STDERR, "Error: wp-cloudflare-page-cache-purge.php can only be run from CLI.\n"); |
| 7 | + exit(1); |
| 8 | +} |
| 9 | + |
| 10 | +$demyxPath = getenv('DEMYX'); |
| 11 | + |
| 12 | +if (!is_string($demyxPath) || trim($demyxPath) === '') { |
| 13 | + fwrite(STDERR, "Error: DEMYX environment variable is not set. It must point to your WordPress root path.\n"); |
| 14 | + exit(2); |
| 15 | +} |
| 16 | + |
| 17 | +$demyxPath = rtrim(trim($demyxPath), '/'); |
| 18 | +$wpLoadPath = $demyxPath . '/wp-load.php'; |
| 19 | +$wpConfigPath = $demyxPath . '/wp-config.php'; |
| 20 | + |
| 21 | +if (!is_file($wpLoadPath)) { |
| 22 | + fwrite(STDERR, "Error: WordPress bootstrap file not found at {$wpLoadPath}.\n"); |
| 23 | + exit(2); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * advanced-cache.php runs early and expects HTTP server vars. |
| 28 | + * In CLI they are usually absent, so provide safe defaults. |
| 29 | + */ |
| 30 | +if (!isset($_SERVER['REQUEST_METHOD'])) { |
| 31 | + $_SERVER['REQUEST_METHOD'] = 'GET'; |
| 32 | +} |
| 33 | + |
| 34 | +if (!isset($_SERVER['REQUEST_URI'])) { |
| 35 | + $_SERVER['REQUEST_URI'] = '/'; |
| 36 | +} |
| 37 | + |
| 38 | +if (!isset($_SERVER['HTTP_HOST']) || $_SERVER['HTTP_HOST'] === '') { |
| 39 | + $host = ''; |
| 40 | + |
| 41 | + if (is_file($wpConfigPath) && is_readable($wpConfigPath)) { |
| 42 | + $wpConfigContent = file_get_contents($wpConfigPath); |
| 43 | + |
| 44 | + if (is_string($wpConfigContent)) { |
| 45 | + if (preg_match("/define\\(\\s*['\\\"]WP_HOME['\\\"]\\s*,\\s*['\\\"]https?:\\/\\/([^'\\\"\\/]+)['\\\"]/i", $wpConfigContent, $matches) === 1) { |
| 46 | + $host = $matches[1]; |
| 47 | + } elseif (preg_match("/define\\(\\s*['\\\"]WP_SITEURL['\\\"]\\s*,\\s*['\\\"]https?:\\/\\/([^'\\\"\\/]+)['\\\"]/i", $wpConfigContent, $matches) === 1) { |
| 48 | + $host = $matches[1]; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if ($host === '') { |
| 54 | + $host = 'localhost'; |
| 55 | + } |
| 56 | + |
| 57 | + $_SERVER['HTTP_HOST'] = $host; |
| 58 | +} |
| 59 | + |
| 60 | +if (!isset($_SERVER['SERVER_NAME']) || $_SERVER['SERVER_NAME'] === '') { |
| 61 | + $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST']; |
| 62 | +} |
| 63 | + |
| 64 | +if (!isset($_SERVER['HTTPS'])) { |
| 65 | + $_SERVER['HTTPS'] = 'on'; |
| 66 | +} |
| 67 | + |
| 68 | +require_once $wpLoadPath; |
| 69 | + |
| 70 | +global $sw_cloudflare_pagecache; |
| 71 | + |
| 72 | +if (!isset($sw_cloudflare_pagecache) || !($sw_cloudflare_pagecache instanceof SW_CLOUDFLARE_PAGECACHE)) { |
| 73 | + fwrite(STDERR, "Error: Super Page Cache plugin is not loaded or active in this WordPress install.\n"); |
| 74 | + exit(3); |
| 75 | +} |
| 76 | + |
| 77 | +$purgeResult = $sw_cloudflare_pagecache->get_cache_controller()->purge_all(false, false); |
| 78 | + |
| 79 | +if ($purgeResult === true) { |
| 80 | + fwrite(STDOUT, "Cache purged successfully.\n"); |
| 81 | + exit(0); |
| 82 | +} |
| 83 | + |
| 84 | +fwrite(STDERR, "Error: Cache purge failed. Check Super Page Cache logs for details.\n"); |
| 85 | +exit(4); |
0 commit comments