From a4cb0b7cf73bdb604c5051b78db1c0fd49d2a273 Mon Sep 17 00:00:00 2001 From: Dean Sas Date: Thu, 11 Jul 2024 21:45:28 +0100 Subject: [PATCH] FlatTermSelector: Be more defensive about termIds It's possible (likely through a bug elsehwere) for FlatTermSelector to completely crash the editor if it can't get termIds from the post being edited. This change prevents that crash. The error can happen when tries to access _termIds.join and _termIds.length even when _termIds is undefined. This is undefined if the current editor state doesn't have any changed for the relevent attribute [0] and the original post doesn't have the relevent attribute [1]. [0] https://github.com/WordPress/gutenberg/blob/4da057ecd6958d40b8f2a83db1152320e5f161e6/packages/editor/src/store/selectors.js#L329 [1] https://github.com/WordPress/gutenberg/blob/4da057ecd6958d40b8f2a83db1152320e5f161e6/packages/editor/src/store/selectors.js#L276 --- .../src/components/post-taxonomies/flat-term-selector.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/editor/src/components/post-taxonomies/flat-term-selector.js b/packages/editor/src/components/post-taxonomies/flat-term-selector.js index 85331fc242a10f..37cea02b0f0f9a 100644 --- a/packages/editor/src/components/post-taxonomies/flat-term-selector.js +++ b/packages/editor/src/components/post-taxonomies/flat-term-selector.js @@ -86,7 +86,7 @@ export function FlatTermSelector( { slug } ) { const query = { ...DEFAULT_QUERY, - include: _termIds.join( ',' ), + include: _termIds?.join( ',' ), per_page: -1, }; @@ -103,7 +103,7 @@ export function FlatTermSelector( { slug } ) { : false, taxonomy: _taxonomy, termIds: _termIds, - terms: _termIds.length + terms: _termIds?.length ? getEntityRecords( 'taxonomy', slug, query ) : EMPTY_ARRAY, hasResolvedTerms: hasFinishedResolution( 'getEntityRecords', [