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 +29 -127 2.5.71.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,9 +12,9 @@
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 18 * Hold the user api key.
20 19 *
21 20 * @var string Api key.
@@ -39,28 +38,12 @@
39 38 if ( ! empty( $api_key ) ) {
40 39 $this->api_key = $api_key;
41 40 }
42 41
43 - return $this->request( '/optml/v1/image/details', 'POST' );
42 + return $this->request( '/image/details', 'POST' );
44 43 }
45 44
46 45 /**
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', [ 'token' => $token ] );
60 - }
61 -
62 - /**
63 46 * Request constructor.
64 47 *
65 48 * @param string $path The request url.
66 49 * @param string $method The request method type.
@@ -67,48 +50,50 @@
67 50 * @param array $params The request method type.
68 51 *
69 52 * @return array|boolean Api data.
70 53 */
71 - private function request( $path, $method = 'GET', $params = [], $extra_headers = [] ) {
54 + private function request( $path, $method = 'GET', $params = array() ) {
72 55
73 56 // Grab the url to which we'll be making the request.
74 57 $url = $this->api_root;
75 - $headers = [
76 - 'Optml-Site' => get_home_url(),
77 - ];
58 + $headers = array();
78 59 if ( ! empty( $this->api_key ) ) {
79 - $headers['Authorization'] = 'Bearer ' . $this->api_key;
60 + $headers = array(
61 + 'Authorization' => 'Bearer ' . $this->api_key,
62 + );
80 63 }
81 - if ( ! empty( $headers ) && is_array( $headers ) ) {
82 - $headers = array_merge( $headers, $extra_headers );
83 - }
84 -
85 64 // If there is a extra, add that as a url var.
86 65 if ( 'GET' === $method && ! empty( $params ) ) {
87 66 foreach ( $params as $key => $val ) {
88 - $url = add_query_arg( [ $key => $val ], $url );
67 + $url = add_query_arg( array( $key => $val ), $url );
89 68 }
90 69 }
91 70 $url = trailingslashit( $this->api_root ) . ltrim( $path, '/' );
92 - $args = $this->build_args( $method, $url, $headers, $params );
93 -
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 + }
94 82 $response = wp_remote_request( $url, $args );
95 83
96 84 if ( is_wp_error( $response ) ) {
97 - return $response;
85 + return false;
98 86 }
99 -
100 87 $response = wp_remote_retrieve_body( $response );
101 88
102 89 if ( empty( $response ) ) {
103 90 return false;
104 91 }
92 +
105 93 $response = json_decode( $response, true );
106 94
107 - if ( isset( $response['id'] ) && is_numeric( $response['id'] ) ) {
108 - return true;
109 - }
110 - if ( ! isset( $response['code'] ) ) {
95 + if ( ! $response['code'] ) {
111 96 return false;
112 97 }
113 98 if ( intval( $response['code'] ) !== 200 ) {
114 99 return false;
@@ -118,33 +103,8 @@
118 103
119 104 }
120 105
121 106 /**
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 = [
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 107 * Register user remotely on optimole.com.
148 108 *
149 109 * @param string $email User email.
150 110 *
@@ -150,17 +110,12 @@
150 110 *
151 111 * @return array|bool Api response.
152 112 */
153 113 public function create_account( $email ) {
154 - return $this->request(
155 - 'optml/v1/user/register-remote',
156 - 'POST',
157 - [
158 - 'email' => $email,
159 - 'version' => OPTML_VERSION,
160 - 'site' => get_home_url(),
161 - ]
162 - );
114 + return $this->request( 'user/register-remote', 'POST', array(
115 + 'email' => $email,
116 + 'site' => get_site_url()
117 + ) );
163 118 }
164 119
165 120 /**
166 121 * Get the optimized images from API.
@@ -173,65 +128,12 @@
173 128 if ( ! empty( $api_key ) ) {
174 129 $this->api_key = $api_key;
175 130 }
176 131
177 - return $this->request( '/optml/v1/stats/images' );
132 + return $this->request( '/stats/images' );
178 133 }
179 134
180 135 /**
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', [ '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 136 * Throw error on object clone
235 137 *
236 138 * The whole idea of the singleton design pattern is that there is a single
237 139 * object therefore, we don't want the object to be cloned.
@@ -236,10 +138,10 @@
236 138 * The whole idea of the singleton design pattern is that there is a single
237 139 * object therefore, we don't want the object to be cloned.
238 140 *
239 141 * @access public
142 + * @since 1.0.0
240 143 * @return void
241 - * @since 1.0.0
242 144 */
243 145 public function __clone() {
244 146 // Cloning instances of the class is forbidden.
245 147 _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; huh?', 'optimole-wp' ), '1.0.0' );
@@ -248,10 +150,10 @@
248 150 /**
249 151 * Disable unserializing of the class
250 152 *
251 153 * @access public
154 + * @since 1.0.0
252 155 * @return void
253 - * @since 1.0.0
254 156 */
255 157 public function __wakeup() {
256 158 // Unserializing instances of the class is forbidden.
257 159 _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; huh?', 'optimole-wp' ), '1.0.0' );