Skip to content

Commit

Permalink
Lodash: Remove some _.get() from editor (#48104)
Browse files Browse the repository at this point in the history
* Lodash: Remove some _.get() from editor

* Default to false where applicable
  • Loading branch information
tyxla authored Feb 22, 2023
1 parent 1e1c962 commit cf8335e
Show file tree
Hide file tree
Showing 12 changed files with 48 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
12 changes: 2 additions & 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,8 @@ 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' ] ?? false,
hasAuthors: authors?.length >= 1,
};
}, [] );
Expand Down
12 changes: 2 additions & 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,8 @@ export default compose(
const { isCurrentPostPublished, getCurrentPostType, getCurrentPost } =
select( editorStore );
return {
hasPublishAction: get(
getCurrentPost(),
[ '_links', 'wp:action-publish' ],
false
),
hasPublishAction:
getCurrentPost()._links?.[ 'wp:action-publish' ] ?? false,
isPublished: isCurrentPostPublished(),
postType: getCurrentPostType(),
};
Expand Down
8 changes: 2 additions & 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,8 @@ export default compose( [
isPostSavingLocked: isPostSavingLocked(),
isPublishable: isEditedPostPublishable(),
isPublished: isCurrentPostPublished(),
hasPublishAction: get(
getCurrentPost(),
[ '_links', 'wp:action-publish' ],
false
),
hasPublishAction:
getCurrentPost()._links?.[ 'wp:action-publish' ] ?? false,
postType: getCurrentPostType(),
postId: getCurrentPostId(),
hasNonPostEntityChanges: hasNonPostEntityChanges(),
Expand Down
12 changes: 2 additions & 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,8 @@ export default compose( [
isBeingScheduled: isEditedPostBeingScheduled(),
isSaving: forceIsSaving || isSavingPost(),
isPublishing: isPublishingPost(),
hasPublishAction: get(
getCurrentPost(),
[ '_links', 'wp:action-publish' ],
false
),
hasPublishAction:
getCurrentPost()._links?.[ 'wp:action-publish' ] ?? false,
postType: getCurrentPostType(),
isAutosaving: isAutosavingPost(),
};
Expand Down
14 changes: 3 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,9 @@ 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' ] ?? false,
isPostTypeViewable: postType?.viewable,
isBeingScheduled: isEditedPostBeingScheduled(),
isDirty: isEditedPostDirty(),
isPublished: isCurrentPostPublished(),
Expand Down
12 changes: 2 additions & 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,8 @@ function PostPublishPanelPrepublish( { children } ) {
getEntityRecord( 'root', '__unstableBase', undefined ) || {};

return {
hasPublishAction: get(
getCurrentPost(),
[ '_links', 'wp:action-publish' ],
false
),
hasPublishAction:
getCurrentPost()._links?.[ 'wp:action-publish' ] ?? false,
isBeingScheduled: isEditedPostBeingScheduled(),
isRequestingSiteIcon: isResolving( 'getEntityRecord', [
'root',
Expand Down
12 changes: 2 additions & 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,8 @@ 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' ] ?? false,
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' ] ?? false,
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,14 @@ 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
: false,
hasAssignAction: _taxonomy
? get(
post,
[
'_links',
'wp:action-assign-' + _taxonomy.rest_base,
],
false
)
? post._links?.[
'wp:action-assign-' + _taxonomy.rest_base
] ?? false
: false,
taxonomy: _taxonomy,
termIds: _termIds,
Expand Down Expand Up @@ -239,30 +228,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,18 @@ 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
: false,
hasAssignAction: _taxonomy
? get(
getCurrentPost(),
[
'_links',
'wp:action-assign-' + _taxonomy.rest_base,
],
false
)
? post._links?.[
'wp:action-assign-' + _taxonomy.rest_base
] ?? false
: false,
terms: _taxonomy
? getEditedPostAttribute( _taxonomy.rest_base )
Expand Down Expand Up @@ -308,14 +294,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 +367,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 +387,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
Loading

1 comment on commit cf8335e

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in cf8335e.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4241419279
📝 Reported issues:

Please sign in to comment.