From 3a7aabb215e4763b079725c8c57467a801ed4c9e Mon Sep 17 00:00:00 2001 From: Jorge M Date: Wed, 27 Mar 2024 23:16:42 +0100 Subject: [PATCH 1/9] Add get all shipping times --- src/DB/Query/ShippingTimeQuery.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/DB/Query/ShippingTimeQuery.php b/src/DB/Query/ShippingTimeQuery.php index 659b68c7ad..1affb7a414 100644 --- a/src/DB/Query/ShippingTimeQuery.php +++ b/src/DB/Query/ShippingTimeQuery.php @@ -43,4 +43,24 @@ protected function sanitize_value( string $column, $value ) { return $value; } + + /** + * Get all shipping times. + * + * @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; + } } From c76e49fea756518cd534fa38ef43cfb1228fd079 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Wed, 27 Mar 2024 23:17:05 +0100 Subject: [PATCH 2/9] Add ShippingTimeQuery to WPCOM proxy --- .../DependencyManagement/IntegrationServiceProvider.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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, From 57203d59fef8eac37091b4462ee733c2cbf49c47 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Wed, 27 Mar 2024 23:17:52 +0100 Subject: [PATCH 3/9] Add target audience and shipping time to the settings endpoint --- src/Integration/WPCOMProxy.php | 71 +++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 6 deletions(-) diff --git a/src/Integration/WPCOMProxy.php b/src/Integration/WPCOMProxy.php index 89bd823596..fcfd1c2400 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; @@ -18,7 +22,25 @@ * * @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 +95,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 +131,48 @@ 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', + function ( WP_REST_Response $response, $handler, WP_REST_Request $request ) { + if ( ! $this->is_gla_request( $request ) ) { + return $response; + } + + if ( $request->get_route() === '/wc/v3/settings/general' ) { + $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 +187,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 +232,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 +258,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; } From 063dc50aeb5ca7e2f2fe480857a979e230ca374f Mon Sep 17 00:00:00 2001 From: Jorge M Date: Thu, 28 Mar 2024 11:42:33 +0100 Subject: [PATCH 4/9] Add docs for rest_request_after_callbacks filter --- src/Integration/WPCOMProxy.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Integration/WPCOMProxy.php b/src/Integration/WPCOMProxy.php index fcfd1c2400..fa03c0072f 100644 --- a/src/Integration/WPCOMProxy.php +++ b/src/Integration/WPCOMProxy.php @@ -136,12 +136,19 @@ protected function register_object_types_filter( string $object_type ): void { protected function register_callbacks() { add_filter( 'rest_request_after_callbacks', - function ( WP_REST_Response $response, $handler, WP_REST_Request $request ) { + /** + * 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' ) { + if ( $request->get_route() === '/wc/v3/settings/general' && $response instanceof WP_REST_Response ) { $data = $response->get_data(); $data[] = [ 'id' => 'gla_target_audience', From 1c7e7430acad271b074dacd410afd0e2a524bae2 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Thu, 28 Mar 2024 11:43:02 +0100 Subject: [PATCH 5/9] Add tests for general settings endpoint --- tests/Unit/Integration/WPCOMProxyTest.php | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/Unit/Integration/WPCOMProxyTest.php b/tests/Unit/Integration/WPCOMProxyTest.php index ec548572bb..5a2820c7ae 100644 --- a/tests/Unit/Integration/WPCOMProxyTest.php +++ b/tests/Unit/Integration/WPCOMProxyTest.php @@ -7,6 +7,7 @@ use Automattic\WooCommerce\GoogleListingsAndAds\Tests\Framework\RESTControllerUnitTest; use Automattic\WooCommerce\GoogleListingsAndAds\Value\ChannelVisibility; use Automattic\WooCommerce\GoogleListingsAndAds\Integration\WPCOMProxy; +use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface; use WC_Meta_Data; use WP_REST_Response; @@ -375,4 +376,42 @@ 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; + + $target_audience = [ + 'countries' => ['US'], + ]; + + update_option( "gla_".OptionsInterface::TARGET_AUDIENCE , $target_audience ); + + // 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 ); + $this->assertEquals( $target_audience, $response_mapped['gla_target_audience']['value'] ); + } } From 5f10066b87cef91ae59cbd61669b5457d1688409 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Thu, 28 Mar 2024 13:38:30 +0100 Subject: [PATCH 6/9] Fix tests phpcs --- tests/Unit/Integration/WPCOMProxyTest.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/Unit/Integration/WPCOMProxyTest.php b/tests/Unit/Integration/WPCOMProxyTest.php index 5a2820c7ae..ff0f302db5 100644 --- a/tests/Unit/Integration/WPCOMProxyTest.php +++ b/tests/Unit/Integration/WPCOMProxyTest.php @@ -378,7 +378,6 @@ public function test_get_coupons_without_gla_syncable_param() { } public function test_get_settings_without_gla_syncable_param() { - $response = $this->do_request( '/wc/v3/settings/general', 'GET' ); $this->assertEquals( 200, $response->get_status() ); @@ -386,18 +385,17 @@ public function test_get_settings_without_gla_syncable_param() { $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 ); - } + $this->assertArrayNotHasKey( 'gla_shipping_times', $response_mapped ); + } public function test_get_settings_with_gla_syncable_param() { - global $wpdb; $target_audience = [ - 'countries' => ['US'], + 'countries' => [ 'US' ], ]; - update_option( "gla_".OptionsInterface::TARGET_AUDIENCE , $target_audience ); + update_option( 'gla_' . OptionsInterface::TARGET_AUDIENCE, $target_audience ); // As the shipping time tables are not created in the test environment, we need to suppress the errors. $wpdb->suppress_errors = true; @@ -411,7 +409,7 @@ public function test_get_settings_with_gla_syncable_param() { $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 ); + $this->assertArrayHasKey( 'gla_shipping_times', $response_mapped ); $this->assertEquals( $target_audience, $response_mapped['gla_target_audience']['value'] ); - } + } } From 0bd0e1341b5ca43b7d89bfa6a6ac4ce66d84bf21 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Thu, 28 Mar 2024 14:02:13 +0100 Subject: [PATCH 7/9] Remove update TARGET_AUDIENCE option to make it consistent with the other tests --- tests/Unit/Integration/WPCOMProxyTest.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/Unit/Integration/WPCOMProxyTest.php b/tests/Unit/Integration/WPCOMProxyTest.php index ff0f302db5..780e433f42 100644 --- a/tests/Unit/Integration/WPCOMProxyTest.php +++ b/tests/Unit/Integration/WPCOMProxyTest.php @@ -7,7 +7,6 @@ use Automattic\WooCommerce\GoogleListingsAndAds\Tests\Framework\RESTControllerUnitTest; use Automattic\WooCommerce\GoogleListingsAndAds\Value\ChannelVisibility; use Automattic\WooCommerce\GoogleListingsAndAds\Integration\WPCOMProxy; -use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface; use WC_Meta_Data; use WP_REST_Response; @@ -391,12 +390,6 @@ public function test_get_settings_without_gla_syncable_param() { public function test_get_settings_with_gla_syncable_param() { global $wpdb; - $target_audience = [ - 'countries' => [ 'US' ], - ]; - - update_option( 'gla_' . OptionsInterface::TARGET_AUDIENCE, $target_audience ); - // As the shipping time tables are not created in the test environment, we need to suppress the errors. $wpdb->suppress_errors = true; @@ -410,6 +403,5 @@ public function test_get_settings_with_gla_syncable_param() { $this->assertArrayHasKey( 'gla_target_audience', $response_mapped ); $this->assertArrayHasKey( 'gla_shipping_times', $response_mapped ); - $this->assertEquals( $target_audience, $response_mapped['gla_target_audience']['value'] ); } } From 657bbf94026e531ea62af97a6aca56fef20464d5 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Thu, 28 Mar 2024 14:04:49 +0100 Subject: [PATCH 8/9] Add missing since --- src/DB/Query/ShippingTimeQuery.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/DB/Query/ShippingTimeQuery.php b/src/DB/Query/ShippingTimeQuery.php index 1affb7a414..ce2089cc4c 100644 --- a/src/DB/Query/ShippingTimeQuery.php +++ b/src/DB/Query/ShippingTimeQuery.php @@ -47,6 +47,8 @@ protected function sanitize_value( string $column, $value ) { /** * Get all shipping times. * + * @since x.x.x + * * @return array */ public function get_all_shipping_times() { From 8ceb779d0a02685bb41e48df57fd769fc5514811 Mon Sep 17 00:00:00 2001 From: Jorge M Date: Tue, 2 Apr 2024 10:43:02 +0200 Subject: [PATCH 9/9] Update since tag --- src/Integration/WPCOMProxy.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Integration/WPCOMProxy.php b/src/Integration/WPCOMProxy.php index fa03c0072f..9144701273 100644 --- a/src/Integration/WPCOMProxy.php +++ b/src/Integration/WPCOMProxy.php @@ -20,6 +20,8 @@ * * 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, OptionsAwareInterface {