| @@ -1,61 +1,349 @@ | ||
| 1 | 1 | <?php |
| 2 | -defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); | |
| 2 | +use Imagify\CLI\CommandInterface; | |
| 3 | 3 | |
| 4 | +defined( 'ABSPATH' ) || exit; | |
| 5 | + | |
| 4 | 6 | /** |
| 5 | - * Check if external requests are blocked for Imagify. | |
| 7 | + * Returns the main instance of the Imagify class. | |
| 6 | 8 | * |
| 7 | - * @since 1.0 | |
| 9 | + * @since 1.6.5 | |
| 8 | 10 | * |
| 9 | - * return bool True if Imagify API can't be called | |
| 11 | + * @return object The Imagify instance. | |
| 10 | 12 | */ |
| 11 | -function is_imagify_blocked() { | |
| 12 | - if ( ! defined( 'WP_HTTP_BLOCK_EXTERNAL' ) || ! WP_HTTP_BLOCK_EXTERNAL ) { | |
| 13 | - return false; | |
| 13 | +function imagify() { | |
| 14 | + return Imagify::get_instance(); | |
| 15 | +} | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * Create a new user on Imagify. | |
| 19 | + * | |
| 20 | + * @param array $data All user data. | |
| 21 | + * @return object | |
| 22 | + */ | |
| 23 | +function add_imagify_user( $data ) { | |
| 24 | + return imagify()->create_user( $data ); | |
| 25 | +} | |
| 26 | + | |
| 27 | +/** | |
| 28 | + * Update your Imagify account. | |
| 29 | + * | |
| 30 | + * @param string $data All user data. | |
| 31 | + * @return object | |
| 32 | + */ | |
| 33 | +function update_imagify_user( $data ) { | |
| 34 | + return imagify()->update_user( $data ); | |
| 35 | +} | |
| 36 | + | |
| 37 | +/** | |
| 38 | + * Get your Imagify account infos. | |
| 39 | + * | |
| 40 | + * @return object | |
| 41 | + */ | |
| 42 | +function get_imagify_user() { | |
| 43 | + $user = get_transient( 'imagify_user_cache' ); | |
| 44 | + if ( false !== $user ) { | |
| 45 | + return $user; | |
| 14 | 46 | } |
| 15 | - | |
| 16 | - if ( defined( 'WP_ACCESSIBLE_HOSTS' ) ) { | |
| 17 | - $accessible_hosts = explode( ',', WP_ACCESSIBLE_HOSTS ); | |
| 18 | - $accessible_hosts = array_map( 'trim', $accessible_hosts ); | |
| 19 | - | |
| 20 | - if ( in_array( '*.imagify.io', $accessible_hosts ) ) { | |
| 21 | - return false; | |
| 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; | |
| 67 | +} | |
| 68 | + | |
| 69 | +/** | |
| 70 | + * Get the Imagify API version. | |
| 71 | + * | |
| 72 | + * @return object | |
| 73 | + */ | |
| 74 | +function get_imagify_api_version() { | |
| 75 | + return imagify()->get_api_version(); | |
| 76 | +} | |
| 77 | + | |
| 78 | +/** | |
| 79 | + * Check your Imagify API key status. | |
| 80 | + * | |
| 81 | + * @param string $data An API key. | |
| 82 | + * @return bool | |
| 83 | + */ | |
| 84 | +function get_imagify_status( $data ) { | |
| 85 | + return imagify()->get_status( $data ); | |
| 86 | +} | |
| 87 | + | |
| 88 | +/** | |
| 89 | + * Optimize an image by uploading it on Imagify. | |
| 90 | + * | |
| 91 | + * @param array $data All image data. | |
| 92 | + * @return object | |
| 93 | + */ | |
| 94 | +function fetch_imagify_image( $data ) { | |
| 95 | + return imagify()->fetch_image( $data ); | |
| 96 | +} | |
| 97 | + | |
| 98 | +/** | |
| 99 | + * Optimize an image by sharing its URL on Imagify. | |
| 100 | + * | |
| 101 | + * @since 1.6.7 $data['image'] can contain the file path (prefered) or the result of `curl_file_create()`. | |
| 102 | + * | |
| 103 | + * @param array $data All image data. | |
| 104 | + * @return object | |
| 105 | + */ | |
| 106 | +function upload_imagify_image( $data ) { | |
| 107 | + return imagify()->upload_image( $data ); | |
| 108 | +} | |
| 109 | + | |
| 110 | +/** | |
| 111 | + * Get Imagify Plans Prices. | |
| 112 | + * | |
| 113 | + * @since 1.5 | |
| 114 | + * | |
| 115 | + * @return object | |
| 116 | + */ | |
| 117 | +function get_imagify_plans_prices() { | |
| 118 | + return imagify()->get_plans_prices(); | |
| 119 | +} | |
| 120 | + | |
| 121 | +/** | |
| 122 | + * Get Imagify All Prices (plans). | |
| 123 | + * | |
| 124 | + * @since 1.5.4 | |
| 125 | + * | |
| 126 | + * @return object | |
| 127 | + */ | |
| 128 | +function get_imagify_all_prices() { | |
| 129 | + return imagify()->get_all_prices(); | |
| 130 | +} | |
| 131 | + | |
| 132 | +/** | |
| 133 | + * Check if Coupon Code exists. | |
| 134 | + * | |
| 135 | + * @since 1.6 | |
| 136 | + * | |
| 137 | + * @param string $coupon the coupon code to check. | |
| 138 | + * @return object | |
| 139 | + */ | |
| 140 | +function check_imagify_coupon_code( $coupon ) { | |
| 141 | + return imagify()->check_coupon_code( $coupon ); | |
| 142 | +} | |
| 143 | + | |
| 144 | +/** | |
| 145 | + * Check if Discount/Promotion is available. | |
| 146 | + * | |
| 147 | + * @since 1.6.3 | |
| 148 | + * | |
| 149 | + * @return object | |
| 150 | + */ | |
| 151 | +function check_imagify_discount() { | |
| 152 | + return imagify()->check_discount(); | |
| 153 | +} | |
| 154 | + | |
| 155 | +/** | |
| 156 | + * Get Maximum image size for free plan. | |
| 157 | + * | |
| 158 | + * @since 1.5.6 | |
| 159 | + * | |
| 160 | + * @return string | |
| 161 | + */ | |
| 162 | +function get_imagify_max_image_size() { | |
| 163 | + $max_image_size = get_transient( 'imagify_max_image_size' ); | |
| 164 | + | |
| 165 | + if ( false === $max_image_size ) { | |
| 166 | + $max_image_size = imagify()->get_public_info(); | |
| 167 | + | |
| 168 | + if ( ! is_wp_error( $max_image_size ) ) { | |
| 169 | + $max_image_size = $max_image_size->max_image_size; | |
| 170 | + set_transient( 'imagify_max_image_size', $max_image_size, 6 * HOUR_IN_SECONDS ); | |
| 22 | 171 | } |
| 23 | 172 | } |
| 24 | - | |
| 25 | - return true; | |
| 173 | + | |
| 174 | + return $max_image_size; | |
| 26 | 175 | } |
| 27 | 176 | |
| 28 | 177 | /** |
| 29 | - * Determine if the Imagify API is available by checking the API version. | |
| 178 | + * Translate a message from our servers. | |
| 30 | 179 | * |
| 31 | - * @since 1.0 | |
| 180 | + * @since 1.6.10 | |
| 32 | 181 | * |
| 33 | - * @return bool True if the Imagify API is available. | |
| 182 | + * @see Imagify::curl_http_call() | |
| 183 | + * @see Imagify::handle_response() | |
| 184 | + * | |
| 185 | + * @param string $message The message from the server (in English). | |
| 186 | + * @return string If in our list, the translated message. The original message otherwise. | |
| 34 | 187 | */ |
| 35 | -function is_imagify_servers_up() { | |
| 36 | - static $imagify_api_version = null; | |
| 37 | - | |
| 38 | - if ( null !== $imagify_api_version ) { | |
| 39 | - return $imagify_api_version; | |
| 188 | +function imagify_translate_api_message( $message ) { | |
| 189 | + if ( ! $message ) { | |
| 190 | + $message = 'Unknown error occurred'; | |
| 40 | 191 | } |
| 41 | - | |
| 42 | - $transient_name = 'imagify_check_api_version'; | |
| 43 | - $transient_expiration = 3 * MINUTE_IN_SECONDS; | |
| 44 | - | |
| 45 | - if ( get_site_transient( $transient_name ) ) { | |
| 46 | - $imagify_api_version = true; | |
| 47 | - return true; | |
| 192 | + | |
| 193 | + if ( is_wp_error( $message ) ) { | |
| 194 | + if ( $message->errors ) { | |
| 195 | + foreach ( (array) $message->errors as $code => $messages ) { | |
| 196 | + if ( $messages ) { | |
| 197 | + $message->errors[ $code ] = array_map( 'imagify_translate_api_message', (array) $messages ); | |
| 198 | + } | |
| 199 | + } | |
| 200 | + } | |
| 201 | + | |
| 202 | + return $message; | |
| 48 | 203 | } |
| 49 | - | |
| 50 | - if ( is_wp_error( get_imagify_api_version() ) ) { | |
| 51 | - $imagify_api_version = false; | |
| 52 | - set_site_transient( $transient_name, 0, $transient_expiration ); | |
| 53 | - | |
| 54 | - return false; | |
| 204 | + | |
| 205 | + if ( is_object( $message ) && ! empty( $message->detail ) ) { | |
| 206 | + $message->detail = imagify_translate_api_message( $message->detail ); | |
| 55 | 207 | } |
| 56 | - | |
| 57 | - $imagify_api_version = true; | |
| 58 | - set_site_transient( $transient_name, 1, $transient_expiration ); | |
| 59 | - | |
| 60 | - return true; | |
| 61 | -} | |
| 208 | + | |
| 209 | + if ( ! is_string( $message ) ) { | |
| 210 | + return $message; | |
| 211 | + } | |
| 212 | + | |
| 213 | + $trim_message = trim( $message, '. ' ); | |
| 214 | + | |
| 215 | + $messages = [ | |
| 216 | + // Local messages from Imagify::curl_http_call() and Imagify::handle_response(). | |
| 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' ), | |
| 235 | + // API messages. | |
| 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' ), | |
| 255 | + 'Upload a valid image. The file you uploaded was either not an image or a corrupted image' => __( 'Invalid or corrupted file.', 'imagify' ), | |
| 256 | + ]; | |
| 257 | + | |
| 258 | + if ( isset( $messages[ $trim_message ] ) ) { | |
| 259 | + return $messages[ $trim_message ]; | |
| 260 | + } | |
| 261 | + | |
| 262 | + // Local message. | |
| 263 | + if ( preg_match( '@^(?:Unknown|An) error occurred \((.+)\)$@', $trim_message, $matches ) ) { | |
| 264 | + /* translators: %s is an error message. */ | |
| 265 | + return sprintf( __( 'An error occurred (%s).', 'imagify' ), esc_html( wp_strip_all_tags( $matches[1] ) ) ); | |
| 266 | + } | |
| 267 | + | |
| 268 | + // Local message. | |
| 269 | + if ( preg_match( '@^Our server returned an error \((.+)\)$@', $trim_message, $matches ) ) { | |
| 270 | + /* translators: %s is an error message. */ | |
| 271 | + return sprintf( __( 'Our server returned an error (%s).', 'imagify' ), esc_html( wp_strip_all_tags( $matches[1] ) ) ); | |
| 272 | + } | |
| 273 | + | |
| 274 | + // API message. | |
| 275 | + if ( preg_match( '@^Custom one time plan starts from (\d+) MB$@', $trim_message, $matches ) ) { | |
| 276 | + /* translators: %s is a formatted number, dont use %d. */ | |
| 277 | + return sprintf( __( 'Custom One Time plan starts from %s MB.', 'imagify' ), number_format_i18n( (int) $matches[1] ) ); | |
| 278 | + } | |
| 279 | + | |
| 280 | + // API message. | |
| 281 | + if ( preg_match( '@^(.*) is not a valid extension$@', $trim_message, $matches ) ) { | |
| 282 | + /* translators: %s is a file extension. */ | |
| 283 | + return sprintf( __( '%s is not a valid extension.', 'imagify' ), sanitize_text_field( $matches[1] ) ); | |
| 284 | + } | |
| 285 | + | |
| 286 | + // API message. | |
| 287 | + if ( preg_match( '@^Request was throttled\. Expected available in ([\d.]+) second$@', $trim_message, $matches ) ) { | |
| 288 | + /* translators: %s is a float number. */ | |
| 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] ) ); | |
| 290 | + } | |
| 291 | + | |
| 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(); | |
| 349 | +} | |