PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 3.6.3
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v3.6.3
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 +40 -21 2.23.6.3 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,15 +120,20 @@
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
@@ -118,28 +138,27 @@
118 138 }
119 139
120 140 $response = wp_remote_request( $url, $args );
121 141
122 - if ( is_wp_error( $response ) ) {
123 - return $response->get_error_message();
124 - } else {
125 - return json_decode( wp_remote_retrieve_body( $response ) );
126 - }
142 + \PoweredCache\Utils\log( sprintf( 'Cloudflare API Response: %s', print_r( wp_remote_retrieve_body( $response ), true ) ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
143 +
144 + return json_decode( wp_remote_retrieve_body( $response ), true );
127 145 }
128 146
129 147 /**
130 148 * Return an instance of the current class, create one if it doesn't exist
131 149 *
132 - * @param string $email CF Email
133 - * @param string $api_key CF API Key
150 + * @param string $email CF Email
151 + * @param string $api_key CF API Key
152 + * @param string $api_token CF API Token
134 153 *
135 154 * @return API
136 155 */
137 - public static function factory( $email, $api_key ) {
156 + public static function factory( $email, $api_key, $api_token ) {
138 157 static $instance = false;
139 158
140 159 if ( ! $instance ) {
141 - $instance = new self( $email, $api_key );
160 + $instance = new self( $email, $api_key, $api_token );
142 161 }
143 162
144 163 return $instance;
145 164 }