From 2ca97714f3c29605eb15f544450d93c9b132b781 Mon Sep 17 00:00:00 2001 From: selul Date: Fri, 9 Oct 2026 19:08:42 +0300 Subject: [PATCH 1/2] fix: do not build polled image URLs before the SDK is initialized poll_optimized_images() turned the API's list into image URLs through the Optimole SDK, which is only initialized for a connected site. On a site without a CDN key and secret it threw "Please initialize the Optimole SDK first". It now returns an empty list there, without calling the API. Co-Authored-By: Claude Opus 5.5 --- inc/rest.php | 5 ++++ tests/test-poll-optimized-images.php | 37 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/inc/rest.php b/inc/rest.php index 59d8be1c..c8e28d9b 100644 --- a/inc/rest.php +++ b/inc/rest.php @@ -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 ); diff --git a/tests/test-poll-optimized-images.php b/tests/test-poll-optimized-images.php index 929d7e56..d1722d28 100644 --- a/tests/test-poll-optimized-images.php +++ b/tests/test-poll-optimized-images.php @@ -182,6 +182,43 @@ 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() { + ( new Optml_Settings() )->update( 'service_data', [] ); + $requests = 0; + $this->http_mock = function () use ( &$requests ) { + ++$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). */ From dd5dcd1369c4f80cbcfa2a8f03d1be4ed7cd7e0d Mon Sep 17 00:00:00 2001 From: selul Date: Sun, 11 Oct 2026 16:15:18 +0300 Subject: [PATCH 2/2] test: add return types to the not-connected polling test Co-Authored-By: Claude Opus 5.5 --- tests/test-poll-optimized-images.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/test-poll-optimized-images.php b/tests/test-poll-optimized-images.php index d1722d28..9235ff25 100644 --- a/tests/test-poll-optimized-images.php +++ b/tests/test-poll-optimized-images.php @@ -186,10 +186,16 @@ public function test_poll_returns_empty_list_when_list_empty() { * 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() { + public function test_poll_returns_empty_list_when_not_connected(): void { ( new Optml_Settings() )->update( 'service_data', [] ); - $requests = 0; - $this->http_mock = function () use ( &$requests ) { + $requests = 0; + + /** + * An API response with one image, counting the requests. + * + * @return array{headers: array, body: string, response: array{code: int, message: string}} + */ + $this->http_mock = function () use ( &$requests ): array { ++$requests; return [ 'headers' => [],