| 1 |
<?php |
| 2 |
use Imagify\CLI\CommandInterface; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 5 |
|
| 6 |
/** |
| 7 |
* Returns the main instance of the Imagify class. |
| 8 |
* |
| 9 |
* @since 1.6.5 |
| 10 |
* |
| 11 |
* @return object The Imagify instance. |
| 12 |
*/ |
| 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 |
return imagify()->get_user(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get the Imagify API version. |
| 48 |
* |
| 49 |
* @return object |
| 50 |
*/ |
| 51 |
function get_imagify_api_version() { |
| 52 |
return imagify()->get_api_version(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Check your Imagify API key status. |
| 57 |
* |
| 58 |
* @param string $data An API key. |
| 59 |
* @return bool |
| 60 |
*/ |
| 61 |
function get_imagify_status( $data ) { |
| 62 |
return imagify()->get_status( $data ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Optimize an image by uploading it on Imagify. |
| 67 |
* |
| 68 |
* @param array $data All image data. |
| 69 |
* @return object |
| 70 |
*/ |
| 71 |
function fetch_imagify_image( $data ) { |
| 72 |
return imagify()->fetch_image( $data ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Optimize an image by sharing its URL on Imagify. |
| 77 |
* |
| 78 |
* @since 1.6.7 $data['image'] can contain the file path (prefered) or the result of `curl_file_create()`. |
| 79 |
* |
| 80 |
* @param array $data All image data. |
| 81 |
* @return object |
| 82 |
*/ |
| 83 |
function upload_imagify_image( $data ) { |
| 84 |
return imagify()->upload_image( $data ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get Imagify Plans Prices. |
| 89 |
* |
| 90 |
* @since 1.5 |
| 91 |
* |
| 92 |
* @return object |
| 93 |
*/ |
| 94 |
function get_imagify_plans_prices() { |
| 95 |
return imagify()->get_plans_prices(); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get Imagify All Prices (plans). |
| 100 |
* |
| 101 |
* @since 1.5.4 |
| 102 |
* |
| 103 |
* @return object |
| 104 |
*/ |
| 105 |
function get_imagify_all_prices() { |
| 106 |
return imagify()->get_all_prices(); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Check if Coupon Code exists. |
| 111 |
* |
| 112 |
* @since 1.6 |
| 113 |
* |
| 114 |
* @param string $coupon the coupon code to check. |
| 115 |
* @return object |
| 116 |
*/ |
| 117 |
function check_imagify_coupon_code( $coupon ) { |
| 118 |
return imagify()->check_coupon_code( $coupon ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Check if Discount/Promotion is available. |
| 123 |
* |
| 124 |
* @since 1.6.3 |
| 125 |
* |
| 126 |
* @return object |
| 127 |
*/ |
| 128 |
function check_imagify_discount() { |
| 129 |
return imagify()->check_discount(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get Maximum image size for free plan. |
| 134 |
* |
| 135 |
* @since 1.5.6 |
| 136 |
* |
| 137 |
* @return string |
| 138 |
*/ |
| 139 |
function get_imagify_max_image_size() { |
| 140 |
$max_image_size = get_transient( 'imagify_max_image_size' ); |
| 141 |
|
| 142 |
if ( false === $max_image_size ) { |
| 143 |
$max_image_size = imagify()->get_public_info(); |
| 144 |
|
| 145 |
if ( ! is_wp_error( $max_image_size ) ) { |
| 146 |
$max_image_size = $max_image_size->max_image_size; |
| 147 |
set_transient( 'imagify_max_image_size', $max_image_size, 6 * HOUR_IN_SECONDS ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
return $max_image_size; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Translate a message from our servers. |
| 156 |
* |
| 157 |
* @since 1.6.10 |
| 158 |
* |
| 159 |
* @see Imagify::curl_http_call() |
| 160 |
* @see Imagify::handle_response() |
| 161 |
* |
| 162 |
* @param string $message The message from the server (in English). |
| 163 |
* @return string If in our list, the translated message. The original message otherwise. |
| 164 |
*/ |
| 165 |
function imagify_translate_api_message( $message ) { |
| 166 |
if ( ! $message ) { |
| 167 |
$message = 'Unknown error occurred'; |
| 168 |
} |
| 169 |
|
| 170 |
if ( is_wp_error( $message ) ) { |
| 171 |
if ( $message->errors ) { |
| 172 |
foreach ( (array) $message->errors as $code => $messages ) { |
| 173 |
if ( $messages ) { |
| 174 |
$message->errors[ $code ] = array_map( 'imagify_translate_api_message', (array) $messages ); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return $message; |
| 180 |
} |
| 181 |
|
| 182 |
if ( is_object( $message ) && ! empty( $message->detail ) ) { |
| 183 |
$message->detail = imagify_translate_api_message( $message->detail ); |
| 184 |
} |
| 185 |
|
| 186 |
if ( ! is_string( $message ) ) { |
| 187 |
return $message; |
| 188 |
} |
| 189 |
|
| 190 |
$trim_message = trim( $message, '. ' ); |
| 191 |
|
| 192 |
$messages = [ |
| 193 |
// Local messages from Imagify::curl_http_call() and Imagify::handle_response(). |
| 194 |
'Could not initialize a new cURL handle' => __( 'Could not initialize a new cURL handle.', 'imagify' ), |
| 195 |
'Unknown error occurred' => sprintf( |
| 196 |
// translators: %1$s = opening link tag, %2$s = closing link tag. |
| 197 |
__( 'An unknown error occurred: %1$sMore info and possible solutions%2$s', 'imagify' ), |
| 198 |
'<a href="https://imagify.io/documentation/optimization-is-stuck/" rel="noopener" target="_blank">', |
| 199 |
'</a>' |
| 200 |
), |
| 201 |
'Your image is too big to be uploaded on our server' => __( 'Your file is too big to be uploaded on our server.', 'imagify' ), |
| 202 |
'Webp is less performant than original' => __( 'WebP file is larger than the original image', 'imagify' ), |
| 203 |
'Our server returned an invalid response' => __( 'Our server returned an invalid response.', 'imagify' ), |
| 204 |
'cURL isn\'t installed on the server' => __( 'cURL is not available on the server.', 'imagify' ), |
| 205 |
// API messages. |
| 206 |
'Authentification not provided' => __( 'Authentication not provided.', 'imagify' ), |
| 207 |
'Cannot create client token' => __( 'Cannot create client token.', 'imagify' ), |
| 208 |
'Confirm your account to continue optimizing image' => __( 'Confirm your account to continue optimizing files.', 'imagify' ), |
| 209 |
'Coupon doesn\'t exist' => __( 'Coupon does not exist.', 'imagify' ), |
| 210 |
'Email field shouldn\'t be empty' => __( 'Email field should not be empty.', 'imagify' ), |
| 211 |
'Email or Password field shouldn\'t be empty' => __( 'This account already exists.', 'imagify' ), |
| 212 |
'Error uploading to data Storage' => __( 'Error uploading to Data Storage.', 'imagify' ), |
| 213 |
'Not able to connect to Data Storage API to get the token' => __( 'Unable to connect to Data Storage API to get the token.', 'imagify' ), |
| 214 |
'Not able to connect to Data Storage API' => __( 'Unable to connect to Data Storage API.', 'imagify' ), |
| 215 |
'Not able to retrieve the token from DataStorage API' => __( 'Unable to retrieve the token from Data Storage API.', 'imagify' ), |
| 216 |
'This email is already registered, you should try another email' => __( 'This email is already registered, you should try another email.', 'imagify' ), |
| 217 |
'This user doesn\'t exit' => __( 'This user does not exist.', 'imagify' ), |
| 218 |
'Too many request, be patient' => __( 'Too many requests, please be patient.', 'imagify' ), |
| 219 |
'Unable to regenerate access token' => __( 'Unable to regenerate access token.', 'imagify' ), |
| 220 |
'User not valid' => __( 'User not valid.', 'imagify' ), |
| 221 |
'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' ), |
| 222 |
'You are not authorized to perform this action' => __( 'You are not authorized to perform this action.', 'imagify' ), |
| 223 |
'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' ), |
| 224 |
'Invalid token' => __( 'Invalid API key', 'imagify' ), |
| 225 |
'Upload a valid image. The file you uploaded was either not an image or a corrupted image' => __( 'Invalid or corrupted file.', 'imagify' ), |
| 226 |
]; |
| 227 |
|
| 228 |
if ( isset( $messages[ $trim_message ] ) ) { |
| 229 |
return $messages[ $trim_message ]; |
| 230 |
} |
| 231 |
|
| 232 |
// Local message. |
| 233 |
if ( preg_match( '@^(?:Unknown|An) error occurred \((.+)\)$@', $trim_message, $matches ) ) { |
| 234 |
/* translators: %s is an error message. */ |
| 235 |
return sprintf( __( 'An error occurred (%s).', 'imagify' ), esc_html( wp_strip_all_tags( $matches[1] ) ) ); |
| 236 |
} |
| 237 |
|
| 238 |
// Local message. |
| 239 |
if ( preg_match( '@^Our server returned an error \((.+)\)$@', $trim_message, $matches ) ) { |
| 240 |
/* translators: %s is an error message. */ |
| 241 |
return sprintf( __( 'Our server returned an error (%s).', 'imagify' ), esc_html( wp_strip_all_tags( $matches[1] ) ) ); |
| 242 |
} |
| 243 |
|
| 244 |
// API message. |
| 245 |
if ( preg_match( '@^Custom one time plan starts from (\d+) MB$@', $trim_message, $matches ) ) { |
| 246 |
/* translators: %s is a formatted number, dont use %d. */ |
| 247 |
return sprintf( __( 'Custom One Time plan starts from %s MB.', 'imagify' ), number_format_i18n( (int) $matches[1] ) ); |
| 248 |
} |
| 249 |
|
| 250 |
// API message. |
| 251 |
if ( preg_match( '@^(.*) is not a valid extension$@', $trim_message, $matches ) ) { |
| 252 |
/* translators: %s is a file extension. */ |
| 253 |
return sprintf( __( '%s is not a valid extension.', 'imagify' ), sanitize_text_field( $matches[1] ) ); |
| 254 |
} |
| 255 |
|
| 256 |
// API message. |
| 257 |
if ( preg_match( '@^Request was throttled\. Expected available in ([\d.]+) second$@', $trim_message, $matches ) ) { |
| 258 |
/* translators: %s is a float number. */ |
| 259 |
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] ) ); |
| 260 |
} |
| 261 |
|
| 262 |
return $message; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Runs the bulk optimization |
| 267 |
* |
| 268 |
* @param array $contexts An array of contexts (WP/Custom folders). |
| 269 |
* @param int $optimization_level Optimization level to use. |
| 270 |
* |
| 271 |
* @return void |
| 272 |
*/ |
| 273 |
function imagify_bulk_optimize( $contexts, $optimization_level ) { |
| 274 |
foreach ( $contexts as $context ) { |
| 275 |
Imagify\Bulk\Bulk::get_instance()->run_optimize( $context, $optimization_level ); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Runs the next-gen generation |
| 281 |
* |
| 282 |
* @param array $contexts An array of contexts (WP/Custom folders). |
| 283 |
* |
| 284 |
* @return void |
| 285 |
*/ |
| 286 |
function imagify_generate_nextgen( $contexts ) { |
| 287 |
Imagify\Bulk\Bulk::get_instance()->run_generate_nextgen( $contexts ); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Add command to WP CLI |
| 292 |
* |
| 293 |
* @param CommandInterface $command Command object. |
| 294 |
* |
| 295 |
* @return void |
| 296 |
*/ |
| 297 |
function imagify_add_command( CommandInterface $command ) { |
| 298 |
if ( ! defined( 'WP_CLI' ) || ! WP_CLI || ! class_exists( '\WP_CLI' ) ) { |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
\WP_CLI::add_command( $command->get_name(), $command, [ |
| 303 |
'shortdesc' => $command->get_description(), |
| 304 |
'synopsis' => $command->get_synopsis(), |
| 305 |
] ); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Checks if the API key is valid |
| 310 |
* |
| 311 |
* @return bool |
| 312 |
*/ |
| 313 |
function imagify_is_api_key_valid() { |
| 314 |
return Imagify_Requirements::is_api_key_valid(); |
| 315 |
} |
| 316 |
|