From 4f335ff1476f5a46914871118905893bb507f3d5 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 1 Aug 2024 08:42:50 +0400 Subject: [PATCH] Avoid errors for post types without a 'menu_icon' (#64015) Unlinked contributors: karan4official. Co-authored-by: Mamaduka Co-authored-by: t-hamano --- .../src/components/add-new-template/utils.js | 16 ++++++++++------ packages/editor/src/store/private-selectors.js | 5 ++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/edit-site/src/components/add-new-template/utils.js b/packages/edit-site/src/components/add-new-template/utils.js index 1e544aeaadfe1b..a2644ec4ad4da9 100644 --- a/packages/edit-site/src/components/add-new-template/utils.js +++ b/packages/edit-site/src/components/add-new-template/utils.js @@ -166,9 +166,11 @@ export function usePostTypeArchiveMenuItems() { // `icon` is the `menu_icon` property of a post type. We // only handle `dashicons` for now, even if the `menu_icon` // also supports urls and svg as values. - icon: postType.icon?.startsWith( 'dashicons-' ) - ? postType.icon.slice( 10 ) - : archive, + icon: + typeof postType.icon === 'string' && + postType.icon.startsWith( 'dashicons-' ) + ? postType.icon.slice( 10 ) + : archive, templatePrefix: 'archive', }; } ) || [], @@ -272,9 +274,11 @@ export const usePostTypeMenuItems = ( onClickMenuItem ) => { // `icon` is the `menu_icon` property of a post type. We // only handle `dashicons` for now, even if the `menu_icon` // also supports urls and svg as values. - icon: icon?.startsWith( 'dashicons-' ) - ? icon.slice( 10 ) - : post, + icon: + typeof icon === 'string' && + icon.startsWith( 'dashicons-' ) + ? icon.slice( 10 ) + : post, templatePrefix: templatePrefixes[ slug ], }; const hasEntities = postTypesInfo?.[ slug ]?.hasEntities; diff --git a/packages/editor/src/store/private-selectors.js b/packages/editor/src/store/private-selectors.js index 8a866b46a6cdd7..05ef26c89fb3dd 100644 --- a/packages/editor/src/store/private-selectors.js +++ b/packages/editor/src/store/private-selectors.js @@ -111,7 +111,10 @@ export const getPostIcon = createRegistrySelector( // `icon` is the `menu_icon` property of a post type. We // only handle `dashicons` for now, even if the `menu_icon` // also supports urls and svg as values. - if ( postTypeEntity?.icon?.startsWith( 'dashicons-' ) ) { + if ( + typeof postTypeEntity?.icon === 'string' && + postTypeEntity.icon.startsWith( 'dashicons-' ) + ) { return postTypeEntity.icon.slice( 10 ); } return pageIcon;