From 8cdd549038524291295f6db612f6a68912007af8 Mon Sep 17 00:00:00 2001 From: Moon Date: Thu, 30 May 2024 06:14:20 +1200 Subject: [PATCH] Use LYS API endpoint for woocommerce_admin_launch_your_store_survey_completed option (#47915) * Add status-completed endpoint * Use new status-completed endpoint * Add changefile(s) from automation for the following project(s): woocommerce * Revert back unwanted changes * Cache /wc-admin/launch-your-store/survey-completed call --------- Co-authored-by: github-actions --- .../pages/launch-store-success/services.tsx | 25 +++++++++++++------ ...dicated-api-endpoint-for-lys-survey-status | 4 +++ .../src/Admin/API/LaunchYourStore.php | 21 ++++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 plugins/woocommerce/changelog/47915-update-47800-use-dedicated-api-endpoint-for-lys-survey-status diff --git a/plugins/woocommerce-admin/client/launch-your-store/hub/main-content/pages/launch-store-success/services.tsx b/plugins/woocommerce-admin/client/launch-your-store/hub/main-content/pages/launch-store-success/services.tsx index 8eec613181b..ca699876b6a 100644 --- a/plugins/woocommerce-admin/client/launch-your-store/hub/main-content/pages/launch-store-success/services.tsx +++ b/plugins/woocommerce-admin/client/launch-your-store/hub/main-content/pages/launch-store-success/services.tsx @@ -1,19 +1,28 @@ /** * External dependencies */ -import { - ONBOARDING_STORE_NAME, - OPTIONS_STORE_NAME, - PLUGINS_STORE_NAME, -} from '@woocommerce/data'; +import { ONBOARDING_STORE_NAME, PLUGINS_STORE_NAME } from '@woocommerce/data'; import { resolveSelect } from '@wordpress/data'; import { fromPromise } from 'xstate5'; +import apiFetch from '@wordpress/api-fetch'; + +type SurveyCompletedResponse = string | null; +let cachedSurveyCompleted: SurveyCompletedResponse = null; +const fetchSurveyCompletedOption = + async (): Promise< SurveyCompletedResponse > => { + if ( cachedSurveyCompleted !== null ) { + return cachedSurveyCompleted; + } + const response = await apiFetch( { + path: `/wc-admin/launch-your-store/survey-completed`, + } ); + cachedSurveyCompleted = response as SurveyCompletedResponse; + return cachedSurveyCompleted; + }; export const fetchCongratsData = fromPromise( async () => { const [ surveyCompleted, tasklists, activePlugins ] = await Promise.all( [ - resolveSelect( OPTIONS_STORE_NAME ).getOption( - 'woocommerce_admin_launch_your_store_survey_completed' - ), + fetchSurveyCompletedOption(), resolveSelect( ONBOARDING_STORE_NAME ).getTaskListsByIds( [ 'setup', 'extended', diff --git a/plugins/woocommerce/changelog/47915-update-47800-use-dedicated-api-endpoint-for-lys-survey-status b/plugins/woocommerce/changelog/47915-update-47800-use-dedicated-api-endpoint-for-lys-survey-status new file mode 100644 index 00000000000..278123c2c1c --- /dev/null +++ b/plugins/woocommerce/changelog/47915-update-47800-use-dedicated-api-endpoint-for-lys-survey-status @@ -0,0 +1,4 @@ +Significance: patch +Type: update + +Replace the use of options endpoint with the LYS API endpoint to query woocommerce_admin_launch_your_store_survey_completed option. \ No newline at end of file diff --git a/plugins/woocommerce/src/Admin/API/LaunchYourStore.php b/plugins/woocommerce/src/Admin/API/LaunchYourStore.php index 69307f226b6..bd0929df739 100644 --- a/plugins/woocommerce/src/Admin/API/LaunchYourStore.php +++ b/plugins/woocommerce/src/Admin/API/LaunchYourStore.php @@ -65,6 +65,18 @@ class LaunchYourStore { ), ) ); + + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/survey-completed', + array( + array( + 'methods' => 'GET', + 'callback' => array( $this, 'has_survey_completed' ), + 'permission_callback' => array( $this, 'must_be_shop_manager_or_admin' ), + ), + ) + ); } /** @@ -125,4 +137,13 @@ class LaunchYourStore { update_option( 'woocommerce_admin_launch_your_store_survey_completed', $request->get_param( 'status' ) ); return new \WP_REST_Response(); } + + /** + * Return woocommerce_admin_launch_your_store_survey_completed option. + * + * @return \WP_REST_Response + */ + public function has_survey_completed() { + return new \WP_REST_Response( get_option( 'woocommerce_admin_launch_your_store_survey_completed', 'no' ) ); + } }