From 145d75a08fc607178ed4bda1f482b4964c4adecb Mon Sep 17 00:00:00 2001 From: Moon Date: Thu, 28 Mar 2024 22:08:46 -0700 Subject: [PATCH] Add private link for LYS (#45915) * Add private link * Add changefile(s) from automation for the following project(s): woocommerce * Use useCopyToClipboard and remove label click behavior * Preload settings for site visibility * Default to live when woocommerce_coming_soon option is not available * Remove use of classnames -- no longer needed * Lint fixes * Lint fixes * Remove changes for label click behavior -- we will work on it in a separate PR * Remove unused import --------- Co-authored-by: github-actions --- .../launch-your-store/settings/slotfill.js | 77 +++++++++++++------ .../launch-your-store/settings/style.scss | 16 ++++ ...45915-update-45904-lys-provide-private-url | 4 + .../src/Admin/Features/LaunchYourStore.php | 25 ++++++ 4 files changed, 98 insertions(+), 24 deletions(-) create mode 100644 plugins/woocommerce/changelog/45915-update-45904-lys-provide-private-url diff --git a/plugins/woocommerce-admin/client/launch-your-store/settings/slotfill.js b/plugins/woocommerce-admin/client/launch-your-store/settings/slotfill.js index 476d34408ed..6a6b2bb836a 100644 --- a/plugins/woocommerce-admin/client/launch-your-store/settings/slotfill.js +++ b/plugins/woocommerce-admin/client/launch-your-store/settings/slotfill.js @@ -5,48 +5,66 @@ import { createSlotFill, ToggleControl, RadioControl, + Button, } from '@wordpress/components'; -import { useState, useEffect } from '@wordpress/element'; +import { useState } from '@wordpress/element'; import { registerPlugin } from '@wordpress/plugins'; import { __ } from '@wordpress/i18n'; import classNames from 'classnames'; +import { useCopyToClipboard } from '@wordpress/compose'; /** * Internal dependencies */ import { SETTINGS_SLOT_FILL_CONSTANT } from '../../settings/settings-slots'; -import { useLaunchYourStore } from '../use-launch-your-store'; import './style.scss'; const { Fill } = createSlotFill( SETTINGS_SLOT_FILL_CONSTANT ); const SiteVisibility = () => { - const { - isLoading, - comingSoon: initialComingSoon = false, - storePagesOnly: initialStorePagesOnly = false, - privateLink: initialPrivateLink = false, - } = useLaunchYourStore(); - const [ comingSoon, setComingSoon ] = useState( initialComingSoon ); - const [ storePagesOnly, setStorePagesOnly ] = useState( - initialStorePagesOnly - ); - const [ privateLink, setPrivateLink ] = useState( initialPrivateLink ); + const shareKey = + window?.wcSettings?.admin?.siteVisibilitySettings + ?.woocommerce_share_key; - useEffect( () => { - if ( ! isLoading ) { - setComingSoon( initialComingSoon ); - setStorePagesOnly( initialStorePagesOnly ); - setPrivateLink( initialPrivateLink ); + const [ comingSoon, setComingSoon ] = useState( + window?.wcSettings?.admin?.siteVisibilitySettings + ?.woocommerce_coming_soon || 'no' + ); + const [ storePagesOnly, setStorePagesOnly ] = useState( + window?.wcSettings?.admin?.siteVisibilitySettings + ?.woocommerce_store_pages_only + ); + const [ privateLink, setPrivateLink ] = useState( + window?.wcSettings?.admin?.siteVisibilitySettings + ?.woocommerce_private_link + ); + + const copyLink = __( 'Copy link', 'woocommerce' ); + const copied = __( 'Copied!', 'woocommerce' ); + const [ copyLinkText, setCopyLinkText ] = useState( copyLink ); + + const getPrivateLink = () => { + if ( storePagesOnly === 'yes' ) { + return ( + window?.wcSettings?.admin?.siteVisibilitySettings + ?.shop_permalink + + '?woo-share=' + + shareKey + ); } - }, [ isLoading ] ); + + return window?.wcSettings?.homeUrl + '?woo-share=' + shareKey; + }; + + const copyClipboardRef = useCopyToClipboard( getPrivateLink, () => { + setCopyLinkText( copied ); + setTimeout( () => { + setCopyLinkText( copyLink ); + }, 2000 ); + } ); return ( -
+
{ 'woocommerce' ) }

+ { privateLink === 'yes' && ( +
+ { getPrivateLink() } + +
+ ) } } checked={ privateLink === 'yes' } diff --git a/plugins/woocommerce-admin/client/launch-your-store/settings/style.scss b/plugins/woocommerce-admin/client/launch-your-store/settings/style.scss index e05520fc3f5..404db0206dc 100644 --- a/plugins/woocommerce-admin/client/launch-your-store/settings/style.scss +++ b/plugins/woocommerce-admin/client/launch-your-store/settings/style.scss @@ -8,6 +8,22 @@ opacity: 0.3; } + .site-visibility-settings-slotfill-private-link { + padding: 8px 16px; + border-radius: 4px; + border: 1px solid #dcdcde; + background: #fff; + display: flex; + justify-content: space-between; + button { + text-decoration: none; + &:focus { + outline: none !important; + box-shadow: none; + } + } + } + .components-form-toggle { .components-form-toggle__input:focus + .components-form-toggle__track { box-shadow: none; diff --git a/plugins/woocommerce/changelog/45915-update-45904-lys-provide-private-url b/plugins/woocommerce/changelog/45915-update-45904-lys-provide-private-url new file mode 100644 index 00000000000..8aef1a2c085 --- /dev/null +++ b/plugins/woocommerce/changelog/45915-update-45904-lys-provide-private-url @@ -0,0 +1,4 @@ +Significance: patch +Type: update + +Add private link with copy link functionality \ No newline at end of file diff --git a/plugins/woocommerce/src/Admin/Features/LaunchYourStore.php b/plugins/woocommerce/src/Admin/Features/LaunchYourStore.php index 20cb3a2950c..07020c3974b 100644 --- a/plugins/woocommerce/src/Admin/Features/LaunchYourStore.php +++ b/plugins/woocommerce/src/Admin/Features/LaunchYourStore.php @@ -14,6 +14,7 @@ class LaunchYourStore { public function __construct() { add_action( 'woocommerce_update_options_general', array( $this, 'save_site_visibility_options' ) ); add_action( 'current_screen', array( $this, 'maybe_create_coming_soon_page' ) ); + add_filter( 'woocommerce_admin_shared_settings', array( $this, 'preload_settings' ) ); } /** @@ -68,4 +69,28 @@ class LaunchYourStore { update_option( $option_name, $page_id_option, true ); } } + + /** + * Preload settings for Site Visibility. + * + * @param array $settings settings array. + * + * @return mixed + */ + public function preload_settings( $settings ) { + $current_screen = get_current_screen(); + $is_setting_page = $current_screen && 'woocommerce_page_wc-settings' === $current_screen->id; + + if ( $is_setting_page ) { + $settings['siteVisibilitySettings'] = array( + 'shop_permalink' => get_permalink( wc_get_page_id( 'shop' ) ), + 'woocommerce_coming_soon' => get_option( 'woocommerce_coming_soon' ), + 'woocommerce_store_pages_only' => get_option( 'woocommerce_store_pages_only' ), + 'woocommerce_private_link' => get_option( 'woocommerce_private_link' ), + 'woocommerce_share_key' => get_option( 'woocommerce_share_key' ), + ); + } + + return $settings; + } }