PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / trunk
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score vtrunk
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / classes / Extensions / Cloudflare / API.php

API.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score trunk, at includes/classes/Extensions/Cloudflare/API.php

178 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cloudflare API functionalities
4 *
5 * @package PoweredCache\Extensions\Cloudflare
6 */
7
8 namespace PoweredCache\Extensions\Cloudflare;
9
10 /**
11 * Class API
12 */
13 class API {
14
15 /**
16 * CF req. endpoint
17 *
18 * @var string
19 */
20 private $end_point = 'https://api.cloudflare.com/client/v4';
21
22 /**
23 * CF API Key
24 *
25 * @var string $api_key
26 */
27 private $api_key;
28
29 /**
30 * CF Email
31 *
32 * @var string $email
33 */
34 private $email;
35
36 /**
37 * Bearer Token
38 *
39 * @link https://api.cloudflare.com/#getting-started-requests
40 * @var string $api_token
41 */
42 private $api_token;
43
44 /**
45 * API constructor.
46 *
47 * @param string $email CF Email
48 * @param string $api_key CF API Key
49 * @param string $api_token Bearer Token
50 */
51 public function __construct( $email = '', $api_key = '', $api_token = '' ) {
52 $this->email = $email;
53 $this->api_key = $api_key;
54 $this->api_token = $api_token;
55 }
56
57 /**
58 * GET CF zones
59 *
60 * @return mixed|string
61 */
62 public function get_zones() {
63 $endpoint = $this->end_point . '/zones';
64
65 $result = $this->remote_request(
66 $endpoint,
67 'GET',
68 array(
69 'page' => 1,
70 'per_page' => 1000,
71 )
72 );
73
74 return $result;
75 }
76
77 /**
78 * Purge given zone
79 *
80 * @param string $zone_id Zone ID
81 * @param string $args deletion args
82 *
83 * @return mixed|string
84 */
85 public function purge( $zone_id, $args = 'all' ) {
86
87 if ( 'all' === $args ) {
88 $data['purge_everything'] = true;
89 }
90
91 if ( is_array( $args ) && array_key_exists( 'files', $args ) ) {
92 $data['files'] = (array) $args['files'];
93 }
94
95 if ( is_array( $args ) && array_key_exists( 'tags', $args ) ) {
96 $data['tags'] = (array) $args['tags'];
97 }
98
99 $endpoint = $this->end_point . '/zones/' . $zone_id . '/purge_cache';
100
101 $result = $this->remote_request( $endpoint, 'POST', $data );
102
103 if ( ! empty( $result['success'] ) ) {
104 return boolval( $result['success'] );
105 }
106
107 return false;
108 }
109
110 /**
111 * Make remote req.
112 *
113 * @param string $url URL
114 * @param string $type req type
115 * @param array $data data
116 *
117 * @return mixed|array
118 */
119 private function remote_request( $url, $type = 'GET', $data = array() ) {
120 $args = array(
121 'method' => $type,
122 'timeout' => 5,
123 'headers' => array(
124 'Content-Type' => 'application/json',
125 ),
126 'sslverify' => false,
127 );
128
129 if ( ! empty( $this->api_token ) ) {
130 $args['headers']['Authorization'] = "Bearer {$this->api_token}";
131 } else {
132 $args['headers']['X-Auth-Email'] = $this->email;
133 $args['headers']['X-Auth-Key'] = $this->api_key;
134 }
135
136 if ( ! empty( $data ) ) {
137 $args['body'] = wp_json_encode( $data );
138 }
139
140 $response = wp_remote_request( $url, $args );
141 if ( is_wp_error( $response ) ) {
142 \PoweredCache\Utils\log( sprintf( 'Cloudflare API Error: %s', $response->get_error_message() ) );
143
144 return [];
145 }
146
147 $response_body = wp_remote_retrieve_body( $response );
148 \PoweredCache\Utils\log( sprintf( 'Cloudflare API Response: %s', print_r( $response_body, true ) ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
149
150 $decoded_response = json_decode( $response_body, true );
151 if ( ! is_array( $decoded_response ) ) {
152 return [];
153 }
154
155 return $decoded_response;
156 }
157
158 /**
159 * Return an instance of the current class, create one if it doesn't exist
160 *
161 * @param string $email CF Email
162 * @param string $api_key CF API Key
163 * @param string $api_token CF API Token
164 *
165 * @return API
166 */
167 public static function factory( $email, $api_key, $api_token ) {
168 static $instance = false;
169
170 if ( ! $instance ) {
171 $instance = new self( $email, $api_key, $api_token );
172 }
173
174 return $instance;
175 }
176
177 }
178