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/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 1a7caa7405..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**: - -- CompleteSetup.php#L61 - ## bulk_edit_save_post **Type**: action @@ -18,14 +10,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 @@ -82,8 +66,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 @@ -228,6 +212,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 @@ -240,26 +240,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 @@ -323,10 +307,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 @@ -336,10 +317,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 @@ -347,26 +331,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 @@ -415,7 +399,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 @@ -502,13 +486,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 @@ -649,6 +633,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 @@ -679,9 +671,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 @@ -713,10 +705,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 @@ -732,7 +724,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 @@ -741,7 +733,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 @@ -749,9 +741,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 @@ -759,7 +751,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 @@ -783,8 +775,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 @@ -832,8 +824,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 diff --git a/src/Internal/DependencyManagement/CoreServiceProvider.php b/src/Internal/DependencyManagement/CoreServiceProvider.php index 74347a6b2b..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, AssetsHandlerInterface::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 e2dbdda1de..0000000000 --- a/src/TaskList/CompleteSetup.php +++ /dev/null @@ -1,104 +0,0 @@ -assets_handler = $assets_handler; - } - - /** - * Register a service. - */ - 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' ); - } - ); - } - - /** - * 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..bdaf399d3e --- /dev/null +++ b/src/TaskList/CompleteSetupTask.php @@ -0,0 +1,109 @@ +task_list = TaskLists::get_list( $list_id ); + TaskLists::add_task( $list_id, $this ); + } + ); + } + /** + * Get the task id. + * + * @return string + */ + public function get_id() { + return 'gla_complete_setup'; + } + + /** + * Get the task name. + * + * @return string + */ + public function get_title() { + return __( + 'Set up Google Listings & Ads', + 'google-listings-and-ads' + ); + } + + /** + * Get the task description. + * + * @return string empty string + */ + public function get_content() { + return ''; + } + + /** + * Get the task completion time. + * + * @return string + */ + public function get_time() { + return __( '20 minutes', 'google-listings-and-ads' ); + } + + /** + * Always dismissable. + * + * @return bool + */ + public function is_dismissable() { + return true; + } + + /** + * Get completion status. + * Forwards from the merchant center setup status. + * + * @return bool + */ + public function is_complete() { + return $this->merchant_center->is_setup_complete(); + } + + /** + * 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/tests/Unit/TaskList/CompleteSetupTaskTest.php b/tests/Unit/TaskList/CompleteSetupTaskTest.php new file mode 100644 index 0000000000..6d7234f6d0 --- /dev/null +++ b/tests/Unit/TaskList/CompleteSetupTaskTest.php @@ -0,0 +1,97 @@ +merchant_center = $this->createMock( MerchantCenterService::class ); + + // 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 ); + } + + /** + * 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 timing and exact instance, only the presence of a task. + */ + public function test_register() { + $this->assertInstanceOf( CompleteSetupTask::class, TaskLists::get_list( 'extended' )->get_task( 'gla_complete_setup' ) ); + + } + + 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_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() ); + } + + 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() ); + } +} 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',