Skip to content

Commit

Permalink
Merge pull request #2560 from woocommerce/fix/settings-format
Browse files Browse the repository at this point in the history
Return settings prop as object in the REST API
  • Loading branch information
jorgemd24 authored Aug 27, 2024
2 parents 59ae632 + 9981934 commit 7882202
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
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 )
);
}
}

0 comments on commit 7882202

Please sign in to comment.