2024-03-08 04:11:52 +00:00
/ * *
* External dependencies
* /
import {
createSlotFill ,
ToggleControl ,
RadioControl ,
2024-03-29 05:08:46 +00:00
Button ,
2024-03-08 04:11:52 +00:00
} from '@wordpress/components' ;
2024-04-05 01:26:30 +00:00
import {
useState ,
createInterpolateElement ,
createElement ,
2024-05-13 14:56:51 +00:00
useEffect ,
2024-08-20 03:17:17 +00:00
useRef ,
2024-04-05 01:26:30 +00:00
} from '@wordpress/element' ;
2024-03-08 04:11:52 +00:00
import { registerPlugin } from '@wordpress/plugins' ;
import { _ _ } from '@wordpress/i18n' ;
2024-05-31 03:49:36 +00:00
import clsx from 'clsx' ;
2024-03-29 05:08:46 +00:00
import { useCopyToClipboard } from '@wordpress/compose' ;
2024-04-04 23:50:26 +00:00
import { recordEvent } from '@woocommerce/tracks' ;
2024-04-16 02:36:43 +00:00
import { getSetting } from '@woocommerce/settings' ;
2024-03-08 04:11:52 +00:00
/ * *
* Internal dependencies
* /
2024-03-11 23:50:22 +00:00
import { SETTINGS _SLOT _FILL _CONSTANT } from '../../settings/settings-slots' ;
2024-03-08 04:11:52 +00:00
import './style.scss' ;
2024-04-20 00:02:53 +00:00
import {
COMING _SOON _PAGE _EDITOR _LINK ,
SITE _VISIBILITY _DOC _LINK ,
} from '../constants' ;
2024-08-20 03:17:17 +00:00
import { ConfirmationModal } from './components/confirmation-modal' ;
2024-03-08 04:11:52 +00:00
const { Fill } = createSlotFill ( SETTINGS _SLOT _FILL _CONSTANT ) ;
2024-03-11 23:50:22 +00:00
const SiteVisibility = ( ) => {
2024-05-13 14:56:51 +00:00
const setting = window ? . wcSettings ? . admin ? . siteVisibilitySettings || { } ;
const shareKey = setting ? . woocommerce _share _key ;
2024-03-29 05:08:46 +00:00
const [ comingSoon , setComingSoon ] = useState (
2024-05-13 14:56:51 +00:00
setting ? . woocommerce _coming _soon || 'no'
2024-03-29 05:08:46 +00:00
) ;
2024-03-08 04:11:52 +00:00
const [ storePagesOnly , setStorePagesOnly ] = useState (
2024-05-13 14:56:51 +00:00
setting ? . woocommerce _store _pages _only || 'no'
2024-03-08 04:11:52 +00:00
) ;
2024-03-29 05:08:46 +00:00
const [ privateLink , setPrivateLink ] = useState (
2024-05-13 14:56:51 +00:00
setting ? . woocommerce _private _link || 'no'
2024-03-29 05:08:46 +00:00
) ;
2024-08-20 03:17:17 +00:00
const formRef = useRef ( null ) ;
const saveButtonRef = useRef ( null ) ;
useEffect ( ( ) => {
const saveButton = document . getElementsByClassName (
'woocommerce-save-button'
) [ 0 ] ;
if ( saveButton ) {
saveButtonRef . current = saveButton ;
}
const form = document . querySelector ( '#mainform' ) ;
if ( form ) {
formRef . current = form ;
}
} , [ ] ) ;
2024-03-29 05:08:46 +00:00
2024-05-13 14:56:51 +00:00
useEffect ( ( ) => {
const initValues = {
comingSoon : setting . woocommerce _coming _soon ,
storePagesOnly : setting . woocommerce _store _pages _only ,
privateLink : setting . woocommerce _private _link || 'no' ,
} ;
const currentValues = { comingSoon , storePagesOnly , privateLink } ;
const saveButton = document . getElementsByClassName (
'woocommerce-save-button'
) [ 0 ] ;
if ( saveButton ) {
saveButton . disabled =
initValues . comingSoon === currentValues . comingSoon &&
initValues . storePagesOnly === currentValues . storePagesOnly &&
initValues . privateLink === currentValues . privateLink ;
}
} , [ comingSoon , storePagesOnly , privateLink ] ) ;
2024-03-29 05:08:46 +00:00
const copyLink = _ _ ( 'Copy link' , 'woocommerce' ) ;
const copied = _ _ ( 'Copied!' , 'woocommerce' ) ;
const [ copyLinkText , setCopyLinkText ] = useState ( copyLink ) ;
2024-03-08 04:11:52 +00:00
2024-03-29 05:08:46 +00:00
const getPrivateLink = ( ) => {
2024-06-14 01:23:32 +00:00
const settings = window ? . wcSettings ;
const homeUrl = settings ? . homeUrl ;
const urlObject = new URL ( homeUrl ) ;
2024-03-29 05:08:46 +00:00
if ( storePagesOnly === 'yes' ) {
2024-06-14 01:23:32 +00:00
const shopPermalink =
settings ? . admin ? . siteVisibilitySettings ? . shop _permalink ;
if ( shopPermalink ) {
urlObject . href = shopPermalink ;
}
2024-03-11 23:50:22 +00:00
}
2024-03-29 05:08:46 +00:00
2024-06-14 01:23:32 +00:00
const params = new URLSearchParams ( urlObject . search ) ;
params . set ( 'woo-share' , shareKey ) ;
urlObject . search = params . toString ( ) ;
return urlObject . toString ( ) ;
2024-03-29 05:08:46 +00:00
} ;
const copyClipboardRef = useCopyToClipboard ( getPrivateLink , ( ) => {
setCopyLinkText ( copied ) ;
setTimeout ( ( ) => {
setCopyLinkText ( copyLink ) ;
} , 2000 ) ;
} ) ;
2024-03-08 04:11:52 +00:00
return (
2024-03-29 05:08:46 +00:00
< div className = "site-visibility-settings-slotfill" >
2024-03-08 04:11:52 +00:00
< input
type = "hidden"
value = { comingSoon }
name = "woocommerce_coming_soon"
/ >
< input
type = "hidden"
value = { storePagesOnly }
name = "woocommerce_store_pages_only"
/ >
< input
type = "hidden"
value = { privateLink }
name = "woocommerce_private_link"
/ >
2024-04-02 02:32:12 +00:00
< h2 > { _ _ ( 'Site visibility' , 'woocommerce' ) } < / h 2 >
2024-03-08 04:11:52 +00:00
< p className = "site-visibility-settings-slotfill-description" >
2024-08-20 05:49:05 +00:00
{ createInterpolateElement (
_ _ (
'Manage how your site appears to visitors. <a>Learn more</a>' ,
'woocommerce'
) ,
{
a : createElement ( 'a' , {
target : '_blank' ,
rel : 'noreferrer' ,
href : SITE _VISIBILITY _DOC _LINK ,
} ) ,
}
2024-03-08 04:11:52 +00:00
) }
< / p >
< div className = "site-visibility-settings-slotfill-section" >
< RadioControl
onChange = { ( ) => {
setComingSoon ( 'yes' ) ;
2024-04-04 23:50:26 +00:00
recordEvent ( 'site_visibility_toggle' , {
status : 'coming_soon' ,
} ) ;
2024-03-08 04:11:52 +00:00
} }
options = { [
{
2024-08-01 09:13:18 +00:00
label : _ _ ( 'Coming soon' , 'woocommerce' ) ,
2024-03-08 04:11:52 +00:00
value : 'yes' ,
} ,
] }
selected = { comingSoon }
/ >
< p className = "site-visibility-settings-slotfill-section-description" >
2024-04-16 02:36:43 +00:00
{ getSetting ( 'currentThemeIsFSETheme' )
? createInterpolateElement (
_ _ (
'Your site is hidden from visitors behind a “Coming soon” landing page until it’ s ready for viewing. You can customize your “Coming soon” landing page via the <a>Editor</a>.' ,
'woocommerce'
) ,
{
a : createElement ( 'a' , {
2024-08-20 05:49:05 +00:00
target : '_blank' ,
2024-04-16 02:36:43 +00:00
href : COMING _SOON _PAGE _EDITOR _LINK ,
} ) ,
}
)
: _ _ (
'Your site is hidden from visitors behind a “Coming soon” landing page until it’ s ready for viewing.' ,
'woocommerce'
) }
2024-03-08 04:11:52 +00:00
< / p >
< div
2024-05-31 03:49:36 +00:00
className = { clsx (
2024-03-08 04:11:52 +00:00
'site-visibility-settings-slotfill-section-content' ,
{
'is-hidden' : comingSoon !== 'yes' ,
}
) }
>
< ToggleControl
label = {
< >
{ _ _ (
2024-08-20 05:49:05 +00:00
'Apply to store pages only' ,
2024-03-08 04:11:52 +00:00
'woocommerce'
) }
< p >
2024-08-20 05:49:05 +00:00
{ _ _ (
'Display a “coming soon” message on your store pages — the rest of your site will remain visible.' ,
'woocommerce'
2024-03-08 04:11:52 +00:00
) }
< / p >
< / >
}
checked = { storePagesOnly === 'yes' }
2024-04-04 23:50:26 +00:00
onChange = { ( enabled ) => {
2024-03-08 04:11:52 +00:00
setStorePagesOnly (
storePagesOnly === 'yes' ? 'no' : 'yes'
) ;
2024-04-04 23:50:26 +00:00
recordEvent (
'site_visibility_restrict_store_pages_only_toggle' ,
{
enabled ,
}
) ;
2024-03-08 04:11:52 +00:00
} }
/ >
< ToggleControl
label = {
< >
{ _ _ (
'Share your site with a private link' ,
'woocommerce'
) }
< p >
{ _ _ (
2024-04-20 00:02:53 +00:00
'Share your site with anyone using a private link.' ,
2024-03-08 04:11:52 +00:00
'woocommerce'
) }
< / p >
< / >
}
checked = { privateLink === 'yes' }
2024-04-04 23:50:26 +00:00
onChange = { ( enabled ) => {
2024-03-08 04:11:52 +00:00
setPrivateLink (
privateLink === 'yes' ? 'no' : 'yes'
) ;
2024-04-04 23:50:26 +00:00
recordEvent (
'site_visibility_share_private_link_toggle' ,
{
enabled ,
}
) ;
2024-03-08 04:11:52 +00:00
} }
/ >
< / d i v >
2024-04-10 10:56:03 +00:00
{ comingSoon === 'yes' && privateLink === 'yes' && (
2024-04-04 23:50:26 +00:00
< div className = "site-visibility-settings-slotfill-private-link" >
2024-04-10 10:56:03 +00:00
< input value = { getPrivateLink ( ) } readOnly / >
2024-04-04 23:50:26 +00:00
< Button
ref = { copyClipboardRef }
variant = "link"
onClick = { ( ) => {
recordEvent (
'site_visibility_private_link_copy'
) ;
} }
>
{ copyLinkText }
< / B u t t o n >
< / d i v >
) }
2024-03-08 04:11:52 +00:00
< / d i v >
< div className = "site-visibility-settings-slotfill-section" >
< RadioControl
onChange = { ( ) => {
setComingSoon ( 'no' ) ;
2024-04-04 23:50:26 +00:00
recordEvent ( 'site_visibility_toggle' , {
status : 'live' ,
} ) ;
2024-03-08 04:11:52 +00:00
} }
options = { [
{
2024-08-01 09:13:18 +00:00
label : _ _ ( 'Live' , 'woocommerce' ) ,
2024-03-08 04:11:52 +00:00
value : 'no' ,
} ,
] }
selected = { comingSoon }
/ >
< p className = "site-visibility-settings-slotfill-section-description" >
2024-04-02 02:32:12 +00:00
{ _ _ (
'Your entire site is visible to everyone.' ,
'woocommerce'
) }
2024-03-08 04:11:52 +00:00
< / p >
< / d i v >
2024-08-20 03:17:17 +00:00
{ formRef . current && saveButtonRef . current ? (
< ConfirmationModal
saveButtonRef = { saveButtonRef }
formRef = { formRef }
currentSetting = { setting }
/ >
) : null }
2024-03-08 04:11:52 +00:00
< / d i v >
) ;
} ;
const SiteVisibilitySlotFill = ( ) => {
return (
< Fill >
2024-03-11 23:50:22 +00:00
< SiteVisibility / >
2024-03-08 04:11:52 +00:00
< / F i l l >
) ;
} ;
export const registerSiteVisibilitySlotFill = ( ) => {
registerPlugin ( 'woocommerce-admin-site-visibility-settings-slotfill' , {
2024-04-04 21:28:58 +00:00
scope : 'woocommerce-site-visibility-settings' ,
2024-03-08 04:11:52 +00:00
render : SiteVisibilitySlotFill ,
} ) ;
} ;