diff --git a/assets/src/blocks/Cookies/CookiesBlock.js b/assets/src/blocks/Cookies/CookiesBlock.js index 84f41f2df0..e0e0f78778 100644 --- a/assets/src/blocks/Cookies/CookiesBlock.js +++ b/assets/src/blocks/Cookies/CookiesBlock.js @@ -1,11 +1,9 @@ import {CookiesEditor} from './CookiesEditor.js'; -export const BLOCK_NAME = 'planet4-blocks/cookies'; - export const registerCookiesBlock = () => { const {registerBlockType} = wp.blocks; - registerBlockType(BLOCK_NAME, { + registerBlockType('planet4-blocks/cookies', { title: 'Cookies', icon: 'welcome-view-site', category: 'planet4-blocks', @@ -41,7 +39,7 @@ export const registerCookiesBlock = () => { }, }, edit: CookiesEditor, - save() { + save: () => { return null; }, }); diff --git a/assets/src/blocks/Cookies/CookiesEditor.js b/assets/src/blocks/Cookies/CookiesEditor.js index 5451fe8e7d..047bb9e5b5 100644 --- a/assets/src/blocks/Cookies/CookiesEditor.js +++ b/assets/src/blocks/Cookies/CookiesEditor.js @@ -1,18 +1,187 @@ -import {CookiesFrontend} from './CookiesFrontend'; +import {FrontendRichText} from '../components/FrontendRichText/FrontendRichText'; +import {CookiesFieldResetButton} from './CookiesFieldResetButton'; -export const CookiesEditor = ({attributes, isSelected, setAttributes}) => { - const toAttribute = attributeName => value => { - if (isSelected) { - setAttributes({[attributeName]: value}); +const {__} = wp.i18n; + +const COOKIES_DEFAULT_COPY = window.p4_vars.options.cookies_default_copy || {}; + +// Planet4 settings(Planet 4 > Cookies > Enable Analytical Cookies). +const ENABLE_ANALYTICAL_COOKIES = window.p4_vars.options.enable_analytical_cookies; + +export const CookiesEditor = ({setAttributes, attributes}) => { + const { + title, + description, + className, + necessary_cookies_name, + necessary_cookies_description, + analytical_cookies_name, + analytical_cookies_description, + all_cookies_name, + all_cookies_description, + } = attributes; + + const getFieldValue = fieldName => { + if (attributes[fieldName] === undefined) { + return COOKIES_DEFAULT_COPY[fieldName] || ''; } + return attributes[fieldName] || ''; }; + const toAttribute = attributeName => value => setAttributes({ + [attributeName]: value, + }); + return ( - +
+
+ +
+ + <> +
+ + {__('Always enabled', 'planet4-blocks')} + +
+
+ + +
+ + {ENABLE_ANALYTICAL_COOKIES && + <> +
+ + +
+
+ + +
+ + } + <> +
+ + +
+
+ + +
+ +
); }; diff --git a/assets/src/blocks/Cookies/CookiesFrontend.js b/assets/src/blocks/Cookies/CookiesFrontend.js index 02301025ee..2795fa805e 100644 --- a/assets/src/blocks/Cookies/CookiesFrontend.js +++ b/assets/src/blocks/Cookies/CookiesFrontend.js @@ -1,6 +1,4 @@ -import {FrontendRichText} from '../components/FrontendRichText/FrontendRichText'; import {removeCookie, useCookie, writeCookie} from './useCookie'; -import {CookiesFieldResetButton} from './CookiesFieldResetButton'; const {__} = wp.i18n; const {useState, useEffect} = wp.element; @@ -36,20 +34,7 @@ const hideCookiesBox = () => { }; export const CookiesFrontend = props => { - const { - isSelected, - title, - description, - necessary_cookies_name, - necessary_cookies_description, - analytical_cookies_name, - analytical_cookies_description, - all_cookies_name, - all_cookies_description, - isEditing, - className, - toAttribute = () => {}, - } = props; + const {title, description, className} = props; // Whether consent was revoked by the user since current page load. const [userRevokedMarketingCookies, setUserRevokedMarketingCookies] = useState(false); @@ -122,84 +107,40 @@ export const CookiesFrontend = props => { return ( <>
- {(isEditing || title) && -
- -
+ {title && +
+

{title}

+
} - {(isEditing || description) && - + {description && +

} - {(isEditing || (isFieldValid('necessary_cookies_name') && isFieldValid('necessary_cookies_description'))) && + {isFieldValid('necessary_cookies_name') && isFieldValid('necessary_cookies_description') && <>

- + + {getFieldValue('necessary_cookies_name')} + {__('Always enabled', 'planet4-blocks')} - {isEditing && - - } -
-
- - {isEditing && - - }
+

+ {getFieldValue('necessary_cookies_description')} +

} - {(ENABLE_ANALYTICAL_COOKIES && (isEditing || (isFieldValid('analytical_cookies_name') && isFieldValid('analytical_cookies_description')))) && + {ENABLE_ANALYTICAL_COOKIES && isFieldValid('analytical_cookies_name') && isFieldValid('analytical_cookies_description') && <>
-
-
- - {isEditing && - - }
+

+ {getFieldValue('analytical_cookies_description')} +

} - {(isEditing || (isFieldValid('all_cookies_name') && isFieldValid('all_cookies_description'))) && + {isFieldValid('all_cookies_name') && isFieldValid('all_cookies_description') && <>
-
-
- - {isEditing && - - }
+

+ {getFieldValue('all_cookies_description')} +

}
diff --git a/assets/src/blocks/Cookies/CookiesScript.js b/assets/src/blocks/Cookies/CookiesScript.js index be9bcf7566..ad8b5ecc23 100644 --- a/assets/src/blocks/Cookies/CookiesScript.js +++ b/assets/src/blocks/Cookies/CookiesScript.js @@ -1,9 +1,8 @@ import {createRoot} from 'react-dom/client'; -import {BLOCK_NAME} from './CookiesBlock'; import {CookiesFrontend} from './CookiesFrontend'; // Fallback for non migrated content. Remove after migration. -document.querySelectorAll(`[data-render="${BLOCK_NAME}"]`).forEach( +document.querySelectorAll('[data-render="planet4-blocks/cookies"]').forEach( blockNode => { const attributes = JSON.parse(blockNode.dataset.attributes); const rootElement = createRoot(blockNode);