Skip to content

Commit

Permalink
Lodash: Remove some _.get() from editor
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed Feb 15, 2023
1 parent 58cd891 commit 0732876
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 167 deletions.
12 changes: 1 addition & 11 deletions packages/editor/src/components/page-attributes/check.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -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;
}

Expand Down
11 changes: 1 addition & 10 deletions packages/editor/src/components/post-author/check.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -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,
};
}, [] );
Expand Down
11 changes: 1 addition & 10 deletions packages/editor/src/components/post-pending-status/check.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -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(),
};
Expand Down
7 changes: 1 addition & 6 deletions packages/editor/src/components/post-publish-button/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { get } from 'lodash';
import classnames from 'classnames';

/**
Expand Down Expand Up @@ -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(),
Expand Down
11 changes: 1 addition & 10 deletions packages/editor/src/components/post-publish-button/label.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -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(),
};
Expand Down
13 changes: 2 additions & 11 deletions packages/editor/src/components/post-publish-panel/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -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(),
Expand Down
11 changes: 1 addition & 10 deletions packages/editor/src/components/post-publish-panel/prepublish.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -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',
Expand Down
11 changes: 1 addition & 10 deletions packages/editor/src/components/post-schedule/check.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -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(),
};
} ),
Expand Down
11 changes: 1 addition & 10 deletions packages/editor/src/components/post-sticky/check.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -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(),
};
} ),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { get } from 'lodash';
import escapeHtml from 'escape-html';

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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' ),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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' ),
Expand All @@ -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 (
Expand Down
11 changes: 1 addition & 10 deletions packages/editor/src/components/post-visibility/check.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { get } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -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(),
};
} ),
Expand Down

0 comments on commit 0732876

Please sign in to comment.