Created
August 4, 2019 08:52
-
-
Save goiblas/0171ae1454269d1843884903b5bb1986 to your computer and use it in GitHub Desktop.
Block restrict content
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
const { registerBlockType } = wp.blocks; | |
const { Fragment } = wp.element; | |
const { InspectorControls } = wp.editor; | |
const { InnerBlocks } = wp.editor; | |
const { PanelBody, SelectControl } = wp.components; | |
registerBlockType( 'block-restrict-content/block-restrict-content', { | |
title: 'Restrict Content', | |
icon: 'lock', | |
category: 'layout', | |
edit: ({ attributes, setAttributes } ) => { | |
const { rolesSelected, roles } = attributes; | |
const template = [ 'core/paragraph', { placeholder: 'Enter the content...' } ]; | |
return ( | |
<Fragment> | |
<InspectorControls> | |
<PanelBody> | |
<SelectControl | |
multiple | |
label='Roles:' | |
value={ rolesSelected } | |
onChange={ ( rolesSelected ) => { | |
setAttributes( { rolesSelected } ) | |
} } | |
options={ roles.map( role => { | |
return { | |
value: role, | |
label: role | |
} | |
} ) } | |
/> | |
</PanelBody> | |
</InspectorControls> | |
<div style={ { padding: "20px 20px 2px", backgroundColor: "#f4f4f4" }}> | |
<div style={{ textTransform: "uppercase", marginBottom: "10px", fontSize: "12px"}}> | |
Available to { rolesSelected.join(", ") } | |
</div> | |
<InnerBlocks template={ [template] } /> | |
</div> | |
</Fragment> | |
) | |
}, | |
save: () => <InnerBlocks.Content /> | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment