![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/mets.corals.io/wp-content/plugins/block-options/src/blocks/import/utils/ |
/** * External dependencies */ import { isString } from 'lodash'; /** * Internal dependencies */ import { readTextFile } from './file'; /** * Import a reusable block from a JSON file. * * @param {File} file File. * @return {Promise} Promise returning the imported reusable block. */ async function importReusableBlock( file ) { const fileContent = await readTextFile( file ); let parsedContent; try { parsedContent = JSON.parse( fileContent ); } catch ( e ) { throw new Error( 'Invalid JSON file' ); } if ( parsedContent.__file !== 'wp_block' || ! parsedContent.title || ! parsedContent.content || ! isString( parsedContent.title ) || ! isString( parsedContent.content ) ) { return importCoreBlocks( parsedContent ); } const postType = await wp.apiFetch( { path: `/wp/v2/types/wp_block` } ); const reusableBlock = await wp.apiFetch( { path: `/wp/v2/${ postType.rest_base }`, data: { title: parsedContent.title, content: parsedContent.content, status: 'publish', }, method: 'POST', } ); if ( reusableBlock.id ) { return '<!-- wp:block {"ref":' + reusableBlock.id + '} /-->'; } throw new Error( 'Invalid Reusable Block JSON file contents' ); } function importCoreBlocks( parsedContent ) { if ( parsedContent.__file !== 'core_block' || ! parsedContent.content || ! isString( parsedContent.content ) ) { throw new Error( 'Invalid JSON file' ); } return parsedContent.content; } export default importReusableBlock;