Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/block-library/src/block/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
51 changes: 28 additions & 23 deletions packages/editor/src/store/effects/reusable-blocks.js
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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',
Expand Down
40 changes: 40 additions & 0 deletions packages/editor/src/store/effects/test/reusable-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ describe( 'reusable blocks effects', () => {
const blockPromise = Promise.resolve( [
{
id: 123,
status: 'publish',
title: {
raw: 'My cool block',
},
content: {
raw: '<!-- wp:test-block {"name":"Big Bird"} /-->',
protected: false,
},
},
] );
Expand Down Expand Up @@ -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: '<!-- wp:test-block {"name":"Big Bird"} /-->',
protected: false,
},
} );
const postTypePromise = Promise.resolve( {
Expand Down Expand Up @@ -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: '<!-- wp:test-block {"name":"Big Bird"} /-->',
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',
Expand Down