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

Avoid confusion around blockTypes/allowedBlockTypes/enabledBlockTypes setting #6069

Merged
merged 3 commits into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
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 ),
Copy link
Member

Choose a reason for hiding this comment

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

Since this line is updated. It can be simplified to:

hasSupportedBlocks: ! isEmpty( allowedBlockTypes )

Copy link
Member Author

@noisysocks noisysocks Apr 10, 2018

Choose a reason for hiding this comment

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

Very confusingly for us PHP folks, _.isEmpty() doesn't work the same way as empty():

$ node
> var _ = require('lodash');
> _.isEmpty(true)
true
> _.isEmpty(false)
true

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' );
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated to this PR, but I wanted to double check if we need to open another issue. Shouldn't this logic verify whether core/block isn't allowed, but also referencedBlockType.name?

Copy link
Member Author

Choose a reason for hiding this comment

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

In my opinion, disabling core/block should prevent you from inserting all shared blocks, but I could be persuaded otherwise. Let's discuss in #5893.

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', {
Copy link
Member

Choose a reason for hiding this comment

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

Both depreciation messages should be also added to https://github.com/WordPress/gutenberg/blob/master/docs/deprecated.md for better visibility.

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