Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return settings prop as object in the REST API #2560

Merged
merged 6 commits into from
Aug 27, 2024
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
13 changes: 7 additions & 6 deletions src/Integration/WPCOMProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function ( $response, $handler, $request ) {
$response->set_data( array_values( $data ) );
}

$response->set_data( $this->prepare_data( $response->get_data() ) );
$response->set_data( $this->prepare_data( $response->get_data(), $request ) );
return $response;
},
10,
Expand All @@ -204,17 +204,18 @@ function ( $response, $handler, $request ) {
/**
* Prepares the data converting the empty arrays in objects for consistency.
*
* @param array $data The response data to parse
* @param array $data The response data to parse
* @param WP_REST_Request $request The request object.
* @return mixed
*/
public function prepare_data( $data ) {
public function prepare_data( $data, $request ) {
if ( ! is_array( $data ) ) {
return $data;
}

foreach ( array_keys( $data ) as $key ) {
if ( is_array( $data[ $key ] ) && empty( $data[ $key ] ) ) {
$data[ $key ] = (object) $data[ $key ];
foreach ( $data as $key => $value ) {
if ( preg_match( '/^\/wc\/v3\/shipping\/zones\/\d+\/methods/', $request->get_route() ) && isset( $value['settings'] ) && empty( $value['settings'] ) ) {
$data[ $key ]['settings'] = (object) $value['settings'];
}
}

Expand Down
36 changes: 26 additions & 10 deletions tests/Unit/Integration/WPCOMProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Psr\Container\ContainerInterface;
use WC_Meta_Data;
use WP_REST_Response;
use WP_REST_Request;


/**
Expand Down Expand Up @@ -458,13 +459,15 @@ public function test_get_settings_with_gla_syncable_param() {
$this->assertArrayHasKey( 'gla_shipping_times', $response_mapped );
}

public function test_get_empty_data_as_object() {
public function test_get_empty_settings_for_shipping_zone_methods_as_object() {
$request = new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/4/methods' );

// dummy data
$data = [
'foo' => 'bar',
'var' => [],
'baz' => null,
'bool' => false,
[
'id' => '1',
'settings' => [],
],
];

$proxy = new WPCOMProxy(
Expand All @@ -474,12 +477,25 @@ public function test_get_empty_data_as_object() {

$this->assertEquals(
[
'foo' => 'bar',
'var' => (object) [],
'baz' => null,
'bool' => false,
[
'id' => '1',
'settings' => (object) [],
],
],
$proxy->prepare_data( $data, $request )
);

// If the request is not for shipping zone methods, the data should not be modified.
$request = new WP_REST_Request( 'GET', '/wc/v3/products' );

$this->assertEquals(
[
[
'id' => '1',
'settings' => [],
],
],
$proxy->prepare_data( $data )
$proxy->prepare_data( $data, $request )
);
}
}