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
10 changes: 2 additions & 8 deletions src/php/Client/Cloud_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/php/Model/Cloud_Snippets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
1 change: 0 additions & 1 deletion tests/phpunit/test-cloud-api-featured.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ private function build_success_response( int $count = 3 ): array {
}

$body = [
'data' => $snippets,
'snippets' => $snippets,
'meta' => [
'total' => $count,
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/test-cloud-api-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 9 additions & 9 deletions tests/phpunit/test-cloud-snippets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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' => [
Expand All @@ -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' => [
Expand All @@ -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 );
}

/**
Expand Down Expand Up @@ -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',
Expand Down
Loading