Skip to content

Commit

Permalink
Merge pull request #2519 from woocommerce/fix/empty-props-as-objects-…
Browse files Browse the repository at this point in the history
…wpcom

Set empty props as objects in WPCOM Proxy responses
  • Loading branch information
puntope committed Aug 20, 2024
2 parents 6a9a419 + 42281ed commit cc158ab
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/Integration/WPCOMProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,47 +158,69 @@ 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,
3
);
}

/**
* 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.
*
Expand Down
27 changes: 27 additions & 0 deletions tests/Unit/Integration/WPCOMProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
);
}
}

0 comments on commit cc158ab

Please sign in to comment.