-
Notifications
You must be signed in to change notification settings - Fork 797
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CookieConsent: use template saved in settings (#31511)
Co-authored-by: Omar Alshaker <omar@omaralshaker.com>
- Loading branch information
1 parent
9ad06c9
commit 79a5462
Showing
10 changed files
with
190 additions
and
2 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/plugins/jetpack/changelog/add-cookie-consent-setting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: enhancement | ||
|
||
Persist block markup and ensure it is rendered in the footer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...ugins/jetpack/extensions/blocks/cookie-consent/test/fixtures/jetpack__cookie-consent.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<!-- wp:jetpack/cookie-consent --> | ||
<div class="wp-block-jetpack-cookie-consent alignwide has-text-color has-background has-text-color has-background" style="color:var(--wp--preset--color--contrast);background-color:var(--wp--preset--color--tertiary);padding-top:1em;padding-right:1em;padding-bottom:1em;padding-left:1em" role="dialog" aria-modal="true"><p>Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use. To find out more, including how to control cookies, see here: <a href="https://automattic.com/cookies/">Cookie Policy</a>.</p><!-- wp:button --> | ||
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button">Accept</a></div> | ||
<!-- /wp:button --><span>365</span></div> | ||
<!-- /wp:jetpack/cookie-consent --> |
30 changes: 30 additions & 0 deletions
30
...ugins/jetpack/extensions/blocks/cookie-consent/test/fixtures/jetpack__cookie-consent.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "jetpack/cookie-consent", | ||
"isValid": true, | ||
"attributes": { | ||
"text": "Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use. To find out more, including how to control cookies, see here: <a href=\"https://automattic.com/cookies/\">Cookie Policy</a>.", | ||
"style": { | ||
"color": { | ||
"text": "var(--wp--preset--color--contrast)", | ||
"background": "var(--wp--preset--color--tertiary)", | ||
"link": "var(--wp--preset--color--contrast)" | ||
}, | ||
"spacing": { | ||
"padding": { | ||
"top": "1em", | ||
"right": "1em", | ||
"bottom": "1em", | ||
"left": "1em" | ||
} | ||
} | ||
}, | ||
"align": "wide", | ||
"consentExpiryDays": 365, | ||
"showOverlay": false | ||
}, | ||
"innerBlocks": [], | ||
"originalContent": "<div class=\"wp-block-jetpack-cookie-consent alignwide has-text-color has-background has-text-color has-background\" style=\"color:var(--wp--preset--color--contrast);background-color:var(--wp--preset--color--tertiary);padding-top:1em;padding-right:1em;padding-bottom:1em;padding-left:1em\" role=\"dialog\" aria-modal=\"true\"><p>Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use. To find out more, including how to control cookies, see here: <a href=\"https://automattic.com/cookies/\">Cookie Policy</a>.</p><span>365</span></div>" | ||
} | ||
] |
23 changes: 23 additions & 0 deletions
23
...etpack/extensions/blocks/cookie-consent/test/fixtures/jetpack__cookie-consent.parsed.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[ | ||
{ | ||
"blockName": "jetpack/cookie-consent", | ||
"attrs": {}, | ||
"innerBlocks": [ | ||
{ | ||
"blockName": "core/button", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n\t<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\">Accept</a></div>\n\t", | ||
"innerContent": [ | ||
"\n\t<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\">Accept</a></div>\n\t" | ||
] | ||
} | ||
], | ||
"innerHTML": "\n<div class=\"wp-block-jetpack-cookie-consent alignwide has-text-color has-background has-text-color has-background\" style=\"color:var(--wp--preset--color--contrast);background-color:var(--wp--preset--color--tertiary);padding-top:1em;padding-right:1em;padding-bottom:1em;padding-left:1em\" role=\"dialog\" aria-modal=\"true\"><p>Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use. To find out more, including how to control cookies, see here: <a href=\"https://automattic.com/cookies/\">Cookie Policy</a>.</p><span>365</span></div>\n", | ||
"innerContent": [ | ||
"\n<div class=\"wp-block-jetpack-cookie-consent alignwide has-text-color has-background has-text-color has-background\" style=\"color:var(--wp--preset--color--contrast);background-color:var(--wp--preset--color--tertiary);padding-top:1em;padding-right:1em;padding-bottom:1em;padding-left:1em\" role=\"dialog\" aria-modal=\"true\"><p>Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use. To find out more, including how to control cookies, see here: <a href=\"https://automattic.com/cookies/\">Cookie Policy</a>.</p>", | ||
null, | ||
"<span>365</span></div>\n" | ||
] | ||
} | ||
] |
3 changes: 3 additions & 0 deletions
3
...ck/extensions/blocks/cookie-consent/test/fixtures/jetpack__cookie-consent.serialized.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<!-- wp:jetpack/cookie-consent --> | ||
<div class="wp-block-jetpack-cookie-consent alignwide has-text-color has-background has-text-color has-background" style="color:var(--wp--preset--color--contrast);background-color:var(--wp--preset--color--tertiary);padding-top:1em;padding-right:1em;padding-bottom:1em;padding-left:1em" role="dialog" aria-modal="true"><p>Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use. To find out more, including how to control cookies, see here: <a href="https://automattic.com/cookies/">Cookie Policy</a>.</p><span>365</span></div> | ||
<!-- /wp:jetpack/cookie-consent --> |
5 changes: 5 additions & 0 deletions
5
projects/plugins/jetpack/extensions/blocks/cookie-consent/test/validate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { name, settings } from '../'; | ||
import runBlockFixtureTests from '../../../shared/test/block-fixtures'; | ||
|
||
const blocks = [ { name: `jetpack/${ name }`, settings } ]; | ||
runBlockFixtureTests( `jetpack/${ name }`, blocks, __dirname ); |
53 changes: 53 additions & 0 deletions
53
...ects/plugins/jetpack/extensions/blocks/cookie-consent/use-save-cookie-consent-settings.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { serialize } from '@wordpress/blocks'; | ||
import { usePrevious } from '@wordpress/compose'; | ||
import { store as coreStore, useEntityProp } from '@wordpress/core-data'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { useEffect, useState, useCallback } from '@wordpress/element'; | ||
|
||
export function useSaveCookieConsentSettings( clientId ) { | ||
const isSaving = useSelect( select => { | ||
const { isSavingPost, isSavingNonPostEntityChanges } = select( 'core/editor' ); | ||
return isSavingPost() || isSavingNonPostEntityChanges(); | ||
}, [] ); | ||
const wasSaving = usePrevious( isSaving ); | ||
const postHasBeenJustSaved = !! ( wasSaving && ! isSaving ); | ||
|
||
const [ , setCookieConsentTemplate ] = useEntityProp( 'root', 'site', 'cookie_consent_template' ); | ||
const { saveEditedEntityRecord } = useDispatch( coreStore ); | ||
const [ isSavingSetting, setIsSavingSetting ] = useState( false ); | ||
|
||
const getBlockById = useSelect( select => { | ||
const { getBlock } = select( 'core/block-editor' ); | ||
return getBlock; | ||
} ); | ||
|
||
const saveTemplate = useCallback( | ||
template => { | ||
setIsSavingSetting( true ); | ||
setCookieConsentTemplate( template ); | ||
saveEditedEntityRecord( 'root', 'site', undefined, { | ||
cookie_consent_template: template, | ||
} ).finally( () => { | ||
setIsSavingSetting( false ); | ||
} ); | ||
}, | ||
[ setIsSavingSetting, setCookieConsentTemplate, saveEditedEntityRecord ] | ||
); | ||
|
||
useEffect( () => { | ||
return () => { | ||
const block = getBlockById( clientId ); | ||
|
||
if ( ! block ) { | ||
saveTemplate( null ); | ||
} | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [] ); | ||
|
||
useEffect( () => { | ||
if ( postHasBeenJustSaved && ! isSavingSetting ) { | ||
saveTemplate( serialize( [ getBlockById( clientId ) ] ) ); | ||
} | ||
}, [ postHasBeenJustSaved, isSavingSetting, saveTemplate, getBlockById, clientId ] ); | ||
} |