|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { __, sprintf } from '@wordpress/i18n'; |
| 5 | +import { Button } from '@wordpress/components'; |
| 6 | +import { RawHTML } from '@wordpress/element'; |
| 7 | +import { useSelect } from '@wordpress/data'; |
| 8 | +import { Warning } from '@wordpress/block-editor'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Internal dependencies |
| 12 | + */ |
| 13 | +import InstallButton from './install-button'; |
| 14 | + |
| 15 | +const filterMissing = ( OriginalComponent ) => ( props ) => { |
| 16 | + const { originalName, originalUndelimitedContent } = props.attributes; |
| 17 | + const { block, hasPermission } = useSelect( |
| 18 | + ( select ) => { |
| 19 | + const { getDownloadableBlocks } = select( 'core/block-directory' ); |
| 20 | + const blocks = getDownloadableBlocks( |
| 21 | + 'block:' + originalName |
| 22 | + ).filter( ( { name } ) => originalName === name ); |
| 23 | + return { |
| 24 | + hasPermission: select( 'core' ).canUser( |
| 25 | + 'read', |
| 26 | + 'block-directory/search' |
| 27 | + ), |
| 28 | + block: blocks.length && blocks[ 0 ], |
| 29 | + }; |
| 30 | + }, |
| 31 | + [ originalName ] |
| 32 | + ); |
| 33 | + |
| 34 | + if ( ! hasPermission || ! block ) { |
| 35 | + return <OriginalComponent { ...props } />; |
| 36 | + } |
| 37 | + |
| 38 | + const actions = [ |
| 39 | + <InstallButton |
| 40 | + key="install" |
| 41 | + block={ block } |
| 42 | + attributes={ props.attributes } |
| 43 | + clientId={ props.clientId } |
| 44 | + />, |
| 45 | + <Button key="convert" onClick={ props.convertToHTML } isLink> |
| 46 | + { __( 'Keep as HTML' ) } |
| 47 | + </Button>, |
| 48 | + ]; |
| 49 | + |
| 50 | + return ( |
| 51 | + <> |
| 52 | + <Warning actions={ actions }> |
| 53 | + { sprintf( |
| 54 | + /* translators: %s: block name */ |
| 55 | + __( |
| 56 | + 'Your site doesn’t include support for the %s block. You can try installing the block, convert it to a Custom HTML block, or remove it entirely.' |
| 57 | + ), |
| 58 | + block.title || originalName |
| 59 | + ) } |
| 60 | + </Warning> |
| 61 | + <RawHTML>{ originalUndelimitedContent }</RawHTML> |
| 62 | + </> |
| 63 | + ); |
| 64 | +}; |
| 65 | + |
| 66 | +export default filterMissing; |
0 commit comments