diff --git a/packages/block-library/src/block/index.php b/packages/block-library/src/block/index.php index 77a281802c50a..aa235f170333e 100644 --- a/packages/block-library/src/block/index.php +++ b/packages/block-library/src/block/index.php @@ -22,6 +22,10 @@ function render_block_core_block( $attributes ) { return ''; } + if ( 'publish' !== $reusable_block->post_status || ! empty( $reusable_block->post_password ) ) { + return ''; + } + return do_blocks( $reusable_block->post_content ); } diff --git a/packages/editor/src/store/effects/reusable-blocks.js b/packages/editor/src/store/effects/reusable-blocks.js index 07ed88ab69d52..acfa3d0aaaefb 100644 --- a/packages/editor/src/store/effects/reusable-blocks.js +++ b/packages/editor/src/store/effects/reusable-blocks.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { castArray, map, uniqueId } from 'lodash'; +import { compact, map, uniqueId } from 'lodash'; import { BEGIN, COMMIT, REVERT } from 'redux-optimist'; /** @@ -61,30 +61,35 @@ export const fetchReusableBlocks = async ( action, store ) => { return; } - let result; - if ( id ) { - result = apiFetch( { path: `/wp/v2/${ postType.rest_base }/${ id }` } ); - } else { - result = apiFetch( { path: `/wp/v2/${ postType.rest_base }?per_page=-1` } ); - } - try { - const reusableBlockOrBlocks = await result; - dispatch( receiveReusableBlocksAction( map( - castArray( reusableBlockOrBlocks ), - ( post ) => { - const parsedBlocks = parse( post.content.raw ); - return { - reusableBlock: { - id: post.id, - title: getPostRawValue( post.title ), - }, - parsedBlock: parsedBlocks.length === 1 ? - parsedBlocks[ 0 ] : - createBlock( 'core/template', {}, parsedBlocks ), - }; + let posts; + + if ( id ) { + posts = [ await apiFetch( { path: `/wp/v2/${ postType.rest_base }/${ id }` } ) ]; + } else { + posts = await apiFetch( { path: `/wp/v2/${ postType.rest_base }?per_page=-1` } ); + } + + const results = compact( map( posts, ( post ) => { + if ( post.status !== 'publish' || post.content.protected ) { + return null; } - ) ) ); + + const parsedBlocks = parse( post.content.raw ); + return { + reusableBlock: { + id: post.id, + title: getPostRawValue( post.title ), + }, + parsedBlock: parsedBlocks.length === 1 ? + parsedBlocks[ 0 ] : + createBlock( 'core/template', {}, parsedBlocks ), + }; + } ) ); + + if ( results.length ) { + dispatch( receiveReusableBlocksAction( results ) ); + } dispatch( { type: 'FETCH_REUSABLE_BLOCKS_SUCCESS', diff --git a/packages/editor/src/store/effects/test/reusable-blocks.js b/packages/editor/src/store/effects/test/reusable-blocks.js index 1e13dba9f9714..071e4f07f4f6f 100644 --- a/packages/editor/src/store/effects/test/reusable-blocks.js +++ b/packages/editor/src/store/effects/test/reusable-blocks.js @@ -70,11 +70,13 @@ describe( 'reusable blocks effects', () => { const blockPromise = Promise.resolve( [ { id: 123, + status: 'publish', title: { raw: 'My cool block', }, content: { raw: '', + protected: false, }, }, ] ); @@ -118,11 +120,13 @@ describe( 'reusable blocks effects', () => { it( 'should fetch a single reusable block', async () => { const blockPromise = Promise.resolve( { id: 123, + status: 'publish', title: { raw: 'My cool block', }, content: { raw: '', + protected: false, }, } ); const postTypePromise = Promise.resolve( { @@ -162,6 +166,42 @@ describe( 'reusable blocks effects', () => { } ); } ); + it( 'should ignore reusable blocks with a trashed post status', async () => { + const blockPromise = Promise.resolve( { + id: 123, + status: 'trash', + title: { + raw: 'My cool block', + }, + content: { + raw: '', + protected: false, + }, + } ); + const postTypePromise = Promise.resolve( { + slug: 'wp_block', rest_base: 'blocks', + } ); + + apiFetch.mockImplementation( ( options ) => { + if ( options.path === '/wp/v2/types/wp_block' ) { + return postTypePromise; + } + + return blockPromise; + } ); + + const dispatch = jest.fn(); + const store = { getState: noop, dispatch }; + + await fetchReusableBlocks( fetchReusableBlocksAction( 123 ), store ); + + expect( dispatch ).toHaveBeenCalledTimes( 1 ); + expect( dispatch ).toHaveBeenCalledWith( { + type: 'FETCH_REUSABLE_BLOCKS_SUCCESS', + id: 123, + } ); + } ); + it( 'should handle an API error', async () => { const blockPromise = Promise.reject( { code: 'unknown_error',