-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
use-site-editor-settings.js
201 lines (193 loc) · 5.72 KB
/
use-site-editor-settings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import { unlock } from '../../lock-unlock';
import inserterMediaCategories from './inserter-media-categories';
function useArchiveLabel( templateSlug ) {
const taxonomyMatches = templateSlug?.match(
/^(category|tag|taxonomy-([^-]+))$|^(((category|tag)|taxonomy-([^-]+))-(.+))$/
);
let taxonomy;
let term;
let isAuthor = false;
let authorSlug;
if ( taxonomyMatches ) {
// If is for a all taxonomies of a type
if ( taxonomyMatches[ 1 ] ) {
taxonomy = taxonomyMatches[ 2 ]
? taxonomyMatches[ 2 ]
: taxonomyMatches[ 1 ];
}
// If is for a all taxonomies of a type
else if ( taxonomyMatches[ 3 ] ) {
taxonomy = taxonomyMatches[ 6 ]
? taxonomyMatches[ 6 ]
: taxonomyMatches[ 4 ];
term = taxonomyMatches[ 7 ];
}
taxonomy = taxonomy === 'tag' ? 'post_tag' : taxonomy;
//getTaxonomy( 'category' );
//wp.data.select('core').getEntityRecords( 'taxonomy', 'category', {slug: 'newcat'} );
} else {
const authorMatches = templateSlug?.match( /^(author)$|^author-(.+)$/ );
if ( authorMatches ) {
isAuthor = true;
if ( authorMatches[ 2 ] ) {
authorSlug = authorMatches[ 2 ];
}
}
}
return useSelect(
( select ) => {
const { getEntityRecords, getTaxonomy, getAuthors } =
select( coreStore );
let archiveTypeLabel;
let archiveNameLabel;
if ( taxonomy ) {
archiveTypeLabel =
getTaxonomy( taxonomy )?.labels?.singular_name;
}
if ( term ) {
const records = getEntityRecords( 'taxonomy', taxonomy, {
slug: term,
per_page: 1,
} );
if ( records && records[ 0 ] ) {
archiveNameLabel = records[ 0 ].name;
}
}
if ( isAuthor ) {
archiveTypeLabel = 'Author';
if ( authorSlug ) {
const authorRecords = getAuthors( { slug: authorSlug } );
if ( authorRecords && authorRecords[ 0 ] ) {
archiveNameLabel = authorRecords[ 0 ].name;
}
}
}
return {
archiveTypeLabel,
archiveNameLabel,
};
},
[ authorSlug, isAuthor, taxonomy, term ]
);
}
export default function useSiteEditorSettings() {
const { setIsInserterOpened } = useDispatch( editSiteStore );
const { storedSettings, canvasMode, templateType, siteSettings } =
useSelect(
( select ) => {
const { canUser, getEntityRecord } = select( coreStore );
const { getSettings, getCanvasMode, getEditedPostType } =
unlock( select( editSiteStore ) );
return {
storedSettings: getSettings( setIsInserterOpened ),
canvasMode: getCanvasMode(),
templateType: getEditedPostType(),
siteSettings: canUser( 'read', 'settings' )
? getEntityRecord( 'root', 'site' )
: undefined,
};
},
[ setIsInserterOpened ]
);
const settingsBlockPatterns =
storedSettings.__experimentalAdditionalBlockPatterns ?? // WP 6.0
storedSettings.__experimentalBlockPatterns; // WP 5.9
const settingsBlockPatternCategories =
storedSettings.__experimentalAdditionalBlockPatternCategories ?? // WP 6.0
storedSettings.__experimentalBlockPatternCategories; // WP 5.9
const {
restBlockPatterns,
restBlockPatternCategories,
templateSlug,
userPatternCategories,
} = useSelect( ( select ) => {
const { getEditedPostType, getEditedPostId } = select( editSiteStore );
const { getEditedEntityRecord, getUserPatternCategories } =
select( coreStore );
const usedPostType = getEditedPostType();
const usedPostId = getEditedPostId();
const _record = getEditedEntityRecord(
'postType',
usedPostType,
usedPostId
);
return {
restBlockPatterns: select( coreStore ).getBlockPatterns(),
restBlockPatternCategories:
select( coreStore ).getBlockPatternCategories(),
templateSlug: _record.slug,
userPatternCategories: getUserPatternCategories(),
};
}, [] );
const archiveLabels = useArchiveLabel( templateSlug );
const blockPatterns = useMemo(
() =>
[
...( settingsBlockPatterns || [] ),
...( restBlockPatterns || [] ),
]
.filter(
( x, index, arr ) =>
index === arr.findIndex( ( y ) => x.name === y.name )
)
.filter( ( { postTypes } ) => {
return (
! postTypes ||
( Array.isArray( postTypes ) &&
postTypes.includes( templateType ) )
);
} ),
[ settingsBlockPatterns, restBlockPatterns, templateType ]
);
const blockPatternCategories = useMemo(
() =>
[
...( settingsBlockPatternCategories || [] ),
...( restBlockPatternCategories || [] ),
].filter(
( x, index, arr ) =>
index === arr.findIndex( ( y ) => x.name === y.name )
),
[ settingsBlockPatternCategories, restBlockPatternCategories ]
);
return useMemo( () => {
const {
__experimentalAdditionalBlockPatterns,
__experimentalAdditionalBlockPatternCategories,
focusMode,
...restStoredSettings
} = storedSettings;
return {
...restStoredSettings,
inserterMediaCategories,
__experimentalBlockPatterns: blockPatterns,
__experimentalBlockPatternCategories: blockPatternCategories,
__experimentalUserPatternCategories: userPatternCategories,
focusMode: canvasMode === 'view' && focusMode ? false : focusMode,
__experimentalArchiveTitleTypeLabel: archiveLabels.archiveTypeLabel,
__experimentalArchiveTitleNameLabel: archiveLabels.archiveNameLabel,
pageOnFront: siteSettings?.page_on_front,
pageForPosts: siteSettings?.page_for_posts,
};
}, [
storedSettings,
blockPatterns,
blockPatternCategories,
userPatternCategories,
canvasMode,
archiveLabels.archiveTypeLabel,
archiveLabels.archiveNameLabel,
siteSettings?.page_on_front,
siteSettings?.page_for_posts,
] );
}