Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions inc/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,11 @@ public function disconnect( WP_REST_Request $request ) {
* @return WP_REST_Response
*/
public function poll_optimized_images( WP_REST_Request $request ) {
// The image URLs are built with the Optimole SDK, which is only initialized for a connected site.
if ( ! ( new Optml_Settings() )->is_connected() ) {
return $this->response( [] );
}

$api_key = $request->get_param( 'api_key' );
$request = new Optml_Api();
$images = $request->get_optimized_images( $api_key );
Expand Down
43 changes: 43 additions & 0 deletions tests/test-poll-optimized-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,49 @@ public function test_poll_returns_empty_list_when_list_empty() {
$this->assertSame( [], $payload['data'] );
}

/**
* Without a connection the SDK is not initialized, so a list cannot be turned into image URLs:
* polling returns an empty list instead of throwing, and does not call the API.
*/
public function test_poll_returns_empty_list_when_not_connected(): void {
( new Optml_Settings() )->update( 'service_data', [] );
$requests = 0;

/**
* An API response with one image, counting the requests.
*
* @return array{headers: array<string, string>, body: string, response: array{code: int, message: string}}
*/
$this->http_mock = function () use ( &$requests ): array {
++$requests;
return [
'headers' => [],
'body' => wp_json_encode(
[
'code' => 200,
'data' => [
'list' => [
[
'url' => 'https://example.com/photo.jpg',
'key' => 'AbC',
],
],
],
]
),
'response' => [
'code' => 200,
'message' => 'OK',
],
];
};

$payload = $this->poll();
$this->assertSame( 'success', $payload['code'] );
$this->assertSame( [], $payload['data'] );
$this->assertSame( 0, $requests );
}

/**
* A valid list is returned (URLs rewritten through Optimole).
*/
Expand Down
Loading