Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusable blocks: Don't show trashed blocks in the editor or frontend #12345

Merged
merged 1 commit into from Nov 28, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/block-library/src/block/index.php
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 '';
}
gziolo marked this conversation as resolved.
Show resolved Hide resolved

return do_blocks( $reusable_block->post_content );
}

Expand Down
51 changes: 28 additions & 23 deletions 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';

/**
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
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