PluginProbe
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN / 1.0.5
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN v1.0.5
1.3.3 1.3.2 1.3.1 1.3.0 1.2.4 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 All 29 releases
xspeed / includes / modules / Cloudflare / CloudflareModule.php

CloudflareModule.php in xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN 1.0.5, at includes/modules/Cloudflare/CloudflareModule.php

218 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cloudflare module — connect a CF zone for purge + dev-mode toggles.
4 *
5 * Free tier (this module): API token / global key auth, zone
6 * verification, manual purge, auto purge on xSpeed's own purge, dev
7 * mode toggle.
8 *
9 * Pro tier (xspeed-pro): APO toggle, edge cache rules, edge cache TTL.
10 * Per FEATURES.md "Cloudflare Integration" §8-10.
11 *
12 * @package XSpeed
13 */
14
15 declare(strict_types=1);
16
17 namespace XSpeed\Modules\Cloudflare;
18
19 defined( 'ABSPATH' ) || exit;
20
21 use XSpeed\Cloudflare;
22 use XSpeed\Module;
23
24 final class CloudflareModule extends Module {
25
26 public const SLUG = 'cloudflare';
27 public const TIER = self::TIER_FREE;
28 public const VERSION = '1.0.0';
29
30 public function ui_metadata(): array {
31 return array(
32 'label' => 'Cloudflare',
33 'icon' => 'Cloud',
34 'description' => 'Connect a Cloudflare zone for automatic edge purging when xSpeed clears its cache, plus a dev-mode toggle.',
35 'custom_panel' => 'CloudflarePanel',
36 );
37 }
38
39 public function settings_schema(): array {
40 return array(
41 'enabled' => array(
42 'type' => 'bool',
43 'default' => false,
44 'label' => 'Enable Cloudflare integration',
45 'description' => 'Use the credentials below to verify your zone and run purges.',
46 ),
47 'auth_method' => array(
48 'type' => 'enum',
49 'default' => 'token',
50 'options' => array( 'token', 'key' ),
51 'option_labels' => array(
52 'token' => 'API Token',
53 'key' => 'Global API Key',
54 ),
55 'label' => 'Authentication',
56 'description' => 'API Tokens (scoped, recommended) or the legacy Global API Key with your account email.',
57 ),
58 'api_token' => array(
59 'type' => 'string',
60 'default' => '',
61 'label' => 'API Token',
62 'description' => 'Create a token at dash.cloudflare.com → My Profile → API Tokens. Needs "Zone → Cache Purge" + "Zone Settings" permissions.',
63 ),
64 'email' => array(
65 'type' => 'string',
66 'default' => '',
67 'label' => 'Account Email',
68 'description' => 'Only used when Authentication is set to Global API Key.',
69 ),
70 'api_key' => array(
71 'type' => 'string',
72 'default' => '',
73 'label' => 'Global API Key',
74 'description' => 'Found at dash.cloudflare.com → My Profile → API Tokens → Global API Key.',
75 ),
76 'zone_id' => array(
77 'type' => 'string',
78 'default' => '',
79 'label' => 'Zone ID',
80 'description' => 'The 32-character hex Zone ID from your domain overview page.',
81 ),
82 'auto_purge_on_update' => array(
83 'type' => 'bool',
84 'default' => true,
85 'label' => 'Auto-purge Cloudflare on xSpeed purge',
86 'description' => 'When xSpeed clears its own cache (post save, settings change, manual purge), trigger a Cloudflare purge too.',
87 ),
88 );
89 }
90
91 public function rest_routes(): array {
92 $default = parent::rest_routes();
93 return array_merge(
94 $default,
95 array(
96 array(
97 'path' => '/verify',
98 'methods' => 'POST',
99 'callback' => array( $this, 'rest_verify' ),
100 ),
101 array(
102 'path' => '/purge',
103 'methods' => 'POST',
104 'callback' => array( $this, 'rest_purge' ),
105 ),
106 array(
107 'path' => '/dev-mode',
108 'methods' => 'POST',
109 'callback' => array( $this, 'rest_dev_mode' ),
110 ),
111 )
112 );
113 }
114
115 public function conflicts(): array {
116 return array(
117 array(
118 'plugin' => 'cloudflare/cloudflare.php',
119 'feature' => 'cloudflare.purge',
120 'strategy' => \XSpeed\Conflict_Registry::STRATEGY_WARN,
121 'reason' => 'The official Cloudflare plugin also auto-purges; keep auto-purge enabled in only one to avoid double API calls.',
122 ),
123 );
124 }
125
126 public function boot(): void {
127 $opts = $this->get_settings();
128 if ( empty( $opts['enabled'] ) ) {
129 return;
130 }
131 if ( ! empty( $opts['auto_purge_on_update'] ) ) {
132 // xSpeed fires this action whenever it purges its own
133 // cache (see Cache::purge_all). Listening here keeps
134 // CF in sync without any new wiring elsewhere.
135 add_action( 'xspeed_after_purge_all', array( $this, 'on_xspeed_purge' ), 10, 0 );
136 }
137 }
138
139 public function on_xspeed_purge(): void {
140 $opts = $this->get_settings();
141 if ( empty( $opts['enabled'] ) || empty( $opts['zone_id'] ) ) {
142 return;
143 }
144 $result = Cloudflare::purge_all( $opts );
145 if ( ! $result['ok'] && class_exists( '\\XSpeed\\Activity_Log' ) ) {
146 \XSpeed\Activity_Log::record(
147 'cloudflare_purge_failed',
148 'Cloudflare auto-purge failed: ' . ( $result['body']['message'] ?? 'unknown error' ),
149 \XSpeed\Activity_Log::WARN
150 );
151 }
152 }
153
154 public function rest_verify( \WP_REST_Request $request ) {
155 return rest_ensure_response( Cloudflare::verify( $this->get_settings() ) );
156 }
157
158 public function rest_purge( \WP_REST_Request $request ) {
159 $params = $request->get_json_params();
160 if ( ! is_array( $params ) ) {
161 $params = array();
162 }
163 $opts = $this->get_settings();
164 if ( isset( $params['urls'] ) && is_array( $params['urls'] ) && ! empty( $params['urls'] ) ) {
165 return rest_ensure_response( Cloudflare::purge_urls( $opts, $params['urls'] ) );
166 }
167 return rest_ensure_response( Cloudflare::purge_all( $opts ) );
168 }
169
170 public function rest_dev_mode( \WP_REST_Request $request ) {
171 $params = $request->get_json_params();
172 $on = ! empty( $params['on'] );
173 return rest_ensure_response( Cloudflare::set_dev_mode( $this->get_settings(), $on ) );
174 }
175
176 public function cli_commands(): array {
177 return array(
178 array(
179 'name' => 'xspeed cf',
180 'callback' => array( $this, 'cli_handler' ),
181 'shortdesc' => 'Cloudflare verify / purge / dev-mode helpers.',
182 'synopsis' => array(
183 array(
184 'type' => 'positional',
185 'name' => 'action',
186 'options' => array( 'verify', 'purge', 'dev-on', 'dev-off' ),
187 'optional' => false,
188 ),
189 ),
190 ),
191 );
192 }
193
194 public function cli_handler( array $args, array $assoc ): void {
195 $opts = $this->get_settings();
196 $action = $args[0] ?? 'verify';
197 switch ( $action ) {
198 case 'verify':
199 $res = Cloudflare::verify( $opts );
200 break;
201 case 'purge':
202 $res = Cloudflare::purge_all( $opts );
203 break;
204 case 'dev-on':
205 $res = Cloudflare::set_dev_mode( $opts, true );
206 break;
207 case 'dev-off':
208 $res = Cloudflare::set_dev_mode( $opts, false );
209 break;
210 default:
211 \WP_CLI::error( "Unknown action: $action" );
212 return;
213 }
214 \WP_CLI::log( 'HTTP ' . $res['status'] . '' . ( $res['ok'] ? 'ok' : 'failed' ) );
215 \WP_CLI::log( wp_json_encode( $res['body'] ) );
216 }
217 }
218