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
← All changes | includes/classes/Extensions/Cloudflare/API.php +50 -20 2.2trunk View file →
@@ -33,16 +33,26 @@
33 33 */
34 34 private $email;
35 35
36 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 + /**
37 45 * API constructor.
38 46 *
39 - * @param string $email CF Email
40 - * @param string $api_key CF API Key
47 + * @param string $email CF Email
48 + * @param string $api_key CF API Key
49 + * @param string $api_token Bearer Token
41 50 */
42 - public function __construct( $email, $api_key ) {
43 - $this->email = $email;
44 - $this->api_key = $api_key;
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;
45 55 }
46 56
47 57 /**
48 58 * GET CF zones
@@ -78,19 +88,24 @@
78 88 $data['purge_everything'] = true;
79 89 }
80 90
81 91 if ( is_array( $args ) && array_key_exists( 'files', $args ) ) {
82 - $data['files'] = $args['files'];
92 + $data['files'] = (array) $args['files'];
83 93 }
94 +
84 95 if ( is_array( $args ) && array_key_exists( 'tags', $args ) ) {
85 - $data['files'] = $args['tags'];
96 + $data['tags'] = (array) $args['tags'];
86 97 }
87 98
88 99 $endpoint = $this->end_point . '/zones/' . $zone_id . '/purge_cache';
89 100
90 - $result = $this->remote_request( $endpoint, 'DELETE' );
101 + $result = $this->remote_request( $endpoint, 'POST', $data );
91 102
92 - return $result;
103 + if ( ! empty( $result['success'] ) ) {
104 + return boolval( $result['success'] );
105 + }
106 +
107 + return false;
93 108 }
94 109
95 110 /**
96 111 * Make remote req.
@@ -98,9 +113,9 @@
98 113 * @param string $url URL
99 114 * @param string $type req type
100 115 * @param array $data data
101 116 *
102 - * @return mixed|string
117 + * @return mixed|array
103 118 */
104 119 private function remote_request( $url, $type = 'GET', $data = array() ) {
105 120 $args = array(
106 121 'method' => $type,
@@ -105,41 +120,56 @@
105 120 $args = array(
106 121 'method' => $type,
107 122 'timeout' => 5,
108 123 'headers' => array(
109 - 'X-Auth-Email' => $this->email,
110 - 'X-Auth-Key' => $this->api_key,
111 124 'Content-Type' => 'application/json',
112 125 ),
113 126 'sslverify' => false,
114 127 );
115 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 +
116 136 if ( ! empty( $data ) ) {
117 137 $args['body'] = wp_json_encode( $data );
118 138 }
119 139
120 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() ) );
121 143
122 - if ( is_wp_error( $response ) ) {
123 - return $response->get_error_message();
124 - } else {
125 - return json_decode( wp_remote_retrieve_body( $response ) );
144 + return [];
126 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;
127 156 }
128 157
129 158 /**
130 159 * Return an instance of the current class, create one if it doesn't exist
131 160 *
132 - * @param string $email CF Email
133 - * @param string $api_key CF API Key
161 + * @param string $email CF Email
162 + * @param string $api_key CF API Key
163 + * @param string $api_token CF API Token
134 164 *
135 165 * @return API
136 166 */
137 - public static function factory( $email, $api_key ) {
167 + public static function factory( $email, $api_key, $api_token ) {
138 168 static $instance = false;
139 169
140 170 if ( ! $instance ) {
141 - $instance = new self( $email, $api_key );
171 + $instance = new self( $email, $api_key, $api_token );
142 172 }
143 173
144 174 return $instance;
145 175 }