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

Add target audiences and shipping times in the settings/general endpoint. #2342

Merged
22 changes: 22 additions & 0 deletions src/DB/Query/ShippingTimeQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,26 @@

return $value;
}

/**
* Get all shipping times.
*
* @since x.x.x
*
* @return array
*/
public function get_all_shipping_times() {
$times = $this->get_results();
$items = [];
foreach ( $times as $time ) {
$data = [
'country_code' => $time['country'],
'time' => $time['time'],
];

Check warning on line 61 in src/DB/Query/ShippingTimeQuery.php

View check run for this annotation

Codecov / codecov/patch

src/DB/Query/ShippingTimeQuery.php#L58-L61

Added lines #L58 - L61 were not covered by tests

$items[ $time['country'] ] = $data;

Check warning on line 63 in src/DB/Query/ShippingTimeQuery.php

View check run for this annotation

Codecov / codecov/patch

src/DB/Query/ShippingTimeQuery.php#L63

Added line #L63 was not covered by tests
}

return $items;
}
}
80 changes: 74 additions & 6 deletions src/Integration/WPCOMProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

namespace Automattic\WooCommerce\GoogleListingsAndAds\Integration;

use Automattic\WooCommerce\GoogleListingsAndAds\DB\Query\ShippingTimeQuery;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use Automattic\WooCommerce\GoogleListingsAndAds\Value\ChannelVisibility;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareInterface;
use WP_REST_Response;
use WP_REST_Request;

