Skip to content

Commit

Permalink
Prevent amp-story-page from being used anywhere other than root
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jul 9, 2018
1 parent e74b86a commit 9cd428e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
33 changes: 32 additions & 1 deletion blocks/amp-story/amp-story-grid-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@ const {
SelectControl
} = wp.components;

export const ALLOWED_BLOCKS = [
'core/button',
'core/code',
'core/embed',
'core/image',
'core/list',
'core/paragraph',
'core/preformatted',
'core/pullquote',
'core/quote',
'core/table',
'core/verse',
'core/video'
];

function setBlockParent( props ) {
if ( ALLOWED_BLOCKS.includes( props.name ) ) {
return {
...props,
...{ parent: [ 'amp/amp-story-grid-layer' ] }
};
}
return props;
}

wp.hooks.addFilter(
'blocks.registerBlockType',
'amp/set-block-parents',
setBlockParent
);

/**
* Register block.
*/
Expand Down Expand Up @@ -70,7 +101,7 @@ export default registerBlockType(
/>
</InspectorControls>
),
<InnerBlocks key='contents' />
<InnerBlocks key='contents' allowedBlocks={ ALLOWED_BLOCKS } />
];
},

Expand Down
2 changes: 1 addition & 1 deletion blocks/amp-story/amp-story-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default registerBlockType(
! props.attributes.id && (
<Notice status="error" isDismissible={ false }>{ __( 'You must supply an ID for the page.', 'amp' ) }</Notice>
),
<InnerBlocks key='contents' />
<InnerBlocks key='contents' allowedBlocks={ [ 'amp/amp-story-grid-layer' ] } />
];
},

Expand Down
35 changes: 2 additions & 33 deletions blocks/amp-story/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,11 @@
import './amp-story-page';
import './amp-story-grid-layer';

const blockParents = {
'core/button': 'amp/amp-story-grid-layer',
'core/code': 'amp/amp-story-grid-layer',
'core/embed': 'amp/amp-story-grid-layer',
'core/image': 'amp/amp-story-grid-layer',
'core/list': 'amp/amp-story-grid-layer',
'core/paragraph': 'amp/amp-story-grid-layer',
'core/preformatted': 'amp/amp-story-grid-layer',
'core/pullquote': 'amp/amp-story-grid-layer',
'core/quote': 'amp/amp-story-grid-layer',
'core/table': 'amp/amp-story-grid-layer',
'core/verse': 'amp/amp-story-grid-layer',
'core/video': 'amp/amp-story-grid-layer'
};

function setBlockParent( props ) {
if ( blockParents[ props.name ] ) {
return Object.assign(
{},
props,
{ parent: [ blockParents[ props.name ] ] }
);
}
return props;
}

wp.hooks.addFilter(
'blocks.registerBlockType',
'amp/set-block-parents',
setBlockParent
);
import { ALLOWED_BLOCKS as LAYER_ALLOWED_BLOCKS } from './amp-story-grid-layer';

// Remove all blocks that are not known to be allowed in AMP Stories (ref. amp-story-cta-layer-allowed-descendants).
window.addEventListener( 'load', () => { // @todo Should be better event.
wp.blocks.getBlockTypes().forEach( function( blockType ) {
if ( -1 === blockType.name.indexOf( 'amp/amp-story-' ) && ! blockParents[ blockType.name ] ) {
if ( -1 === blockType.name.indexOf( 'amp/amp-story-' ) && ! LAYER_ALLOWED_BLOCKS.includes( blockType.name ) ) {
wp.blocks.unregisterBlockType( blockType.name );
}
} );
Expand Down

0 comments on commit 9cd428e

Please sign in to comment.