|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { __, sprintf } from '@wordpress/i18n'; |
| 5 | +import { useMemo, useState } from '@wordpress/element'; |
| 6 | +import { useDispatch, useSelect, useRegistry } from '@wordpress/data'; |
| 7 | +import { |
| 8 | + Button, |
| 9 | + FlexBlock, |
| 10 | + FlexItem, |
| 11 | + SelectControl, |
| 12 | + __experimentalHStack as HStack, |
| 13 | + __experimentalSpacer as Spacer, |
| 14 | +} from '@wordpress/components'; |
| 15 | +import { |
| 16 | + switchToBlockType, |
| 17 | + getPossibleBlockTransformations, |
| 18 | +} from '@wordpress/blocks'; |
| 19 | +import { store as coreStore } from '@wordpress/core-data'; |
| 20 | +import { store as noticesStore } from '@wordpress/notices'; |
| 21 | + |
| 22 | +/** |
| 23 | + * Internal dependencies |
| 24 | + */ |
| 25 | +import { useCreateTemplatePartFromBlocks } from './utils/hooks'; |
| 26 | +import { transformWidgetToBlock } from './utils/transformers'; |
| 27 | + |
| 28 | +export function TemplatePartImportControls( { area, setAttributes } ) { |
| 29 | + const [ selectedSidebar, setSelectedSidebar ] = useState( '' ); |
| 30 | + const [ isBusy, setIsBusy ] = useState( false ); |
| 31 | + |
| 32 | + const registry = useRegistry(); |
| 33 | + const sidebars = useSelect( ( select ) => { |
| 34 | + return select( coreStore ).getSidebars( { |
| 35 | + per_page: -1, |
| 36 | + _fields: 'id,name,description,status,widgets', |
| 37 | + } ); |
| 38 | + }, [] ); |
| 39 | + const { createErrorNotice } = useDispatch( noticesStore ); |
| 40 | + |
| 41 | + const createFromBlocks = useCreateTemplatePartFromBlocks( |
| 42 | + area, |
| 43 | + setAttributes |
| 44 | + ); |
| 45 | + |
| 46 | + const options = useMemo( () => { |
| 47 | + const sidebarOptions = ( sidebars ?? [] ) |
| 48 | + .filter( |
| 49 | + ( widgetArea ) => |
| 50 | + widgetArea.id !== 'wp_inactive_widgets' && |
| 51 | + widgetArea.widgets.length > 0 |
| 52 | + ) |
| 53 | + .map( ( widgetArea ) => { |
| 54 | + return { |
| 55 | + value: widgetArea.id, |
| 56 | + label: widgetArea.name, |
| 57 | + }; |
| 58 | + } ); |
| 59 | + |
| 60 | + if ( ! sidebarOptions.length ) { |
| 61 | + return []; |
| 62 | + } |
| 63 | + |
| 64 | + return [ |
| 65 | + { value: '', label: __( 'Select widget area' ) }, |
| 66 | + ...sidebarOptions, |
| 67 | + ]; |
| 68 | + }, [ sidebars ] ); |
| 69 | + |
| 70 | + async function createFromWidgets( event ) { |
| 71 | + event.preventDefault(); |
| 72 | + |
| 73 | + if ( isBusy || ! selectedSidebar ) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + setIsBusy( true ); |
| 78 | + |
| 79 | + const sidebar = options.find( |
| 80 | + ( { value } ) => value === selectedSidebar |
| 81 | + ); |
| 82 | + const { getWidgets } = registry.resolveSelect( coreStore ); |
| 83 | + |
| 84 | + // The widgets API always returns a successful response. |
| 85 | + const widgets = await getWidgets( { |
| 86 | + sidebar: sidebar.value, |
| 87 | + _embed: 'about', |
| 88 | + } ); |
| 89 | + |
| 90 | + const skippedWidgets = new Set(); |
| 91 | + const blocks = widgets.flatMap( ( widget ) => { |
| 92 | + const block = transformWidgetToBlock( widget ); |
| 93 | + |
| 94 | + if ( block.name !== 'core/legacy-widget' ) { |
| 95 | + return block; |
| 96 | + } |
| 97 | + |
| 98 | + const transforms = getPossibleBlockTransformations( [ |
| 99 | + block, |
| 100 | + ] ).filter( ( item ) => { |
| 101 | + // The block without any transformations can't be a wildcard. |
| 102 | + if ( ! item.transforms ) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + const hasWildCardFrom = item.transforms?.from?.find( |
| 107 | + ( from ) => from.blocks && from.blocks.includes( '*' ) |
| 108 | + ); |
| 109 | + const hasWildCardTo = item.transforms?.to?.find( |
| 110 | + ( to ) => to.blocks && to.blocks.includes( '*' ) |
| 111 | + ); |
| 112 | + |
| 113 | + return ! hasWildCardFrom && ! hasWildCardTo; |
| 114 | + } ); |
| 115 | + |
| 116 | + // Skip the block if we have no matching transformations. |
| 117 | + if ( ! transforms.length ) { |
| 118 | + skippedWidgets.add( widget.id_base ); |
| 119 | + return []; |
| 120 | + } |
| 121 | + |
| 122 | + // Try transforming the Legacy Widget into a first matching block. |
| 123 | + return switchToBlockType( block, transforms[ 0 ].name ); |
| 124 | + } ); |
| 125 | + |
| 126 | + await createFromBlocks( |
| 127 | + blocks, |
| 128 | + /* translators: %s: name of the widget area */ |
| 129 | + sprintf( __( 'Widget area: %s' ), sidebar.label ) |
| 130 | + ); |
| 131 | + |
| 132 | + if ( skippedWidgets.size ) { |
| 133 | + createErrorNotice( |
| 134 | + sprintf( |
| 135 | + /* translators: %s: the list of widgets */ |
| 136 | + __( 'Unable to import the following widgets: %s.' ), |
| 137 | + Array.from( skippedWidgets ).join( ', ' ) |
| 138 | + ), |
| 139 | + { |
| 140 | + type: 'snackbar', |
| 141 | + } |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + setIsBusy( false ); |
| 146 | + } |
| 147 | + |
| 148 | + return ( |
| 149 | + <Spacer marginBottom="4"> |
| 150 | + <HStack as="form" onSubmit={ createFromWidgets }> |
| 151 | + <FlexBlock> |
| 152 | + <SelectControl |
| 153 | + label={ __( 'Import widget area' ) } |
| 154 | + value={ selectedSidebar } |
| 155 | + options={ options } |
| 156 | + onChange={ ( value ) => setSelectedSidebar( value ) } |
| 157 | + disabled={ ! options.length } |
| 158 | + __next36pxDefaultSize |
| 159 | + __nextHasNoMarginBottom |
| 160 | + /> |
| 161 | + </FlexBlock> |
| 162 | + <FlexItem |
| 163 | + style={ { |
| 164 | + marginBottom: '8px', |
| 165 | + marginTop: 'auto', |
| 166 | + } } |
| 167 | + > |
| 168 | + <Button |
| 169 | + variant="primary" |
| 170 | + type="submit" |
| 171 | + isBusy={ isBusy } |
| 172 | + aria-disabled={ isBusy || ! selectedSidebar } |
| 173 | + > |
| 174 | + { __( 'Import' ) } |
| 175 | + </Button> |
| 176 | + </FlexItem> |
| 177 | + </HStack> |
| 178 | + </Spacer> |
| 179 | + ); |
| 180 | +} |
0 commit comments