diff --git a/src/php/Client/Cloud_API.php b/src/php/Client/Cloud_API.php index d3697158..19c21289 100644 --- a/src/php/Client/Cloud_API.php +++ b/src/php/Client/Cloud_API.php @@ -509,15 +509,9 @@ public function get_featured_snippets( int $page = 1, int $per_page = 10, array return new Cloud_Snippets(); } - $body = wp_remote_retrieve_body( $response ); - - if ( ! $body ) { - return new Cloud_Snippets(); - } - - $json = json_decode( $body, true ); + $json = self::unpack_request_json( $response ); - if ( ! is_array( $json ) || ! isset( $json['data'] ) ) { + if ( ! $json ) { return new Cloud_Snippets(); } diff --git a/src/php/Model/Cloud_Snippets.php b/src/php/Model/Cloud_Snippets.php index 77f323db..6de036f0 100644 --- a/src/php/Model/Cloud_Snippets.php +++ b/src/php/Model/Cloud_Snippets.php @@ -107,7 +107,7 @@ private function normalize_cloud_api( $initial_data ): array { if ( is_array( $initial_data ) ) { $meta = $initial_data['meta'] ?? []; - $result['snippets'] = $initial_data['data'] ?? $initial_data['snippets'] ?? []; + $result['snippets'] = $initial_data['snippets'] ?? []; $result['cloud_id_rev'] = $initial_data['cloud_id_rev'] ?? []; if ( $meta ) { diff --git a/tests/phpunit/test-cloud-api-featured.php b/tests/phpunit/test-cloud-api-featured.php index dc493dc6..4fdf422e 100644 --- a/tests/phpunit/test-cloud-api-featured.php +++ b/tests/phpunit/test-cloud-api-featured.php @@ -127,7 +127,6 @@ private function build_success_response( int $count = 3 ): array { } $body = [ - 'data' => $snippets, 'snippets' => $snippets, 'meta' => [ 'total' => $count, diff --git a/tests/phpunit/test-cloud-api-search.php b/tests/phpunit/test-cloud-api-search.php index 693ee85c..b33dd167 100644 --- a/tests/phpunit/test-cloud-api-search.php +++ b/tests/phpunit/test-cloud-api-search.php @@ -88,7 +88,7 @@ private function build_response( int $count = 2, int $total = 42 ): array { } $body = [ - 'data' => $snippets, + 'snippets' => $snippets, 'meta' => [ 'total' => $total, 'total_pages' => 5, diff --git a/tests/phpunit/test-cloud-snippets.php b/tests/phpunit/test-cloud-snippets.php index 9a7081f7..06fbb342 100644 --- a/tests/phpunit/test-cloud-snippets.php +++ b/tests/phpunit/test-cloud-snippets.php @@ -20,7 +20,7 @@ class Cloud_Snippets_Test extends TestCase { public function test_normalizes_full_envelope(): void { $result = new Cloud_Snippets( [ - 'data' => [ + 'snippets' => [ [ 'id' => 1, 'name' => 'First', @@ -57,11 +57,11 @@ public function test_normalizes_full_envelope(): void { } /** - * The `snippets` key is used when no `data` key is present. + * Snippets are read from the `snippets` key of the envelope. * * @return void */ - public function test_falls_back_to_snippets_key(): void { + public function test_reads_snippets_key(): void { $result = new Cloud_Snippets( [ 'snippets' => [ @@ -79,11 +79,11 @@ public function test_falls_back_to_snippets_key(): void { } /** - * The `data` key takes precedence over the `snippets` key when both are present. + * The `snippets` key is authoritative; a legacy `data` key is ignored. * * @return void */ - public function test_data_key_takes_precedence_over_snippets_key(): void { + public function test_legacy_data_key_is_ignored(): void { $result = new Cloud_Snippets( [ 'data' => [ @@ -99,14 +99,14 @@ public function test_data_key_takes_precedence_over_snippets_key(): void { 'snippets' => [ [ 'id' => 9, - 'name' => 'Ignored', + 'name' => 'Used', ], ], - 'meta' => [ 'total' => 2 ], + 'meta' => [ 'total' => 1 ], ] ); - $this->assertCount( 2, $result->snippets ); + $this->assertCount( 1, $result->snippets ); } /** @@ -161,7 +161,7 @@ public function test_empty_input_uses_defaults(): void { public function test_missing_meta_keeps_default_totals(): void { $result = new Cloud_Snippets( [ - 'data' => [ + 'snippets' => [ [ 'id' => 1, 'name' => 'Lonely',