Skip to content

Commit

Permalink
Merge pull request #6069 from WordPress/fix/require-getInserterItems-…
Browse files Browse the repository at this point in the history
…argument

Avoid confusion around blockTypes/allowedBlockTypes/enabledBlockTypes setting
  • Loading branch information
noisysocks authored Apr 10, 2018
2 parents 43a58b2 + d1ee410 commit 04cbab4
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 34 deletions.
2 changes: 2 additions & 0 deletions docs/deprecated.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Gutenberg's deprecation policy is intended to support backwards-compatibility fo
## 2.8.0

- `Original autocompleter interface in wp.components.Autocomplete` updated. Please use `latest autocompleter interface` instead. See: https://github.com/WordPress/gutenberg/blob/master/components/autocomplete/README.md.
- `getInserterItems`: the `allowedBlockTypes` argument is now mandatory.
- `getFrecentInserterItems`: the `allowedBlockTypes` argument is now mandatory.

## 2.7.0

Expand Down
8 changes: 4 additions & 4 deletions editor/components/inserter-with-shortcuts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ function InserterWithShortcuts( { items, isLocked, onInsert } ) {

export default compose(
withContext( 'editor' )( ( settings ) => {
const { templateLock, blockTypes } = settings;
const { templateLock, allowedBlockTypes } = settings;

return {
isLocked: !! templateLock,
enabledBlockTypes: blockTypes,
allowedBlockTypes,
};
} ),
connect(
( state, { enabledBlockTypes } ) => ( {
items: getFrecentInserterItems( state, enabledBlockTypes, 4 ),
( state, { allowedBlockTypes } ) => ( {
items: getFrecentInserterItems( state, allowedBlockTypes, 4 ),
} )
),
withDispatch( ( dispatch, ownProps ) => {
Expand Down
4 changes: 2 additions & 2 deletions editor/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ export default compose( [
},
} ) ),
withContext( 'editor' )( ( settings ) => {
const { blockTypes, templateLock } = settings;
const { allowedBlockTypes, templateLock } = settings;

return {
hasSupportedBlocks: true === blockTypes || ! isEmpty( blockTypes ),
hasSupportedBlocks: true === allowedBlockTypes || ! isEmpty( allowedBlockTypes ),
isLocked: !! templateLock,
};
} ),
Expand Down
10 changes: 5 additions & 5 deletions editor/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,17 @@ export class InserterMenu extends Component {

export default compose(
withContext( 'editor' )( ( settings ) => {
const { blockTypes } = settings;
const { allowedBlockTypes } = settings;

return {
enabledBlockTypes: blockTypes,
allowedBlockTypes,
};
} ),
connect(
( state, ownProps ) => {
( state, { allowedBlockTypes } ) => {
return {
items: getInserterItems( state, ownProps.enabledBlockTypes ),
frecentItems: getFrecentInserterItems( state, ownProps.enabledBlockTypes ),
items: getInserterItems( state, allowedBlockTypes ),
frecentItems: getFrecentInserterItems( state, allowedBlockTypes ),
};
},
{ fetchSharedBlocks }
Expand Down
2 changes: 1 addition & 1 deletion editor/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const DEFAULT_SETTINGS = {
maxWidth: 608,

// Allowed block types for the editor, defaulting to true (all supported).
blockTypes: true,
allowedBlockTypes: true,
};

class EditorProvider extends Component {
Expand Down
59 changes: 39 additions & 20 deletions editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { serialize, getBlockType, getBlockTypes } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
import { moment } from '@wordpress/date';
import { deprecated } from '@wordpress/utils';

/***
* Module constants
Expand Down Expand Up @@ -1208,17 +1209,17 @@ export function getNotices( state ) {
* Given a regular block type, constructs an item that appears in the inserter.
*
* @param {Object} state Global application state.
* @param {string[]|boolean} enabledBlockTypes Enabled block types, or true/false to enable/disable all types.
* @param {string[]|boolean} allowedBlockTypes Allowed block types, or true/false to enable/disable all types.
* @param {Object} blockType Block type, likely from getBlockType().
*
* @return {Editor.InserterItem} Item that appears in inserter.
*/
function buildInserterItemFromBlockType( state, enabledBlockTypes, blockType ) {
if ( ! enabledBlockTypes || ! blockType ) {
function buildInserterItemFromBlockType( state, allowedBlockTypes, blockType ) {
if ( ! allowedBlockTypes || ! blockType ) {
return null;
}

const blockTypeIsDisabled = Array.isArray( enabledBlockTypes ) && ! includes( enabledBlockTypes, blockType.name );
const blockTypeIsDisabled = Array.isArray( allowedBlockTypes ) && ! includes( allowedBlockTypes, blockType.name );
if ( blockTypeIsDisabled ) {
return null;
}
Expand All @@ -1243,17 +1244,17 @@ function buildInserterItemFromBlockType( state, enabledBlockTypes, blockType ) {
* Given a shared block, constructs an item that appears in the inserter.
*
* @param {Object} state Global application state.
* @param {string[]|boolean} enabledBlockTypes Enabled block types, or true/false to enable/disable all types.
* @param {string[]|boolean} allowedBlockTypes Allowed block types, or true/false to enable/disable all types.
* @param {Object} sharedBlock Shared block, likely from getSharedBlock().
*
* @return {Editor.InserterItem} Item that appears in inserter.
*/
function buildInserterItemFromSharedBlock( state, enabledBlockTypes, sharedBlock ) {
if ( ! enabledBlockTypes || ! sharedBlock ) {
function buildInserterItemFromSharedBlock( state, allowedBlockTypes, sharedBlock ) {
if ( ! allowedBlockTypes || ! sharedBlock ) {
return null;
}

const blockTypeIsDisabled = Array.isArray( enabledBlockTypes ) && ! includes( enabledBlockTypes, 'core/block' );
const blockTypeIsDisabled = Array.isArray( allowedBlockTypes ) && ! includes( allowedBlockTypes, 'core/block' );
if ( blockTypeIsDisabled ) {
return null;
}
Expand Down Expand Up @@ -1285,21 +1286,30 @@ function buildInserterItemFromSharedBlock( state, enabledBlockTypes, sharedBlock
* items (e.g. a regular block type) and dynamic items (e.g. a shared block).
*
* @param {Object} state Global application state.
* @param {string[]|boolean} enabledBlockTypes Enabled block types, or true/false to enable/disable all types.
* @param {string[]|boolean} allowedBlockTypes Allowed block types, or true/false to enable/disable all types.
*
* @return {Editor.InserterItem[]} Items that appear in inserter.
*/
export function getInserterItems( state, enabledBlockTypes = true ) {
if ( ! enabledBlockTypes ) {
export function getInserterItems( state, allowedBlockTypes ) {
if ( allowedBlockTypes === undefined ) {
allowedBlockTypes = true;
deprecated( 'getInserterItems with no allowedBlockTypes argument', {
version: '2.8',
alternative: 'getInserterItems with an explcit allowedBlockTypes argument',
plugin: 'Gutenberg',
} );
}

if ( ! allowedBlockTypes ) {
return [];
}

const staticItems = getBlockTypes().map( blockType =>
buildInserterItemFromBlockType( state, enabledBlockTypes, blockType )
buildInserterItemFromBlockType( state, allowedBlockTypes, blockType )
);

const dynamicItems = getSharedBlocks( state ).map( sharedBlock =>
buildInserterItemFromSharedBlock( state, enabledBlockTypes, sharedBlock )
buildInserterItemFromSharedBlock( state, allowedBlockTypes, sharedBlock )
);

const items = [ ...staticItems, ...dynamicItems ];
Expand All @@ -1319,19 +1329,19 @@ function fillWithCommonBlocks( inserts ) {
return unionWith( items, commonInserts, areInsertsEqual );
}

function getItemsFromInserts( state, inserts, enabledBlockTypes = true, maximum = MAX_RECENT_BLOCKS ) {
if ( ! enabledBlockTypes ) {
function getItemsFromInserts( state, inserts, allowedBlockTypes, maximum = MAX_RECENT_BLOCKS ) {
if ( ! allowedBlockTypes ) {
return [];
}

const items = fillWithCommonBlocks( inserts ).map( insert => {
if ( insert.ref ) {
const sharedBlock = getSharedBlock( state, insert.ref );
return buildInserterItemFromSharedBlock( state, enabledBlockTypes, sharedBlock );
return buildInserterItemFromSharedBlock( state, allowedBlockTypes, sharedBlock );
}

const blockType = getBlockType( insert.name );
return buildInserterItemFromBlockType( state, enabledBlockTypes, blockType );
return buildInserterItemFromBlockType( state, allowedBlockTypes, blockType );
} );

return compact( items ).slice( 0, maximum );
Expand All @@ -1345,12 +1355,21 @@ function getItemsFromInserts( state, inserts, enabledBlockTypes = true, maximum
* https://en.wikipedia.org/wiki/Frecency
*
* @param {Object} state Global application state.
* @param {string[]|boolean} enabledBlockTypes Enabled block types, or true/false to enable/disable all types.
* @param {string[]|boolean} allowedBlockTypes Allowed block types, or true/false to enable/disable all types.
* @param {number} maximum Number of items to return.
*
* @return {Editor.InserterItem[]} Items that appear in the 'Recent' tab.
*/
export function getFrecentInserterItems( state, enabledBlockTypes = true, maximum = MAX_RECENT_BLOCKS ) {
export function getFrecentInserterItems( state, allowedBlockTypes, maximum = MAX_RECENT_BLOCKS ) {
if ( allowedBlockTypes === undefined ) {
allowedBlockTypes = true;
deprecated( 'getFrecentInserterItems with no allowedBlockTypes argument', {
version: '2.8',
alternative: 'getFrecentInserterItems with an explcit allowedBlockTypes argument',
plugin: 'Gutenberg',
} );
}

const calculateFrecency = ( time, count ) => {
if ( ! time ) {
return count;
Expand All @@ -1372,7 +1391,7 @@ export function getFrecentInserterItems( state, enabledBlockTypes = true, maximu
const sortedInserts = values( state.preferences.insertUsage )
.sort( ( a, b ) => calculateFrecency( b.time, b.count ) - calculateFrecency( a.time, a.count ) )
.map( ( { insert } ) => insert );
return getItemsFromInserts( state, sortedInserts, enabledBlockTypes, maximum );
return getItemsFromInserts( state, sortedInserts, allowedBlockTypes, maximum );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion editor/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2527,7 +2527,7 @@ describe( 'selectors', () => {
};

const blockTypes = getBlockTypes().filter( blockType => ! blockType.isPrivate );
expect( getInserterItems( state ) ).toHaveLength( blockTypes.length );
expect( getInserterItems( state, true ) ).toHaveLength( blockTypes.length );
} );

it( 'should properly list a regular block type', () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ function gutenberg_editor_scripts_and_styles( $hook ) {
$editor_settings = array(
'alignWide' => $align_wide || ! empty( $gutenberg_theme_support[0]['wide-images'] ), // Backcompat. Use `align-wide` outside of `gutenberg` array.
'availableTemplates' => wp_get_theme()->get_page_templates( get_post( $post_to_edit['id'] ) ),
'blockTypes' => $allowed_block_types,
'allowedBlockTypes' => $allowed_block_types,
'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
'disablePostFormats' => ! current_theme_supports( 'post-formats' ),
'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title', 'gutenberg' ), $post ),
Expand Down

0 comments on commit 04cbab4

Please sign in to comment.