PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.7
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.7
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 / functions / api.php

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

317 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' );
3
4 /**
5 * Returns the main instance of the Imagify class.
6 *
7 * @since 1.6.5
8 * @author Grégory Viguier
9 *
10 * @return object The Imagify instance.
11 */
12 function imagify() {
13 return Imagify::get_instance();
14 }
15
16 /**
17 * Create a new user on Imagify.
18 *
19 * @param array $data All user data.
20 * @return object
21 */
22 function add_imagify_user( $data ) {
23 return imagify()->create_user( $data );
24 }
25
26 /**
27 * Update your Imagify account.
28 *
29 * @param string $data All user data.
30 * @return object
31 */
32 function update_imagify_user( $data ) {
33 return imagify()->update_user( $data );
34 }
35
36 /**
37 * Get your Imagify account infos.
38 *
39 * @return object
40 */
41 function get_imagify_user() {
42 return imagify()->get_user();
43 }
44
45 /**
46 * Get the Imagify API version.
47 *
48 * @return object
49 */
50 function get_imagify_api_version() {
51 return imagify()->get_api_version();
52 }
53
54 /**
55 * Check your Imagify API key status.
56 *
57 * @param string $data An API key.
58 * @return bool
59 */
60 function get_imagify_status( $data ) {
61 return imagify()->get_status( $data );
62 }
63
64 /**
65 * Optimize an image by uploading it on Imagify.
66 *
67 * @param array $data All image data.
68 * @return object
69 */
70 function fetch_imagify_image( $data ) {
71 return imagify()->fetch_image( $data );
72 }
73
74 /**
75 * Optimize an image by sharing its URL on Imagify.
76 *
77 * @since 1.6.7 $data['image'] can contain the file path (prefered) or the result of `curl_file_create()`.
78 *
79 * @param array $data All image data.
80 * @return object
81 */
82 function upload_imagify_image( $data ) {
83 return imagify()->upload_image( $data );
84 }
85
86 /**
87 * Get Imagify Plans Prices.
88 *
89 * @since 1.5
90 * @author Geoffrey Crofte
91 *
92 * @return object
93 */
94 function get_imagify_plans_prices() {
95 return imagify()->get_plans_prices();
96 }
97
98 /**
99 * Get Imagify Plans Prices.
100 *
101 * @since 1.5
102 * @author Geoffrey Crofte
103 *
104 * @return object
105 */
106 function get_imagify_packs_prices() {
107 return imagify()->get_packs_prices();
108 }
109
110 /**
111 * Get Imagify All Prices (plan & packs).
112 *
113 * @since 1.5.4
114 * @author Geoffrey Crofte
115 *
116 * @return object
117 */
118 function get_imagify_all_prices() {
119 return imagify()->get_all_prices();
120 }
121
122 /**
123 * Check if Coupon Code exists.
124 *
125 * @since 1.6
126 * @author Geoffrey Crofte
127 *
128 * @param string $coupon the coupon code to check.
129 * @return object
130 */
131 function check_imagify_coupon_code( $coupon ) {
132 return imagify()->check_coupon_code( $coupon );
133 }
134
135 /**
136 * Check if Discount/Promotion is available.
137 *
138 * @since 1.6.3
139 * @author Geoffrey Crofte
140 *
141 * @return object
142 */
143 function check_imagify_discount() {
144 return imagify()->check_discount();
145 }
146
147 /**
148 * Get Maximum image size for free plan.
149 *
150 * @since 1.5.6
151 * @author Remy Perona
152 *
153 * @return string
154 */
155 function get_imagify_max_image_size() {
156 $max_image_size = get_transient( 'imagify_max_image_size' );
157
158 if ( false === $max_image_size ) {
159 $max_image_size = imagify()->get_public_info();
160
161 if ( ! is_wp_error( $max_image_size ) ) {
162 $max_image_size = $max_image_size->max_image_size;
163 set_transient( 'imagify_max_image_size', $max_image_size, 6 * HOUR_IN_SECONDS );
164 }
165 }
166
167 return $max_image_size;
168 }
169
170 /**
171 * Check if external requests are blocked for Imagify.
172 *
173 * @since 1.0
174 *
175 * return bool True if Imagify API can't be called.
176 */
177 function is_imagify_blocked() {
178 if ( ! defined( 'WP_HTTP_BLOCK_EXTERNAL' ) || ! WP_HTTP_BLOCK_EXTERNAL ) {
179 return false;
180 }
181
182 if ( ! defined( 'WP_ACCESSIBLE_HOSTS' ) ) {
183 return true;
184 }
185
186 $accessible_hosts = explode( ',', WP_ACCESSIBLE_HOSTS );
187 $accessible_hosts = array_map( 'trim', $accessible_hosts );
188
189 return ! in_array( '*.imagify.io', $accessible_hosts, true );
190 }
191
192 /**
193 * Determine if the Imagify API is available by checking the API version.
194 *
195 * @since 1.0
196 *
197 * @return bool True if the Imagify API is available.
198 */
199 function is_imagify_servers_up() {
200 static $is_up;
201
202 if ( isset( $is_up ) ) {
203 return $is_up;
204 }
205
206 $transient_name = 'imagify_check_api_version';
207 $transient_expiration = 3 * MINUTE_IN_SECONDS;
208
209 if ( get_site_transient( $transient_name ) ) {
210 $is_up = true;
211 return $is_up;
212 }
213
214 if ( is_wp_error( get_imagify_api_version() ) ) {
215 set_site_transient( $transient_name, 0, $transient_expiration );
216
217 $is_up = false;
218 return $is_up;
219 }
220
221 set_site_transient( $transient_name, 1, $transient_expiration );
222
223 $is_up = true;
224 return $is_up;
225 }
226
227 /**
228 * Translate a message from our servers.
229 *
230 * @since 1.6.10
231 * @author Grégory Viguier
232 * @see Imagify::curl_http_call()
233 * @see Imagify::handle_response()
234 *
235 * @param string $message The message from the server (in English).
236 * @return string If in our list, the translated message. The original message otherwise.
237 */
238 function imagify_translate_api_message( $message ) {
239 if ( ! $message ) {
240 return imagify_translate_api_message( 'Unknown error occurred' );
241 }
242
243 if ( is_wp_error( $message ) ) {
244 if ( $message->errors ) {
245 foreach ( (array) $message->errors as $code => $messages ) {
246 if ( $messages ) {
247 $message->errors[ $code ] = array_map( 'imagify_translate_api_message', (array) $messages );
248 }
249 }
250 }
251
252 return $message;
253 }
254
255 if ( is_object( $message ) && ! empty( $message->detail ) ) {
256 $message->detail = imagify_translate_api_message( $message->detail );
257 }
258
259 if ( ! is_string( $message ) ) {
260 return $message;
261 }
262
263 $trim_message = trim( $message, '. ' );
264
265 $messages = array(
266 // Local messages from Imagify::curl_http_call() and Imagify::handle_response().
267 'Unknown error occurred' => __( 'Unknown error occurred.', 'imagify' ),
268 'Your image is too big to be uploaded on our server' => __( 'Your image is too big to be uploaded on our server.', 'imagify' ),
269 'cURL isn\'t installed on the server' => __( 'cURL is not installed on the server.', 'imagify' ),
270 // API messages.
271 'Authentification not provided' => __( 'Authentication not provided.', 'imagify' ),
272 'Cannot create client token' => __( 'Cannot create client token.', 'imagify' ),
273 'Confirm your account to continue optimizing image' => __( 'Confirm your account to continue optimizing images.', 'imagify' ),
274 'Coupon doesn\'t exist' => __( 'Coupon does not exist.', 'imagify' ),
275 'Email field shouldn\'t be empty' => __( 'Email field should not be empty.', 'imagify' ),
276 'Email or Password field shouldn\'t be empty' => __( 'This account already exists.', 'imagify' ),
277 'Error uploading to data Storage' => __( 'Error uploading to Data Storage.', 'imagify' ),
278 'Not able to connect to Data Storage API to get the token' => __( 'Unable to connect to Data Storage API to get the token.', 'imagify' ),
279 'Not able to connect to Data Storage API' => __( 'Unable to connect to Data Storage API.', 'imagify' ),
280 'Not able to retrieve the token from DataStorage API' => __( 'Unable to retrieve the token from Data Storage API.', 'imagify' ),
281 'This email is already registered, you should try another email' => __( 'This email is already registered, you should try another email.', 'imagify' ),
282 'This user doesn\'t exit' => __( 'This user does not exist.', 'imagify' ),
283 'Too many request, be patient' => __( 'Too many requests, please be patient.', 'imagify' ),
284 'Unable to regenerate access token' => __( 'Unable to regenerate access token.', 'imagify' ),
285 'User not valid' => __( 'User not valid.', 'imagify' ),
286 'WELL DONE. This image is already compressed, no further compression required' => __( 'WELL DONE. This image is already optimized, no further optimization is required.', 'imagify' ),
287 'You are not authorized to perform this action' => __( 'You are not authorized to perform this action.', 'imagify' ),
288 'You\'ve consumed all your data. You have to upgrade your account to continue' => __( 'You have consumed all your data. You have to upgrade your account to continue.', 'imagify' ),
289 'Invalid token' => __( 'Invalid API key', 'imagify' ),
290 'Upload a valid image. The file you uploaded was either not an image or a corrupted image' => __( 'Invalid or corrupted file.', 'imagify' ),
291 );
292
293 if ( isset( $messages[ $trim_message ] ) ) {
294 return $messages[ $trim_message ];
295 }
296
297 // Local message.
298 if ( preg_match( '@^Unknown error occurred \((\d+)(.*?)\)$@', $trim_message, $matches ) ) {
299 /* translators: 1 is a http status code, 2 is an error message. */
300 return sprintf( __( 'Unknown error occurred (%1$d%2$s).', 'imagify' ), $matches[1], esc_html( strip_tags( $matches[2] ) ) );
301 }
302
303 // API message.
304 if ( preg_match( '@^Custom one time plan starts from (\d+) MB$@', $trim_message, $matches ) ) {
305 /* translators: %s is a formatted number, dont use %d. */
306 return sprintf( __( 'Custom One Time plan starts from %s MB.', 'imagify' ), number_format_i18n( (int) $matches[1] ) );
307 }
308
309 // API message.
310 if ( preg_match( '@^(.*) is not a valid extension$@', $trim_message, $matches ) ) {
311 /* translators: %s is a file extension. */
312 return sprintf( __( '%s is not a valid extension.', 'imagify' ), sanitize_text_field( $matches[1] ) );
313 }
314
315 return $message;
316 }
317