PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
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
← All changes | inc/functions/api.php +133 -57 1.10 → trunk View file →
@@ -1,12 +1,13 @@
1 1 <?php
2 -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
2 +use Imagify\CLI\CommandInterface;
3 3
4 +defined( 'ABSPATH' ) || exit;
5 +
4 6 /**
5 7 * Returns the main instance of the Imagify class.
6 8 *
7 9 * @since 1.6.5
8 - * @author Grégory Viguier
9 10 *
10 11 * @return object The Imagify instance.
11 12 */
12 13 function imagify() {
@@ -38,9 +39,32 @@
38 39 *
39 40 * @return object
40 41 */
41 42 function get_imagify_user() {
42 - return imagify()->get_user();
43 + $user = get_transient( 'imagify_user_cache' );
44 + if ( false !== $user ) {
45 + return $user;
46 + }
47 +
48 + $user = imagify()->get_user();
49 +
50 + // Fill user object with missed details before saving the transient.
51 + if ( is_wp_error( $user ) ) {
52 + $user->id = 0;
53 + $user->email = '';
54 + $user->plan_id = 0;
55 + $user->plan_label = '';
56 + $user->quota = 0;
57 + $user->extra_quota = 0;
58 + $user->extra_quota_consumed = 0;
59 + $user->consumed_current_month_quota = 0;
60 + $user->next_date_update = null;
61 + $user->is_active = false;
62 + $user->is_monthly = false;
63 + }
64 +
65 + set_transient( 'imagify_user_cache', $user, 5 * MINUTE_IN_SECONDS );
66 + return $user;
43 67 }
44 68
45 69 /**
46 70 * Get the Imagify API version.
@@ -85,10 +109,9 @@
85 109
86 110 /**
87 111 * Get Imagify Plans Prices.
88 112 *
89 - * @since 1.5
90 - * @author Geoffrey Crofte
113 + * @since 1.5
91 114 *
92 115 * @return object
93 116 */
94 117 function get_imagify_plans_prices() {
@@ -95,27 +118,14 @@
95 118 return imagify()->get_plans_prices();
96 119 }
97 120
98 121 /**
99 - * Get Imagify Plans Prices.
122 + * Get Imagify All Prices (plans).
100 123 *
101 - * @since 1.5
102 - * @author Geoffrey Crofte
124 + * @since 1.5.4
103 125 *
104 126 * @return object
105 127 */
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 128 function get_imagify_all_prices() {
119 129 return imagify()->get_all_prices();
120 130 }
121 131
@@ -121,10 +131,9 @@
121 131
122 132 /**
123 133 * Check if Coupon Code exists.
124 134 *
125 - * @since 1.6
126 - * @author Geoffrey Crofte
135 + * @since 1.6
127 136 *
128 137 * @param string $coupon the coupon code to check.
129 138 * @return object
130 139 */
@@ -134,10 +143,9 @@
134 143
135 144 /**
136 145 * Check if Discount/Promotion is available.
137 146 *
138 - * @since 1.6.3
139 - * @author Geoffrey Crofte
147 + * @since 1.6.3
140 148 *
141 149 * @return object
142 150 */
143 151 function check_imagify_discount() {
@@ -146,10 +154,9 @@
146 154
147 155 /**
148 156 * Get Maximum image size for free plan.
149 157 *
150 - * @since 1.5.6
151 - * @author Remy Perona
158 + * @since 1.5.6
152 159 *
153 160 * @return string
154 161 */
155 162 function get_imagify_max_image_size() {
@@ -169,19 +176,19 @@
169 176
170 177 /**
171 178 * Translate a message from our servers.
172 179 *
173 - * @since 1.6.10
174 - * @author Grégory Viguier
175 - * @see Imagify::curl_http_call()
176 - * @see Imagify::handle_response()
180 + * @since 1.6.10
177 181 *
182 + * @see Imagify::curl_http_call()
183 + * @see Imagify::handle_response()
184 + *
178 185 * @param string $message The message from the server (in English).
179 186 * @return string If in our list, the translated message. The original message otherwise.
180 187 */
181 188 function imagify_translate_api_message( $message ) {
182 189 if ( ! $message ) {
183 - return imagify_translate_api_message( 'Unknown error occurred' );
190 + $message = 'Unknown error occurred';
184 191 }
185 192
186 193 if ( is_wp_error( $message ) ) {
187 194 if ( $message->errors ) {
@@ -204,37 +211,50 @@
204 211 }
205 212
206 213 $trim_message = trim( $message, '. ' );
207 214
208 - $messages = array(
215 + $messages = [
209 216 // Local messages from Imagify::curl_http_call() and Imagify::handle_response().
210 - 'Could not initialize a new cURL handle' => __( 'Could not initialize a new cURL handle.', 'imagify' ),
211 - 'Unknown error occurred' => __( 'Unknown error occurred.', 'imagify' ),
212 - 'Your image is too big to be uploaded on our server' => __( 'Your file is too big to be uploaded on our server.', 'imagify' ),
213 - 'Our server returned an invalid response' => __( 'Our server returned an invalid response.', 'imagify' ),
214 - 'cURL isn\'t installed on the server' => __( 'cURL is not available on the server.', 'imagify' ),
217 + 'Could not initialize a new cURL handle' => __( 'Could not initialize a new cURL handle.', 'imagify' ),
218 + 'Unknown error occurred' => sprintf(
219 + // translators: %1$s = opening link tag, %2$s = closing link tag.
220 + __( 'An unknown error occurred: %1$sMore info and possible solutions%2$s', 'imagify' ),
221 + '<a href="https://imagify.io/documentation/optimization-is-stuck/" rel="noopener" target="_blank">',
222 + '</a>'
223 + ),
224 + 'Your image is too big to be uploaded on our server' => __( 'Your file is too big to be uploaded on our server.', 'imagify' ),
225 + 'Webp is less performant than original' => __( 'WebP file is larger than the original image', 'imagify' ),
226 +
227 + /*
228 + * The AVIF wording below has not been confirmed against a live API response: it mirrors the WebP
229 + * pattern. Both casings are mapped so the message is translated whichever one the API returns.
230 + */
231 + 'Avif is less performant than original' => __( 'AVIF file is larger than the original image', 'imagify' ),
232 + 'AVIF is less performant than original' => __( 'AVIF file is larger than the original image', 'imagify' ),
233 + 'Our server returned an invalid response' => __( 'Our server returned an invalid response.', 'imagify' ),
234 + 'cURL isn\'t installed on the server' => __( 'cURL is not available on the server.', 'imagify' ),
215 235 // API messages.
216 - 'Authentification not provided' => __( 'Authentication not provided.', 'imagify' ),
217 - 'Cannot create client token' => __( 'Cannot create client token.', 'imagify' ),
218 - 'Confirm your account to continue optimizing image' => __( 'Confirm your account to continue optimizing files.', 'imagify' ),
219 - 'Coupon doesn\'t exist' => __( 'Coupon does not exist.', 'imagify' ),
220 - 'Email field shouldn\'t be empty' => __( 'Email field should not be empty.', 'imagify' ),
221 - 'Email or Password field shouldn\'t be empty' => __( 'This account already exists.', 'imagify' ),
222 - 'Error uploading to data Storage' => __( 'Error uploading to Data Storage.', 'imagify' ),
223 - 'Not able to connect to Data Storage API to get the token' => __( 'Unable to connect to Data Storage API to get the token.', 'imagify' ),
224 - 'Not able to connect to Data Storage API' => __( 'Unable to connect to Data Storage API.', 'imagify' ),
225 - 'Not able to retrieve the token from DataStorage API' => __( 'Unable to retrieve the token from Data Storage API.', 'imagify' ),
226 - 'This email is already registered, you should try another email' => __( 'This email is already registered, you should try another email.', 'imagify' ),
227 - 'This user doesn\'t exit' => __( 'This user does not exist.', 'imagify' ),
228 - 'Too many request, be patient' => __( 'Too many requests, please be patient.', 'imagify' ),
229 - 'Unable to regenerate access token' => __( 'Unable to regenerate access token.', 'imagify' ),
230 - 'User not valid' => __( 'User not valid.', 'imagify' ),
231 - 'WELL DONE. This image is already compressed, no further compression required' => __( 'WELL DONE. This media file is already optimized, no further optimization is required.', 'imagify' ),
232 - 'You are not authorized to perform this action' => __( 'You are not authorized to perform this action.', 'imagify' ),
233 - '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' ),
234 - 'Invalid token' => __( 'Invalid API key', 'imagify' ),
236 + 'Authentification not provided' => __( 'Authentication not provided.', 'imagify' ),
237 + 'Cannot create client token' => __( 'Cannot create client token.', 'imagify' ),
238 + 'Confirm your account to continue optimizing image' => __( 'Confirm your account to continue optimizing files.', 'imagify' ),
239 + 'Coupon doesn\'t exist' => __( 'Coupon does not exist.', 'imagify' ),
240 + 'Email field shouldn\'t be empty' => __( 'Email field should not be empty.', 'imagify' ),
241 + 'Email or Password field shouldn\'t be empty' => __( 'This account already exists.', 'imagify' ),
242 + 'Error uploading to data Storage' => __( 'Error uploading to Data Storage.', 'imagify' ),
243 + 'Not able to connect to Data Storage API to get the token' => __( 'Unable to connect to Data Storage API to get the token.', 'imagify' ),
244 + 'Not able to connect to Data Storage API' => __( 'Unable to connect to Data Storage API.', 'imagify' ),
245 + 'Not able to retrieve the token from DataStorage API' => __( 'Unable to retrieve the token from Data Storage API.', 'imagify' ),
246 + 'This email is already registered, you should try another email' => __( 'This email is already registered, you should try another email.', 'imagify' ),
247 + 'This user doesn\'t exit' => __( 'This user does not exist.', 'imagify' ),
248 + 'Too many request, be patient' => __( 'Too many requests, please be patient.', 'imagify' ),
249 + 'Unable to regenerate access token' => __( 'Unable to regenerate access token.', 'imagify' ),
250 + 'User not valid' => __( 'User not valid.', 'imagify' ),
251 + 'WELL DONE. This image is already compressed, no further compression required' => __( 'WELL DONE. This media file is already optimized, no further optimization is required.', 'imagify' ),
252 + 'You are not authorized to perform this action' => __( 'You are not authorized to perform this action.', 'imagify' ),
253 + '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' ),
254 + 'Invalid token' => __( 'Invalid API key', 'imagify' ),
235 255 'Upload a valid image. The file you uploaded was either not an image or a corrupted image' => __( 'Invalid or corrupted file.', 'imagify' ),
236 - );
256 + ];
237 257
238 258 if ( isset( $messages[ $trim_message ] ) ) {
239 259 return $messages[ $trim_message ];
240 260 }
@@ -269,5 +289,61 @@
269 289 return sprintf( _n( 'Request was throttled. Expected available in %s second.', 'Request was throttled. Expected available in %s seconds.', (int) $matches[1], 'imagify' ), sanitize_text_field( $matches[1] ) );
270 290 }
271 291
272 292 return $message;
293 +}
294 +
295 +/**
296 + * Runs the bulk optimization
297 + *
298 + * @param array $contexts An array of contexts (WP/Custom folders).
299 + * @param int $optimization_level Optimization level to use.
300 + *
301 + * @return void
302 + */
303 +function imagify_bulk_optimize( $contexts, $optimization_level ) {
304 + foreach ( $contexts as $context ) {
305 + Imagify\Bulk\Bulk::get_instance()->run_optimize( $context, $optimization_level );
306 + }
307 +}
308 +
309 +/**
310 + * Runs the next-gen generation
311 + *
312 + * @param array $contexts An array of contexts (WP/Custom folders).
313 + *
314 + * @return void
315 + */
316 +function imagify_generate_nextgen( $contexts ) {
317 + Imagify\Bulk\Bulk::get_instance()->run_generate_nextgen( $contexts, imagify_nextgen_images_formats() );
318 +}
319 +
320 +/**
321 + * Add command to WP CLI
322 + *
323 + * @param CommandInterface $command Command object.
324 + *
325 + * @return void
326 + */
327 +function imagify_add_command( CommandInterface $command ) {
328 + if ( ! defined( 'WP_CLI' ) || ! WP_CLI || ! class_exists( '\WP_CLI' ) ) {
329 + return;
330 + }
331 +
332 + \WP_CLI::add_command(
333 + $command->get_name(),
334 + $command,
335 + [
336 + 'shortdesc' => $command->get_description(),
337 + 'synopsis' => $command->get_synopsis(),
338 + ]
339 + );
340 +}
341 +
342 +/**
343 + * Checks if the API key is valid
344 + *
345 + * @return bool
346 + */
347 +function imagify_is_api_key_valid() {
348 + return Imagify_Requirements::is_api_key_valid();
273 349 }