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
60 changes: 38 additions & 22 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ import { store as reusableBlocksStore } from '@wordpress/reusable-blocks';
/**
* Internal dependencies
*/
import useNoRecursiveRenders from './use-no-recursive-renders';

export default function ReusableBlockEdit( { attributes: { ref }, clientId } ) {
const [ hasAlreadyRendered, RecursionProvider ] = useNoRecursiveRenders(
ref
);
const { isMissing, hasResolved } = useSelect(
( select ) => {
const persistedBlock = select( coreStore ).getEntityRecord(
Expand Down Expand Up @@ -83,6 +87,16 @@ export default function ReusableBlockEdit( { attributes: { ref }, clientId } ) {

const blockProps = useBlockProps();

if ( hasAlreadyRendered ) {
return (
<div { ...blockProps }>
<Warning>
{ __( 'Block cannot be rendered inside itself.' ) }
</Warning>
</div>
);
}

if ( isMissing ) {
return (
<div { ...blockProps }>
Expand All @@ -104,28 +118,30 @@ export default function ReusableBlockEdit( { attributes: { ref }, clientId } ) {
}

return (
<div { ...blockProps }>
<BlockControls>
<ToolbarGroup>
<ToolbarButton
onClick={ () => convertBlockToStatic( clientId ) }
>
{ __( 'Convert to regular blocks' ) }
</ToolbarButton>
</ToolbarGroup>
</BlockControls>
<InspectorControls>
<PanelBody>
<TextControl
label={ __( 'Name' ) }
value={ title }
onChange={ setTitle }
/>
</PanelBody>
</InspectorControls>
<div className="block-library-block__reusable-block-container">
{ <div { ...innerBlocksProps } /> }
<RecursionProvider>
<div { ...blockProps }>
<BlockControls>
<ToolbarGroup>
<ToolbarButton
onClick={ () => convertBlockToStatic( clientId ) }
>
{ __( 'Convert to regular blocks' ) }
</ToolbarButton>
</ToolbarGroup>
</BlockControls>
<InspectorControls>
<PanelBody>
<TextControl
label={ __( 'Name' ) }
value={ title }
onChange={ setTitle }
/>
</PanelBody>
</InspectorControls>
<div className="block-library-block__reusable-block-container">
{ <div { ...innerBlocksProps } /> }
</div>
</div>
</div>
</RecursionProvider>
);
}
27 changes: 27 additions & 0 deletions packages/block-library/src/block/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @return string Rendered HTML of the referenced block.
*/
function render_block_core_block( $attributes ) {
static $seen_refs = array();

if ( empty( $attributes['ref'] ) ) {
return '';
}
Expand All @@ -22,6 +24,31 @@ function render_block_core_block( $attributes ) {
return '';
}

if ( in_array( $attributes['ref'], $seen_refs, true ) ) {
if ( ! is_admin() ) {
trigger_error(
sprintf(
// translators: %s is the user-provided title of the reusable block.
__( 'Could not render Reusable Block <strong>%s</strong>: blocks cannot be rendered inside themselves.' ),
$reusable_block->post_title
),
E_USER_WARNING
);
}

// WP_DEBUG_DISPLAY must only be honored when WP_DEBUG. This precedent
// is set in `wp_debug_mode()`.
$is_debug = defined( 'WP_DEBUG' ) && WP_DEBUG &&
defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY;

return $is_debug ?
// translators: Visible only in the front end, this warning takes the place of a faulty block.
__( '[block rendering halted]' ) :
'';
}

$seen_refs[] = $attributes['ref'];

if ( 'publish' !== $reusable_block->post_status || ! empty( $reusable_block->post_password ) ) {
return '';
}
Expand Down
100 changes: 100 additions & 0 deletions packages/block-library/src/block/test/use-no-recursive-renders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* External dependencies
*/
import { render } from '@testing-library/react';

/**
* WordPress dependencies
*/
import { Fragment } from '@wordpress/element';

// Mimics a block's Edit component, such as ReusableBlockEdit, which is capable
// of calling itself depending on its `ref` attribute.
function Edit( { attributes: { ref } } ) {
const [ hasAlreadyRendered, RecursionProvider ] = useNoRecursiveRenders(
ref
);

if ( hasAlreadyRendered ) {
return <div className="wp-block__reusable-block--halted">Halt</div>;
}

return (
<RecursionProvider>
<div className="wp-block__reusable-block">
{ ref === 'SIMPLE' && <p>Done</p> }
{ ref === 'SINGLY-RECURSIVE' && (
<Edit attributes={ { ref } } />
) }
{ ref === 'MUTUALLY-RECURSIVE-1' && (
<Edit attributes={ { ref: 'MUTUALLY-RECURSIVE-2' } } />
) }
{ ref === 'MUTUALLY-RECURSIVE-2' && (
<Edit attributes={ { ref: 'MUTUALLY-RECURSIVE-1' } } />
) }
</div>
</RecursionProvider>
);
}

/**
* Internal dependencies
*/
import useNoRecursiveRenders from '../use-no-recursive-renders';

describe( 'useNoRecursiveRenders', () => {
it( 'allows a single block to render', () => {
const { container } = render(
<Edit attributes={ { ref: 'SIMPLE' } } />
);
expect(
container.querySelectorAll( '.wp-block__reusable-block' )
).toHaveLength( 1 );
expect(
container.querySelectorAll( '.wp-block__reusable-block--halted' )
).toHaveLength( 0 );
} );

it( 'allows equal but sibling blocks to render', () => {
const { container } = render(
<Fragment>
<Edit attributes={ { ref: 'SIMPLE' } } />
<Edit attributes={ { ref: 'SIMPLE' } } />
</Fragment>
);
expect(
container.querySelectorAll( '.wp-block__reusable-block' )
).toHaveLength( 2 );
expect(
container.querySelectorAll( '.wp-block__reusable-block--halted' )
).toHaveLength( 0 );
} );

it( 'prevents a block from rendering itself', () => {
const { container } = render(
<Fragment>
<Edit attributes={ { ref: 'SINGLY-RECURSIVE' } } />
</Fragment>
);
expect(
container.querySelectorAll( '.wp-block__reusable-block' )
).toHaveLength( 1 );
expect(
container.querySelectorAll( '.wp-block__reusable-block--halted' )
).toHaveLength( 1 );
} );

it( 'prevents mutual recursion between two blocks', () => {
const { container } = render(
<Fragment>
<Edit attributes={ { ref: 'MUTUALLY-RECURSIVE-1' } } />
</Fragment>
);
expect(
container.querySelectorAll( '.wp-block__reusable-block' )
).toHaveLength( 2 );
expect(
container.querySelectorAll( '.wp-block__reusable-block--halted' )
).toHaveLength( 1 );
} );
} );
29 changes: 29 additions & 0 deletions packages/block-library/src/block/use-no-recursive-renders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* WordPress dependencies
*/
import {
createContext,
useCallback,
useContext,
useMemo,
} from '@wordpress/element';

const RenderedRefsContext = createContext( [] );

export default function useNoRecursiveRenders( ref ) {
const previouslyRenderedRefs = useContext( RenderedRefsContext );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker for this PR. I wanted to share my thought on how to make it general enough to use with other blocks that require the same guard (Post Content or Template Part). It looks like it would be enough to rename ref to something more generic like uniqueId and use Set rather than array of strings for checks to support compound values for more complex blocks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, thanks for the note!

const hasAlreadyRendered = previouslyRenderedRefs.includes( ref );
const newRenderedRefs = useMemo( () => [ ...previouslyRenderedRefs, ref ], [
ref,
previouslyRenderedRefs,
] );
const Provider = useCallback(
( { children } ) => (
<RenderedRefsContext.Provider value={ newRenderedRefs }>
{ children }
</RenderedRefsContext.Provider>
),
[ newRenderedRefs ]
);
return [ hasAlreadyRendered, Provider ];
}