Skip to content

Commit

Permalink
Utils: Deprecate buildTermTree function (#7976)
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo authored Jul 16, 2018
1 parent b00aea3 commit 581522d
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 8 deletions.
6 changes: 1 addition & 5 deletions components/query-controls/category-select.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/**
* WordPress dependencies
*/
import { buildTermsTree } from '@wordpress/utils';

/**
* Internal dependencies
*/
import { buildTermsTree } from './terms';
import TreeSelect from '../tree-select';

export default function CategorySelect( { label, noOptionLabel, categoriesList, selectedCategoryId, onChange } ) {
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions docs/reference/deprecated.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Gutenberg's deprecation policy is intended to support backwards-compatibility fo
- `wp.element.pure` has been removed. Please use `wp.compose.pure` instead.
- `wp.element.compose` has been removed. Please use `wp.compose.compose` instead.
- `wp.element.createHigherOrderComponent` has been removed. Please use `wp.compose.createHigherOrderComponent` instead.
- `wp.utils.buildTermsTree` has been removed.

## 3.4.0

Expand Down
6 changes: 5 additions & 1 deletion editor/components/page-attributes/parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import { stringify } from 'querystringify';
import { __ } from '@wordpress/i18n';
import { TreeSelect, withAPIData } from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { buildTermsTree } from '@wordpress/utils';
import { withSelect, withDispatch } from '@wordpress/data';

/**
* Internal dependencies
*/
import { buildTermsTree } from '../../utils/terms';

export function PageAttributesParent( { parent, postType, items, onUpdateParent } ) {
const isHierarchical = get( postType, [ 'hierarchical' ], false );
const parentPageLabel = get( postType, [ 'labels', 'parent_item_colon' ] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import { stringify } from 'querystring';
import { __, _x, sprintf } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { TreeSelect, withAPIData, withSpokenMessages, Button } from '@wordpress/components';
import { buildTermsTree } from '@wordpress/utils';
import { withSelect, withDispatch } from '@wordpress/data';
import apiRequest from '@wordpress/api-request';
import { withInstanceId, compose } from '@wordpress/compose';

/**
* Internal dependencies
*/
import { buildTermsTree } from '../../utils/terms';

/**
* Module Constants
*/
Expand Down
28 changes: 28 additions & 0 deletions editor/utils/terms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* External dependencies
*/
import { groupBy } from 'lodash';

/**
* Returns terms in a tree form.
*
* @param {Array} flatTerms Array of terms in flat format.
*
* @return {Array} Array of terms in tree format.
*/
export function buildTermsTree( flatTerms ) {
const termsByParent = groupBy( flatTerms, 'parent' );
const fillWithChildren = ( terms ) => {
return terms.map( ( term ) => {
const children = termsByParent[ term.id ];
return {
...term,
children: children && children.length ?
fillWithChildren( children ) :
[],
};
} );
};

return fillWithChildren( termsByParent[ '0' ] || [] );
}
File renamed without changes.
33 changes: 33 additions & 0 deletions utils/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,39 @@
import * as keycodesSource from '@wordpress/keycodes';
import deprecated from '@wordpress/deprecated';

/**
* External dependencies
*/
import { groupBy } from 'lodash';

/**
* Returns terms in a tree form.
*
* @param {Array} flatTerms Array of terms in flat format.
*
* @return {Array} Array of terms in tree format.
*/
export function buildTermsTree( flatTerms ) {
deprecated( 'wp.utils.buildTermsTree', {
version: '3.5',
plugin: 'Gutenberg',
} );
const termsByParent = groupBy( flatTerms, 'parent' );
const fillWithChildren = ( terms ) => {
return terms.map( ( term ) => {
const children = termsByParent[ term.id ];
return {
...term,
children: children && children.length ?
fillWithChildren( children ) :
[],
};
} );
};

return fillWithChildren( termsByParent[ '0' ] || [] );
}

// keycodes
const wrapKeycodeFunction = ( source, functionName ) => ( ...args ) => {
deprecated( `wp.utils.keycodes.${ functionName }`, {
Expand Down
1 change: 0 additions & 1 deletion utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { decodeEntities } from './entities';
export { decodeEntities };

export * from './mediaupload';
export * from './terms';

// Deprecations
export * from './deprecated';

0 comments on commit 581522d

Please sign in to comment.