diff --git a/src/blocks/homepage-articles/store.js b/src/blocks/homepage-articles/store.js index 116a6f671..11b993d16 100644 --- a/src/blocks/homepage-articles/store.js +++ b/src/blocks/homepage-articles/store.js @@ -17,7 +17,7 @@ import { addQueryArgs } from '@wordpress/url'; * Internal dependencies */ import metadata from './block.json'; -import { getBlockQueries, sanitizePostList } from './utils'; +import { getBlockQueries, sanitizePostList, recursivelyGetBlocks } from './utils'; const { name } = metadata; export const STORE_NAMESPACE = `newspack-blocks/${ name }`; @@ -138,15 +138,7 @@ const createFetchPostsSaga = blockNames => { yield put( { type: 'DISABLE_UI' } ); - // Ensure innerBlocks are populated for widget area blocks. - // See https://github.com/WordPress/gutenberg/issues/32607#issuecomment-890728216. - const blocks = getBlocks().map( block => { - const innerBlocks = select( 'core/block-editor' ).getBlocks( block.clientId ); - return { - ...block, - innerBlocks, - }; - } ); + const blocks = recursivelyGetBlocks( getBlocks ); const blockQueries = getBlockQueries( blocks, blockNames ); diff --git a/src/blocks/homepage-articles/utils.ts b/src/blocks/homepage-articles/utils.ts index 1110768d0..a1c0f76c9 100644 --- a/src/blocks/homepage-articles/utils.ts +++ b/src/blocks/homepage-articles/utils.ts @@ -271,3 +271,23 @@ export const postsBlockDispatch = ( triggerReflow: isEditorBlock ? dispatch( STORE_NAMESPACE ).reflow : () => undefined, }; }; + +// Ensure innerBlocks are populated for some blocks (e.g. `widget-area` and `post-content`). +// See https://github.com/WordPress/gutenberg/issues/32607#issuecomment-890728216. +// See https://github.com/Automattic/wp-calypso/issues/91839. +export const recursivelyGetBlocks = ( + getBlocks: ( clientId?: string ) => Block[], + blocks: Block[] = getBlocks() +) => { + return blocks.map( ( block ) => { + let innerBlocks = + block.innerBlocks.length === 0 + ? getBlocks( block.clientId ) + : block.innerBlocks; + innerBlocks = recursivelyGetBlocks( getBlocks, innerBlocks ); + return { + ...block, + innerBlocks, + }; + }); +};