From ecdee9b1a0cca5439fcf3735e686e3162be4f402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Wed, 26 Jul 2023 22:39:25 +0200 Subject: [PATCH 01/21] Add task using `TaskLists::add_task` API, `woocommerce_admin_onboarding_task_list` is deprecated. Fixes https://github.com/woocommerce/google-listings-and-ads/issues/2024 Remove `remove_woocommerce_extended_task_list_item` hook. --- js/src/tasks/complete-setup/index.js | 45 --------- src/Hooks/README.md | 8 -- .../CoreServiceProvider.php | 2 +- src/TaskList/CompleteSetup.php | 95 +++---------------- src/TaskList/CompleteSetupTask.php | 90 ++++++++++++++++++ src/TaskList/TaskListTrait.php | 36 ------- webpack.config.js | 5 - 7 files changed, 105 insertions(+), 176 deletions(-) delete mode 100644 js/src/tasks/complete-setup/index.js create mode 100644 src/TaskList/CompleteSetupTask.php delete mode 100644 src/TaskList/TaskListTrait.php diff --git a/js/src/tasks/complete-setup/index.js b/js/src/tasks/complete-setup/index.js deleted file mode 100644 index c5439c41ad..0000000000 --- a/js/src/tasks/complete-setup/index.js +++ /dev/null @@ -1,45 +0,0 @@ -/** - * External dependencies - */ - -import { __ } from '@wordpress/i18n'; -import { addFilter } from '@wordpress/hooks'; -import { getHistory } from '@woocommerce/navigation'; - -/** - * Internal dependencies - */ -import { getGetStartedUrl, getDashboardUrl } from '.~/utils/urls'; - -/* global glaTaskData */ - -/** - * Use the 'woocommerce_admin_onboarding_task_list' filter to add a task. - */ -addFilter( - 'woocommerce_admin_onboarding_task_list', - 'google-listings-and-ads', - ( tasks ) => { - return [ - ...tasks, - { - key: 'gla_complete_setup', - title: __( - 'Set up Google Listings & Ads', - 'google-listings-and-ads' - ), - completed: glaTaskData.isComplete, - onClick: () => { - // Redirect to the GLA get started or dashboard page. - const nextUrl = glaTaskData.isComplete - ? getDashboardUrl() - : getGetStartedUrl(); - getHistory().push( nextUrl ); - }, - visible: true, - time: __( '20 minutes', 'google-listings-and-ads' ), - isDismissable: true, - }, - ]; - } -); diff --git a/src/Hooks/README.md b/src/Hooks/README.md index 1a7caa7405..abb6778673 100644 --- a/src/Hooks/README.md +++ b/src/Hooks/README.md @@ -18,14 +18,6 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p - BulkEditInitializer.php#L36 -## remove_woocommerce_extended_task_list_item - -**Type**: action - -**Used in**: - -- CompleteSetup.php#L102 - ## woocommerce_admin_disabled **Type**: filter diff --git a/src/Internal/DependencyManagement/CoreServiceProvider.php b/src/Internal/DependencyManagement/CoreServiceProvider.php index 74347a6b2b..702f928e38 100644 --- a/src/Internal/DependencyManagement/CoreServiceProvider.php +++ b/src/Internal/DependencyManagement/CoreServiceProvider.php @@ -299,7 +299,7 @@ function ( ...$arguments ) { $this->conditionally_share_with_tags( EventTracking::class, ContainerInterface::class ); $this->conditionally_share_with_tags( RESTControllers::class, ContainerInterface::class ); $this->conditionally_share_with_tags( ConnectionTest::class, ContainerInterface::class ); - $this->conditionally_share_with_tags( CompleteSetup::class, AssetsHandlerInterface::class ); + $this->conditionally_share_with_tags( CompleteSetup::class ); $this->conditionally_share_with_tags( GlobalSiteTag::class, AssetsHandlerInterface::class, GoogleGtagJs::class, ProductHelper::class, WC::class, WP::class ); $this->share_with_tags( SiteVerificationMeta::class ); $this->conditionally_share_with_tags( MerchantSetupCompleted::class ); diff --git a/src/TaskList/CompleteSetup.php b/src/TaskList/CompleteSetup.php index e2dbdda1de..3f3c3b3a4a 100644 --- a/src/TaskList/CompleteSetup.php +++ b/src/TaskList/CompleteSetup.php @@ -3,102 +3,35 @@ namespace Automattic\WooCommerce\GoogleListingsAndAds\TaskList; -use Automattic\WooCommerce\GoogleListingsAndAds\Assets\AdminScriptWithBuiltDependenciesAsset; -use Automattic\WooCommerce\GoogleListingsAndAds\Assets\Asset; -use Automattic\WooCommerce\GoogleListingsAndAds\Assets\AssetsHandlerInterface; -use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\AdminConditional; -use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Conditional; -use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Deactivateable; +use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists; use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable; use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service; -use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterAwareInterface; -use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterAwareTrait; -use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper; -use Automattic\WooCommerce\GoogleListingsAndAds\Value\BuiltScriptDependencyArray; +use Automattic\WooCommerce\GoogleListingsAndAds\TaskList\CompleteSetupTask; /** * Class CompleteSetup * * @package Automattic\WooCommerce\GoogleListingsAndAds\TaskList */ -class CompleteSetup implements Deactivateable, Service, Registerable, Conditional, MerchantCenterAwareInterface { - - use AdminConditional; - use MerchantCenterAwareTrait; - use PluginHelper; - use TaskListTrait; - - /** - * @var AssetsHandlerInterface - */ - protected $assets_handler; - - /** - * CompleteSetup constructor. - * - * @param AssetsHandlerInterface $assets_handler - */ - public function __construct( AssetsHandlerInterface $assets_handler ) { - $this->assets_handler = $assets_handler; - } +class CompleteSetup implements Service, Registerable { /** * Register a service. + * + * Add CompleteSetupTask to the extended task list on init. */ public function register(): void { - $this->assets_handler->add_many( $this->get_assets() ); - add_action( - 'admin_enqueue_scripts', - function () { - if ( ! $this->should_register_tasks() ) { - return; - } - - $this->assets_handler->enqueue_many( $this->get_assets() ); - - // argument matches the task "key" property - do_action( 'add_woocommerce_extended_task_list_item', 'gla_complete_setup' ); + 'init', + function() { + $task_list = 'extended'; + $task = new CompleteSetupTask( + TaskLists::get_list( $task_list ) + ); + TaskLists::add_task( $task_list, $task ); + + do_action( 'add_woocommerce_extended_task_list_item', $task->get_id() ); } ); } - - /** - * Return an array of assets. - * - * @return Asset[] - */ - protected function get_assets(): array { - $assets[] = ( new AdminScriptWithBuiltDependenciesAsset( - 'gla-task-complete-setup', - 'js/build/task-complete-setup', - "{$this->get_root_dir()}/js/build/task-complete-setup.asset.php", - new BuiltScriptDependencyArray( - [ - 'dependencies' => [], - 'version' => (string) filemtime( "{$this->get_root_dir()}/js/build/task-complete-setup.js" ), - ] - ), - function () { - return $this->should_register_tasks(); - } - ) )->add_localization( - 'glaTaskData', - [ - 'isComplete' => $this->merchant_center->is_setup_complete(), - ] - ); - - return $assets; - } - - /** - * Deactivate the service. - * - * @return void - */ - public function deactivate(): void { - // argument matches the task "key" property - do_action( 'remove_woocommerce_extended_task_list_item', 'gla_complete_setup' ); - } } diff --git a/src/TaskList/CompleteSetupTask.php b/src/TaskList/CompleteSetupTask.php new file mode 100644 index 0000000000..7ed83cab18 --- /dev/null +++ b/src/TaskList/CompleteSetupTask.php @@ -0,0 +1,90 @@ +merchant_center && $this->merchant_center->is_setup_complete() || false; + } + + /** + * Get the action URL. + * + * @return string Start page or dashboard is the setup is completed. + */ + public function get_action_url() { + if ( ! $this->is_complete() ) { + return admin_url( 'admin.php?page=wc-admin&path=/google/start' ); + } + + return admin_url( 'admin.php?page=wc-admin&path=/google/dashboard' ); + } + +} diff --git a/src/TaskList/TaskListTrait.php b/src/TaskList/TaskListTrait.php deleted file mode 100644 index b0c427c261..0000000000 --- a/src/TaskList/TaskListTrait.php +++ /dev/null @@ -1,36 +0,0 @@ -check_should_show_tasks(); - } - - /** - * Helper function to check if UI should show tasks. - * - * @return bool - */ - private function check_should_show_tasks(): bool { - $setup_list = TaskLists::get_list( 'setup' ); - $extended_list = TaskLists::get_list( 'extended' ); - - return ( $setup_list && ! $setup_list->is_hidden() ) || ( $extended_list && ! $extended_list->is_hidden() ); - } -} diff --git a/webpack.config.js b/webpack.config.js index a4efb2f4f1..553fa70d1b 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -136,11 +136,6 @@ const webpackConfig = { ], entry: { index: path.resolve( process.cwd(), 'js/src', 'index.js' ), - 'task-complete-setup': path.resolve( - process.cwd(), - 'js/src/tasks/complete-setup', - 'index.js' - ), 'custom-inputs': path.resolve( process.cwd(), 'js/src/custom-inputs', From 86f75915d18a18e5ed1c48d5136e06d998e5ea11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Thu, 27 Jul 2023 14:16:12 +0200 Subject: [PATCH 02/21] Make CompleteSetupTask register itself remove the need for `CompleteSetup` class --- .../CoreServiceProvider.php | 6 +-- src/TaskList/CompleteSetup.php | 37 ------------------- src/TaskList/CompleteSetupTask.php | 27 ++++++++++++-- 3 files changed, 27 insertions(+), 43 deletions(-) delete mode 100644 src/TaskList/CompleteSetup.php diff --git a/src/Internal/DependencyManagement/CoreServiceProvider.php b/src/Internal/DependencyManagement/CoreServiceProvider.php index 702f928e38..09af6495d0 100644 --- a/src/Internal/DependencyManagement/CoreServiceProvider.php +++ b/src/Internal/DependencyManagement/CoreServiceProvider.php @@ -108,7 +108,7 @@ use Automattic\WooCommerce\GoogleListingsAndAds\Shipping\ZoneMethodsParser; use Automattic\WooCommerce\GoogleListingsAndAds\Shipping\ShippingZone; use Automattic\WooCommerce\GoogleListingsAndAds\Shipping\ZoneLocationsParser; -use Automattic\WooCommerce\GoogleListingsAndAds\TaskList\CompleteSetup; +use Automattic\WooCommerce\GoogleListingsAndAds\TaskList\CompleteSetupTask; use Automattic\WooCommerce\GoogleListingsAndAds\Tracking\Events\ActivatedEvents; use Automattic\WooCommerce\GoogleListingsAndAds\Tracking\Events\SiteClaimEvents; use Automattic\WooCommerce\GoogleListingsAndAds\Tracking\Events\SiteVerificationEvents; @@ -146,7 +146,7 @@ class CoreServiceProvider extends AbstractServiceProvider { AssetsHandlerInterface::class => true, BulkEditInitializer::class => true, ContactInformationNote::class => true, - CompleteSetup::class => true, + CompleteSetupTask::class => true, CompleteSetupNote::class => true, CouponBulkEdit::class => true, CouponHelper::class => true, @@ -299,7 +299,7 @@ function ( ...$arguments ) { $this->conditionally_share_with_tags( EventTracking::class, ContainerInterface::class ); $this->conditionally_share_with_tags( RESTControllers::class, ContainerInterface::class ); $this->conditionally_share_with_tags( ConnectionTest::class, ContainerInterface::class ); - $this->conditionally_share_with_tags( CompleteSetup::class ); + $this->share_with_tags( CompleteSetupTask::class ); $this->conditionally_share_with_tags( GlobalSiteTag::class, AssetsHandlerInterface::class, GoogleGtagJs::class, ProductHelper::class, WC::class, WP::class ); $this->share_with_tags( SiteVerificationMeta::class ); $this->conditionally_share_with_tags( MerchantSetupCompleted::class ); diff --git a/src/TaskList/CompleteSetup.php b/src/TaskList/CompleteSetup.php deleted file mode 100644 index 3f3c3b3a4a..0000000000 --- a/src/TaskList/CompleteSetup.php +++ /dev/null @@ -1,37 +0,0 @@ -get_id() ); - } - ); - } -} diff --git a/src/TaskList/CompleteSetupTask.php b/src/TaskList/CompleteSetupTask.php index 7ed83cab18..dba9ce0931 100644 --- a/src/TaskList/CompleteSetupTask.php +++ b/src/TaskList/CompleteSetupTask.php @@ -4,6 +4,9 @@ namespace Automattic\WooCommerce\GoogleListingsAndAds\TaskList; use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task; +use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists; +use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable; +use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service; use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterAwareInterface; use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterAwareTrait; @@ -12,10 +15,28 @@ * * @package Automattic\WooCommerce\GoogleListingsAndAds\TaskList */ -class CompleteSetupTask extends Task implements MerchantCenterAwareInterface { +class CompleteSetupTask extends Task implements Service, Registerable, MerchantCenterAwareInterface { use MerchantCenterAwareTrait; + /** + * Register a service. + * + * Add itself to the extended task list on init. + */ + public function register(): void { + add_action( + 'init', + function() { + $list_id = 'extended'; + + $this->task_list = TaskLists::get_list( $list_id ); + TaskLists::add_task( $list_id, $this ); + + do_action( 'add_woocommerce_extended_task_list_item', $this->get_id() ); + } + ); + } /** * Get the task id. * @@ -52,7 +73,7 @@ public function get_content() { * @return string */ public function get_time() { - return __( '20 minutes', 'woocommerce' ); + return __( '20 minutes', 'google-listings-and-ads' ); } /** @@ -71,7 +92,7 @@ public function is_dismissable() { * @return bool */ public function is_complete() { - return $this->merchant_center && $this->merchant_center->is_setup_complete() || false; + return $this->merchant_center->is_setup_complete(); } /** From d64ae3621daa16a2d7d6d201a76b000780b819ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Thu, 27 Jul 2023 14:19:52 +0200 Subject: [PATCH 03/21] Add npm script to generate hook's docs. Re-generate those docs. --- package.json | 1 + src/Hooks/README.md | 120 +++++++++++++++++++++++--------------------- 2 files changed, 65 insertions(+), 56 deletions(-) diff --git a/package.json b/package.json index e2e6e66f23..4509b4013b 100644 --- a/package.json +++ b/package.json @@ -88,6 +88,7 @@ "check-licenses": "wp-scripts check-licenses", "dev": "NODE_ENV=development wp-scripts build", "dewps:woo": "node bin/list-woo-dewped.mjs", + "doc:hooks": "php bin/HooksDocsGenerator.php", "doc:tracking": "woocommerce-grow-jsdoc ./js/src", "docker:up": "WP_VERSION=6.1 npx wc-e2e docker:up", "docker:down": "npx wc-e2e docker:down", diff --git a/src/Hooks/README.md b/src/Hooks/README.md index abb6778673..efbc664671 100644 --- a/src/Hooks/README.md +++ b/src/Hooks/README.md @@ -8,7 +8,7 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: -- CompleteSetup.php#L61 +- CompleteSetupTask.php#L36 ## bulk_edit_save_post @@ -74,8 +74,8 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: -- AttributesForm.php#L69 - AttributeManager.php#L295 +- AttributesForm.php#L69 ## woocommerce_gla_attribute_hidden_product_types_$ATTRIBUTE_ID @@ -220,6 +220,22 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: +- MerchantCenterService.php#L305 +- MerchantStatuses.php#L334 +- MerchantStatuses.php#L357 +- ProductMetaQueryHelper.php#L92 +- ProductMetaQueryHelper.php#L123 +- BatchProductHelper.php#L208 +- BatchProductHelper.php#L231 +- ProductHelper.php#L459 +- ProductHelper.php#L491 +- ProductRepository.php#L304 +- ProductSyncer.php#L149 +- ProductSyncer.php#L159 +- ProductSyncer.php#L235 +- ProductSyncer.php#L245 +- SyncerHooks.php#L197 +- WCProductAdapter.php#L203 - CouponHelper.php#L255 - CouponHelper.php#L292 - CouponSyncer.php#L102 @@ -232,26 +248,10 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p - CouponSyncer.php#L308 - CouponSyncer.php#L327 - SyncerHooks.php#L177 -- BatchProductHelper.php#L208 -- BatchProductHelper.php#L231 -- ProductHelper.php#L459 -- ProductHelper.php#L491 -- ProductRepository.php#L304 -- ProductSyncer.php#L149 -- ProductSyncer.php#L159 -- ProductSyncer.php#L235 -- ProductSyncer.php#L245 -- SyncerHooks.php#L197 -- WCProductAdapter.php#L203 -- ProductMetaQueryHelper.php#L92 -- ProductMetaQueryHelper.php#L123 -- IssuesController.php#L96 -- MerchantCenterService.php#L305 -- MerchantStatuses.php#L334 -- MerchantStatuses.php#L357 - ActionSchedulerJobMonitor.php#L117 - ActionSchedulerJobMonitor.php#L126 - CleanupSyncedProducts.php#L74 +- IssuesController.php#L96 ## woocommerce_gla_deleted_promotions @@ -315,10 +315,7 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: -- CouponMetaHandler.php#L220 -- CouponSyncer.php#L409 -- CouponSyncer.php#L447 -- CouponSyncer.php#L465 +- ProductMetaQueryHelper.php#L139 - BatchProductHelper.php#L248 - ProductHelper.php#L351 - ProductHelper.php#L567 @@ -328,10 +325,13 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p - ProductSyncer.php#L357 - ProductSyncer.php#L372 - AttributeManager.php#L269 +- CouponMetaHandler.php#L220 +- CouponSyncer.php#L409 +- CouponSyncer.php#L447 +- CouponSyncer.php#L465 - PHPView.php#L136 - PHPView.php#L164 - PHPView.php#L208 -- ProductMetaQueryHelper.php#L139 ## woocommerce_gla_exception @@ -339,26 +339,26 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: +- ScriptWithBuiltDependenciesAsset.php#L66 - WooCommercePreOrders.php#L111 - WooCommercePreOrders.php#L131 +- NoteInitializer.php#L74 +- NoteInitializer.php#L116 +- ProductSyncer.php#L134 +- ProductSyncer.php#L220 +- CouponSyncer.php#L202 +- CouponSyncer.php#L292 +- PHPView.php#L87 - DateTime.php#L44 - DateTime.php#L80 - ChannelVisibilityMetaBox.php#L190 - CouponChannelVisibilityMetaBox.php#L197 -- GoogleServiceProvider.php#L222 -- CouponSyncer.php#L202 -- CouponSyncer.php#L292 -- ProductSyncer.php#L134 -- ProductSyncer.php#L220 -- NoteInitializer.php#L74 -- NoteInitializer.php#L116 -- PHPView.php#L87 -- Connection.php#L95 +- PluginUpdate.php#L75 +- GoogleServiceProvider.php#L223 - ContactInformationController.php#L242 - ProductVisibilityController.php#L193 - SettingsSyncController.php#L79 -- PluginUpdate.php#L75 -- ScriptWithBuiltDependenciesAsset.php#L66 +- Connection.php#L95 - ClearProductStatsCache.php#L61 ## woocommerce_gla_force_run_install @@ -407,7 +407,7 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: -- GoogleServiceProvider.php#L246 +- GoogleServiceProvider.php#L247 - Connection.php#L70 - Connection.php#L91 - Connection.php#L126 @@ -494,13 +494,13 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: -- Merchant.php#L91 -- Merchant.php#L139 -- Merchant.php#L171 -- Merchant.php#L190 -- Merchant.php#L237 -- Merchant.php#L282 -- Merchant.php#L335 +- Merchant.php#L92 +- Merchant.php#L140 +- Merchant.php#L172 +- Merchant.php#L191 +- Merchant.php#L247 +- Merchant.php#L292 +- Merchant.php#L354 - MerchantReport.php#L95 ## woocommerce_gla_mc_settings_sync @@ -641,6 +641,14 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p - WCProductAdapter.php#L767 +## woocommerce_gla_product_query_args + +**Type**: filter + +**Used in**: + +- ProductRepository.php#L360 + ## woocommerce_gla_products_delete_retry_on_failure **Type**: filter @@ -671,9 +679,9 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: -- Middleware.php#L592 - RequestReviewController.php#L110 - RequestReviewController.php#L122 +- Middleware.php#L592 ## woocommerce_gla_request_review_response @@ -705,10 +713,10 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: -- Merchant.php#L92 +- AccountService.php#L365 +- Merchant.php#L93 - Middleware.php#L268 - Middleware.php#L274 -- AccountService.php#L365 ## woocommerce_gla_site_claim_overwrite_required @@ -724,7 +732,7 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: -- Merchant.php#L89 +- Merchant.php#L90 - Middleware.php#L263 ## woocommerce_gla_site_url @@ -733,7 +741,7 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: -- PluginHelper.php#L180 +- PluginHelper.php#L189 ## woocommerce_gla_site_verify_failure @@ -741,9 +749,9 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: -- SiteVerification.php#L56 -- SiteVerification.php#L64 -- SiteVerification.php#L85 +- SiteVerification.php#L58 +- SiteVerification.php#L66 +- SiteVerification.php#L87 ## woocommerce_gla_site_verify_success @@ -751,7 +759,7 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: -- SiteVerification.php#L83 +- SiteVerification.php#L85 ## woocommerce_gla_supported_coupon_types @@ -775,8 +783,8 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: -- SiteVerification.php#L118 -- SiteVerification.php#L154 +- SiteVerification.php#L120 +- SiteVerification.php#L162 ## woocommerce_gla_tax_excluded @@ -824,8 +832,8 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this p **Used in**: -- PluginHelper.php#L165 -- PluginHelper.php#L169 +- PluginHelper.php#L174 +- PluginHelper.php#L178 ## woocommerce_gla_weight_unit From bcde18ffe9970e8f97ef838b2fc3a94f881b5270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Thu, 27 Jul 2023 14:39:07 +0200 Subject: [PATCH 04/21] Add tests for CompleteSetupTask --- tests/Unit/TaskList/CompleteSetupTaskTest.php | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/Unit/TaskList/CompleteSetupTaskTest.php diff --git a/tests/Unit/TaskList/CompleteSetupTaskTest.php b/tests/Unit/TaskList/CompleteSetupTaskTest.php new file mode 100644 index 0000000000..31df1269df --- /dev/null +++ b/tests/Unit/TaskList/CompleteSetupTaskTest.php @@ -0,0 +1,75 @@ +merchant_center = $this->createMock( MerchantCenterService::class ); + + $this->task = new CompleteSetupTask(); + $this->task->set_merchant_center_object( $this->merchant_center ); + } + + public function test_id() { + $this->assertEquals( 'gla_complete_setup', $this->task->get_id() ); + } + + public function test_title() { + $this->assertEquals( 'Set up Google Listings & Ads', $this->task->get_title() ); + } + + public function test_time() { + $this->assertEquals( '20 minutes', $this->task->get_time() ); + } + public function test_dismissable() { + $this->assertTrue( $this->task->is_dismissable() ); + } + + public function test_is_complete_and_mc_setup_not_complete() { + $this->merchant_center->method( 'is_setup_complete' )->willReturn( false ); + + $this->assertFalse( $this->task->is_complete() ); + } + + public function test_is_complete_and_mc_setup_complete() { + $this->merchant_center->method( 'is_setup_complete' )->willReturn( true ); + + $this->assertTrue( $this->task->is_complete() ); + } + + public function test_get_action_url_and_mc_setup_not_complete() { + $this->merchant_center->method( 'is_setup_complete' )->willReturn( false ); + + $this->assertStringContainsString( 'admin.php?page=wc-admin&path=/google/start', $this->task->get_action_url() ); + } + + public function test_get_action_url_and_mc_setup_complete() { + $this->merchant_center->method( 'is_setup_complete' )->willReturn( true ); + + $this->assertStringContainsString( 'admin.php?page=wc-admin&path=/google/dashboard', $this->task->get_action_url() ); + } +} From 29f2e3695cd64b23762c0eb0c05e5daa0d98a815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Thu, 27 Jul 2023 16:05:55 +0200 Subject: [PATCH 05/21] Add tests for register and content methods. --- tests/Unit/TaskList/CompleteSetupTaskTest.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Unit/TaskList/CompleteSetupTaskTest.php b/tests/Unit/TaskList/CompleteSetupTaskTest.php index 31df1269df..86e6f5fa04 100644 --- a/tests/Unit/TaskList/CompleteSetupTaskTest.php +++ b/tests/Unit/TaskList/CompleteSetupTaskTest.php @@ -3,6 +3,7 @@ namespace Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\tasks; +use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists; use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService; use Automattic\WooCommerce\GoogleListingsAndAds\TaskList\CompleteSetupTask; use Automattic\WooCommerce\GoogleListingsAndAds\Tests\Framework\UnitTest; @@ -28,12 +29,30 @@ class CompleteSetupTaskTest extends UnitTest { */ public function setUp(): void { parent::setUp(); + // Mock tasks list. + TaskLists::clear_lists(); + TaskLists::add_list( [ 'id' => 'extended' ] ); + // Mock MC setup. $this->merchant_center = $this->createMock( MerchantCenterService::class ); $this->task = new CompleteSetupTask(); $this->task->set_merchant_center_object( $this->merchant_center ); } + /** + * Test that after the task is registered and `init` fired, + * the task is added to the `extended` list and the `add_woocommerce_extended_task_list_item` action is called. + */ + public function test_register() { + $this->task->register(); + do_action( 'init', 1 ); + + $this->assertGreaterThan( 0, did_action( 'add_woocommerce_extended_task_list_item' ) ); + + $this->assertNotNull( TaskLists::get_list( 'extended' )->get_task( $this->task->get_id() ) ); + $this->assertEquals( $this->task, TaskLists::get_list( 'extended' )->get_task( $this->task->get_id() ) ); + } + public function test_id() { $this->assertEquals( 'gla_complete_setup', $this->task->get_id() ); } @@ -42,9 +61,14 @@ public function test_title() { $this->assertEquals( 'Set up Google Listings & Ads', $this->task->get_title() ); } + public function test_content() { + $this->assertEquals( '', $this->task->get_content() ); + } + public function test_time() { $this->assertEquals( '20 minutes', $this->task->get_time() ); } + public function test_dismissable() { $this->assertTrue( $this->task->is_dismissable() ); } From e9d7c2178f24d21356b5b3349d5604b1aeecae53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Fri, 28 Jul 2023 17:15:08 +0200 Subject: [PATCH 06/21] Test the presence of CompleteSetupTask created on pre-test `init` Addresses https://github.com/woocommerce/google-listings-and-ads/pull/2026#discussion_r1276475848 --- tests/Unit/TaskList/CompleteSetupTaskTest.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/Unit/TaskList/CompleteSetupTaskTest.php b/tests/Unit/TaskList/CompleteSetupTaskTest.php index 86e6f5fa04..c46bda8738 100644 --- a/tests/Unit/TaskList/CompleteSetupTaskTest.php +++ b/tests/Unit/TaskList/CompleteSetupTaskTest.php @@ -29,9 +29,6 @@ class CompleteSetupTaskTest extends UnitTest { */ public function setUp(): void { parent::setUp(); - // Mock tasks list. - TaskLists::clear_lists(); - TaskLists::add_list( [ 'id' => 'extended' ] ); // Mock MC setup. $this->merchant_center = $this->createMock( MerchantCenterService::class ); @@ -44,13 +41,7 @@ public function setUp(): void { * the task is added to the `extended` list and the `add_woocommerce_extended_task_list_item` action is called. */ public function test_register() { - $this->task->register(); - do_action( 'init', 1 ); - - $this->assertGreaterThan( 0, did_action( 'add_woocommerce_extended_task_list_item' ) ); - $this->assertNotNull( TaskLists::get_list( 'extended' )->get_task( $this->task->get_id() ) ); - $this->assertEquals( $this->task, TaskLists::get_list( 'extended' )->get_task( $this->task->get_id() ) ); } public function test_id() { From 1c7b5fba988d0265e99b7dae5122b768e6462153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Fri, 28 Jul 2023 17:24:29 +0200 Subject: [PATCH 07/21] Test the methods of registered `CompleteSetupTask` As that is what actually matters from the feature's behavior perspective. --- tests/Unit/TaskList/CompleteSetupTaskTest.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/Unit/TaskList/CompleteSetupTaskTest.php b/tests/Unit/TaskList/CompleteSetupTaskTest.php index c46bda8738..03d99d1b5c 100644 --- a/tests/Unit/TaskList/CompleteSetupTaskTest.php +++ b/tests/Unit/TaskList/CompleteSetupTaskTest.php @@ -32,16 +32,19 @@ public function setUp(): void { // Mock MC setup. $this->merchant_center = $this->createMock( MerchantCenterService::class ); - $this->task = new CompleteSetupTask(); + // Fetch the task from the global list. + $this->task = TaskLists::get_list( 'extended' )->get_task( 'gla_complete_setup' ); $this->task->set_merchant_center_object( $this->merchant_center ); } /** - * Test that after the task is registered and `init` fired, - * the task is added to the `extended` list and the `add_woocommerce_extended_task_list_item` action is called. + * Test the task is added to the `extended` list. + * + * The addition should happen in `register` method, then `init` called before the test was started, + * so we do not precisely assert that timing. */ public function test_register() { - $this->assertNotNull( TaskLists::get_list( 'extended' )->get_task( $this->task->get_id() ) ); + $this->assertNotNull( TaskLists::get_list( 'extended' )->get_task( 'gla_complete_setup' ) ); } public function test_id() { From bcab4d41b36e740cb7adcfb09c8e8e547486a1de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Fri, 28 Jul 2023 17:25:52 +0200 Subject: [PATCH 08/21] Remove `add_woocommerce_extended_task_list_item` hook https://github.com/woocommerce/google-listings-and-ads/pull/2026#discussion_r1277468721 --- src/Hooks/README.md | 8 -------- src/TaskList/CompleteSetupTask.php | 2 -- 2 files changed, 10 deletions(-) diff --git a/src/Hooks/README.md b/src/Hooks/README.md index efbc664671..74feb0d3dc 100644 --- a/src/Hooks/README.md +++ b/src/Hooks/README.md @@ -2,14 +2,6 @@ A list of hooks, i.e `actions` and `filters`, that are defined or used in this project. -## add_woocommerce_extended_task_list_item - -**Type**: action - -**Used in**: - -- CompleteSetupTask.php#L36 - ## bulk_edit_save_post **Type**: action diff --git a/src/TaskList/CompleteSetupTask.php b/src/TaskList/CompleteSetupTask.php index dba9ce0931..bdaf399d3e 100644 --- a/src/TaskList/CompleteSetupTask.php +++ b/src/TaskList/CompleteSetupTask.php @@ -32,8 +32,6 @@ function() { $this->task_list = TaskLists::get_list( $list_id ); TaskLists::add_task( $list_id, $this ); - - do_action( 'add_woocommerce_extended_task_list_item', $this->get_id() ); } ); } From 14c47af51c49ad3d6c57702924fa9d16e4289736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Fri, 28 Jul 2023 17:31:58 +0200 Subject: [PATCH 09/21] Update PHPdoc in `CompleteSetupTaskTest` Co-authored-by: Mik --- tests/Unit/TaskList/CompleteSetupTaskTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/TaskList/CompleteSetupTaskTest.php b/tests/Unit/TaskList/CompleteSetupTaskTest.php index 03d99d1b5c..43ec48e9ef 100644 --- a/tests/Unit/TaskList/CompleteSetupTaskTest.php +++ b/tests/Unit/TaskList/CompleteSetupTaskTest.php @@ -18,7 +18,7 @@ */ class CompleteSetupTaskTest extends UnitTest { - /** @var MerchantCenterService $merchant_center */ + /** @var MerchantCenterService|MockObject $merchant_center */ protected $merchant_center; /** @var CompleteSetupTask $task */ From 057b8bfb3a80884f4c135cae6263e912590b1988 Mon Sep 17 00:00:00 2001 From: Eason Su Date: Tue, 1 Aug 2023 17:06:19 +0800 Subject: [PATCH 10/21] Make the gtag events script work with the Fast Refresh development mode. --- src/Google/GlobalSiteTag.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/Google/GlobalSiteTag.php b/src/Google/GlobalSiteTag.php index b948ad1019..71cf569be6 100644 --- a/src/Google/GlobalSiteTag.php +++ b/src/Google/GlobalSiteTag.php @@ -202,6 +202,7 @@ function () use ( $gtag_events ) { ] ); + $this->register_js_for_fast_refresh_dev(); $this->assets_handler->enqueue( $gtag_events ); } ); @@ -485,4 +486,39 @@ private static function is_first_time_customer( $customer_email ): bool { $orders = $query->get_orders(); return count( $orders ) === 1 ? true : false; } + + /** + * This method ONLY works during development in the Fast Refresh mode. + * + * The runtime.js and react-refresh-runtime.js files are created when the front-end development is + * running `npm run start:hot`, and they need to be loaded to make the gtag-events scrips work. + */ + private function register_js_for_fast_refresh_dev() { + // This file exists only when running `npm run start:hot` + $runtime_path = "{$this->get_root_dir()}/js/build/runtime.js"; + + if ( ! file_exists( $runtime_path ) ) { + return; + } + + $plugin_url = $this->get_plugin_url(); + + wp_enqueue_script( + 'gla-webpack-rumtime', + "{$plugin_url}/js/build/runtime.js", + [], + (string) filemtime( $runtime_path ), + false + ); + + // This script is one of the gtag-events dependencies, and its handle is wp-react-refresh-runtime. + // Ref: js/build/gtag-events.asset.php + wp_register_script( + 'wp-react-refresh-runtime', + "{$plugin_url}/js/build-dev/react-refresh-runtime.js", + [ 'gla-webpack-rumtime' ], + $this->get_version(), + false + ); + } } From ff83ba824d8bee84d4880fcb31a258c6a01811ef Mon Sep 17 00:00:00 2001 From: Eason Su Date: Tue, 1 Aug 2023 18:02:48 +0800 Subject: [PATCH 11/21] Fix "rumtime" typos --- src/Admin/Admin.php | 4 ++-- src/Google/GlobalSiteTag.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Admin/Admin.php b/src/Admin/Admin.php index d7c995404b..1c9b5faf44 100644 --- a/src/Admin/Admin.php +++ b/src/Admin/Admin.php @@ -286,12 +286,12 @@ private function inject_fast_refresh_for_dev( $scripts ) { $plugin_url = $this->get_plugin_url(); $scripts->add( - 'gla-webpack-rumtime', + 'gla-webpack-runtime', "{$plugin_url}/js/build/runtime.js", [], (string) filemtime( $runtime_path ) ); - $react_script->deps[] = 'gla-webpack-rumtime'; + $react_script->deps[] = 'gla-webpack-runtime'; if ( ! in_array( 'wp-react-refresh-entry', $react_script->deps, true ) ) { $scripts->add( diff --git a/src/Google/GlobalSiteTag.php b/src/Google/GlobalSiteTag.php index 71cf569be6..c158001063 100644 --- a/src/Google/GlobalSiteTag.php +++ b/src/Google/GlobalSiteTag.php @@ -504,7 +504,7 @@ private function register_js_for_fast_refresh_dev() { $plugin_url = $this->get_plugin_url(); wp_enqueue_script( - 'gla-webpack-rumtime', + 'gla-webpack-runtime', "{$plugin_url}/js/build/runtime.js", [], (string) filemtime( $runtime_path ), @@ -516,7 +516,7 @@ private function register_js_for_fast_refresh_dev() { wp_register_script( 'wp-react-refresh-runtime', "{$plugin_url}/js/build-dev/react-refresh-runtime.js", - [ 'gla-webpack-rumtime' ], + [ 'gla-webpack-runtime' ], $this->get_version(), false ); From 5565e313b501c2e0d9d7fd2e4d565b37fd027ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Tue, 1 Aug 2023 14:34:52 +0200 Subject: [PATCH 12/21] Assert the type of registered CompleteSetupTask Addresses https://github.com/woocommerce/google-listings-and-ads/pull/2026#pullrequestreview-1554669318 --- tests/Unit/TaskList/CompleteSetupTaskTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/Unit/TaskList/CompleteSetupTaskTest.php b/tests/Unit/TaskList/CompleteSetupTaskTest.php index 43ec48e9ef..ed8820dbdd 100644 --- a/tests/Unit/TaskList/CompleteSetupTaskTest.php +++ b/tests/Unit/TaskList/CompleteSetupTaskTest.php @@ -38,13 +38,15 @@ public function setUp(): void { } /** - * Test the task is added to the `extended` list. + * Test a CompleteSetupTask is added to the `extended` list. * - * The addition should happen in `register` method, then `init` called before the test was started, - * so we do not precisely assert that timing. + * The addition should happen in `register` method, then `init` called before the test was started. + * So, we do not precisely assert timing and exact instance, only the presence of a task. */ public function test_register() { $this->assertNotNull( TaskLists::get_list( 'extended' )->get_task( 'gla_complete_setup' ) ); + $this->assertInstanceOf( CompleteSetupTask::class, TaskLists::get_list( 'extended' )->get_task( 'gla_complete_setup' ) ); + } public function test_id() { From 37332943e19eca5585ed71798d55d0f1c3017715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Tue, 1 Aug 2023 15:29:52 +0200 Subject: [PATCH 13/21] Add more descriptive failure message in `CompleteSetupTaskTest::setUp` in case the task is not found. Addresses https://github.com/woocommerce/google-listings-and-ads/pull/2026#pullrequestreview-1554669318 --- tests/Unit/TaskList/CompleteSetupTaskTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Unit/TaskList/CompleteSetupTaskTest.php b/tests/Unit/TaskList/CompleteSetupTaskTest.php index ed8820dbdd..6d7234f6d0 100644 --- a/tests/Unit/TaskList/CompleteSetupTaskTest.php +++ b/tests/Unit/TaskList/CompleteSetupTaskTest.php @@ -34,6 +34,9 @@ public function setUp(): void { // Fetch the task from the global list. $this->task = TaskLists::get_list( 'extended' )->get_task( 'gla_complete_setup' ); + if ( ! $this->task ) { + $this->fail( '`gla_complete_setup` task not found in the extended list.' ); + } $this->task->set_merchant_center_object( $this->merchant_center ); } @@ -44,7 +47,6 @@ public function setUp(): void { * So, we do not precisely assert timing and exact instance, only the presence of a task. */ public function test_register() { - $this->assertNotNull( TaskLists::get_list( 'extended' )->get_task( 'gla_complete_setup' ) ); $this->assertInstanceOf( CompleteSetupTask::class, TaskLists::get_list( 'extended' )->get_task( 'gla_complete_setup' ) ); } From caba28b5f3401bf4836646f47c685225277e4688 Mon Sep 17 00:00:00 2001 From: Eason Su Date: Thu, 3 Aug 2023 15:58:36 +0800 Subject: [PATCH 14/21] Added test cases about compatibility to `AdaptiveForm` component. --- .../adaptive-form/adaptive-form.test.js | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/js/src/components/adaptive-form/adaptive-form.test.js b/js/src/components/adaptive-form/adaptive-form.test.js index 3b343535c6..9cab108370 100644 --- a/js/src/components/adaptive-form/adaptive-form.test.js +++ b/js/src/components/adaptive-form/adaptive-form.test.js @@ -196,4 +196,113 @@ describe( 'AdaptiveForm', () => { expect( inspect ).toHaveBeenLastCalledWith( false, 0 ); } ); + + describe( 'Compatibly patches', () => { + it( 'Should update all changes to values for the synchronous multiple calls to `setValue`', async () => { + render( + + { ( { setValue, values } ) => { + return ( + <> +