PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / trunk
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript vtrunk
4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 1.9.4 1.9.5 1.9.6 All 170 releases
← All changes | inc/cache/class-berqCloudflareAPIHandler.php +46 -31 4.1.13trunk View file →
@@ -31,9 +31,11 @@
31 31 $rule_found = false;
32 32
33 33 if (!empty($rules)) {
34 34 foreach ($rules as $rule) {
35 - if (!empty($rule['action_parameters']['description']) && $rule['action_parameters']['description'] == 'BerqWP cache rules' && $rule['action_parameters']['enabled'] == true) {
35 + // Cloudflare stores description and enabled on the rule object,
36 + // not inside action_parameters.
37 + if (isset($rule['description']) && $rule['description'] === 'BerqWP cache rules') {
36 38 $rule_found = true;
37 39 break;
38 40 }
39 41 }
@@ -39,9 +41,8 @@
39 41 }
40 42 }
41 43
42 44 if (!$rule_found) {
43 - $this->delete_rule_by_description('BerqWP cache rules');
44 45 $this->update_cache_rules();
45 46 }
46 47 }
47 48
@@ -85,15 +86,12 @@
85 86 'message' => "No rule found with description: {$description}",
86 87 ];
87 88 }
88 89
89 - // Step 3: Prepare the updated ruleset payload
90 - $ruleset['rules'] = $updatedRules;
91 - unset($ruleset['last_updated']);
92 -
93 - // Step 4: Send the update request
90 + // Step 3: Send only the rules array back to Cloudflare.
91 + // Sending read-only fields (id, version, kind, phase) is non-idiomatic and fragile.
94 92 $update_endpoint = "zones/{$this->zone_id}/rulesets/{$ruleset['id']}";
95 - $update_response = $this->make_request($update_endpoint, 'PUT', $ruleset);
93 + $update_response = $this->make_request($update_endpoint, 'PUT', ['rules' => $updatedRules]);
96 94
97 95 // Step 5: Return result
98 96 if (isset($update_response['success']) && $update_response['success']) {
99 97 return [
@@ -114,19 +112,23 @@
114 112 $response = $this->make_request($endpoint, 'POST', [
115 113 'purge_everything' => true
116 114 ]);
117 115
118 - if ($response['success']) {
116 + if (is_array($response) && isset($response['success']) && $response['success']) {
119 117 return [
120 118 'success' => true,
121 119 'message' => 'All cache purged successfully.',
122 120 ];
123 - } else {
124 - return [
125 - 'success' => false,
126 - 'message' => 'Failed to purge cache: ' . implode(' ', array_column($response['errors'], 'message'))
127 - ];
128 121 }
122 +
123 + $errors = is_array($response) && isset($response['errors'])
124 + ? implode(' ', array_column($response['errors'], 'message'))
125 + : 'Unknown error';
126 +
127 + return [
128 + 'success' => false,
129 + 'message' => 'Failed to purge cache: ' . $errors,
130 + ];
129 131 }
130 132
131 133 // Method to flush a specific URL from cache
132 134 public function flush_url($url) {
@@ -134,19 +136,23 @@
134 136 $response = $this->make_request($endpoint, 'POST', [
135 137 'files' => [$url]
136 138 ]);
137 139
138 - if ($response['success']) {
140 + if (is_array($response) && isset($response['success']) && $response['success']) {
139 141 return [
140 142 'success' => true,
141 143 'message' => "Cache for {$url} purged successfully."
142 144 ];
143 - } else {
144 - return [
145 - 'success' => false,
146 - 'message' => 'Failed to purge URL: ' . implode(' ', array_column($response['errors'], 'message'))
147 - ];
148 145 }
146 +
147 + $errors = is_array($response) && isset($response['errors'])
148 + ? implode(' ', array_column($response['errors'], 'message'))
149 + : 'Unknown error';
150 +
151 + return [
152 + 'success' => false,
153 + 'message' => 'Failed to purge URL: ' . $errors,
154 + ];
149 155 }
150 156
151 157 public function update_cache_rules()
152 158 {
@@ -151,22 +157,31 @@
151 157 public function update_cache_rules()
152 158 {
153 159 $endpoint = "zones/{$this->zone_id}/rulesets/phases/http_request_cache_settings/entrypoint";
154 160
155 - $cache_rules = [
156 - 'rules' => [
157 - [
158 - 'expression' => 'not (http.cookie contains "wordpress_logged_in_") and not (http.request.uri.path contains ".xml" or http.request.uri.path contains ".txt" or http.request.uri.path contains ".gz" or http.request.uri.path contains "sitemap")',
159 - 'action' => 'set_cache_settings',
160 - 'action_parameters' => [
161 - 'cache' => true,
162 - ],
163 - 'description' => 'BerqWP cache rules',
164 - ],
161 + // GET existing rules so we don't wipe other CF cache rules in the zone.
162 + $existing = $this->get_cache_ruleset();
163 + $existing_rules = !empty($existing['result']['rules']) ? $existing['result']['rules'] : [];
164 +
165 + // Remove any old BerqWP rule, keep everything else.
166 + $merged = [];
167 + foreach ($existing_rules as $rule) {
168 + if (!isset($rule['description']) || $rule['description'] !== 'BerqWP cache rules') {
169 + $merged[] = $rule;
170 + }
171 + }
172 +
173 + // Append the canonical BerqWP cache rule.
174 + $merged[] = [
175 + 'expression' => 'not (http.request.method ne "GET" or http.cookie contains "wordpress_logged_in_" or http.request.uri.path contains "/wp-admin" or http.request.uri.path contains ".xml" or http.request.uri.path contains ".txt" or http.request.uri.path contains ".gz" or http.request.uri.path contains "sitemap" or http.request.uri.query ne "")',
176 + 'action' => 'set_cache_settings',
177 + 'action_parameters' => [
178 + 'cache' => true,
165 179 ],
180 + 'description' => 'BerqWP cache rules',
166 181 ];
167 182
168 - $response = $this->make_request($endpoint, 'PUT', $cache_rules);
183 + $response = $this->make_request($endpoint, 'PUT', ['rules' => $merged]);
169 184
170 185 if (is_array($response) && isset($response['success']) && $response['success']) {
171 186 return [
172 187 'success' => true,