(function( element, blockEditor, blocksConfig ) { const el = element.createElement; const __ = wp.i18n.__; const availableBlocks = blocksConfig.blockTypes; const { SelectControl } = wp.components; const knownBlocks = { 'sociallocker': { 'title': __('Social Locker', 'bizpanda'), 'description': __('Hides content inside the block behind a social locker.', 'bizpanda'), 'keywords': [ 'locker', 'sociallocker', 'social locker', 'social', 'lock' ], 'transformsFrom': ['bizpanda/signinlocker', 'bizpanda/emaillocker'], 'icon': }, 'signinlocker': { 'title': __('Sign-In Locker', 'bizpanda'), 'description': __('Hides content inside the block behind a sign-in locker.', 'bizpanda'), 'keywords': [ 'locker', 'signinlocker', 'signin locker', 'social', 'lock' ], 'transformsFrom': ['bizpanda/sociallocker', 'bizpanda/emaillocker'], 'icon': }, 'emaillocker': { 'title': __('Email Locker', 'bizpanda'), 'description': __('Hides content inside the block behind an email locker.', 'bizpanda'), 'keywords': [ 'locker', 'emaillocker', 'email locker', 'optin', 'opt-in', 'lock' ], 'transformsFrom': ['bizpanda/sociallocker', 'bizpanda/signinlocker'], 'icon': } }; /** * Locker Select */ class LockerSelect extends wp.element.Component { constructor() { super(...arguments); this.state = { isLoading: true, selectedId: this.props.lockerId ? parseInt( this.props.lockerId ) : 0, options: [] }; this.handleSelectChange = this.handleSelectChange.bind(this); } componentDidMount() { const self = this; const request = jQuery.ajax(window.ajaxurl, { type: 'post', dataType: 'json', data: { action: 'get_opanda_lockers', shortcode: self.props.shortcode } }); request.done(function( data ){ const options = []; let hasSelected = false; let defaultLocker = false; for ( let locker of data ) { if ( self.state.selectedId && self.state.selectedId === parseInt( locker.id ) ) { hasSelected = true; } const item = { label: locker.title, value: locker.id, shortcode: locker.shortcode }; if ( !defaultLocker ) defaultLocker = item; options.push(item); } if ( !hasSelected && defaultLocker ) { console.log(defaultLocker); self.props.onChange(defaultLocker); } self.setState({ isLoading: false, options: options, selectedId: hasSelected ? self.state.selectedId : defaultLocker.value }); }); } handleSelectChange( lockerId ) { let option = null; for ( let optionItem of this.state.options ) { if ( parseInt( optionItem.value ) === parseInt( lockerId ) ) { option = optionItem; this.setState({ selectedId: lockerId ? parseInt( lockerId ) : 0 }); break; } } this.props.onChange( option ); } render() { const self = this; let options = this.state.options; if ( this.state.isLoading ) { options = [{label: __('Loading...', 'bizpanda'), value: 0}]; } const hasLockers = options.length > 0; if ( !hasLockers ) { options = [{label: __('[ - empty - ]', 'bizpanda'), value: 0}]; } const hasEditableItem = ( !this.state.isLoading && hasLockers && this.state.selectedId ) ? true : false; const hasAddAbility = !this.state.isLoading; return ( <> { ( hasAddAbility || hasEditableItem ) &&
|
} { hasEditableItem && {__('Edit', 'bizpanda')} } { hasAddAbility && {__('Add', 'bizpanda')} } ); } } for ( let pluginBlockType of availableBlocks ) { if ( !knownBlocks[pluginBlockType] ) continue; const shortcode = pluginBlockType; const blockName = 'bizpanda/' + pluginBlockType; const blockTitle = knownBlocks[pluginBlockType].title; const blockDescription = knownBlocks[pluginBlockType].description; const blockIcon = knownBlocks[pluginBlockType].icon; const blockKeywords = knownBlocks[pluginBlockType].keywords; const blockTransformsFrom = knownBlocks[pluginBlockType].transformsFrom; wp.blocks.registerBlockType(blockName, { title: blockTitle, description: blockDescription, icon: blockIcon, category: 'widgets', keywords: blockKeywords, attributes: { id: { type: 'number' }, }, transforms: { from: [ { type: 'block', blocks: blockTransformsFrom, transform: function ( attributes ) { return wp.blocks.createBlock( blockName, {...attributes}); }, }, ] }, edit: function(props) { const elements = []; // if selected, shows the settings if ( props.isSelected ) { const onChange = (option) => { console.log( option ); props.setAttributes({ id: ( option && option.value ) ? parseInt( option.value ) : null }); }; const configWrap = (
); elements.push(configWrap); } const previewWrap = (
) elements.push(previewWrap); return (
{elements}
); }, save: function(props) { return (
); } }); } })( window.wp.element, window.wp.blockEditor, window.__bizpanda_locker_blocks );