| 1 |
import { downloadableBlocksManifest } from '@page-creator/lib/blocks'; |
| 2 |
import { useInstalledPluginsCache } from '@page-creator/state/plugins-cache'; |
| 3 |
import { store as blockDirectoryStore } from '@wordpress/block-directory'; |
| 4 |
import { dispatch } from '@wordpress/data'; |
| 5 |
|
| 6 |
const supportedBlocks = ['contact-form-7', 'simplybook']; |
| 7 |
|
| 8 |
export const installBlocks = async ({ patterns }) => { |
| 9 |
const { installBlockType } = dispatch(blockDirectoryStore); |
| 10 |
const { installedPlugins, updateInstalledPlugins } = |
| 11 |
useInstalledPluginsCache.getState(); |
| 12 |
|
| 13 |
const code = patterns |
| 14 |
.flatMap((p) => p.patternReplacementCode) |
| 15 |
.filter(Boolean); |
| 16 |
|
| 17 |
// Look for any blocks we support installing |
| 18 |
const foundBlocks = supportedBlocks.filter( |
| 19 |
(block) => |
| 20 |
code.some((c) => c.includes(block)) && !installedPlugins.includes(block), |
| 21 |
); // Install the blocks |
| 22 |
for (const block of foundBlocks) { |
| 23 |
const blockManifest = downloadableBlocksManifest[block]; |
| 24 |
if (blockManifest) await installBlockType(blockManifest); |
| 25 |
} |
| 26 |
|
| 27 |
await updateInstalledPlugins(); |
| 28 |
}; |
| 29 |
|