/** * BLOCK:Complianz Documents block * * Registering the Complianz Privacy Suite documents block with Gutenberg. */ import * as api from './utils/api'; const { __ } = wp.i18n; const { registerBlockType } = wp.blocks; const { InspectorControls, BlockControls, useBlockProps} = wp.blockEditor; const { PanelBody, PanelRow, SelectControl, TextControl, TextareaControl, ToolbarButton, ToolbarGroup, Icon} = wp.components; import {useState, useEffect} from "@wordpress/element"; import DOMPurify from 'dompurify'; /** * Set custom Complianz Icon */ // SVG icon const iconEl = () => ( } /> ); const selectDocument = ({ className, isSelected, attributes, setAttributes }) => { const [documents, setDocuments] = useState([]); const [documentDataLoaded, setDocumentDataLoaded] = useState(false); const [selectedDocument, setSelectedDocument] = useState(attributes.selectedDocument); const [documentSyncStatus, setDocumentSyncStatus] = useState(attributes.documentSyncStatus); const [customDocumentHtml, setCustomDocumentHtml] = useState(''); useEffect(() => { api.getDocuments().then( ( response ) => { let documents = response.data; setDocuments(documents); setDocumentDataLoaded(true); let documentData = false; if ( documents && 0 !== selectedDocument ) { // If we have a selected document, find that document and add it. documentData = documents.find( ( item ) => { return item.id === selectedDocument } ); if (documents.length === 0) { setAttributes({ hasDocuments: false, }); } } let tempHtml = ''; if ( documentData ) { tempHtml = documentData.content; } if ( attributes.customDocument && attributes.customDocument.length>0 ){ tempHtml = attributes.customDocument; } setCustomDocumentHtml(tempHtml); }); }, []) const onChangeSelectDocument = (value) => { // Set the state setSelectedDocument(value); // Set the attributes setAttributes({ selectedDocument: value, }); } const onChangeCustomDocument = (html) => { setAttributes({ customDocument: html, }); } const onChangeSelectDocumentSyncStatus = (value) =>{ setDocumentSyncStatus(value); setAttributes({ documentSyncStatus: value, }); //we reset the customDocument data const selectedDocumentData = documents.find((item) => { return item.id === selectedDocument }); setCustomDocumentHtml(selectedDocumentData.content); setAttributes({ customDocument: selectedDocumentData.content, }); } let options = [{value: 0, label: __('Select a document', 'complianz-gdpr')}]; let output = __('Loading...', 'complianz-gdpr'); let document_status_options = [ {value: 'sync', label: __('Synchronize document with Complianz', 'complianz-gdpr')}, {value: 'unlink', label: __('Edit document and stop synchronization', 'complianz-gdpr')}, ]; if ( !attributes.hasDocuments ){ output = __('No documents found. Please finish the Complianz Privacy Suite wizard to generate documents', 'complianz-gdpr'); } //preview if ( attributes.preview ) { return( preview ); } if ( documentDataLoaded ) { output = isSelected ? __("Select a document type from the dropdownlist", 'complianz-gdpr') : __('Click this block to show the options', 'complianz-gdpr'); documents.forEach((item) => { options.push({value: item.id, label: item.title}); }); } //load content if ( attributes.selectedDocument!==0 && documentDataLoaded && attributes.selectedDocument.length>0 ) { const documentData = documents.find((item) => { return item.id === attributes.selectedDocument }); if (documentData) output = documentData.content; } if ( documentSyncStatus==='sync' ) { return [ !!isSelected && ( onChangeSelectDocument(e) } value={ selectedDocument } label={__('Select a document', 'complianz-gdpr')} options={options}/> onChangeSelectDocumentSyncStatus(e) } value={documentSyncStatus} label={__('Document sync status', 'complianz-gdpr')} options={document_status_options}/> ),
, ] } else { let html = documentDataLoaded ? customDocumentHtml : __('Loading...', 'complianz-gdpr'); let syncClassName = className + ' cmplz-unlinked-mode'; return [ !!isSelected && ( onChangeSelectDocument(e) } value={attributes.selectedDocument} label={__('Select a document', 'complianz-gdpr')} options={options}/> onChangeSelectDocumentSyncStatus(e) } value={documentSyncStatus} label={__('Document sync status', 'complianz-gdpr')} options={document_status_options}/> ),
onChangeCustomDocument(e.currentTarget.innerHTML)} dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(customDocumentHtml) }} className={syncClassName} >
] } } /** * Register: a Gutenberg Block. * * Registers a new block provided a unique name and an object defining its * behavior. Once registered, the block is made editor as an option to any * editor interface where blocks are implemented. * * @link https://wordpress.org/gutenberg/handbook/block-api/ * @param {string} name Block name. * @param {Object} settings Block settings. * @return {?WPBlock} The block, if it has been successfully * registered; otherwise `undefined`. */ registerBlockType('complianz/document', { title: __('Legal document - Complianz', 'complianz-gdpr'), // Block title. icon: iconEl, // Block icon from Dashicons → https://developer.wordpress.org/resource/dashicons/. category: 'widgets', example: { attributes: { 'preview' : true, }, }, keywords: [ __('Privacy Statement', 'complianz-gdpr'), __('Cookie Policy', 'complianz-gdpr'), __('Disclaimer', 'complianz-gdpr'), ], //className: 'cmplz-document', attributes: { documentSyncStatus: { type: 'string', default: 'sync' }, customDocument: { type: 'string', default: '' }, hasDocuments: { type: 'string', default: 'false', }, content: { type: 'string', source: 'children', selector: 'p', }, selectedDocument: { type: 'string', default: '', }, documents: { type: 'array', }, document: { type: 'array', }, preview: { type: 'boolean', default: false, } }, /** * The edit function describes the structure of your block in the context of the editor. * This represents what the editor will render when the block is used. * * The "edit" property must be a valid function. * * @link https://wordpress.org/gutenberg/handbook/block-api/block-edit-save/ */ edit:selectDocument, /** * The save function defines the way in which the different attributes should be combined * into the final markup, which is then serialized by Gutenberg into post_content. * * The "save" property must be specified and must be a valid function. * * @link https://wordpress.org/gutenberg/handbook/block-api/block-edit-save/ */ save: function() { // Rendering in PHP return null; }, }); registerBlockType( 'complianz/consent-area', { title: __( 'Consent Area Block' ), icon: iconEl, category: 'widgets', example: { attributes: { 'preview' : true, }, }, attributes: { category:{ type: 'string', default: 'marketing', }, service:{ type: 'service', default: 'general', }, blockId:{ type: 'string', default: '' }, postId:{ type: 'integer', default: -1, }, placeholderContent: { type: 'string', default: '', }, consentedContent: { type: 'string', default: '', }, }, edit: ( props ) => { const { getCurrentPostId } = wp.data.select("core/editor"); const postId = getCurrentPostId(); const { attributes, setAttributes , isSelected, className} = props; const [ view, setView ] = useState( 'consented' ); const [ isPreview, setIsPreview ] = useState( false ); const onViewChange = ( value ) => { setView( value ); }; useEffect( () => { setAttributes( { postId: postId, category:attributes.category, service:attributes.service } ); if (attributes.blockId==='') { let blockId = (Math.random() + 1).toString(36).substring(7); setAttributes( { blockId: blockId } ); } }, [attributes] ); const blockProps = useBlockProps(); let disabled = !complianz.user_can_unfiltered_html; return [ !!isSelected && ( setAttributes( { category: value } ) } /> setAttributes( { service: value } ) } /> onViewChange(value) } /> ),
{ setIsPreview(false) } > HTML setIsPreview(true) } > { __( 'Preview' ) } } {disabled &&
{__("You do not have sufficient permissions to edit this block.","complianz-gdpr")}
} { !isPreview && <> { view==='placeholder' && setAttributes( { placeholderContent: value } ) } /> } { view === 'consented' && setAttributes( { consentedContent: value } ) } /> } } { !!isPreview && <> { view === 'placeholder' &&
} { view === 'consented' &&
} }
] }, save: function() { return null; }, } );