From 8e8f241d62236867757a7e6fc6243f3b8aef8330 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Mon, 9 Apr 2018 14:22:16 +1000 Subject: [PATCH 1/3] Make getInserterItems() require its enabledBlockTypes argument Having this argument optional implies that it's safe to not provide one, when in reality that will probably result in a bug. --- editor/store/selectors.js | 25 ++++++++++++++++++++++--- editor/store/test/selectors.js | 2 +- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/editor/store/selectors.js b/editor/store/selectors.js index ff512bca0a1832..203ca0aedd197c 100644 --- a/editor/store/selectors.js +++ b/editor/store/selectors.js @@ -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 @@ -1289,7 +1290,16 @@ function buildInserterItemFromSharedBlock( state, enabledBlockTypes, sharedBlock * * @return {Editor.InserterItem[]} Items that appear in inserter. */ -export function getInserterItems( state, enabledBlockTypes = true ) { +export function getInserterItems( state, enabledBlockTypes ) { + if ( enabledBlockTypes === undefined ) { + enabledBlockTypes = true; + deprecated( 'getInserterItems with no enabledBlockTypes argument', { + version: '2.8', + alternative: 'getInserterItems with an explcit enabledBlockTypes argument', + plugin: 'Gutenberg', + } ); + } + if ( ! enabledBlockTypes ) { return []; } @@ -1319,7 +1329,7 @@ function fillWithCommonBlocks( inserts ) { return unionWith( items, commonInserts, areInsertsEqual ); } -function getItemsFromInserts( state, inserts, enabledBlockTypes = true, maximum = MAX_RECENT_BLOCKS ) { +function getItemsFromInserts( state, inserts, enabledBlockTypes, maximum = MAX_RECENT_BLOCKS ) { if ( ! enabledBlockTypes ) { return []; } @@ -1350,7 +1360,16 @@ function getItemsFromInserts( state, inserts, enabledBlockTypes = true, maximum * * @return {Editor.InserterItem[]} Items that appear in the 'Recent' tab. */ -export function getFrecentInserterItems( state, enabledBlockTypes = true, maximum = MAX_RECENT_BLOCKS ) { +export function getFrecentInserterItems( state, enabledBlockTypes, maximum = MAX_RECENT_BLOCKS ) { + if ( enabledBlockTypes === undefined ) { + enabledBlockTypes = true; + deprecated( 'getFrecentInserterItems with no enabledBlockTypes argument', { + version: '2.8', + alternative: 'getFrecentInserterItems with an explcit enabledBlockTypes argument', + plugin: 'Gutenberg', + } ); + } + const calculateFrecency = ( time, count ) => { if ( ! time ) { return count; diff --git a/editor/store/test/selectors.js b/editor/store/test/selectors.js index 44814ad4c1d70c..e89c0f445f0ab2 100644 --- a/editor/store/test/selectors.js +++ b/editor/store/test/selectors.js @@ -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', () => { From e68a0a35f988049ab5479fa0d88695e11fae12f8 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Mon, 9 Apr 2018 14:30:12 +1000 Subject: [PATCH 2/3] Rename `enabledBlockTypes` and `blockTypes` to `allowedBlockTypes` Using the same name for this feature throughout the codebase will hopefully help developers search for it and better understand it. --- .../inserter-with-shortcuts/index.js | 8 +-- editor/components/inserter/index.js | 4 +- editor/components/inserter/menu.js | 10 ++-- editor/components/provider/index.js | 2 +- editor/store/selectors.js | 56 +++++++++---------- lib/client-assets.php | 2 +- 6 files changed, 41 insertions(+), 41 deletions(-) diff --git a/editor/components/inserter-with-shortcuts/index.js b/editor/components/inserter-with-shortcuts/index.js index dbb662f7d84277..a36910c668a931 100644 --- a/editor/components/inserter-with-shortcuts/index.js +++ b/editor/components/inserter-with-shortcuts/index.js @@ -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 ) => { diff --git a/editor/components/inserter/index.js b/editor/components/inserter/index.js index d8fb08ec6d18e8..d5406801ea3a01 100644 --- a/editor/components/inserter/index.js +++ b/editor/components/inserter/index.js @@ -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, }; } ), diff --git a/editor/components/inserter/menu.js b/editor/components/inserter/menu.js index 57703f14abd2e2..7ac132d76740da 100644 --- a/editor/components/inserter/menu.js +++ b/editor/components/inserter/menu.js @@ -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 } diff --git a/editor/components/provider/index.js b/editor/components/provider/index.js index 1258e274e21d1d..7226a15b0b13f9 100644 --- a/editor/components/provider/index.js +++ b/editor/components/provider/index.js @@ -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 { diff --git a/editor/store/selectors.js b/editor/store/selectors.js index 203ca0aedd197c..7285e02d7e9511 100644 --- a/editor/store/selectors.js +++ b/editor/store/selectors.js @@ -1209,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; } @@ -1244,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; } @@ -1286,30 +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 ) { - if ( enabledBlockTypes === undefined ) { - enabledBlockTypes = true; - deprecated( 'getInserterItems with no enabledBlockTypes argument', { +export function getInserterItems( state, allowedBlockTypes ) { + if ( allowedBlockTypes === undefined ) { + allowedBlockTypes = true; + deprecated( 'getInserterItems with no allowedBlockTypes argument', { version: '2.8', - alternative: 'getInserterItems with an explcit enabledBlockTypes argument', + alternative: 'getInserterItems with an explcit allowedBlockTypes argument', plugin: 'Gutenberg', } ); } - if ( ! enabledBlockTypes ) { + 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 ]; @@ -1329,19 +1329,19 @@ function fillWithCommonBlocks( inserts ) { return unionWith( items, commonInserts, areInsertsEqual ); } -function getItemsFromInserts( state, inserts, enabledBlockTypes, 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 ); @@ -1355,17 +1355,17 @@ function getItemsFromInserts( state, inserts, enabledBlockTypes, maximum = MAX_R * 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, maximum = MAX_RECENT_BLOCKS ) { - if ( enabledBlockTypes === undefined ) { - enabledBlockTypes = true; - deprecated( 'getFrecentInserterItems with no enabledBlockTypes argument', { +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 enabledBlockTypes argument', + alternative: 'getFrecentInserterItems with an explcit allowedBlockTypes argument', plugin: 'Gutenberg', } ); } @@ -1391,7 +1391,7 @@ export function getFrecentInserterItems( state, enabledBlockTypes, maximum = MAX 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 ); } /** diff --git a/lib/client-assets.php b/lib/client-assets.php index f9150a51887120..341e3bfdaada31 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -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 ), From d1ee410942d27bc42d509128b300b8603fee0ecf Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Tue, 10 Apr 2018 11:24:50 +1000 Subject: [PATCH 3/3] Update deprecated.md to reflect allowedBlockTypes being made mandatory --- docs/deprecated.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/deprecated.md b/docs/deprecated.md index 2bcae158b2b2cd..97b9d25eff1475 100644 --- a/docs/deprecated.md +++ b/docs/deprecated.md @@ -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