From 07328764fb691586ab77acc3a33d3923b989cc16 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Wed, 15 Feb 2023 17:44:12 +0200 Subject: [PATCH] Lodash: Remove some _.get() from editor --- .../src/components/page-attributes/check.js | 12 +---- .../src/components/post-author/check.js | 11 +---- .../components/post-pending-status/check.js | 11 +---- .../components/post-publish-button/index.js | 7 +-- .../components/post-publish-button/label.js | 11 +---- .../components/post-publish-panel/index.js | 13 +---- .../post-publish-panel/prepublish.js | 11 +---- .../src/components/post-schedule/check.js | 11 +---- .../src/components/post-sticky/check.js | 11 +---- .../post-taxonomies/flat-term-selector.js | 42 ++++------------ .../hierarchical-term-selector.js | 48 +++++-------------- .../src/components/post-visibility/check.js | 11 +---- 12 files changed, 32 insertions(+), 167 deletions(-) diff --git a/packages/editor/src/components/page-attributes/check.js b/packages/editor/src/components/page-attributes/check.js index 584d70bd7b79c3..f36edc1f11ec06 100644 --- a/packages/editor/src/components/page-attributes/check.js +++ b/packages/editor/src/components/page-attributes/check.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -21,14 +16,9 @@ export function PageAttributesCheck( { children } ) { return getPostType( getEditedPostAttribute( 'type' ) ); }, [] ); - const supportsPageAttributes = get( - postType, - [ 'supports', 'page-attributes' ], - false - ); // Only render fields if post type supports page attributes or available templates exist. - if ( ! supportsPageAttributes ) { + if ( ! postType?.supports?.[ 'page-attributes' ] ) { return null; } diff --git a/packages/editor/src/components/post-author/check.js b/packages/editor/src/components/post-author/check.js index 13a270bd0e3e6f..f30f9452597778 100644 --- a/packages/editor/src/components/post-author/check.js +++ b/packages/editor/src/components/post-author/check.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -21,11 +16,7 @@ export default function PostAuthorCheck( { children } ) { const post = select( editorStore ).getCurrentPost(); const authors = select( coreStore ).getUsers( AUTHORS_QUERY ); return { - hasAssignAuthorAction: get( - post, - [ '_links', 'wp:action-assign-author' ], - false - ), + hasAssignAuthorAction: post._links?.[ 'wp:action-assign-author' ], hasAuthors: authors?.length >= 1, }; }, [] ); diff --git a/packages/editor/src/components/post-pending-status/check.js b/packages/editor/src/components/post-pending-status/check.js index 70e374418e0b96..dc97d741b9bd3f 100644 --- a/packages/editor/src/components/post-pending-status/check.js +++ b/packages/editor/src/components/post-pending-status/check.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -31,11 +26,7 @@ export default compose( const { isCurrentPostPublished, getCurrentPostType, getCurrentPost } = select( editorStore ); return { - hasPublishAction: get( - getCurrentPost(), - [ '_links', 'wp:action-publish' ], - false - ), + hasPublishAction: getCurrentPost()._links?.[ 'wp:action-publish' ], isPublished: isCurrentPostPublished(), postType: getCurrentPostType(), }; diff --git a/packages/editor/src/components/post-publish-button/index.js b/packages/editor/src/components/post-publish-button/index.js index 73254e99e9556e..f6134c9c498a97 100644 --- a/packages/editor/src/components/post-publish-button/index.js +++ b/packages/editor/src/components/post-publish-button/index.js @@ -1,7 +1,6 @@ /** * External dependencies */ -import { get } from 'lodash'; import classnames from 'classnames'; /** @@ -242,11 +241,7 @@ export default compose( [ isPostSavingLocked: isPostSavingLocked(), isPublishable: isEditedPostPublishable(), isPublished: isCurrentPostPublished(), - hasPublishAction: get( - getCurrentPost(), - [ '_links', 'wp:action-publish' ], - false - ), + hasPublishAction: getCurrentPost()._links?.[ 'wp:action-publish' ], postType: getCurrentPostType(), postId: getCurrentPostId(), hasNonPostEntityChanges: hasNonPostEntityChanges(), diff --git a/packages/editor/src/components/post-publish-button/label.js b/packages/editor/src/components/post-publish-button/label.js index db26da73037bb9..71623e44e1d148 100644 --- a/packages/editor/src/components/post-publish-button/label.js +++ b/packages/editor/src/components/post-publish-button/label.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -64,11 +59,7 @@ export default compose( [ isBeingScheduled: isEditedPostBeingScheduled(), isSaving: forceIsSaving || isSavingPost(), isPublishing: isPublishingPost(), - hasPublishAction: get( - getCurrentPost(), - [ '_links', 'wp:action-publish' ], - false - ), + hasPublishAction: getCurrentPost()._links?.[ 'wp:action-publish' ], postType: getCurrentPostType(), isAutosaving: isAutosavingPost(), }; diff --git a/packages/editor/src/components/post-publish-panel/index.js b/packages/editor/src/components/post-publish-panel/index.js index c9583254f73597..5c0b183a7ad408 100644 --- a/packages/editor/src/components/post-publish-panel/index.js +++ b/packages/editor/src/components/post-publish-panel/index.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -153,12 +148,8 @@ export default compose( [ const postType = getPostType( getEditedPostAttribute( 'type' ) ); return { - hasPublishAction: get( - getCurrentPost(), - [ '_links', 'wp:action-publish' ], - false - ), - isPostTypeViewable: get( postType, [ 'viewable' ], false ), + hasPublishAction: getCurrentPost()._links?.[ 'wp:action-publish' ], + isPostTypeViewable: postType?.viewable, isBeingScheduled: isEditedPostBeingScheduled(), isDirty: isEditedPostDirty(), isPublished: isCurrentPostPublished(), diff --git a/packages/editor/src/components/post-publish-panel/prepublish.js b/packages/editor/src/components/post-publish-panel/prepublish.js index 274cfea879973f..941d488ff6a904 100644 --- a/packages/editor/src/components/post-publish-panel/prepublish.js +++ b/packages/editor/src/components/post-publish-panel/prepublish.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -42,11 +37,7 @@ function PostPublishPanelPrepublish( { children } ) { getEntityRecord( 'root', '__unstableBase', undefined ) || {}; return { - hasPublishAction: get( - getCurrentPost(), - [ '_links', 'wp:action-publish' ], - false - ), + hasPublishAction: getCurrentPost()._links?.[ 'wp:action-publish' ], isBeingScheduled: isEditedPostBeingScheduled(), isRequestingSiteIcon: isResolving( 'getEntityRecord', [ 'root', diff --git a/packages/editor/src/components/post-schedule/check.js b/packages/editor/src/components/post-schedule/check.js index b6bec2a5094a1a..d0198bf9d40778 100644 --- a/packages/editor/src/components/post-schedule/check.js +++ b/packages/editor/src/components/post-schedule/check.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -26,11 +21,7 @@ export default compose( [ withSelect( ( select ) => { const { getCurrentPost, getCurrentPostType } = select( editorStore ); return { - hasPublishAction: get( - getCurrentPost(), - [ '_links', 'wp:action-publish' ], - false - ), + hasPublishAction: getCurrentPost()._links?.[ 'wp:action-publish' ], postType: getCurrentPostType(), }; } ), diff --git a/packages/editor/src/components/post-sticky/check.js b/packages/editor/src/components/post-sticky/check.js index 80007647f67f82..3693e0b76669a0 100644 --- a/packages/editor/src/components/post-sticky/check.js +++ b/packages/editor/src/components/post-sticky/check.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -26,11 +21,7 @@ export default compose( [ withSelect( ( select ) => { const post = select( editorStore ).getCurrentPost(); return { - hasStickyAction: get( - post, - [ '_links', 'wp:action-sticky' ], - false - ), + hasStickyAction: post._links?.[ 'wp:action-sticky' ], postType: select( editorStore ).getCurrentPostType(), }; } ), diff --git a/packages/editor/src/components/post-taxonomies/flat-term-selector.js b/packages/editor/src/components/post-taxonomies/flat-term-selector.js index 7e1ba149e6fbb3..e89444774e174c 100644 --- a/packages/editor/src/components/post-taxonomies/flat-term-selector.js +++ b/packages/editor/src/components/post-taxonomies/flat-term-selector.js @@ -1,7 +1,6 @@ /** * External dependencies */ -import { get } from 'lodash'; import escapeHtml from 'escape-html'; /** @@ -106,24 +105,10 @@ export function FlatTermSelector( { slug } ) { return { hasCreateAction: _taxonomy - ? get( - post, - [ - '_links', - 'wp:action-create-' + _taxonomy.rest_base, - ], - false - ) + ? post._links?.[ 'wp:action-create-' + _taxonomy.rest_base ] : false, hasAssignAction: _taxonomy - ? get( - post, - [ - '_links', - 'wp:action-assign-' + _taxonomy.rest_base, - ], - false - ) + ? post._links?.[ 'wp:action-assign-' + _taxonomy.rest_base ] : false, taxonomy: _taxonomy, termIds: _termIds, @@ -239,30 +224,23 @@ export function FlatTermSelector( { slug } ) { } const newTermIds = [ ...termIds, newTerm.id ]; + const defaultName = slug === 'post_tag' ? __( 'Tag' ) : __( 'Term' ); const termAddedMessage = sprintf( /* translators: %s: term name. */ _x( '%s added', 'term' ), - get( - taxonomy, - [ 'labels', 'singular_name' ], - slug === 'post_tag' ? __( 'Tag' ) : __( 'Term' ) - ) + taxonomy?.labels?.singular_name ?? defaultName ); speak( termAddedMessage, 'assertive' ); onUpdateTerms( newTermIds ); } - const newTermLabel = get( - taxonomy, - [ 'labels', 'add_new_item' ], - slug === 'post_tag' ? __( 'Add new tag' ) : __( 'Add new Term' ) - ); - const singularName = get( - taxonomy, - [ 'labels', 'singular_name' ], - slug === 'post_tag' ? __( 'Tag' ) : __( 'Term' ) - ); + const newTermLabel = + taxonomy?.labels?.add_new_item ?? + ( slug === 'post_tag' ? __( 'Add new tag' ) : __( 'Add new Term' ) ); + const singularName = + taxonomy?.labels?.singular_name ?? + ( slug === 'post_tag' ? __( 'Tag' ) : __( 'Term' ) ); const termAddedLabel = sprintf( /* translators: %s: term name. */ _x( '%s added', 'term' ), diff --git a/packages/editor/src/components/post-taxonomies/hierarchical-term-selector.js b/packages/editor/src/components/post-taxonomies/hierarchical-term-selector.js index 460b86c4ac8fbd..cfdf3ae2be99ba 100644 --- a/packages/editor/src/components/post-taxonomies/hierarchical-term-selector.js +++ b/packages/editor/src/components/post-taxonomies/hierarchical-term-selector.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -180,27 +175,14 @@ export function HierarchicalTermSelector( { slug } ) { const { getTaxonomy, getEntityRecords, isResolving } = select( coreStore ); const _taxonomy = getTaxonomy( slug ); + const post = getCurrentPost(); return { hasCreateAction: _taxonomy - ? get( - getCurrentPost(), - [ - '_links', - 'wp:action-create-' + _taxonomy.rest_base, - ], - false - ) + ? post._links?.[ 'wp:action-create-' + _taxonomy.rest_base ] : false, hasAssignAction: _taxonomy - ? get( - getCurrentPost(), - [ - '_links', - 'wp:action-assign-' + _taxonomy.rest_base, - ], - false - ) + ? post._links?.[ 'wp:action-assign-' + _taxonomy.rest_base ] : false, terms: _taxonomy ? getEditedPostAttribute( _taxonomy.rest_base ) @@ -308,14 +290,12 @@ export function HierarchicalTermSelector( { slug } ) { parent: formParent ? formParent : undefined, } ); + const defaultName = + slug === 'category' ? __( 'Category' ) : __( 'Term' ); const termAddedMessage = sprintf( /* translators: %s: taxonomy name */ _x( '%s added', 'term' ), - get( - taxonomy, - [ 'labels', 'singular_name' ], - slug === 'category' ? __( 'Category' ) : __( 'Term' ) - ) + taxonomy?.labels?.singular_name ?? defaultName ); speak( termAddedMessage, 'assertive' ); setAdding( false ); @@ -383,11 +363,9 @@ export function HierarchicalTermSelector( { slug } ) { fallbackIsCategory, fallbackIsNotCategory ) => - get( - taxonomy, - [ 'labels', labelProperty ], - slug === 'category' ? fallbackIsCategory : fallbackIsNotCategory - ); + taxonomy?.labels?.[ labelProperty ] ?? + ( slug === 'category' ? fallbackIsCategory : fallbackIsNotCategory ); + const newTermButtonLabel = labelWithFallback( 'add_new_item', __( 'Add new category' ), @@ -405,12 +383,8 @@ export function HierarchicalTermSelector( { slug } ) { ); const noParentOption = `— ${ parentSelectLabel } —`; const newTermSubmitLabel = newTermButtonLabel; - const filterLabel = get( - taxonomy, - [ 'labels', 'search_items' ], - __( 'Search Terms' ) - ); - const groupLabel = get( taxonomy, [ 'name' ], __( 'Terms' ) ); + const filterLabel = taxonomy?.labels?.search_items ?? __( 'Search Terms' ); + const groupLabel = taxonomy?.name ?? __( 'Terms' ); const showFilter = availableTerms.length >= MIN_TERMS_COUNT_FOR_FILTER; return ( diff --git a/packages/editor/src/components/post-visibility/check.js b/packages/editor/src/components/post-visibility/check.js index c2a0814369be82..b73149a3eb8f0c 100644 --- a/packages/editor/src/components/post-visibility/check.js +++ b/packages/editor/src/components/post-visibility/check.js @@ -1,8 +1,3 @@ -/** - * External dependencies - */ -import { get } from 'lodash'; - /** * WordPress dependencies */ @@ -23,11 +18,7 @@ export default compose( [ withSelect( ( select ) => { const { getCurrentPost, getCurrentPostType } = select( editorStore ); return { - hasPublishAction: get( - getCurrentPost(), - [ '_links', 'wp:action-publish' ], - false - ), + hasPublishAction: getCurrentPost()._links?.[ 'wp:action-publish' ], postType: getCurrentPostType(), }; } ),