| 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 |
* Hold the user api key. |
| 20 |
* |
| 21 |
* @var string Api key. |
| 22 |
*/ |
| 23 |
private $api_key; |
| 24 |
|
| 25 |
/** |
| 26 |
* Optml_Api constructor. |
| 27 |
*/ |
| 28 |
public function __construct() { |
| 29 |
$settings = new Optml_Settings(); |
| 30 |
$this->api_key = $settings->get( 'api_key' ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get user data from service. |
| 35 |
* |
| 36 |
* @return array|bool User data. |
| 37 |
*/ |
| 38 |
public function get_user_data( $api_key = '' ) { |
| 39 |
if ( ! empty( $api_key ) ) { |
| 40 |
$this->api_key = $api_key; |
| 41 |
} |
| 42 |
|
| 43 |
return $this->request( '/optml/v1/image/details', 'POST' ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get cache token from service. |
| 48 |
* |
| 49 |
* @return array|bool User data. |
| 50 |
*/ |
| 51 |
public function get_cache_token( $token = '', $api_key = '' ) { |
| 52 |
if ( ! empty( $api_key ) ) { |
| 53 |
$this->api_key = $api_key; |
| 54 |
} |
| 55 |
$lock = get_transient( 'optml_cache_lock' ); |
| 56 |
if ( $lock === 'yes' ) { |
| 57 |
return new WP_Error( 'cache_throttle', __( 'You can clear cache only once per 5 minutes.', 'optimole-wp' ) ); |
| 58 |
} |
| 59 |
return $this->request( '/optml/v1/cache/tokens', 'POST', array( 'token' => $token ) ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Request constructor. |
| 64 |
* |
| 65 |
* @param string $path The request url. |
| 66 |
* @param string $method The request method type. |
| 67 |
* @param array $params The request method type. |
| 68 |
* |
| 69 |
* @return array|boolean Api data. |
| 70 |
*/ |
| 71 |
private function request( $path, $method = 'GET', $params = array(), $extra_headers = [] ) { |
| 72 |
|
| 73 |
// Grab the url to which we'll be making the request. |
| 74 |
$url = $this->api_root; |
| 75 |
$headers = array( |
| 76 |
'Optml-Site' => get_home_url(), |
| 77 |
); |
| 78 |
if ( ! empty( $this->api_key ) ) { |
| 79 |
$headers['Authorization'] = 'Bearer ' . $this->api_key; |
| 80 |
} |
| 81 |
if ( ! empty( $headers ) && is_array( $headers ) ) { |
| 82 |
$headers = array_merge( $headers, $extra_headers ); |
| 83 |
} |
| 84 |
|
| 85 |
// If there is a extra, add that as a url var. |
| 86 |
if ( 'GET' === $method && ! empty( $params ) ) { |
| 87 |
foreach ( $params as $key => $val ) { |
| 88 |
$url = add_query_arg( array( $key => $val ), $url ); |
| 89 |
} |
| 90 |
} |
| 91 |
$url = trailingslashit( $this->api_root ) . ltrim( $path, '/' ); |
| 92 |
$args = $this->build_args( $method, $url, $headers, $params ); |
| 93 |
|
| 94 |
$response = wp_remote_request( $url, $args ); |
| 95 |
|
| 96 |
if ( is_wp_error( $response ) ) { |
| 97 |
return $response; |
| 98 |
} |
| 99 |
|
| 100 |
$response = wp_remote_retrieve_body( $response ); |
| 101 |
|
| 102 |
if ( empty( $response ) ) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
$response = json_decode( $response, true ); |
| 106 |
|
| 107 |
if ( isset( $response['id'] ) && is_numeric( $response['id'] ) ) { |
| 108 |
return true; |
| 109 |
} |
| 110 |
if ( ! isset( $response['code'] ) ) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
if ( intval( $response['code'] ) !== 200 ) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
return $response['data']; |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Builds Request arguments array. |
| 123 |
* |
| 124 |
* @param string $method Request method (GET | POST | PUT | UPDATE | DELETE). |
| 125 |
* @param string $url Request URL. |
| 126 |
* @param array $headers Headers Array. |
| 127 |
* @param array $params Additional params for the Request. |
| 128 |
* |
| 129 |
* @return array |
| 130 |
*/ |
| 131 |
private function build_args( $method, $url, $headers, $params ) { |
| 132 |
$args = array( |
| 133 |
'method' => $method, |
| 134 |
'timeout' => 45, |
| 135 |
'user-agent' => 'Optimle WP (v' . OPTML_VERSION . ') ', |
| 136 |
'sslverify' => false, |
| 137 |
'headers' => $headers, |
| 138 |
); |
| 139 |
if ( $method !== 'GET' ) { |
| 140 |
$args['body'] = $params; |
| 141 |
} |
| 142 |
|
| 143 |
return $args; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Register user remotely on optimole.com. |
| 148 |
* |
| 149 |
* @param string $email User email. |
| 150 |
* |
| 151 |
* @return array|bool Api response. |
| 152 |
*/ |
| 153 |
public function create_account( $email ) { |
| 154 |
return $this->request( |
| 155 |
'optml/v1/user/register-remote', |
| 156 |
'POST', |
| 157 |
array( |
| 158 |
'email' => $email, |
| 159 |
'version' => OPTML_VERSION, |
| 160 |
'site' => get_home_url(), |
| 161 |
) |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Get the optimized images from API. |
| 167 |
* |
| 168 |
* @param string $api_key the api key. |
| 169 |
* |
| 170 |
* @return array|bool |
| 171 |
*/ |
| 172 |
public function get_optimized_images( $api_key = '' ) { |
| 173 |
if ( ! empty( $api_key ) ) { |
| 174 |
$this->api_key = $api_key; |
| 175 |
} |
| 176 |
|
| 177 |
return $this->request( '/optml/v1/stats/images' ); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Get the watermarks from API. |
| 182 |
* |
| 183 |
* @param string $api_key The API key. |
| 184 |
* |
| 185 |
* @return array|bool |
| 186 |
*/ |
| 187 |
public function get_watermarks( $api_key = '' ) { |
| 188 |
if ( ! empty( $api_key ) ) { |
| 189 |
$this->api_key = $api_key; |
| 190 |
} |
| 191 |
|
| 192 |
return $this->request( '/optml/v1/settings/watermark' ); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Remove the watermark from the API. |
| 197 |
* |
| 198 |
* @param integer $post_id The watermark post ID. |
| 199 |
* @param string $api_key The API key. |
| 200 |
* |
| 201 |
* @return array|bool |
| 202 |
*/ |
| 203 |
public function remove_watermark( $post_id, $api_key = '' ) { |
| 204 |
if ( ! empty( $api_key ) ) { |
| 205 |
$this->api_key = $api_key; |
| 206 |
} |
| 207 |
|
| 208 |
return $this->request( '/optml/v1/settings/watermark', 'DELETE', array( 'watermark' => $post_id ) ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Add watermark. |
| 213 |
* |
| 214 |
* @param array $file The file to be uploaded. |
| 215 |
* |
| 216 |
* @return array|bool|mixed|object |
| 217 |
*/ |
| 218 |
public function add_watermark( $file ) { |
| 219 |
|
| 220 |
$headers = [ |
| 221 |
'Content-Disposition' => 'attachment; filename=' . $file['file']['name'], |
| 222 |
]; |
| 223 |
|
| 224 |
$response = $this->request( 'wp/v2/media', 'POST', file_get_contents( $file['file']['tmp_name'] ), $headers ); |
| 225 |
|
| 226 |
if ( $response === false ) { |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
return $response; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Throw error on object clone |
| 235 |
* |
| 236 |
* The whole idea of the singleton design pattern is that there is a single |
| 237 |
* object therefore, we don't want the object to be cloned. |
| 238 |
* |
| 239 |
* @access public |
| 240 |
* @return void |
| 241 |
* @since 1.0.0 |
| 242 |
*/ |
| 243 |
public function __clone() { |
| 244 |
// Cloning instances of the class is forbidden. |
| 245 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'optimole-wp' ), '1.0.0' ); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Disable unserializing of the class |
| 250 |
* |
| 251 |
* @access public |
| 252 |
* @return void |
| 253 |
* @since 1.0.0 |
| 254 |
*/ |
| 255 |
public function __wakeup() { |
| 256 |
// Unserializing instances of the class is forbidden. |
| 257 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'optimole-wp' ), '1.0.0' ); |
| 258 |
} |
| 259 |
} |
| 260 |
|