| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The class defines way of connecting this user to the Optimole Dashboard. |
| 5 |
* |
| 6 |
* @codeCoverageIgnore |
| 7 |
* @package \Optimole\Inc |
| 8 |
* @author Optimole <friends@optimole.com> |
| 9 |
*/ |
| 10 |
final class Optml_Api { |
| 11 |
|
| 12 |
/** |
| 13 |
* Optimole root api url. |
| 14 |
* |
| 15 |
* @var string Api root. |
| 16 |
*/ |
| 17 |
private $api_root = 'https://dashboard.optimole.com/api/'; |
| 18 |
/** |
| 19 |
* Optimole upload api root url. |
| 20 |
* |
| 21 |
* @var string Api root. |
| 22 |
*/ |
| 23 |
private $upload_api_root = 'https://generateurls-prod.i.optimole.com/upload'; |
| 24 |
/** |
| 25 |
* Hold the user api key. |
| 26 |
* |
| 27 |
* @var string Api key. |
| 28 |
*/ |
| 29 |
private $api_key; |
| 30 |
/** |
| 31 |
* Optml_Api constructor. |
| 32 |
*/ |
| 33 |
public function __construct() { |
| 34 |
$settings = new Optml_Settings(); |
| 35 |
$this->api_key = $settings->get( 'api_key' ); |
| 36 |
if ( defined( 'OPTIML_API_ROOT' ) && constant( 'OPTIML_API_ROOT' ) ) { |
| 37 |
$this->api_root = constant( 'OPTIML_API_ROOT' ); |
| 38 |
} |
| 39 |
if ( defined( 'OPTIML_UPLOAD_API_ROOT' ) && constant( 'OPTIML_UPLOAD_API_ROOT' ) ) { |
| 40 |
$this->upload_api_root = constant( 'OPTIML_UPLOAD_API_ROOT' ); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Connect to the service. |
| 46 |
* |
| 47 |
* @param string $api_key Api key. |
| 48 |
* |
| 49 |
* @return array|bool |
| 50 |
*/ |
| 51 |
public function connect( $api_key = '' ) { |
| 52 |
if ( ! empty( $api_key ) ) { |
| 53 |
$this->api_key = $api_key; |
| 54 |
} |
| 55 |
|
| 56 |
return $this->request( '/optml/v2/account/connect', 'POST' ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get user data from service. |
| 61 |
* |
| 62 |
* @return array|bool User data. |
| 63 |
*/ |
| 64 |
public function get_user_data( $api_key = '', $application = '' ) { |
| 65 |
if ( ! empty( $api_key ) ) { |
| 66 |
$this->api_key = $api_key; |
| 67 |
} |
| 68 |
|
| 69 |
return $this->request( '/optml/v2/account/details', 'POST', [ 'application' => $application ] ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get cache token from service. |
| 74 |
* |
| 75 |
* @return array|bool User data. |
| 76 |
*/ |
| 77 |
public function get_cache_token( $token = '', $api_key = '' ) { |
| 78 |
if ( ! empty( $api_key ) ) { |
| 79 |
$this->api_key = $api_key; |
| 80 |
} |
| 81 |
$lock = get_transient( 'optml_cache_lock' ); |
| 82 |
if ( $lock === 'yes' ) { |
| 83 |
return new WP_Error( 'cache_throttle', __( 'You can clear cache only once per 5 minutes.', 'optimole-wp' ) ); |
| 84 |
} |
| 85 |
return $this->request( '/optml/v1/cache/tokens', 'POST', [ 'token' => $token ] ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Request constructor. |
| 90 |
* |
| 91 |
* @param string $path The request url. |
| 92 |
* @param string $method The request method type. |
| 93 |
* @param array $params The request method type. |
| 94 |
* |
| 95 |
* @return array|boolean Api data. |
| 96 |
*/ |
| 97 |
private function request( $path, $method = 'GET', $params = [], $extra_headers = [] ) { |
| 98 |
|
| 99 |
$headers = [ |
| 100 |
'Optml-Site' => get_home_url(), |
| 101 |
]; |
| 102 |
if ( ! empty( $this->api_key ) ) { |
| 103 |
$headers['Authorization'] = 'Bearer ' . $this->api_key; |
| 104 |
} |
| 105 |
if ( ! empty( $headers ) && is_array( $headers ) ) { |
| 106 |
$headers = array_merge( $headers, $extra_headers ); |
| 107 |
} |
| 108 |
$url = trailingslashit( $this->api_root ) . ltrim( $path, '/' ); |
| 109 |
// If there is a extra, add that as a url var. |
| 110 |
if ( 'GET' === $method && ! empty( $params ) ) { |
| 111 |
foreach ( $params as $key => $val ) { |
| 112 |
$url = add_query_arg( [ $key => $val ], $url ); |
| 113 |
} |
| 114 |
} |
| 115 |
$args = $this->build_args( $method, $url, $headers, $params ); |
| 116 |
|
| 117 |
$response = wp_remote_request( $url, $args ); |
| 118 |
|
| 119 |
if ( is_wp_error( $response ) ) { |
| 120 |
return $response; |
| 121 |
} |
| 122 |
|
| 123 |
$response = wp_remote_retrieve_body( $response ); |
| 124 |
|
| 125 |
if ( empty( $response ) ) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
$response = json_decode( $response, true ); |
| 129 |
|
| 130 |
if ( isset( $response['id'] ) && is_numeric( $response['id'] ) ) { |
| 131 |
return true; |
| 132 |
} |
| 133 |
if ( ! isset( $response['code'] ) ) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
if ( intval( $response['code'] ) !== 200 ) { |
| 137 |
if ( $path === 'optml/v1/user/register-remote' |
| 138 |
&& isset( $response['error'] ) |
| 139 |
&& $response['error'] === 'ERROR: This email is already registered, please choose another one.' ) { |
| 140 |
return 'email_registered'; |
| 141 |
} |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
return $response['data']; |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Builds Request arguments array. |
| 151 |
* |
| 152 |
* @param string $method Request method (GET | POST | PUT | UPDATE | DELETE). |
| 153 |
* @param string $url Request URL. |
| 154 |
* @param array $headers Headers Array. |
| 155 |
* @param array|string $params Additional params for the Request. |
| 156 |
* |
| 157 |
* @return array |
| 158 |
*/ |
| 159 |
private function build_args( $method, $url, $headers, $params ) { |
| 160 |
$args = [ |
| 161 |
'method' => $method, |
| 162 |
'timeout' => 45, |
| 163 |
'user-agent' => 'Optimle WP (v' . OPTML_VERSION . ') ', |
| 164 |
'sslverify' => false, |
| 165 |
'headers' => $headers, |
| 166 |
]; |
| 167 |
if ( $method !== 'GET' ) { |
| 168 |
$args['body'] = $params; |
| 169 |
} |
| 170 |
|
| 171 |
return $args; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Upload image to our servers using the generated signed url. |
| 176 |
* |
| 177 |
* @param string $upload_url The signed to url to upload the image to. |
| 178 |
* @param string $content_type Image mime type, it must match the actual mime type of the image. |
| 179 |
* @param string $image Image data from file_get_contents. |
| 180 |
* @return mixed |
| 181 |
*/ |
| 182 |
public function upload_image( $upload_url, $content_type, $image ) { |
| 183 |
$args = $this->build_args( 'PUT', '', ['content-type' => $content_type], $image ); |
| 184 |
return wp_remote_request( $upload_url, $args ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Check if the optimized url is available. |
| 189 |
* |
| 190 |
* @param string $url The optimized url to check. |
| 191 |
* @return bool Whether or not the url is valid. |
| 192 |
*/ |
| 193 |
public function check_optimized_url( $url ) { |
| 194 |
$response = wp_remote_get( $url, ['timeout' => 30] ); |
| 195 |
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
return true; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get options for the signed urls api call. |
| 203 |
* |
| 204 |
* @param string $original_url Image original url. |
| 205 |
* @param string $delete Whether to delete a bucket object or not(ie. generate signed upload url). |
| 206 |
* @param string $table_id Remote id used on our servers. |
| 207 |
* @param string $update_table False or success. |
| 208 |
* @param string $get_url Whether to return a get url or not. |
| 209 |
* @param string $width Original image width. |
| 210 |
* @param string $height Original image height. |
| 211 |
* @param int $file_size Original file size. |
| 212 |
* @return array |
| 213 |
*/ |
| 214 |
public function call_upload_api( $original_url = '', $delete = 'false', $table_id = '', $update_table = 'false', $get_url = 'false', $width = 'auto', $height = 'auto', $file_size = 0 ) { |
| 215 |
$body = [ |
| 216 |
'secret' => Optml_Config::$secret, |
| 217 |
'userKey' => Optml_Config::$key, |
| 218 |
'originalUrl' => $original_url, |
| 219 |
'deleteUrl' => $delete, |
| 220 |
'id' => $table_id, |
| 221 |
'updateDynamo' => $update_table, |
| 222 |
'getUrl' => $get_url, |
| 223 |
'width' => $width, |
| 224 |
'height' => $height, |
| 225 |
'originalFileSize' => $file_size, |
| 226 |
]; |
| 227 |
$body = wp_json_encode( $body ); |
| 228 |
|
| 229 |
$options = [ |
| 230 |
'body' => $body, |
| 231 |
'headers' => [ |
| 232 |
'Content-Type' => 'application/json', |
| 233 |
], |
| 234 |
'timeout' => 60, |
| 235 |
'blocking' => true, |
| 236 |
'sslverify' => false, |
| 237 |
'data_format' => 'body', |
| 238 |
]; |
| 239 |
return wp_remote_post( $this->upload_api_root, $options ); |
| 240 |
} |
| 241 |
/** |
| 242 |
* Register user remotely on optimole.com. |
| 243 |
* |
| 244 |
* @param string $email User email. |
| 245 |
* |
| 246 |
* @return array|bool Api response. |
| 247 |
*/ |
| 248 |
public function create_account( $email ) { |
| 249 |
return $this->request( |
| 250 |
'optml/v1/user/register-remote', |
| 251 |
'POST', |
| 252 |
[ |
| 253 |
'email' => $email, |
| 254 |
'version' => OPTML_VERSION, |
| 255 |
'site' => get_home_url(), |
| 256 |
] |
| 257 |
); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Get the optimized images from API. |
| 262 |
* |
| 263 |
* @param string $api_key the api key. |
| 264 |
* |
| 265 |
* @return array|bool |
| 266 |
*/ |
| 267 |
public function get_optimized_images( $api_key = '' ) { |
| 268 |
if ( ! empty( $api_key ) ) { |
| 269 |
$this->api_key = $api_key; |
| 270 |
} |
| 271 |
|
| 272 |
return $this->request( '/optml/v1/stats/images' ); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Get the watermarks from API. |
| 277 |
* |
| 278 |
* @param string $api_key The API key. |
| 279 |
* |
| 280 |
* @return array|bool |
| 281 |
*/ |
| 282 |
public function get_watermarks( $api_key = '' ) { |
| 283 |
if ( ! empty( $api_key ) ) { |
| 284 |
$this->api_key = $api_key; |
| 285 |
} |
| 286 |
|
| 287 |
return $this->request( '/optml/v1/settings/watermark' ); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Remove the watermark from the API. |
| 292 |
* |
| 293 |
* @param integer $post_id The watermark post ID. |
| 294 |
* @param string $api_key The API key. |
| 295 |
* |
| 296 |
* @return array|bool |
| 297 |
*/ |
| 298 |
public function remove_watermark( $post_id, $api_key = '' ) { |
| 299 |
if ( ! empty( $api_key ) ) { |
| 300 |
$this->api_key = $api_key; |
| 301 |
} |
| 302 |
|
| 303 |
return $this->request( '/optml/v1/settings/watermark', 'DELETE', [ 'watermark' => $post_id ] ); |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Add watermark. |
| 308 |
* |
| 309 |
* @param array $file The file to be uploaded. |
| 310 |
* |
| 311 |
* @return array|bool|mixed|object |
| 312 |
*/ |
| 313 |
public function add_watermark( $file ) { |
| 314 |
|
| 315 |
$headers = [ |
| 316 |
'Content-Disposition' => 'attachment; filename=' . $file['file']['name'], |
| 317 |
]; |
| 318 |
|
| 319 |
$response = $this->request( 'wp/v2/media', 'POST', file_get_contents( $file['file']['tmp_name'] ), $headers ); |
| 320 |
|
| 321 |
if ( $response === false ) { |
| 322 |
return false; |
| 323 |
} |
| 324 |
|
| 325 |
return $response; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Call the images endpoint. |
| 330 |
* |
| 331 |
* @param integer $page Page used to advance the search. |
| 332 |
* @param array $domains Domains to filter by. |
| 333 |
* @param string $search The string to search inside the originURL. |
| 334 |
* @return mixed The decoded json response from the api. |
| 335 |
*/ |
| 336 |
public function get_cloud_images( $page = 0, $domains = [], $search = '' ) { |
| 337 |
|
| 338 |
$params = ['key' => Optml_Config::$key ]; |
| 339 |
$params['page'] = $page; |
| 340 |
$params['size'] = 40; |
| 341 |
if ( $search !== '' ) { |
| 342 |
$params['search'] = $search; |
| 343 |
} |
| 344 |
|
| 345 |
if ( ! empty( $domains ) ) { |
| 346 |
$params['domains'] = implode( ',', $domains ); |
| 347 |
} |
| 348 |
return $this->request( 'optml/v2/media/browser', 'GET', $params ); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Throw error on object clone |
| 353 |
* |
| 354 |
* The whole idea of the singleton design pattern is that there is a single |
| 355 |
* object therefore, we don't want the object to be cloned. |
| 356 |
* |
| 357 |
* @access public |
| 358 |
* @return void |
| 359 |
* @since 1.0.0 |
| 360 |
*/ |
| 361 |
public function __clone() { |
| 362 |
// Cloning instances of the class is forbidden. |
| 363 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'optimole-wp' ), '1.0.0' ); |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Disable unserializing of the class |
| 368 |
* |
| 369 |
* @access public |
| 370 |
* @return void |
| 371 |
* @since 1.0.0 |
| 372 |
*/ |
| 373 |
public function __wakeup() { |
| 374 |
// Unserializing instances of the class is forbidden. |
| 375 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'optimole-wp' ), '1.0.0' ); |
| 376 |
} |
| 377 |
} |
| 378 |
|