-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Settings: Allow Users to Insert Header Code #86079
base: trunk
Are you sure you want to change the base?
Changes from all commits
65ee5f3
3181733
68d9726
41b73cd
6c3af5c
e77dc8c
c86ee66
2f8c8df
0597568
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { Button, Card, Gridicon } from '@automattic/components'; | ||
import { useTranslate } from 'i18n-calypso'; | ||
import { useEffect, useState } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import DocumentHead from 'calypso/components/data/document-head'; | ||
import QuerySiteSettings from 'calypso/components/data/query-site-settings'; | ||
import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation'; | ||
import FormTextarea from 'calypso/components/forms/form-textarea'; | ||
import HeaderCake from 'calypso/components/header-cake'; | ||
import InlineSupportLink from 'calypso/components/inline-support-link'; | ||
import Main from 'calypso/components/main'; | ||
import NavigationHeader from 'calypso/components/navigation-header'; | ||
import { successNotice, errorNotice } from 'calypso/state/notices/actions'; | ||
import getSiteSetting from 'calypso/state/selectors/get-site-setting'; | ||
import { saveSiteSettings } from 'calypso/state/site-settings/actions'; | ||
import { | ||
isRequestingSiteSettings, | ||
isSavingSiteSettings, | ||
isSiteSettingsSaveSuccessful, | ||
getSiteSettingsSaveError, | ||
} from 'calypso/state/site-settings/selectors'; | ||
import { getSelectedSiteId, getSelectedSiteSlug } from 'calypso/state/ui/selectors'; | ||
import './style.scss'; | ||
|
||
const HeaderCodeSettings = () => { | ||
const translate = useTranslate(); | ||
const dispatch = useDispatch(); | ||
|
||
const [ editedCode, setEditedCode ] = useState( '' ); | ||
|
||
const siteId = useSelector( ( state ) => getSelectedSiteId( state ) ); | ||
const headCode = useSelector( ( state ) => | ||
getSiteSetting( state, siteId, 'jetpack_header_code' ) | ||
); | ||
const isRequestingSettings = useSelector( ( state ) => | ||
isRequestingSiteSettings( state, siteId ) | ||
); | ||
const isSaveFailure = useSelector( ( state ) => getSiteSettingsSaveError( state, siteId ) ); | ||
const isSaveSuccess = useSelector( ( state ) => isSiteSettingsSaveSuccessful( state, siteId ) ); | ||
const isSavingSettings = useSelector( ( state ) => isSavingSiteSettings( state, siteId ) ); | ||
const siteSlug = useSelector( ( state ) => getSelectedSiteSlug( state, siteId ) ); | ||
|
||
const saveSettings = () => { | ||
dispatch( | ||
saveSiteSettings( siteId, { | ||
jetpack_header_code: editedCode, | ||
} ) | ||
); | ||
}; | ||
|
||
const noticeOptions = { | ||
duration: 4000, | ||
}; | ||
|
||
useEffect( () => { | ||
if ( isSaveSuccess ) { | ||
dispatch( successNotice( translate( 'Code updated successfully.' ), noticeOptions ) ); | ||
} else if ( isSaveFailure ) { | ||
dispatch( errorNotice( translate( 'There was an error saving your code.' ), noticeOptions ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question about errors: will we get more details about the error from the API? If yes, can we be a bit more specific about what kind of error occurred? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked at this further, and it seems that although there's error details, I'd need to manually add a switch case for each one. It doesn't seem like that's been consolidated anywhere in Calypso (unless I'm missing something!), so the rest of Settings just does a generic error. |
||
} | ||
}, [ isSaveSuccess, isSaveFailure ] ); | ||
|
||
useEffect( () => { | ||
if ( headCode ) { | ||
setEditedCode( headCode ); | ||
} | ||
}, [ headCode ] ); | ||
|
||
return ( | ||
<Main className="header-code"> | ||
<DocumentHead title={ translate( 'Header Code' ) } /> | ||
{ siteId && <QuerySiteSettings siteId={ siteId } /> } | ||
<NavigationHeader | ||
title={ translate( 'Header Code' ) } | ||
subtitle={ translate( 'Insert HTML meta tags and JavaScript code to your site.' ) } | ||
/> | ||
<HeaderCake backHref={ `/settings/general/${ siteSlug }` } isCompact> | ||
<h1>{ translate( 'Header Code' ) }</h1> | ||
</HeaderCake> | ||
<Card> | ||
<p className="header-code__text"> | ||
{ translate( | ||
'The following code will be inserted within {{code}}<head>{{/code}} on your site. {{link}}Learn more{{/link}}.', | ||
{ | ||
components: { | ||
code: <code />, | ||
link: <InlineSupportLink supportContext="insert-header-code" showIcon={ false } />, | ||
}, | ||
} | ||
) } | ||
</p> | ||
|
||
<FormTextarea | ||
className="header-code__editor" | ||
disabled={ isRequestingSettings || isSavingSettings } | ||
value={ editedCode } | ||
onChange={ ( input ) => setEditedCode( input.target.value ) } | ||
/> | ||
<FormSettingExplanation className="header-code__warning"> | ||
<Gridicon icon="info" size={ 18 } /> | ||
<span> | ||
{ translate( | ||
"This code will be run for all your site's viewers. Please make sure that you understand any code and trust its source before adding it here." | ||
) } | ||
</span> | ||
</FormSettingExplanation> | ||
<div className="header-code__button"> | ||
<Button | ||
disabled={ headCode === editedCode || ! editedCode || isRequestingSettings } | ||
primary | ||
busy={ isSavingSettings } | ||
onClick={ saveSettings } | ||
> | ||
{ translate( 'Save' ) } | ||
</Button> | ||
</div> | ||
</Card> | ||
</Main> | ||
); | ||
}; | ||
|
||
export default HeaderCodeSettings; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.header-code__editor { | ||
font-family: $code; | ||
font-size: $font-body-small; | ||
min-height: 250px; | ||
} | ||
|
||
.header-code .header-code__warning { | ||
display: flex; | ||
font-style: normal; | ||
margin-bottom: 1.5em; | ||
|
||
.gridicon { | ||
margin-right: 4px; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,12 +56,14 @@ class SiteTools extends Component { | |
showClone, | ||
showDeleteContent, | ||
showDeleteSite, | ||
showHeaderCode, | ||
showManageConnection, | ||
showStartSiteTransfer, | ||
siteId, | ||
} = this.props; | ||
|
||
const changeAddressLink = `/domains/manage/${ siteSlug }`; | ||
const headerCodeLink = `/settings/header-code/${ siteSlug }`; | ||
const startOverLink = `/settings/start-over/${ siteSlug }`; | ||
const startSiteTransferLink = `/settings/start-site-transfer/${ siteSlug }`; | ||
const deleteSiteLink = `/settings/delete-site/${ siteSlug }`; | ||
|
@@ -74,6 +76,9 @@ class SiteTools extends Component { | |
"Remove all posts, pages, and media to start fresh while keeping your site's address." | ||
); | ||
|
||
const headerCode = translate( 'Header code' ); | ||
const headerCodeText = translate( 'Insert code within the <head> of your site.' ); | ||
|
||
const deleteSite = translate( 'Delete your site permanently' ); | ||
const deleteSiteText = translate( | ||
"Delete all your posts, pages, media, and data, and give up your site's address." | ||
|
@@ -117,6 +122,13 @@ class SiteTools extends Component { | |
description={ copyText } | ||
/> | ||
) } | ||
{ showHeaderCode && ( | ||
<SiteToolsLink | ||
href={ headerCodeLink } | ||
title={ headerCode } | ||
description={ headerCodeText } | ||
/> | ||
) } | ||
{ showClone && ( | ||
<SiteToolsLink href={ cloneUrl } title={ cloneTitle } description={ cloneText } /> | ||
) } | ||
|
@@ -219,6 +231,7 @@ export default compose( [ | |
showClone: 'active' === rewindState.state && ! isAtomic, | ||
showDeleteContent: isAtomic || ( ! isJetpack && ! isVip && ! isP2Hub ), | ||
showDeleteSite: ( ! isJetpack || isAtomic ) && ! isVip && sitePurchasesLoaded, | ||
showHeaderCode: isJetpack || isAtomic, | ||
Aurorum marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we show this only to admins? (if the current user has the capability to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessary in this case! The entire page is blocked for non-admins. |
||
showManageConnection: isJetpack && ! isAtomic, | ||
showStartSiteTransfer, | ||
siteId, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will all site types support this? I see Jetpack sites will, but what about WP.com simple / Atomic / etc.?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code in Automattic/jetpack#34881 explicitly excludes Simple. As things stand I don't see anything stopping it from Atomic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, we should make sure to redirect in Calypso if someone tries to access this with a WP.com simple site.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comments below!