| 1 |
import { __ } from '@wordpress/i18n'; |
| 2 |
import { addFilter } from '@wordpress/hooks'; |
| 3 |
import { createHigherOrderComponent } from '@wordpress/compose'; |
| 4 |
import { InspectorControls } from '@wordpress/block-editor'; |
| 5 |
import { PanelBody, SelectControl } from '@wordpress/components'; |
| 6 |
|
| 7 |
const friendsBlockVisibility = createHigherOrderComponent( ( BlockEdit ) => { |
| 8 |
return ( props ) => { |
| 9 |
const { attributes, setAttributes } = props; |
| 10 |
let friendsVisibility = ''; |
| 11 |
if ( typeof attributes.className !== 'undefined' ) { |
| 12 |
if ( /\bonly-friends\b/.test( attributes.className ) ) { |
| 13 |
friendsVisibility = 'only-friends'; |
| 14 |
} else if ( /\bnot-friends\b/.test( attributes.className ) ) { |
| 15 |
friendsVisibility = 'not-friends'; |
| 16 |
} |
| 17 |
} |
| 18 |
return ( |
| 19 |
<> |
| 20 |
<BlockEdit { ...props } /> |
| 21 |
<InspectorControls> |
| 22 |
<PanelBody className="friends-block-visibility" title={ __( 'Friends Visibility', 'friends' ) }> |
| 23 |
<SelectControl |
| 24 |
label={ __( 'Block visibility', 'friends' ) } |
| 25 |
onChange={ friendsVisibility => { |
| 26 |
const className = ((attributes.className || '').replace( /\b(only|not)-friends\b/g, '' ) + ' ' + friendsVisibility).replace(/^\s+|\s+$/, '' ); |
| 27 |
setAttributes( { className } ) } |
| 28 |
} |
| 29 |
value={ friendsVisibility } |
| 30 |
options={ [ |
| 31 |
{ |
| 32 |
label: __( 'For everyone', 'friends' ), |
| 33 |
value: '', |
| 34 |
}, |
| 35 |
{ |
| 36 |
label: __( 'Only friends', 'friends' ), |
| 37 |
value: 'only-friends', |
| 38 |
}, |
| 39 |
{ |
| 40 |
label: __( 'Everyone except friends', 'friends' ), |
| 41 |
value: 'not-friends', |
| 42 |
}, |
| 43 |
] } |
| 44 |
/> |
| 45 |
</PanelBody> |
| 46 |
</InspectorControls> |
| 47 |
</> |
| 48 |
); |
| 49 |
}; |
| 50 |
}, 'withFriendsBlockVisibility' ); |
| 51 |
|
| 52 |
addFilter( 'editor.BlockEdit', 'friends/block-visibility', friendsBlockVisibility ); |
| 53 |
|