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