PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.5
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.5
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / api / imagify.php

imagify.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.5, at inc/api/imagify.php

355 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Create a new user on Imagify.
5 *
6 * @param array $data All user data.
7 * @return object
8 **/
9 function add_imagify_user( $data ) {
10 return Imagify()->createUser( $data );
11 }
12
13 /**
14 * Update your Imagify account.
15 *
16 * @param string $data All user data.
17 * @return object
18 **/
19 function update_imagify_user( $data ) {
20 return Imagify()->updateUser( $data );
21 }
22
23 /**
24 * Get your Imagify account infos.
25 *
26 * @return object
27 **/
28 function get_imagify_user() {
29 return Imagify()->getUser();
30 }
31
32 /**
33 * Get the Imagify API version.
34 *
35 * @return object
36 **/
37 function get_imagify_api_version() {
38 return Imagify()->getApiVersion();
39 }
40
41 /**
42 * Check your Imagify API key status.
43 *
44 * @return bool
45 **/
46 function get_imagify_status( $data ) {
47 return Imagify()->getStatus( $data );
48 }
49
50 /**
51 * Optimize an image by uploading it on Imagify.
52 *
53 * @param array $data All image data.
54 * @return object
55 **/
56 function fetch_imagify_image( $data ) {
57 return Imagify()->fetchImage( $data );
58 }
59
60 /**
61 * Optimize an image by sharing its URL on Imagify.
62 *
63 * @param array $data All image data.
64 * @return object
65 **/
66 function upload_imagify_image( $data ) {
67 return Imagify()->uploadImage( $data );
68 }
69
70 /**
71 * Get Imagify Plans Prices
72 *
73 * @return object
74 * @since 1.5
75 * @author Geoffrey Crofte
76 **/
77 function get_imagify_plans_prices() {
78 return Imagify()->getPlansPrices();
79 }
80
81 /**
82 * Get Imagify Plans Prices
83 *
84 * @return object
85 * @since 1.5
86 * @author Geoffrey Crofte
87 **/
88 function get_imagify_packs_prices() {
89 return Imagify()->getPacksPrices();
90 }
91
92 /**
93 * Imagify.io API for WordPress
94 */
95 class Imagify {
96 /**
97 * The Imagify API endpoint
98 */
99 const API_ENDPOINT = 'https://app.imagify.io/api/';
100
101 /**
102 * The Imagify API key
103 */
104 private $apiKey = '';
105
106 /**
107 * HTTP headers
108 */
109 private $headers = array();
110
111 /**
112 * @var The single instance of the class
113 */
114 protected static $_instance = null;
115
116 /**
117 * The constructor
118 *
119 * @return void
120 **/
121 public function __construct()
122 {
123 // check if the WordPress plugin is activated and the API key is stored in the options
124 if ( defined( 'IMAGIFY_VERSION' ) && function_exists( 'get_imagify_option' ) ) {
125 $apiKey = get_imagify_option( 'api_key', false );
126 $this->apiKey = ( $apiKey ) ? $apiKey : $this->apiKey;
127 }
128
129 // check if the API key is defined with the PHP constant (it's ovveride the WordPress plugin option
130 if ( defined( 'IMAGIFY_API_KEY' ) && IMAGIFY_API_KEY ) {
131 $this->apiKey = IMAGIFY_API_KEY;
132 }
133
134 $this->headers['Accept'] = 'Accept: application/json';
135 $this->headers['Content-Type'] = 'Content-Type: application/json';
136 $this->headers['Authorization'] = 'Authorization: token ' . $this->apiKey;
137 }
138
139 /**
140 * Main Imagify Instance
141 *
142 * Ensures only one instance of class is loaded or can be loaded.
143 *
144 * @static
145 * @return Main instance
146 */
147 public static function instance() {
148 if ( is_null( self::$_instance ) ) {
149 self::$_instance = new self();
150 }
151
152 return self::$_instance;
153 }
154
155 /**
156 * Create a user on your Imagify account.
157 *
158 * @param array $data All user data. Details here: --
159 * @return object
160 **/
161 public function createUser( $data ) {
162 unset( $this->headers['Authorization'], $this->headers['Accept'], $this->headers['Content-Type'] );
163
164 $data['from_plugin'] = true;
165 $args = array(
166 'method' => 'POST',
167 'post_data' => $data
168 );
169
170 return $this->httpCall( 'users/', $args );
171 }
172
173 /**
174 * Get your Imagify account infos.
175 *
176 * @return object
177 **/
178 public function getUser() {
179 static $user;
180
181 if ( ! isset( $user ) ) {
182 $user = $this->httpCall( 'users/me/', array( 'timeout' => 10 ) );
183 }
184
185 return $user;
186 }
187
188 /**
189 * Check your Imagify API key status.
190 *
191 * @return object
192 **/
193 public function getStatus( $data ) {
194 static $status;
195
196 if ( ! isset( $status ) ) {
197 unset( $this->headers['Accept'], $this->headers['Content-Type'] );
198 $this->headers['Authorization'] = 'Authorization: token ' . $data;
199
200 $status = $this->httpCall( 'status/', array( 'timeout' => 10 ) );
201 }
202
203 return $status;
204 }
205
206 /**
207 * Get the Imagify API version.
208 *
209 * @return object
210 **/
211 public function getApiVersion() {
212 static $api_version;
213
214 if ( ! isset( $api_version ) ) {
215 unset( $this->headers['Accept'], $this->headers['Content-Type'] );
216
217 $api_version = $this->httpCall( 'version/', array( 'timeout' => 5 ) );
218 }
219
220 return $api_version;
221 }
222
223 /**
224 * Update an existing user on your Imagify account.
225 *
226 * @param string $data All user data. Details here: --
227 * @return object
228 **/
229 public function updateUser( $data ) {
230 $args = array(
231 'method' => 'PUT',
232 'post_data' => $data,
233 'timeout' => 10
234 );
235
236 return $this->httpCall( 'users/me/', $args );
237 }
238
239 /**
240 * Optimize an image from its binary content.
241 *
242 * @param string $data All options. Details here: --
243 * @return object
244 **/
245 public function uploadImage( $data ) {
246 if ( isset( $this->headers['Accept'], $this->headers['Content-Type'] ) ) {
247 unset( $this->headers['Accept'], $this->headers['Content-Type'] );
248 }
249
250 $args = array(
251 'method' => 'POST',
252 'post_data' => $data
253 );
254
255 return $this->httpCall( 'upload/', $args );
256 }
257
258 /**
259 * Optimize an image from its URL.
260 *
261 * @param string $data All options. Details here: --
262 * @return object
263 **/
264 public function fetchImage( $data ) {
265 $args = array(
266 'method' => 'POST',
267 'post_data' => json_encode( $data )
268 );
269 return $this->httpCall( 'fetch/', $args );
270 }
271
272 /**
273 * Get prices for plans
274 *
275 * @return object
276 */
277 public function getPlansPrices() {
278 return $this->httpCall( 'pricing/plan/' );
279 }
280
281 /**
282 * Get prices for packs (one time)
283 *
284 * @return object
285 */
286 public function getPacksPrices() {
287 return $this->httpCall( 'pricing/pack/' );
288 }
289
290 /**
291 * Make an HTTP call using curl.
292 *
293 * @param string $url The URL to call
294 * @param array $args The request args
295 * @return object
296 **/
297 private function httpCall( $url, $args = array() ) {
298 $default = array(
299 'method' => 'GET',
300 'post_data' => null,
301 'timeout' => 45
302 );
303 $args = array_merge( $default, $args );
304
305 // Check if php-curl is enabled
306 if ( ! function_exists( 'curl_init' ) || ! function_exists( 'curl_exec' ) ) {
307 return new WP_Error( 'curl', 'cURL isn\'t installed on the server.' );
308 }
309
310 try {
311 $ch = curl_init();
312
313 if ( 'POST' == $args['method'] ) {
314 curl_setopt( $ch, CURLOPT_POST, true );
315 curl_setopt( $ch, CURLOPT_POSTFIELDS, $args['post_data'] );
316 }
317
318 curl_setopt( $ch, CURLOPT_URL, self::API_ENDPOINT . $url );
319 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
320 curl_setopt( $ch, CURLOPT_HTTPHEADER, $this->headers );
321 curl_setopt( $ch, CURLOPT_TIMEOUT, $args['timeout'] );
322 @curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
323 curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
324 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
325
326 $response = json_decode( curl_exec( $ch ) );
327 $error = curl_error( $ch );
328 $http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
329
330 curl_close( $ch );
331 } catch( Exception $e ) {
332 return new WP_Error( 'curl', 'Unknown error occurred' );
333 }
334
335 if ( 200 != $http_code && isset( $response->code, $response->detail ) ) {
336 return new WP_Error( $http_code, $response->detail );
337 } elseif ( 200 != $http_code ) {
338 $http_code = (int) $http_code;
339 $error = '' != $error ? ' - ' . htmlentities( $error ) : '';
340 return new WP_Error( $http_code, "Unknown error occurred ({$http_code}{$error}) " );
341 } else {
342 return $response;
343 }
344
345 return $response;
346 }
347 }
348
349 /**
350 * Returns the main instance of Imagify to prevent the need to use globals.
351 */
352 function Imagify() {
353 return Imagify::instance();
354 }
355 $GLOBALS['imagify'] = Imagify();