Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lodash: Remove some _.get() from editor #48104

Merged
merged 2 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' ] ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

getPostType() could return undefined.

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(
Copy link
Member Author

Choose a reason for hiding this comment

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

getCurrentPost() will always be an object:

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,
Copy link
Member Author

Choose a reason for hiding this comment

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

getPostType() could return undefined.

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
Copy link
Member Author

Choose a reason for hiding this comment

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

taxonomy could be undefined.

);

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 ??
Copy link
Member Author

Choose a reason for hiding this comment

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

taxonomy could be undefined.

( slug === 'post_tag' ? __( 'Add new tag' ) : __( 'Add new Term' ) );
const singularName =
taxonomy?.labels?.singular_name ??
Copy link
Member Author

Choose a reason for hiding this comment

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

taxonomy could be undefined.

( 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
Copy link
Member Author

Choose a reason for hiding this comment

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

taxonomy could be undefined.

);
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 ] ??
Copy link
Member Author

Choose a reason for hiding this comment

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

taxonomy could be undefined.

( 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' );
Copy link
Member Author

Choose a reason for hiding this comment

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

taxonomy could be undefined.

const groupLabel = taxonomy?.name ?? __( 'Terms' );
Copy link
Member Author

Choose a reason for hiding this comment

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

taxonomy could be undefined.

const showFilter = availableTerms.length >= MIN_TERMS_COUNT_FOR_FILTER;

return (
Expand Down
Loading