diff --git a/src/Integration/WPCOMProxy.php b/src/Integration/WPCOMProxy.php index 7193e0b57e..74912f1f1c 100644 --- a/src/Integration/WPCOMProxy.php +++ b/src/Integration/WPCOMProxy.php @@ -158,40 +158,42 @@ protected function register_callbacks() { add_filter( 'rest_request_after_callbacks', /** - * Add the Google Listings and Ads settings to the settings/general response. + * Add the Google for WooCommerce and Ads settings to the settings/general response. * * @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response The response object. * @param mixed $handler The handler. * @param WP_REST_Request $request The request object. */ function ( $response, $handler, $request ) { - if ( ! $this->is_gla_request( $request ) ) { + if ( ! $this->is_gla_request( $request ) || ! $response instanceof WP_REST_Response ) { return $response; } - if ( $request->get_route() === '/wc/v3/settings/general' && $response instanceof WP_REST_Response ) { - $data = $response->get_data(); + $data = $response->get_data(); + + if ( $request->get_route() === '/wc/v3/settings/general' ) { $data[] = [ 'id' => 'gla_target_audience', - 'label' => 'Google Listings and Ads: Target Audience', + 'label' => 'Google for WooCommerce: Target Audience', 'value' => $this->options->get( OptionsInterface::TARGET_AUDIENCE, [] ), ]; $data[] = [ 'id' => 'gla_shipping_times', - 'label' => 'Google Listings and Ads: Shipping Times', + 'label' => 'Google for WooCommerce: Shipping Times', 'value' => $this->shipping_time_query->get_all_shipping_times(), ]; $data[] = [ 'id' => 'gla_language', - 'label' => 'Google Listings and Ads: Store language', + 'label' => 'Google for WooCommerce: Store language', 'value' => get_locale(), ]; $response->set_data( array_values( $data ) ); } + $response->set_data( $this->prepare_data( $response->get_data() ) ); return $response; }, 10, @@ -199,6 +201,26 @@ function ( $response, $handler, $request ) { ); } + /** + * Prepares the data converting the empty arrays in objects for consistency. + * + * @param array $data The response data to parse + * @return mixed + */ + public function prepare_data( $data ) { + 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 ]; + } + } + + return $data; + } + /** * Whether the request is coming from the WPCOM proxy. * diff --git a/tests/Unit/Integration/WPCOMProxyTest.php b/tests/Unit/Integration/WPCOMProxyTest.php index 946e4c3405..3a86ba31c5 100644 --- a/tests/Unit/Integration/WPCOMProxyTest.php +++ b/tests/Unit/Integration/WPCOMProxyTest.php @@ -2,6 +2,8 @@ namespace Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\API\Site\Controllers\Ads; +use Automattic\WooCommerce\GoogleListingsAndAds\DB\Query\ShippingTimeQuery; +use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\AttributeManager; use Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper; use Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper; use Automattic\WooCommerce\GoogleListingsAndAds\Tests\Framework\RESTControllerUnitTest; @@ -455,4 +457,29 @@ public function test_get_settings_with_gla_syncable_param() { $this->assertArrayHasKey( 'gla_target_audience', $response_mapped ); $this->assertArrayHasKey( 'gla_shipping_times', $response_mapped ); } + + public function test_get_empty_data_as_object() { + // dummy data + $data = [ + 'foo' => 'bar', + 'var' => [], + 'baz' => null, + 'bool' => false, + ]; + + $proxy = new WPCOMProxy( + $this->container->get( ShippingTimeQuery::class ), + $this->container->get( AttributeManager::class ) + ); + + $this->assertEquals( + [ + 'foo' => 'bar', + 'var' => (object) [], + 'baz' => null, + 'bool' => false, + ], + $proxy->prepare_data( $data ) + ); + } }