| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Create a new user on Imagify. |
| 5 |
* |
| 6 |
* @param array $data All user data. |
| 7 |
* @return object |
| 8 |
**/ |
| 9 |
function add_imagify_user( $data ) { |
| 10 |
return Imagify()->createUser( $data ); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Update your Imagify account. |
| 15 |
* |
| 16 |
* @param string $data All user data. |
| 17 |
* @return object |
| 18 |
**/ |
| 19 |
function update_imagify_user( $data ) { |
| 20 |
return Imagify()->updateUser( $data ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Get your Imagify account infos. |
| 25 |
* |
| 26 |
* @return object |
| 27 |
**/ |
| 28 |
function get_imagify_user() { |
| 29 |
return Imagify()->getUser(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get the Imagify API version. |
| 34 |
* |
| 35 |
* @return object |
| 36 |
**/ |
| 37 |
function get_imagify_api_version() { |
| 38 |
return Imagify()->getApiVersion(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Check your Imagify API key status. |
| 43 |
* |
| 44 |
* @return bool |
| 45 |
**/ |
| 46 |
function get_imagify_status( $data ) { |
| 47 |
return Imagify()->getStatus( $data ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Optimize an image by uploading it on Imagify. |
| 52 |
* |
| 53 |
* @param array $data All image data. |
| 54 |
* @return object |
| 55 |
**/ |
| 56 |
function fetch_imagify_image( $data ) { |
| 57 |
return Imagify()->fetchImage( $data ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Optimize an image by sharing its URL on Imagify. |
| 62 |
* |
| 63 |
* @param array $data All image data. |
| 64 |
* @return object |
| 65 |
**/ |
| 66 |
function upload_imagify_image( $data ) { |
| 67 |
return Imagify()->uploadImage( $data ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Imagify.io API for WordPress |
| 72 |
*/ |
| 73 |
class Imagify { |
| 74 |
/** |
| 75 |
* The Imagify API endpoint |
| 76 |
*/ |
| 77 |
const API_ENDPOINT = 'https://app.imagify.io/api/'; |
| 78 |
|
| 79 |
/** |
| 80 |
* The Imagify API key |
| 81 |
*/ |
| 82 |
private $apiKey = ''; |
| 83 |
|
| 84 |
/** |
| 85 |
* HTTP headers |
| 86 |
*/ |
| 87 |
private $headers = array(); |
| 88 |
|
| 89 |
/** |
| 90 |
* @var The single instance of the class |
| 91 |
*/ |
| 92 |
protected static $_instance = null; |
| 93 |
|
| 94 |
/** |
| 95 |
* The constructor |
| 96 |
* |
| 97 |
* @return void |
| 98 |
**/ |
| 99 |
public function __construct() |
| 100 |
{ |
| 101 |
// check if the WordPress plugin is activated and the API key is stored in the options |
| 102 |
if ( defined( 'IMAGIFY_VERSION' ) && function_exists( 'get_imagify_option' ) ) { |
| 103 |
$apiKey = get_imagify_option( 'api_key', false ); |
| 104 |
$this->apiKey = ( $apiKey ) ? $apiKey : $this->apiKey; |
| 105 |
} |
| 106 |
|
| 107 |
// check if the API key is defined with the PHP constant (it's ovveride the WordPress plugin option |
| 108 |
if ( defined( 'IMAGIFY_API_KEY' ) && IMAGIFY_API_KEY ) { |
| 109 |
$this->apiKey = IMAGIFY_API_KEY; |
| 110 |
} |
| 111 |
|
| 112 |
$this->headers['Accept'] = 'Accept: application/json'; |
| 113 |
$this->headers['Content-Type'] = 'Content-Type: application/json'; |
| 114 |
$this->headers['Authorization'] = 'Authorization: token ' . $this->apiKey; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Main Imagify Instance |
| 119 |
* |
| 120 |
* Ensures only one instance of class is loaded or can be loaded. |
| 121 |
* |
| 122 |
* @static |
| 123 |
* @return Main instance |
| 124 |
*/ |
| 125 |
public static function instance() { |
| 126 |
if ( is_null( self::$_instance ) ) { |
| 127 |
self::$_instance = new self(); |
| 128 |
} |
| 129 |
|
| 130 |
return self::$_instance; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Create a user on your Imagify account. |
| 135 |
* |
| 136 |
* @param array $data All user data. Details here: -- |
| 137 |
* @return object |
| 138 |
**/ |
| 139 |
public function createUser( $data ) { |
| 140 |
unset( $this->headers['Authorization'], $this->headers['Accept'], $this->headers['Content-Type'] ); |
| 141 |
|
| 142 |
$data['from_plugin'] = true; |
| 143 |
$args = array( |
| 144 |
'method' => 'POST', |
| 145 |
'post_data' => $data |
| 146 |
); |
| 147 |
|
| 148 |
return $this->httpCall( 'users/', $args ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get your Imagify account infos. |
| 153 |
* |
| 154 |
* @return object |
| 155 |
**/ |
| 156 |
public function getUser() { |
| 157 |
static $user; |
| 158 |
|
| 159 |
if ( ! isset( $user ) ) { |
| 160 |
$user = $this->httpCall( 'users/me/', array( 'timeout' => 10 ) ); |
| 161 |
} |
| 162 |
|
| 163 |
return $user; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Check your Imagify API key status. |
| 168 |
* |
| 169 |
* @return object |
| 170 |
**/ |
| 171 |
public function getStatus( $data ) { |
| 172 |
static $status; |
| 173 |
|
| 174 |
if ( ! isset( $status ) ) { |
| 175 |
unset( $this->headers['Accept'], $this->headers['Content-Type'] ); |
| 176 |
$this->headers['Authorization'] = 'Authorization: token ' . $data; |
| 177 |
|
| 178 |
$status = $this->httpCall( 'status/', array( 'timeout' => 10 ) ); |
| 179 |
} |
| 180 |
|
| 181 |
return $status; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Get the Imagify API version. |
| 186 |
* |
| 187 |
* @return object |
| 188 |
**/ |
| 189 |
public function getApiVersion() { |
| 190 |
static $api_version; |
| 191 |
|
| 192 |
if ( ! isset( $api_version ) ) { |
| 193 |
unset( $this->headers['Accept'], $this->headers['Content-Type'] ); |
| 194 |
|
| 195 |
$api_version = $this->httpCall( 'version/', array( 'timeout' => 5 ) ); |
| 196 |
} |
| 197 |
|
| 198 |
return $api_version; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Update an existing user on your Imagify account. |
| 203 |
* |
| 204 |
* @param string $data All user data. Details here: -- |
| 205 |
* @return object |
| 206 |
**/ |
| 207 |
public function updateUser( $data ) { |
| 208 |
$args = array( |
| 209 |
'method' => 'PUT', |
| 210 |
'post_data' => $data, |
| 211 |
'timeout' => 10 |
| 212 |
); |
| 213 |
|
| 214 |
return $this->httpCall( 'users/me/', $args ); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Optimize an image from its binary content. |
| 219 |
* |
| 220 |
* @param string $data All options. Details here: -- |
| 221 |
* @return object |
| 222 |
**/ |
| 223 |
public function uploadImage( $data ) { |
| 224 |
if ( isset( $this->headers['Accept'], $this->headers['Content-Type'] ) ) { |
| 225 |
unset( $this->headers['Accept'], $this->headers['Content-Type'] ); |
| 226 |
} |
| 227 |
|
| 228 |
$args = array( |
| 229 |
'method' => 'POST', |
| 230 |
'post_data' => $data |
| 231 |
); |
| 232 |
|
| 233 |
return $this->httpCall( 'upload/', $args ); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Optimize an image from its URL. |
| 238 |
* |
| 239 |
* @param string $data All options. Details here: -- |
| 240 |
* @return object |
| 241 |
**/ |
| 242 |
public function fetchImage( $data ) { |
| 243 |
$args = array( |
| 244 |
'method' => 'POST', |
| 245 |
'post_data' => json_encode( $data ) |
| 246 |
); |
| 247 |
return $this->httpCall( 'fetch/', $args ); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Make an HTTP call using curl. |
| 252 |
* |
| 253 |
* @param string $url The URL to call |
| 254 |
* @param array $args The request args |
| 255 |
* @return object |
| 256 |
**/ |
| 257 |
private function httpCall( $url, $args = array() ) { |
| 258 |
$default = array( |
| 259 |
'method' => 'GET', |
| 260 |
'post_data' => null, |
| 261 |
'timeout' => 45 |
| 262 |
); |
| 263 |
$args = array_merge( $default, $args ); |
| 264 |
|
| 265 |
// Check if php-curl is enabled |
| 266 |
if ( ! function_exists( 'curl_init' ) || ! function_exists( 'curl_exec' ) ) { |
| 267 |
return new WP_Error( 'curl', 'cURL isn\'t installed on the server.' ); |
| 268 |
} |
| 269 |
|
| 270 |
try { |
| 271 |
$ch = curl_init(); |
| 272 |
$is_ssl = ( isset( $_SERVER['HTTPS'] ) && ( 'on' == strtolower( $_SERVER['HTTPS'] ) || '1' == $_SERVER['HTTPS'] ) ) || ( isset( $_SERVER['SERVER_PORT'] ) && ( '443' == $_SERVER['SERVER_PORT'] ) ); |
| 273 |
|
| 274 |
if ( 'POST' == $args['method'] ) { |
| 275 |
curl_setopt( $ch, CURLOPT_POST, true ); |
| 276 |
curl_setopt( $ch, CURLOPT_POSTFIELDS, $args['post_data'] ); |
| 277 |
} |
| 278 |
|
| 279 |
curl_setopt( $ch, CURLOPT_URL, self::API_ENDPOINT . $url ); |
| 280 |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); |
| 281 |
curl_setopt( $ch, CURLOPT_HTTPHEADER, $this->headers ); |
| 282 |
curl_setopt( $ch, CURLOPT_TIMEOUT, $args['timeout'] ); |
| 283 |
@curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); |
| 284 |
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, $is_ssl ); |
| 285 |
|
| 286 |
$response = json_decode( curl_exec( $ch ) ); |
| 287 |
$error = curl_error( $ch ); |
| 288 |
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); |
| 289 |
|
| 290 |
curl_close( $ch ); |
| 291 |
} catch( Exception $e ) { |
| 292 |
return new WP_Error( 'curl', 'Unknown error occurred' ); |
| 293 |
} |
| 294 |
|
| 295 |
if ( 200 != $http_code && isset( $response->code, $response->detail ) ) { |
| 296 |
return new WP_Error( $http_code, $response->detail ); |
| 297 |
} elseif ( 200 != $http_code ) { |
| 298 |
return new WP_Error( $http_code, 'Unknown error occurred' ); |
| 299 |
} else { |
| 300 |
return $response; |
| 301 |
} |
| 302 |
|
| 303 |
return $response; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Returns the main instance of Imagify to prevent the need to use globals. |
| 309 |
*/ |
| 310 |
function Imagify() { |
| 311 |
return Imagify::instance(); |
| 312 |
} |
| 313 |
$GLOBALS['imagify'] = Imagify(); |