Skip to content

Commit ce06a00

Browse files
committed
use sdk for offloading images
1 parent 4a3223c commit ce06a00

File tree

6 files changed

+173
-194
lines changed

6 files changed

+173
-194
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"require": {
5656
"php": ">=7.4",
5757
"codeinwp/themeisle-sdk": "^3.3",
58-
"codeinwp/optimole-sdk": "^1.0",
58+
"codeinwp/optimole-sdk": "^1.1",
5959
"enshrined/svg-sanitize": "^0.18.0"
6060
}
6161
}

composer.lock

Lines changed: 15 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inc/api.php

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ final class Optml_Api {
1515
* @var string Api root.
1616
*/
1717
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';
2418
/**
2519
* Optimole onboard api root url.
2620
*
@@ -48,9 +42,6 @@ public function __construct() {
4842
if ( defined( 'OPTIML_API_ROOT' ) ) {
4943
$this->api_root = constant( 'OPTIML_API_ROOT' );
5044
}
51-
if ( defined( 'OPTIML_UPLOAD_API_ROOT' ) ) {
52-
$this->upload_api_root = constant( 'OPTIML_UPLOAD_API_ROOT' );
53-
}
5445
if ( defined( 'OPTIML_ONBOARD_API_ROOT' ) ) {
5546
$this->onboard_api_root = constant( 'OPTIML_ONBOARD_API_ROOT' );
5647
}
@@ -246,19 +237,6 @@ private function build_args( $method, $url, $headers, $params ) {
246237
return $args;
247238
}
248239

249-
/**
250-
* Upload image to our servers using the generated signed url.
251-
*
252-
* @param string $upload_url The signed to url to upload the image to.
253-
* @param string $content_type Image mime type, it must match the actual mime type of the image.
254-
* @param string $image Image data from file_get_contents.
255-
* @return mixed
256-
*/
257-
public function upload_image( $upload_url, $content_type, $image ) {
258-
$args = $this->build_args( 'PUT', '', ['content-type' => $content_type], $image );
259-
return wp_remote_request( $upload_url, $args );
260-
}
261-
262240
/**
263241
* Check if the optimized url is available.
264242
*
@@ -275,47 +253,6 @@ public function check_optimized_url( $url ) {
275253
return true;
276254
}
277255

278-
/**
279-
* Get options for the signed urls api call.
280-
*
281-
* @param string $original_url Image original url.
282-
* @param string $delete Whether to delete a bucket object or not(ie. generate signed upload url).
283-
* @param string $table_id Remote id used on our servers.
284-
* @param string $update_table False or success.
285-
* @param string $get_url Whether to return a get url or not.
286-
* @param string $width Original image width.
287-
* @param string $height Original image height.
288-
* @param int $file_size Original file size.
289-
* @return array|WP_Error
290-
*/
291-
public function call_upload_api( $original_url = '', $delete = 'false', $table_id = '', $update_table = 'false', $get_url = 'false', $width = 'auto', $height = 'auto', $file_size = 0 ) {
292-
$body = [
293-
'secret' => Optml_Config::$secret,
294-
'userKey' => Optml_Config::$key,
295-
'originalUrl' => $original_url,
296-
'deleteUrl' => $delete,
297-
'id' => $table_id,
298-
'updateDynamo' => $update_table,
299-
'getUrl' => $get_url,
300-
'width' => $width,
301-
'height' => $height,
302-
'originalFileSize' => $file_size,
303-
];
304-
$body = wp_json_encode( $body );
305-
306-
$options = [
307-
'body' => $body,
308-
'headers' => [
309-
'Content-Type' => 'application/json',
310-
],
311-
'timeout' => 60,
312-
'blocking' => true,
313-
'sslverify' => false,
314-
'data_format' => 'body',
315-
];
316-
return wp_remote_post( $this->upload_api_root, $options );
317-
}
318-
319256
/**
320257
* Send a list of images to upload.
321258
*

inc/config.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,23 @@ public static function init( $service_settings = [] ) {
127127
self::$service_url = constant( 'OPTML_CUSTOM_DOMAIN' );
128128
}
129129

130-
Optimole::init( self::$key, [ 'domain' => parse_url( self::$service_url, PHP_URL_HOST ) ] );
130+
$options = [ 'domain' => parse_url( self::$service_url, PHP_URL_HOST ) ];
131+
132+
if ( defined( 'OPTIML_API_ROOT' ) ) {
133+
$options['dashboard_api_url'] = OPTIML_API_ROOT;
134+
}
135+
136+
if ( defined( 'OPTIML_UPLOAD_API_ROOT' ) ) {
137+
$options['upload_api_url'] = OPTIML_UPLOAD_API_ROOT;
138+
}
139+
140+
if ( isset( $service_settings['cdn_key'], $service_settings['cdn_secret'] ) ) {
141+
$options['upload_api_credentials'] = [
142+
'userKey' => $service_settings['cdn_key'],
143+
'secret' => $service_settings['cdn_secret'],
144+
];
145+
}
146+
147+
Optimole::init( self::$key, $options );
131148
}
132149
}

0 commit comments

Comments
 (0)