PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.0.19
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.0.19
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
searchpro / inc / cache / class-berqCloudflareAPIHandler.php

class-berqCloudflareAPIHandler.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 4.0.19, at inc/cache/class-berqCloudflareAPIHandler.php

213 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH'))
3 exit;
4
5 class BerqCloudflareAPIHandler
6 {
7 private $api_url = 'https://api.cloudflare.com/client/v4/';
8 private $api_email;
9 private $api_key;
10 private $zone_id;
11
12 public function __construct($api_email, $api_key, $zone_id)
13 {
14 $this->api_email = $api_email;
15 $this->api_key = $api_key;
16 $this->zone_id = $zone_id;
17 }
18
19 public function verify_credentials()
20 {
21 $endpoint = "zones/{$this->zone_id}";
22 $response = $this->make_request($endpoint, 'GET');
23
24 return isset($response['success']) && $response['success'];
25 }
26
27 public function add_rule()
28 {
29 $rules_rep = $this->get_cache_ruleset();
30 $rules = !empty($rules_rep['result']['rules']) ? $rules_rep['result']['rules'] : false;
31 $rule_found = false;
32
33 if (!empty($rules)) {
34 foreach ($rules as $rule) {
35 if (!empty($rule['action_parameters']['description']) && $rule['action_parameters']['description'] == 'BerqWP cache rules' && $rule['action_parameters']['enabled'] == true) {
36 $rule_found = true;
37 break;
38 }
39 }
40 }
41
42 if (!$rule_found) {
43 $this->delete_rule_by_description('BerqWP cache rules');
44 $this->update_cache_rules();
45 }
46 }
47
48 public function get_cache_ruleset()
49 {
50 $endpoint = "zones/{$this->zone_id}/rulesets/phases/http_request_cache_settings/entrypoint";
51 return $this->make_request($endpoint, 'GET');
52 }
53
54 public function delete_rule_by_description($description) {
55 // Step 1: Get the ruleset from Cloudflare
56 $response = $this->get_cache_ruleset();
57
58 // Handle API errors
59 if (!isset($response['success']) || !$response['success']) {
60 return [
61 'success' => false,
62 'message' => 'Failed to fetch ruleset: ' . ($response['errors'][0]['message'] ?? 'Unknown error'),
63 ];
64 }
65
66 // Extract the ruleset and its rules
67 $ruleset = $response['result'] ?? [];
68 $rules = $ruleset['rules'] ?? [];
69
70 // Step 2: Find and remove the rule by description
71 $updatedRules = [];
72 $found = false;
73
74 foreach ($rules as $rule) {
75 if (isset($rule['description']) && $rule['description'] === $description) {
76 $found = true;
77 } else {
78 $updatedRules[] = $rule; // Keep rules that don't match
79 }
80 }
81
82 if (!$found) {
83 return [
84 'success' => false,
85 'message' => "No rule found with description: {$description}",
86 ];
87 }
88
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
94 $update_endpoint = "zones/{$this->zone_id}/rulesets/{$ruleset['id']}";
95 $update_response = $this->make_request($update_endpoint, 'PUT', $ruleset);
96
97 // Step 5: Return result
98 if (isset($update_response['success']) && $update_response['success']) {
99 return [
100 'success' => true,
101 'message' => 'Rule deleted successfully.',
102 ];
103 } else {
104 return [
105 'success' => false,
106 'message' => 'Failed to update ruleset: ' .
107 implode(' ', array_column($update_response['errors'] ?? [], 'message')),
108 ];
109 }
110 }
111
112 public function purge_all_cache() {
113 $endpoint = "zones/{$this->zone_id}/purge_cache";
114 $response = $this->make_request($endpoint, 'POST', [
115 'purge_everything' => true
116 ]);
117
118 if ($response['success']) {
119 return [
120 'success' => true,
121 'message' => 'All cache purged successfully.',
122 ];
123 } else {
124 return [
125 'success' => false,
126 'message' => 'Failed to purge cache: ' . implode(' ', array_column($response['errors'], 'message'))
127 ];
128 }
129 }
130
131 // Method to flush a specific URL from cache
132 public function flush_url($url) {
133 $endpoint = "zones/{$this->zone_id}/purge_cache";
134 $response = $this->make_request($endpoint, 'POST', [
135 'files' => [$url]
136 ]);
137
138 if ($response['success']) {
139 return [
140 'success' => true,
141 'message' => "Cache for {$url} purged successfully."
142 ];
143 } else {
144 return [
145 'success' => false,
146 'message' => 'Failed to purge URL: ' . implode(' ', array_column($response['errors'], 'message'))
147 ];
148 }
149 }
150
151 public function update_cache_rules()
152 {
153 $endpoint = "zones/{$this->zone_id}/rulesets/phases/http_request_cache_settings/entrypoint";
154
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 ],
165 ],
166 ];
167
168 $response = $this->make_request($endpoint, 'PUT', $cache_rules);
169
170 if (is_array($response) && isset($response['success']) && $response['success']) {
171 return [
172 'success' => true,
173 'message' => 'Cache rules updated successfully'
174 ];
175 }
176
177 return [
178 'success' => false,
179 'message' => is_array($response) && isset($response['errors']) ? json_encode($response['errors']) : 'Unknown error occurred'
180 ];
181 }
182
183 private function make_request($endpoint, $method = 'GET', $data = null)
184 {
185 $url = $this->api_url . $endpoint;
186
187 $args = [
188 'method' => $method,
189 'headers' => [
190 'Content-Type' => 'application/json',
191 'X-Auth-Email' => $this->api_email,
192 'X-Auth-Key' => $this->api_key
193 ],
194 'timeout' => 30
195 ];
196
197 if ($data !== null) {
198 $args['body'] = json_encode($data);
199 }
200
201 $response = wp_remote_request($url, $args);
202
203 if (is_wp_error($response)) {
204 return [
205 'success' => false,
206 'errors' => [['message' => $response->get_error_message()]]
207 ];
208 }
209
210 $body = wp_remote_retrieve_body($response);
211 return json_decode($body, true);
212 }
213 }