PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 2.0.1
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v2.0.1
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 2.0.1, at includes/classes/Extensions/Cloudflare/API.php

148 lines 2.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 * API constructor.
38 *
39 * @param string $email CF Email
40 * @param string $api_key CF API Key
41 */
42 public function __construct( $email, $api_key ) {
43 $this->email = $email;
44 $this->api_key = $api_key;
45 }
46
47 /**
48 * GET CF zones
49 *
50 * @return mixed|string
51 */
52 public function get_zones() {
53 $endpoint = $this->end_point . '/zones';
54
55 $result = $this->remote_request(
56 $endpoint,
57 'GET',
58 array(
59 'page' => 1,
60 'per_page' => 1000,
61 )
62 );
63
64 return $result;
65 }
66
67 /**
68 * Purge given zone
69 *
70 * @param string $zone_id Zone ID
71 * @param string $args deletion args
72 *
73 * @return mixed|string
74 */
75 public function purge( $zone_id, $args = 'all' ) {
76
77 if ( 'all' === $args ) {
78 $data['purge_everything'] = true;
79 }
80
81 if ( is_array( $args ) && array_key_exists( 'files', $args ) ) {
82 $data['files'] = $args['files'];
83 }
84 if ( is_array( $args ) && array_key_exists( 'tags', $args ) ) {
85 $data['files'] = $args['tags'];
86 }
87
88 $endpoint = $this->end_point . '/zones/' . $zone_id . '/purge_cache';
89
90 $result = $this->remote_request( $endpoint, 'DELETE' );
91
92 return $result;
93 }
94
95 /**
96 * Make remote req.
97 *
98 * @param string $url URL
99 * @param string $type req type
100 * @param array $data data
101 *
102 * @return mixed|string
103 */
104 private function remote_request( $url, $type = 'GET', $data = array() ) {
105 $args = array(
106 'method' => $type,
107 'timeout' => 5,
108 'headers' => array(
109 'X-Auth-Email' => $this->email,
110 'X-Auth-Key' => $this->api_key,
111 'Content-Type' => 'application/json',
112 ),
113 'sslverify' => false,
114 );
115
116 if ( ! empty( $data ) ) {
117 $args['body'] = wp_json_encode( $data );
118 }
119
120 $response = wp_remote_request( $url, $args );
121
122 if ( is_wp_error( $response ) ) {
123 return $response->get_error_message();
124 } else {
125 return json_decode( wp_remote_retrieve_body( $response ) );
126 }
127 }
128
129 /**
130 * Return an instance of the current class, create one if it doesn't exist
131 *
132 * @param string $email CF Email
133 * @param string $api_key CF API Key
134 *
135 * @return API
136 */
137 public static function factory( $email, $api_key ) {
138 static $instance = false;
139
140 if ( ! $instance ) {
141 $instance = new self( $email, $api_key );
142 }
143
144 return $instance;
145 }
146
147 }
148