Skip to content

Commit 0bf02f3

Browse files
committed
Limit post type choices to public types
1 parent dedbd2c commit 0bf02f3

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/wp-api.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ export const enum PostTypeConst {
1414
Post = 'post',
1515
Page = 'page',
1616
}
17-
export type PostType = string;
17+
18+
export interface PostTypeObj {
19+
slug: string;
20+
name: string;
21+
}
22+
export type PostType = string | PostTypeObj;
1823

1924
export interface Term {
2025
id: string;

src/wp-publish-modal.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,21 @@ export class WpPublishModal extends AbstractModal {
172172
});
173173

174174
if (!this.matterData?.postId) {
175+
const defaultType = params.postType
176+
const defaultTypeSlug = 'string' === typeof defaultType ? defaultType : defaultType.slug;
177+
175178
new Setting(contentEl)
176179
.setName(this.t('publishModal_postType'))
177180
.addDropdown((dropdown) => {
178181
this.postTypes.items.forEach(it => {
179-
dropdown.addOption(it, it);
182+
if ('string' === typeof it) {
183+
dropdown.addOption(it, it);
184+
} else {
185+
dropdown.addOption(it.slug , it.name);
186+
}
180187
});
181188
dropdown
182-
.setValue(params.postType)
189+
.setValue(defaultTypeSlug)
183190
.onChange((value) => {
184191
params.postType = value as PostType;
185192
this.display(params);

src/wp-rest-client.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class WpRestClient extends AbstractWordPressClient {
115115

116116
async getPostTypes(certificate: WordPressAuthParams): Promise<PostType[]> {
117117
const data: SafeAny = await this.client.httpGet(
118-
getUrl(this.context.endpoints?.getPostTypes, 'wp-json/wp/v2/types'),
118+
getUrl(this.context.endpoints?.getPostTypes, 'wp-json/wp/v2/types?context=edit'),
119119
{
120120
headers: this.context.getHeaders(certificate)
121121
});
@@ -290,7 +290,10 @@ class WpRestClientCommonContext implements WpRestClientContext {
290290
}),
291291
toPostTypes: (response: SafeAny): PostType[] => {
292292
if (isObject(response)) {
293-
return Object.keys(response);
293+
return Object.entries(response)
294+
// We only publish to viewable post types, and never to an attachment
295+
.filter(([slug, { viewable }]) => viewable && 'attachment' !== slug)
296+
.map(([slug, { labels :{singular_name} }]: [string, { labels: {singular_name: string} }]) => ({slug, name: singular_name}));
294297
}
295298
return [];
296299
}

0 commit comments

Comments
 (0)