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