| 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 |
* Translate a message from our servers. |
| 172 |
* |
| 173 |
* @since 1.6.10 |
| 174 |
* @author Grégory Viguier |
| 175 |
* @see Imagify::curl_http_call() |
| 176 |
* @see Imagify::handle_response() |
| 177 |
* |
| 178 |
* @param string $message The message from the server (in English). |
| 179 |
* @return string If in our list, the translated message. The original message otherwise. |
| 180 |
*/ |
| 181 |
function imagify_translate_api_message( $message ) { |
| 182 |
if ( ! $message ) { |
| 183 |
return imagify_translate_api_message( 'Unknown error occurred' ); |
| 184 |
} |
| 185 |
|
| 186 |
if ( is_wp_error( $message ) ) { |
| 187 |
if ( $message->errors ) { |
| 188 |
foreach ( (array) $message->errors as $code => $messages ) { |
| 189 |
if ( $messages ) { |
| 190 |
$message->errors[ $code ] = array_map( 'imagify_translate_api_message', (array) $messages ); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return $message; |
| 196 |
} |
| 197 |
|
| 198 |
if ( is_object( $message ) && ! empty( $message->detail ) ) { |
| 199 |
$message->detail = imagify_translate_api_message( $message->detail ); |
| 200 |
} |
| 201 |
|
| 202 |
if ( ! is_string( $message ) ) { |
| 203 |
return $message; |
| 204 |
} |
| 205 |
|
| 206 |
$trim_message = trim( $message, '. ' ); |
| 207 |
|
| 208 |
$messages = array( |
| 209 |
// 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' ), |
| 215 |
// 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' ), |
| 235 |
'Upload a valid image. The file you uploaded was either not an image or a corrupted image' => __( 'Invalid or corrupted file.', 'imagify' ), |
| 236 |
); |
| 237 |
|
| 238 |
if ( isset( $messages[ $trim_message ] ) ) { |
| 239 |
return $messages[ $trim_message ]; |
| 240 |
} |
| 241 |
|
| 242 |
// Local message. |
| 243 |
if ( preg_match( '@^(?:Unknown|An) error occurred \((.+)\)$@', $trim_message, $matches ) ) { |
| 244 |
/* translators: %s is an error message. */ |
| 245 |
return sprintf( __( 'An error occurred (%s).', 'imagify' ), esc_html( wp_strip_all_tags( $matches[1] ) ) ); |
| 246 |
} |
| 247 |
|
| 248 |
// Local message. |
| 249 |
if ( preg_match( '@^Our server returned an error \((.+)\)$@', $trim_message, $matches ) ) { |
| 250 |
/* translators: %s is an error message. */ |
| 251 |
return sprintf( __( 'Our server returned an error (%s).', 'imagify' ), esc_html( wp_strip_all_tags( $matches[1] ) ) ); |
| 252 |
} |
| 253 |
|
| 254 |
// API message. |
| 255 |
if ( preg_match( '@^Custom one time plan starts from (\d+) MB$@', $trim_message, $matches ) ) { |
| 256 |
/* translators: %s is a formatted number, dont use %d. */ |
| 257 |
return sprintf( __( 'Custom One Time plan starts from %s MB.', 'imagify' ), number_format_i18n( (int) $matches[1] ) ); |
| 258 |
} |
| 259 |
|
| 260 |
// API message. |
| 261 |
if ( preg_match( '@^(.*) is not a valid extension$@', $trim_message, $matches ) ) { |
| 262 |
/* translators: %s is a file extension. */ |
| 263 |
return sprintf( __( '%s is not a valid extension.', 'imagify' ), sanitize_text_field( $matches[1] ) ); |
| 264 |
} |
| 265 |
|
| 266 |
// API message. |
| 267 |
if ( preg_match( '@^Request was throttled\. Expected available in ([\d.]+) second$@', $trim_message, $matches ) ) { |
| 268 |
/* translators: %s is a float number. */ |
| 269 |
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 |
} |
| 271 |
|
| 272 |
return $message; |
| 273 |
} |
| 274 |
|