Skip to content

Commit

Permalink
Enable block visibility filtering
Browse files Browse the repository at this point in the history
For private, internal, or potentially deprecated blocks
  • Loading branch information
aduth committed Mar 27, 2017
1 parent c358ca0 commit 68ce6e9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Registers a new block provided a unique slug and an object defining its behavior
- `save( attributes: Object ): WPElement` - Returns an element describing the markup of a block to be saved in the published content. This function is called before save and when switching to an editor's HTML view.
- `controls: string[]` - Slugs for controls to be made available to block. See also: [`wp.blocks.registerControl`](#wpblocksregistercontrol-slug-string-settings-object-)
- `encodeAttributes( attributes: Object ): Object` - Called when save markup is generated, this function allows you to control which attributes are to be encoded in the block comment metadata. By default, all attribute values not defined in the block's `attributes` property are serialized to the comment metadata. If defined, this function should return the subset of attributes to encode, or `null` to bypass default behavior.
- `isVisible: boolean` - Whether the block is to be made available as an option in the block inserter overlay. Defaults to `true`.

### `wp.blocks.registerControl( slug: string, settings: Object )`

Expand Down
9 changes: 9 additions & 0 deletions blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,12 @@ export function getBlockAttributes( blockNode, blockSettings ) {
export function getBlocks() {
return Object.values( blocks );
}

/**
* Returns all public registered blocks.
*
* @return {Array} Block settings
*/
export function getVisibleBlocks() {
return getBlocks().filter( ( block ) => false !== block.isVisible );
}
17 changes: 17 additions & 0 deletions blocks/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,21 @@ describe( 'blocks', () => {
] );
} );
} );

describe( 'getVisibleBlocks()', () => {
it( 'should return an empty array at first', () => {
expect( blocks.getVisibleBlocks() ).to.eql( [] );
} );

it( 'should return all registered public blocks', () => {
blocks.registerBlock( 'core/visible-block', { isVisible: true } );
blocks.registerBlock( 'core/inferred-visible-block' );
blocks.registerBlock( 'core/non-visible-block', { isVisible: false } );

expect( blocks.getVisibleBlocks().map( ( block ) => block.slug ) ).eql( [
'core/visible-block',
'core/inferred-visible-block'
] );
} );
} );
} );
2 changes: 2 additions & 0 deletions editor/blocks/generic-block/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { html } = wp.blocks.query;

wp.blocks.registerBlock( 'wp/generic', {
isVisible: false,

attributes: {
html: html()
},
Expand Down
2 changes: 1 addition & 1 deletion editor/inserter/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Inserter = () => {
const blocks = wp.blocks.getBlocks();
const blocks = wp.blocks.getVisibleBlocks();

return (
<div className="inserter">
Expand Down

0 comments on commit 68ce6e9

Please sign in to comment.