Skip to content

Commit

Permalink
Lodash: Remove _.get() for post type usages (#48121)
Browse files Browse the repository at this point in the history
* Lodash: Remove _.get() for post type usages

* Address feedback

* Fix typo
  • Loading branch information
tyxla authored Feb 21, 2023
1 parent c4c4a86 commit dfb0145
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 106 deletions.
7 changes: 1 addition & 6 deletions packages/edit-post/src/components/device-preview/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 @@ -35,7 +30,7 @@ export default function DevicePreview() {
hasActiveMetaboxes: select( editPostStore ).hasMetaBoxes(),
isSaving: select( editPostStore ).isSavingMetaBoxes(),
isPostSaveable: select( editorStore ).isEditedPostSaveable(),
isViewable: get( postType, [ 'viewable' ], false ),
isViewable: postType?.viewable ?? false,
deviceType:
select( editPostStore ).__experimentalGetPreviewDeviceType(),
};
Expand Down
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 @@ -99,11 +98,7 @@ function FullscreenModeClose( { showTooltip, icon, href } ) {
post_type: postType.slug,
} )
}
label={ get(
postType,
[ 'labels', 'view_items' ],
__( 'Back' )
) }
label={ postType?.labels?.view_items ?? __( 'Back' ) }
showTooltip={ showTooltip }
>
{ buttonIcon }
Expand Down
13 changes: 3 additions & 10 deletions packages/edit-post/src/components/sidebar/featured-image/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 @@ -35,11 +30,9 @@ function FeaturedImage( { isEnabled, isOpened, postType, onTogglePanel } ) {
return (
<PostFeaturedImageCheck>
<PanelBody
title={ get(
postType,
[ 'labels', 'featured_image' ],
__( 'Featured image' )
) }
title={
postType?.labels?.featured_image ?? __( 'Featured image' )
}
opened={ isOpened }
onToggle={ onTogglePanel }
>
Expand Down
13 changes: 3 additions & 10 deletions packages/edit-post/src/components/sidebar/page-attributes/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 @@ -52,11 +47,9 @@ export function PageAttributes() {
return (
<PageAttributesCheck>
<PanelBody
title={ get(
postType,
[ 'labels', 'attributes' ],
__( 'Page attributes' )
) }
title={
postType?.labels?.attributes ?? __( 'Page attributes' )
}
opened={ isOpened }
onToggle={ onTogglePanel }
>
Expand Down
82 changes: 41 additions & 41 deletions packages/editor/src/components/page-attributes/parent.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { get } from 'lodash';
import removeAccents from 'remove-accents';

/**
Expand Down Expand Up @@ -44,47 +43,48 @@ export const getItemPriority = ( name, searchValue ) => {
export function PageAttributesParent() {
const { editPost } = useDispatch( editorStore );
const [ fieldValue, setFieldValue ] = useState( false );
const { parentPost, parentPostId, items, postType } = useSelect(
( select ) => {
const { getPostType, getEntityRecords, getEntityRecord } =
select( coreStore );
const { getCurrentPostId, getEditedPostAttribute } =
select( editorStore );
const postTypeSlug = getEditedPostAttribute( 'type' );
const pageId = getEditedPostAttribute( 'parent' );
const pType = getPostType( postTypeSlug );
const postId = getCurrentPostId();
const isHierarchical = get( pType, [ 'hierarchical' ], false );
const query = {
per_page: 100,
exclude: postId,
parent_exclude: postId,
orderby: 'menu_order',
order: 'asc',
_fields: 'id,title,parent',
};

// Perform a search when the field is changed.
if ( !! fieldValue ) {
query.search = fieldValue;
}

return {
parentPostId: pageId,
parentPost: pageId
? getEntityRecord( 'postType', postTypeSlug, pageId )
: null,
items: isHierarchical
? getEntityRecords( 'postType', postTypeSlug, query )
: [],
postType: pType,
};
},
[ fieldValue ]
);
const { isHierarchical, parentPost, parentPostId, items, postType } =
useSelect(
( select ) => {
const { getPostType, getEntityRecords, getEntityRecord } =
select( coreStore );
const { getCurrentPostId, getEditedPostAttribute } =
select( editorStore );
const postTypeSlug = getEditedPostAttribute( 'type' );
const pageId = getEditedPostAttribute( 'parent' );
const pType = getPostType( postTypeSlug );
const postId = getCurrentPostId();
const postIsHierarchical = pType?.hierarchical ?? false;
const query = {
per_page: 100,
exclude: postId,
parent_exclude: postId,
orderby: 'menu_order',
order: 'asc',
_fields: 'id,title,parent',
};

// Perform a search when the field is changed.
if ( !! fieldValue ) {
query.search = fieldValue;
}

return {
isHierarchical: postIsHierarchical,
parentPostId: pageId,
parentPost: pageId
? getEntityRecord( 'postType', postTypeSlug, pageId )
: null,
items: postIsHierarchical
? getEntityRecords( 'postType', postTypeSlug, query )
: [],
postType: pType,
};
},
[ fieldValue ]
);

const isHierarchical = get( postType, [ 'hierarchical' ], false );
const parentPageLabel = get( postType, [ 'labels', 'parent_item_colon' ] );
const parentPageLabel = postType?.labels?.parent_item_colon;
const pageItems = items || [];

const parentOptions = useMemo( () => {
Expand Down
15 changes: 5 additions & 10 deletions packages/editor/src/components/post-featured-image/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 @@ -105,7 +100,6 @@ function PostFeaturedImage( {
const mediaUpload = useSelect( ( select ) => {
return select( blockEditorStore ).getSettings().mediaUpload;
}, [] );
const postLabel = get( postType, [ 'labels' ], {} );
const { mediaWidth, mediaHeight, mediaSourceUrl } = getMediaDetails(
media,
currentPostId
Expand Down Expand Up @@ -159,7 +153,7 @@ function PostFeaturedImage( {
<MediaUploadCheck fallback={ instructions }>
<MediaUpload
title={
postLabel.featured_image ||
postType?.labels?.featured_image ||
DEFAULT_FEATURE_IMAGE_LABEL
}
onSelect={ onUpdateImage }
Expand Down Expand Up @@ -201,7 +195,8 @@ function PostFeaturedImage( {
{ isLoading && <Spinner /> }
{ ! featuredImageId &&
! isLoading &&
( postLabel.set_featured_image ||
( postType?.labels
?.set_featured_image ||
DEFAULT_SET_FEATURE_IMAGE_LABEL ) }
</Button>
<DropZone onFilesDrop={ onDropFiles } />
Expand All @@ -215,7 +210,7 @@ function PostFeaturedImage( {
{ media && (
<MediaUpload
title={
postLabel.featured_image ||
postType?.labels?.featured_image ||
DEFAULT_FEATURE_IMAGE_LABEL
}
onSelect={ onUpdateImage }
Expand All @@ -237,7 +232,7 @@ function PostFeaturedImage( {
variant="link"
isDestructive
>
{ postLabel.remove_featured_image ||
{ postType?.labels?.remove_featured_image ||
DEFAULT_REMOVE_FEATURE_IMAGE_LABEL }
</Button>
</MediaUploadCheck>
Expand Down
7 changes: 1 addition & 6 deletions packages/editor/src/components/post-locked-modal/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 @@ -164,7 +159,7 @@ export default function PostLockedModal() {
_wpnonce: postLockUtils.nonce,
} );
const allPostsUrl = addQueryArgs( 'edit.php', {
post_type: get( postType, [ 'slug' ] ),
post_type: postType?.slug,
} );
const allPostsLabel = __( 'Exit editor' );
return (
Expand Down
3 changes: 1 addition & 2 deletions packages/editor/src/components/post-preview-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 @@ -256,7 +255,7 @@ export default compose( [
forcePreviewLink !== undefined ? forcePreviewLink : previewLink,
isSaveable: isEditedPostSaveable(),
isAutosaveable: forceIsAutosaveable || isEditedPostAutosaveable(),
isViewable: get( postType, [ 'viewable' ], false ),
isViewable: postType?.viewable ?? false,
isDraft:
[ 'draft', 'auto-draft' ].indexOf(
getEditedPostAttribute( 'status' )
Expand Down
11 changes: 3 additions & 8 deletions packages/editor/src/components/post-publish-panel/postpublish.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 @@ -90,9 +85,9 @@ class PostPublishPanelPostpublish extends Component {

render() {
const { children, isScheduled, post, postType } = this.props;
const postLabel = get( postType, [ 'labels', 'singular_name' ] );
const viewPostLabel = get( postType, [ 'labels', 'view_item' ] );
const addNewPostLabel = get( postType, [ 'labels', 'add_new_item' ] );
const postLabel = postType?.labels?.singular_name;
const viewPostLabel = postType?.labels?.view_item;
const addNewPostLabel = postType?.labels?.add_new_item;
const link =
post.status === 'future' ? getFuturePostUrl( post ) : post.link;
const addLink = addQueryArgs( 'post-new.php', {
Expand Down
9 changes: 2 additions & 7 deletions packages/editor/src/store/utils/notice-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import { __ } from '@wordpress/i18n';
*/
import { SAVE_POST_NOTICE_ID, TRASH_POST_NOTICE_ID } from '../constants';

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

/**
* Builds the arguments for a success notification dispatch.
*
Expand All @@ -24,7 +19,7 @@ import { get } from 'lodash';
export function getNotificationArgumentsForSaveSuccess( data ) {
const { previousPost, post, postType } = data;
// Autosaves are neither shown a notice nor redirected.
if ( get( data.options, [ 'isAutosave' ] ) ) {
if ( data.options?.isAutosave ) {
return [];
}

Expand All @@ -38,7 +33,7 @@ export function getNotificationArgumentsForSaveSuccess( data ) {
const willPublish = publishStatus.includes( post.status );

let noticeMessage;
let shouldShowLink = get( postType, [ 'viewable' ], false );
let shouldShowLink = postType?.viewable ?? false;
let isDraft;

// Always should a notice, which will be spoken for accessibility.
Expand Down

0 comments on commit dfb0145

Please sign in to comment.