From 2898efa648ada5e5b94868eec5c87037529c4fa0 Mon Sep 17 00:00:00 2001 From: RJ <27843274+rjchow@users.noreply.github.com> Date: Mon, 13 May 2024 09:52:49 +1000 Subject: [PATCH] add: lys badge tracks (#46509) --- .../client/launch-your-store/status/index.tsx | 16 ++++++++ .../changelog/add-lys-badge-tracks | 4 ++ .../src/Admin/Features/LaunchYourStore.php | 38 ++++++++++++------- 3 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 plugins/woocommerce/changelog/add-lys-badge-tracks diff --git a/plugins/woocommerce-admin/client/launch-your-store/status/index.tsx b/plugins/woocommerce-admin/client/launch-your-store/status/index.tsx index b01ea94ded4..2e333b28506 100644 --- a/plugins/woocommerce-admin/client/launch-your-store/status/index.tsx +++ b/plugins/woocommerce-admin/client/launch-your-store/status/index.tsx @@ -6,6 +6,7 @@ import { Icon, moreVertical, edit, cog } from '@wordpress/icons'; import { Dropdown, Button, MenuGroup, MenuItem } from '@wordpress/components'; import { getAdminLink, getSetting } from '@woocommerce/settings'; import classnames from 'classnames'; +import { recordEvent } from '@woocommerce/tracks'; /** * Internal dependencies @@ -66,6 +67,16 @@ export const LaunchYourStoreStatus = ( { <> { + recordEvent( + 'launch_your_store_badge_menu_manage_site_visibility_click', + { + site_visibility: isComingSoon + ? 'coming_soon' + : 'live', + } + ); + } } // @ts-expect-error Prop gets passed down to underlying button https://developer.wordpress.org/block-editor/reference-guides/components/menu-item/#props href={ getAdminLink( 'admin.php?page=wc-settings&tab=site-visibility' @@ -80,6 +91,11 @@ export const LaunchYourStoreStatus = ( { { isComingSoon && getSetting( 'currentThemeIsFSETheme' ) && ( { + recordEvent( + 'launch_your_store_badge_menu_customize_coming_soon_click' + ); + } } // @ts-expect-error Prop gets passed down to underlying button https://developer.wordpress.org/block-editor/reference-guides/components/menu-item/#props href={ COMING_SOON_PAGE_EDITOR_LINK diff --git a/plugins/woocommerce/changelog/add-lys-badge-tracks b/plugins/woocommerce/changelog/add-lys-badge-tracks new file mode 100644 index 00000000000..69a5410e141 --- /dev/null +++ b/plugins/woocommerce/changelog/add-lys-badge-tracks @@ -0,0 +1,4 @@ +Significance: patch +Type: add + +Add tracks events for the LYS badge diff --git a/plugins/woocommerce/src/Admin/Features/LaunchYourStore.php b/plugins/woocommerce/src/Admin/Features/LaunchYourStore.php index 80865f089d5..b6528fcab0a 100644 --- a/plugins/woocommerce/src/Admin/Features/LaunchYourStore.php +++ b/plugins/woocommerce/src/Admin/Features/LaunchYourStore.php @@ -28,30 +28,42 @@ class LaunchYourStore { * @return void */ public function save_site_visibility_options() { - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized - if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'woocommerce-settings' ) ) { + $nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : ''; + if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'woocommerce-settings' ) ) { return; } + // options to allowed update and their allowed values. $options = array( 'woocommerce_coming_soon' => array( 'yes', 'no' ), 'woocommerce_store_pages_only' => array( 'yes', 'no' ), 'woocommerce_private_link' => array( 'yes', 'no' ), ); - $at_least_one_saved = false; - foreach ( $options as $name => $option ) { - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized - if ( isset( $_POST[ $name ] ) && in_array( $_POST[ $name ], $option, true ) ) { - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized - update_option( $name, wp_unslash( $_POST[ $name ] ) ); - $at_least_one_saved = true; - } - } + $event_data = array(); - if ( $at_least_one_saved ) { - wc_admin_record_tracks_event( 'site_visibility_saved' ); + foreach ( $options as $name => $allowed_values ) { + $current_value = get_option( $name, 'not set' ); + $new_value = $current_value; + + if ( isset( $_POST[ $name ] ) ) { + $input_value = sanitize_text_field( wp_unslash( $_POST[ $name ] ) ); + + // no-op if input value is invalid. + if ( in_array( $input_value, $allowed_values, true ) ) { + update_option( $name, $input_value ); + $new_value = $input_value; + + // log the transition if there is one. + if ( $current_value !== $new_value ) { + $enabled_or_disabled = 'yes' === $new_value ? 'enabled' : 'disabled'; + $event_data[ $name . '_toggled' ] = $enabled_or_disabled; + } + } + } + $event_data[ $name ] = $new_value; } + wc_admin_record_tracks_event( 'site_visibility_saved', $event_data ); } /**