From f5a48b143055d0a650699ac3132dd3435baa33fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20P=C3=A9rez=20Pellicer?= <5908855+puntope@users.noreply.github.com> Date: Fri, 23 Aug 2024 11:31:43 +0400 Subject: [PATCH 1/5] Make settings an object --- src/Integration/WPCOMProxy.php | 10 ++++++--- tests/Unit/Integration/WPCOMProxyTest.php | 26 +++++++++++++++-------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/Integration/WPCOMProxy.php b/src/Integration/WPCOMProxy.php index 74912f1f1c..93fc08521e 100644 --- a/src/Integration/WPCOMProxy.php +++ b/src/Integration/WPCOMProxy.php @@ -212,9 +212,9 @@ public function prepare_data( $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 ( is_array( $data[ $key ] ) && isset( $data[ $key ]['settings'] ) ) { + $data[ $key ]['settings'] = (object) $data[ $key ]['settings']; } } @@ -339,6 +339,10 @@ public function prepare_response( WP_REST_Response $response, $item, WP_REST_Req $data['gla_attributes'] = (object) $attr; } + if ( isset( $data['settings'] ) ) { + $data['settings'] = (object) $data['settings']; + } + foreach ( $data['meta_data'] ?? [] as $key => $meta ) { if ( str_starts_with( $meta->key, '_' ) && ! str_starts_with( $meta->key, '_wc_gla' ) ) { unset( $data['meta_data'][ $key ] ); diff --git a/tests/Unit/Integration/WPCOMProxyTest.php b/tests/Unit/Integration/WPCOMProxyTest.php index 3a86ba31c5..9c3750d625 100644 --- a/tests/Unit/Integration/WPCOMProxyTest.php +++ b/tests/Unit/Integration/WPCOMProxyTest.php @@ -458,13 +458,17 @@ 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_as_object() { // dummy data $data = [ - 'foo' => 'bar', - 'var' => [], - 'baz' => null, - 'bool' => false, + [ + 'test' => [], + 'settings' => [], + ], + [ + 'test' => [ 'title' => 'foo' ], + 'settings' => [ 'title' => 'foo' ], + ], ]; $proxy = new WPCOMProxy( @@ -474,10 +478,14 @@ public function test_get_empty_data_as_object() { $this->assertEquals( [ - 'foo' => 'bar', - 'var' => (object) [], - 'baz' => null, - 'bool' => false, + [ + 'test' => [], + 'settings' => (object) [], + ], + [ + 'test' => [ 'title' => 'foo' ], + 'settings' => (object) [ 'title' => 'foo' ], + ], ], $proxy->prepare_data( $data ) ); From 9774e44f1ac87792bfc750a0676778e78a4f75e8 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Tue, 27 Aug 2024 11:24:05 +0200 Subject: [PATCH 2/5] Fix shipping method settings format --- src/Integration/WPCOMProxy.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Integration/WPCOMProxy.php b/src/Integration/WPCOMProxy.php index 93fc08521e..69b2c61eed 100644 --- a/src/Integration/WPCOMProxy.php +++ b/src/Integration/WPCOMProxy.php @@ -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, @@ -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 ( $data as $key => $value ) { - if ( is_array( $data[ $key ] ) && isset( $data[ $key ]['settings'] ) ) { - $data[ $key ]['settings'] = (object) $data[ $key ]['settings']; + if ( preg_match( '/^\/wc\/v3\/shipping\/zones\/\d+\/methods/', $request->get_route() ) && isset( $value['settings'] ) && empty( $value['settings'] ) ) { + $data[ $key ]['settings'] = (object) $value['settings']; } } From 74a55ad6dcec03ccb71f7b398c76bda410d4c7c0 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Tue, 27 Aug 2024 11:24:34 +0200 Subject: [PATCH 3/5] Add tests for shipping method settings format --- tests/Unit/Integration/WPCOMProxyTest.php | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/tests/Unit/Integration/WPCOMProxyTest.php b/tests/Unit/Integration/WPCOMProxyTest.php index 9c3750d625..ae97a5263c 100644 --- a/tests/Unit/Integration/WPCOMProxyTest.php +++ b/tests/Unit/Integration/WPCOMProxyTest.php @@ -14,6 +14,7 @@ use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Psr\Container\ContainerInterface; use WC_Meta_Data; use WP_REST_Response; +use WP_REST_Request; /** @@ -458,16 +459,14 @@ public function test_get_settings_with_gla_syncable_param() { $this->assertArrayHasKey( 'gla_shipping_times', $response_mapped ); } - public function test_get_empty_settings_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 = [ [ - 'test' => [], - 'settings' => [], - ], - [ - 'test' => [ 'title' => 'foo' ], - 'settings' => [ 'title' => 'foo' ], + 'method_id' => 'flat_rate', + 'settings' => [], ], ]; @@ -479,15 +478,11 @@ public function test_get_empty_settings_as_object() { $this->assertEquals( [ [ - 'test' => [], - 'settings' => (object) [], - ], - [ - 'test' => [ 'title' => 'foo' ], - 'settings' => (object) [ 'title' => 'foo' ], + 'method_id' => 'flat_rate', + 'settings' => (object) [], ], ], - $proxy->prepare_data( $data ) + $proxy->prepare_data( $data, $request ) ); } } From 552286107f6a0f017a2b423956f5ba75abea5563 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Tue, 27 Aug 2024 11:29:16 +0200 Subject: [PATCH 4/5] Add tests for setting for a different endpoint than shipping zones methods --- tests/Unit/Integration/WPCOMProxyTest.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/Unit/Integration/WPCOMProxyTest.php b/tests/Unit/Integration/WPCOMProxyTest.php index 3e0fee627f..8af32bf0c2 100644 --- a/tests/Unit/Integration/WPCOMProxyTest.php +++ b/tests/Unit/Integration/WPCOMProxyTest.php @@ -465,8 +465,8 @@ public function test_get_empty_settings_for_shipping_zone_methods_as_object() { // dummy data $data = [ [ - 'method_id' => 'flat_rate', - 'settings' => [], + 'id' => '1', + 'settings' => [], ], ]; @@ -478,8 +478,21 @@ public function test_get_empty_settings_for_shipping_zone_methods_as_object() { $this->assertEquals( [ [ - 'method_id' => 'flat_rate', - 'settings' => (object) [], + '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, $request ) From 99819347762ccdd10dd606163a1f1c34fbd89283 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Tue, 27 Aug 2024 11:47:01 +0200 Subject: [PATCH 5/5] Remove converting to object all settings regarless of the endpoint --- src/Integration/WPCOMProxy.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Integration/WPCOMProxy.php b/src/Integration/WPCOMProxy.php index 69b2c61eed..5109bbd4b7 100644 --- a/src/Integration/WPCOMProxy.php +++ b/src/Integration/WPCOMProxy.php @@ -340,10 +340,6 @@ public function prepare_response( WP_REST_Response $response, $item, WP_REST_Req $data['gla_attributes'] = (object) $attr; } - if ( isset( $data['settings'] ) ) { - $data['settings'] = (object) $data['settings']; - } - foreach ( $data['meta_data'] ?? [] as $key => $meta ) { if ( str_starts_with( $meta->key, '_' ) && ! str_starts_with( $meta->key, '_wc_gla' ) ) { unset( $data['meta_data'][ $key ] );