Expand All @@ -16,9 +20,29 @@
*
* Initializes the hooks to filter the data sent to the WPCOM proxy depending on the query parameter gla_syncable.
*
* @since x.x.x
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Integration
*/
class WPCOMProxy implements Service, Registerable {
class WPCOMProxy implements Service, Registerable, OptionsAwareInterface {

use OptionsAwareTrait;

/**
* The ShippingTimeQuery object.
*
* @var ShippingTimeQuery
*/
protected $shipping_time_query;

/**
* WPCOMProxy constructor.
*
* @param ShippingTimeQuery $shipping_time_query The ShippingTimeQuery object.
*/
public function __construct( ShippingTimeQuery $shipping_time_query ) {
$this->shipping_time_query = $shipping_time_query;

Check warning on line 44 in src/Integration/WPCOMProxy.php

View check run for this annotation

Codecov / codecov/patch

src/Integration/WPCOMProxy.php#L43-L44

Added lines #L43 - L44 were not covered by tests
}

/**
* The meta key used to filter the items.
Expand Down Expand Up @@ -73,6 +97,8 @@
}
);

$this->register_callbacks();

Check warning on line 100 in src/Integration/WPCOMProxy.php

View check run for this annotation

Codecov / codecov/patch

src/Integration/WPCOMProxy.php#L100

Added line #L100 was not covered by tests

foreach ( array_keys( self::$post_types_to_filter ) as $object_type ) {
$this->register_object_types_filter( $object_type );
}
Expand Down Expand Up @@ -107,13 +133,55 @@
}

/**
* Whether the data should be filtered.
* Register the callbacks.
*/
protected function register_callbacks() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed @since param

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually since all the class is new maybe we can add it at the top of the class

add_filter(
'rest_request_after_callbacks',

Check warning on line 140 in src/Integration/WPCOMProxy.php

View check run for this annotation

Codecov / codecov/patch

src/Integration/WPCOMProxy.php#L139-L140

Added lines #L139 - L140 were not covered by tests
/**
* Add the Google Listings 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 ) {

Check warning on line 148 in src/Integration/WPCOMProxy.php

View check run for this annotation

Codecov / codecov/patch

src/Integration/WPCOMProxy.php#L148

Added line #L148 was not covered by tests
if ( ! $this->is_gla_request( $request ) ) {
return $response;
}

if ( $request->get_route() === '/wc/v3/settings/general' && $response instanceof WP_REST_Response ) {
$data = $response->get_data();
$data[] = [
'id' => 'gla_target_audience',
'label' => 'Google Listings and Ads: Target Audience',
'value' => $this->options->get( OptionsInterface::TARGET_AUDIENCE, [] ),
];

$data[] = [
'id' => 'gla_shipping_times',
'label' => 'Google Listings and Ads: Shipping Times',
'value' => $this->shipping_time_query->get_all_shipping_times(),
];

$response->set_data( array_values( $data ) );
}

return $response;
},
10,
3
);

Check warning on line 174 in src/Integration/WPCOMProxy.php

View check run for this annotation

Codecov / codecov/patch

src/Integration/WPCOMProxy.php#L171-L174

Added lines #L171 - L174 were not covered by tests
}

/**
* Whether the request is coming from the WPCOM proxy.
*
* @param WP_REST_Request $request The request object.
*
* @return bool
*/
protected function should_filter_data( WP_REST_Request $request ): bool {
protected function is_gla_request( WP_REST_Request $request ): bool {
// WPCOM proxy will set the gla_syncable to 1 if the request is coming from the proxy and it is the Google App.
return $request->get_param( 'gla_syncable' ) === '1';
}
Expand All @@ -128,7 +196,7 @@
* @return WP_REST_Response The response object updated.
*/
public function filter_response_by_syncable_item( $response, $item, WP_REST_Request $request ): WP_REST_Response {
if ( ! $this->should_filter_data( $request ) ) {
if ( ! $this->is_gla_request( $request ) ) {
return $response;
}

Expand Down Expand Up @@ -173,7 +241,7 @@
* @return array The query args updated.
* */
public function filter_by_metaquery( array $args, WP_REST_Request $request ): array {
if ( ! $this->should_filter_data( $request ) ) {
if ( ! $this->is_gla_request( $request ) ) {
return $args;
}

Expand All @@ -199,7 +267,7 @@
* @return WP_REST_Response The response object updated.
*/
public function filter_metadata( WP_REST_Response $response, $item, WP_REST_Request $request ): WP_REST_Response {
if ( ! $this->should_filter_data( $request ) ) {
if ( ! $this->is_gla_request( $request ) ) {
return $response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Automattic\WooCommerce\GoogleListingsAndAds\Internal\DependencyManagement;

use Automattic\WooCommerce\GoogleListingsAndAds\DB\Query\ShippingTimeQuery;
use Automattic\WooCommerce\GoogleListingsAndAds\Exception\ValidateInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use Automattic\WooCommerce\GoogleListingsAndAds\Integration\IntegrationInitializer;
Expand Down Expand Up @@ -48,7 +49,7 @@
$this->share_with_tags( WooCommerceProductBundles::class, AttributeManager::class );
$this->share_with_tags( WooCommercePreOrders::class, ProductHelper::class );
$this->conditionally_share_with_tags( JetpackWPCOM::class );
$this->share_with_tags( WPCOMProxy::class );
$this->share_with_tags( WPCOMProxy::class, ShippingTimeQuery::class );

Check warning on line 52 in src/Internal/DependencyManagement/IntegrationServiceProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Internal/DependencyManagement/IntegrationServiceProvider.php#L52

Added line #L52 was not covered by tests

$this->share_with_tags(
IntegrationInitializer::class,
Expand Down
29 changes: 29 additions & 0 deletions tests/Unit/Integration/WPCOMProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,33 @@ public function test_get_coupons_without_gla_syncable_param() {
$this->assertEquals( $this->get_test_metadata(), $this->format_metadata( $response_mapped[ $coupon_1->get_id() ]['meta_data'] ) );
$this->assertEquals( $this->get_test_metadata( 'dont-sync-and-show' ), $this->format_metadata( $response_mapped[ $coupon_2->get_id() ]['meta_data'] ) );
}

public function test_get_settings_without_gla_syncable_param() {
$response = $this->do_request( '/wc/v3/settings/general', 'GET' );

$this->assertEquals( 200, $response->get_status() );

$response_mapped = $this->maps_the_response_with_the_item_id( $response );

$this->assertArrayNotHasKey( 'gla_target_audience', $response_mapped );
$this->assertArrayNotHasKey( 'gla_shipping_times', $response_mapped );
}

public function test_get_settings_with_gla_syncable_param() {
global $wpdb;

// As the shipping time tables are not created in the test environment, we need to suppress the errors.
$wpdb->suppress_errors = true;

$response = $this->do_request( '/wc/v3/settings/general', 'GET', [ 'gla_syncable' => '1' ] );

$wpdb->suppress_errors = false;

$this->assertEquals( 200, $response->get_status() );

$response_mapped = $this->maps_the_response_with_the_item_id( $response );

$this->assertArrayHasKey( 'gla_target_audience', $response_mapped );
$this->assertArrayHasKey( 'gla_shipping_times', $response_mapped );
}
}
Loading