diff --git a/src/DB/Query/ShippingTimeQuery.php b/src/DB/Query/ShippingTimeQuery.php index 659b68c7ad..ce2089cc4c 100644 --- a/src/DB/Query/ShippingTimeQuery.php +++ b/src/DB/Query/ShippingTimeQuery.php @@ -43,4 +43,26 @@ protected function sanitize_value( string $column, $value ) { 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'], + ]; + + $items[ $time['country'] ] = $data; + } + + return $items; + } } diff --git a/src/Integration/WPCOMProxy.php b/src/Integration/WPCOMProxy.php index 89bd823596..9144701273 100644 --- a/src/Integration/WPCOMProxy.php +++ b/src/Integration/WPCOMProxy.php @@ -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; @@ -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; + } /** * The meta key used to filter the items. @@ -73,6 +97,8 @@ function ( $valid_vars ) { } ); + $this->register_callbacks(); + foreach ( array_keys( self::$post_types_to_filter ) as $object_type ) { $this->register_object_types_filter( $object_type ); } @@ -107,13 +133,55 @@ protected function register_object_types_filter( string $object_type ): void { } /** - * Whether the data should be filtered. + * Register the callbacks. + */ + protected function register_callbacks() { + add_filter( + 'rest_request_after_callbacks', + /** + * 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 ) { + 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 + ); + } + + /** + * 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'; } @@ -128,7 +196,7 @@ protected function should_filter_data( WP_REST_Request $request ): bool { * @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; } @@ -173,7 +241,7 @@ public function filter_response_by_syncable_item( $response, $item, WP_REST_Requ * @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; } @@ -199,7 +267,7 @@ public function filter_by_metaquery( array $args, WP_REST_Request $request ): ar * @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; } diff --git a/src/Internal/DependencyManagement/IntegrationServiceProvider.php b/src/Internal/DependencyManagement/IntegrationServiceProvider.php index d38296beb2..7fbae7bbca 100644 --- a/src/Internal/DependencyManagement/IntegrationServiceProvider.php +++ b/src/Internal/DependencyManagement/IntegrationServiceProvider.php @@ -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; @@ -48,7 +49,7 @@ public function register(): void { $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 ); $this->share_with_tags( IntegrationInitializer::class, diff --git a/tests/Unit/Integration/WPCOMProxyTest.php b/tests/Unit/Integration/WPCOMProxyTest.php index ec548572bb..780e433f42 100644 --- a/tests/Unit/Integration/WPCOMProxyTest.php +++ b/tests/Unit/Integration/WPCOMProxyTest.php @@ -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 ); + } }