Skip to content

Commit 69a416f

Browse files
DataViews: Removing mapping of user patterns to temporary object (#63042)
Co-authored-by: youknowriad <[email protected]> Co-authored-by: ntsekouras <[email protected]>
1 parent 88f44fd commit 69a416f

File tree

8 files changed

+114
-87
lines changed

8 files changed

+114
-87
lines changed

packages/edit-site/src/components/page-patterns/index.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function PreviewWrapper( { item, onClick, ariaDescribedBy, children } ) {
117117

118118
function Preview( { item, viewType } ) {
119119
const descriptionId = useId();
120+
const description = item.description || item?.excerpt?.raw;
120121
const isUserPattern = item.type === PATTERN_TYPES.user;
121122
const isTemplatePart = item.type === TEMPLATE_PART_POST_TYPE;
122123
const [ backgroundColor ] = useGlobalStyle( 'color.background' );
@@ -143,7 +144,7 @@ function Preview( { item, viewType } ) {
143144
<PreviewWrapper
144145
item={ item }
145146
onClick={ onClick }
146-
ariaDescribedBy={ item.description ? descriptionId : undefined }
147+
ariaDescribedBy={ !! description ? descriptionId : undefined }
147148
>
148149
{ isEmpty && isTemplatePart && __( 'Empty template part' ) }
149150
{ isEmpty && ! isTemplatePart && __( 'Empty pattern' ) }
@@ -156,9 +157,9 @@ function Preview( { item, viewType } ) {
156157
</Async>
157158
) }
158159
</PreviewWrapper>
159-
{ item.description && (
160+
{ !! description && (
160161
<div hidden id={ descriptionId }>
161-
{ item.description }
162+
{ description }
162163
</div>
163164
) }
164165
</div>
@@ -222,7 +223,7 @@ function Title( { item } ) {
222223
// See https://github.com/WordPress/gutenberg/pull/51898#discussion_r1243399243.
223224
tabIndex="-1"
224225
>
225-
{ title || item.name }
226+
{ title }
226227
</Button>
227228
) }
228229
</Flex>
@@ -300,23 +301,20 @@ export default function DataviewsPatterns() {
300301
header: __( 'Sync status' ),
301302
id: 'sync-status',
302303
render: ( { item } ) => {
304+
const syncStatus =
305+
'wp_pattern_sync_status' in item
306+
? item.wp_pattern_sync_status ||
307+
PATTERN_SYNC_TYPES.full
308+
: PATTERN_SYNC_TYPES.unsynced;
303309
// User patterns can have their sync statuses checked directly.
304310
// Non-user patterns are all unsynced for the time being.
305311
return (
306312
<span
307-
className={ `edit-site-patterns__field-sync-status-${ item.syncStatus }` }
313+
className={ `edit-site-patterns__field-sync-status-${ syncStatus }` }
308314
>
309315
{
310-
(
311-
SYNC_FILTERS.find(
312-
( { value } ) =>
313-
value === item.syncStatus
314-
) ||
315-
SYNC_FILTERS.find(
316-
( { value } ) =>
317-
value ===
318-
PATTERN_SYNC_TYPES.unsynced
319-
)
316+
SYNC_FILTERS.find(
317+
( { value } ) => value === syncStatus
320318
).label
321319
}
322320
</span>

packages/edit-site/src/components/page-patterns/search-items.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,42 @@ import {
2424
} from '../../utils/constants';
2525

2626
// Default search helpers.
27-
const defaultGetName = ( item ) =>
28-
item.type !== TEMPLATE_PART_POST_TYPE ? item.name || '' : '';
29-
export const defaultGetTitle = ( item ) =>
30-
typeof item.title === 'string' ? item.title : item.title.rendered;
31-
const defaultGetDescription = ( item ) => item.description || '';
27+
const defaultGetName = ( item ) => {
28+
if ( item.type === PATTERN_TYPES.user ) {
29+
return item.slug;
30+
}
31+
32+
if ( item.type === TEMPLATE_PART_POST_TYPE ) {
33+
return '';
34+
}
35+
36+
return item.name || '';
37+
};
38+
39+
export const defaultGetTitle = ( item ) => {
40+
if ( typeof item.title === 'string' ) {
41+
return item.title;
42+
}
43+
44+
if ( item.title && item.title.rendered ) {
45+
return item.title.rendered;
46+
}
47+
48+
if ( item.title && item.title.raw ) {
49+
return item.title.raw;
50+
}
51+
52+
return '';
53+
};
54+
55+
const defaultGetDescription = ( item ) => {
56+
if ( item.type === PATTERN_TYPES.user ) {
57+
return item.excerpt.raw;
58+
}
59+
60+
return item.description || '';
61+
};
62+
3263
const defaultGetKeywords = ( item ) => item.keywords || [];
3364
const defaultHasCategory = () => false;
3465

packages/edit-site/src/components/page-patterns/use-patterns.js

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ const selectPatterns = createSelector(
128128
patterns: themePatterns,
129129
isResolving: isResolvingThemePatterns,
130130
} = selectThemePatterns( select );
131-
const { patterns: userPatterns, isResolving: isResolvingUserPatterns } =
132-
selectUserPatterns( select );
131+
const {
132+
patterns: userPatterns,
133+
isResolving: isResolvingUserPatterns,
134+
categories: userPatternCategories,
135+
} = selectUserPatterns( select );
133136

134137
let patterns = [
135138
...( themePatterns || [] ),
@@ -141,20 +144,44 @@ const selectPatterns = createSelector(
141144
// Non-user patterns are all unsynced for the time being.
142145
patterns = patterns.filter( ( pattern ) => {
143146
return pattern.type === PATTERN_TYPES.user
144-
? pattern.syncStatus === syncStatus
147+
? ( pattern.wp_pattern_sync_status ||
148+
PATTERN_SYNC_TYPES.full ) === syncStatus
145149
: syncStatus === PATTERN_SYNC_TYPES.unsynced;
146150
} );
147151
}
148152

149153
if ( categoryId ) {
150154
patterns = searchItems( patterns, search, {
151155
categoryId,
152-
hasCategory: ( item, currentCategory ) =>
153-
item.categories?.includes( currentCategory ),
156+
hasCategory: ( item, currentCategory ) => {
157+
if ( item.type === PATTERN_TYPES.user ) {
158+
return item.wp_pattern_category.some(
159+
( catId ) =>
160+
userPatternCategories.find(
161+
( cat ) => cat.id === catId
162+
)?.slug === currentCategory
163+
);
164+
}
165+
return item.categories?.includes( currentCategory );
166+
},
154167
} );
155168
} else {
156169
patterns = searchItems( patterns, search, {
157-
hasCategory: ( item ) => ! item.hasOwnProperty( 'categories' ),
170+
hasCategory: ( item ) => {
171+
if ( item.type === PATTERN_TYPES.user ) {
172+
return (
173+
userPatternCategories?.length &&
174+
( ! item.wp_pattern_category?.length ||
175+
! item.wp_pattern_category.some( ( catId ) =>
176+
userPatternCategories.find(
177+
( cat ) => cat.id === catId
178+
)
179+
) )
180+
);
181+
}
182+
183+
return ! item.hasOwnProperty( 'categories' );
184+
},
158185
} );
159186
}
160187
return {
@@ -168,41 +195,6 @@ const selectPatterns = createSelector(
168195
]
169196
);
170197

171-
/**
172-
* Converts a post of type `wp_block` to a 'pattern item' that more closely
173-
* matches the structure of theme provided patterns.
174-
*
175-
* @param {Object} patternPost The `wp_block` record being normalized.
176-
* @param {Map} categories A Map of user created categories.
177-
*
178-
* @return {Object} The normalized item.
179-
*/
180-
const convertPatternPostToItem = ( patternPost, categories ) => ( {
181-
blocks: parse( patternPost.content.raw, {
182-
__unstableSkipMigrationLogs: true,
183-
} ),
184-
...( patternPost.wp_pattern_category.length > 0 && {
185-
categories: patternPost.wp_pattern_category.map(
186-
( patternCategoryId ) =>
187-
categories && categories.get( patternCategoryId )
188-
? categories.get( patternCategoryId ).slug
189-
: patternCategoryId
190-
),
191-
} ),
192-
termLabels: patternPost.wp_pattern_category.map( ( patternCategoryId ) =>
193-
categories?.get( patternCategoryId )
194-
? categories.get( patternCategoryId ).label
195-
: patternCategoryId
196-
),
197-
id: patternPost.id,
198-
name: patternPost.slug,
199-
syncStatus: patternPost.wp_pattern_sync_status || PATTERN_SYNC_TYPES.full,
200-
title: patternPost.title.raw,
201-
type: patternPost.type,
202-
description: patternPost.excerpt.raw,
203-
patternPost,
204-
} );
205-
206198
const selectUserPatterns = createSelector(
207199
( select, syncStatus, search = '' ) => {
208200
const {
@@ -222,12 +214,7 @@ const selectUserPatterns = createSelector(
222214
userPatternCategories.forEach( ( userCategory ) =>
223215
categories.set( userCategory.id, userCategory )
224216
);
225-
let patterns = patternPosts
226-
? patternPosts.map( ( record ) =>
227-
convertPatternPostToItem( record, categories )
228-
)
229-
: EMPTY_PATTERN_LIST;
230-
217+
let patterns = patternPosts ?? EMPTY_PATTERN_LIST;
231218
const isResolving = isResolvingSelector( 'getEntityRecords', [
232219
'postType',
233220
PATTERN_TYPES.user,
@@ -236,7 +223,9 @@ const selectUserPatterns = createSelector(
236223

237224
if ( syncStatus ) {
238225
patterns = patterns.filter(
239-
( pattern ) => pattern.syncStatus === syncStatus
226+
( pattern ) =>
227+
pattern.wp_pattern_sync_status ||
228+
PATTERN_SYNC_TYPES.full === syncStatus
240229
);
241230
}
242231

packages/edit-site/src/components/sidebar-navigation-screen-patterns/use-pattern-categories.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,21 @@ export default function usePatternCategories() {
5858

5959
// Update the category counts to reflect user registered patterns.
6060
userPatterns.forEach( ( pattern ) => {
61-
pattern.categories?.forEach( ( category ) => {
61+
pattern.wp_pattern_category?.forEach( ( catId ) => {
62+
const category = userPatternCategories.find(
63+
( cat ) => cat.id === catId
64+
)?.name;
6265
if ( categoryMap[ category ] ) {
6366
categoryMap[ category ].count += 1;
6467
}
6568
} );
6669
// If the pattern has no categories, add it to uncategorized.
67-
if ( ! pattern.categories?.length ) {
70+
if (
71+
! pattern.wp_pattern_category?.length ||
72+
! pattern.wp_pattern_category.some( ( catId ) =>
73+
userPatternCategories.find( ( cat ) => cat.id === catId )
74+
)
75+
) {
6876
categoryMap.uncategorized.count += 1;
6977
}
7078
} );

packages/editor/src/components/post-actions/actions.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { store as editorStore } from '../../store';
3333
import { unlock } from '../../lock-unlock';
3434
import { exportPatternAsJSONAction } from './export-pattern-action';
3535
import { CreateTemplatePartModalContents } from '../create-template-part-modal';
36+
import { getItemTitle } from '../../dataviews/actions/utils';
3637

3738
// Patterns.
3839
const { PATTERN_TYPES, CreatePatternModalContents, useDuplicatePatternProps } =
@@ -72,13 +73,6 @@ function isTemplateRemovable( template ) {
7273
);
7374
}
7475

75-
function getItemTitle( item ) {
76-
if ( typeof item.title === 'string' ) {
77-
return decodeEntities( item.title );
78-
}
79-
return decodeEntities( item.title?.rendered || '' );
80-
}
81-
8276
const trashPostAction = {
8377
id: 'move-to-trash',
8478
label: __( 'Move to Trash' ),
@@ -805,10 +799,8 @@ export const duplicatePatternAction = {
805799
modalHeader: _x( 'Duplicate pattern', 'action label' ),
806800
RenderModal: ( { items, closeModal } ) => {
807801
const [ item ] = items;
808-
const isThemePattern = item.type === PATTERN_TYPES.theme;
809802
const duplicatedProps = useDuplicatePatternProps( {
810-
pattern:
811-
isThemePattern || ! item.patternPost ? item : item.patternPost,
803+
pattern: item,
812804
onSuccess: () => closeModal(),
813805
} );
814806
return (

packages/editor/src/components/post-actions/export-pattern-action.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
1515
* Internal dependencies
1616
*/
1717
import { unlock } from '../../lock-unlock';
18+
import { getItemTitle } from '../../dataviews/actions/utils';
1819

1920
// Patterns.
2021
const { PATTERN_TYPES } = unlock( patternsPrivateApis );
@@ -23,9 +24,9 @@ function getJsonFromItem( item ) {
2324
return JSON.stringify(
2425
{
2526
__file: item.type,
26-
title: item.title || item.name,
27-
content: item.patternPost.content.raw,
28-
syncStatus: item.patternPost.wp_pattern_sync_status,
27+
title: getItemTitle( item ),
28+
content: item.content.raw,
29+
syncStatus: item.wp_pattern_sync_status,
2930
},
3031
null,
3132
2
@@ -45,14 +46,16 @@ export const exportPatternAsJSONAction = {
4546
callback: async ( items ) => {
4647
if ( items.length === 1 ) {
4748
return downloadBlob(
48-
`${ kebabCase( items[ 0 ].title || items[ 0 ].name ) }.json`,
49+
`${ kebabCase(
50+
getItemTitle( items[ 0 ] ) || items[ 0 ].slug
51+
) }.json`,
4952
getJsonFromItem( items[ 0 ] ),
5053
'application/json'
5154
);
5255
}
5356
const nameCount = {};
5457
const filesToZip = items.map( ( item ) => {
55-
const name = kebabCase( item.title || item.name );
58+
const name = kebabCase( getItemTitle( item ) || item.slug );
5659
nameCount[ name ] = ( nameCount[ name ] || 0 ) + 1;
5760
return {
5861
name: `${

packages/editor/src/dataviews/actions/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ export function getItemTitle( item: Post ) {
2424
if ( typeof item.title === 'string' ) {
2525
return decodeEntities( item.title );
2626
}
27-
return decodeEntities( item.title?.rendered || '' );
27+
if ( 'rendered' in item.title ) {
28+
return decodeEntities( item.title.rendered );
29+
}
30+
if ( 'raw' in item.title ) {
31+
return decodeEntities( item.title.raw );
32+
}
33+
return '';
2834
}
2935

3036
/**

packages/editor/src/dataviews/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type PostStatus =
99

1010
export interface BasePost {
1111
status?: PostStatus;
12-
title: string | { rendered: string };
12+
title: string | { rendered: string } | { raw: string };
1313
type: string;
1414
id: string | number;
1515
}

0 commit comments

Comments
 (0)