Skip to content

Commit

Permalink
feat: Now you can set post category by fetching categories with XML-RPC.
Browse files Browse the repository at this point in the history
  • Loading branch information
devbean committed Apr 22, 2022
1 parent 08f53be commit 2393092
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/abstract-wp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from './wp-client';
import { marked } from 'marked';
import { WpPublishModal } from './wp-publish-modal';
import { Term } from './wp-api';


export abstract class AbstractWordPressClient implements WordPressClient {
Expand All @@ -28,6 +29,13 @@ export abstract class AbstractWordPressClient implements WordPressClient {
}
): Promise<WordPressClientResult>;

abstract getCategories(
wp: {
userName: string,
password: string
}
): Promise<Term[]>;

newPost(defaultPostParams?: WordPressPostParams): Promise<WordPressClientResult> {
return new Promise((resolve, reject) => {
const { workspace } = this.app;
Expand All @@ -48,9 +56,14 @@ export abstract class AbstractWordPressClient implements WordPressClient {
postParams: defaultPostParams
}, loginModal);
} else {
const categories = await this.getCategories({
userName,
password
});
new WpPublishModal(
this.app,
this.plugin,
categories,
async (postParams, publishModal) => {
await this.doPublish({
title,
Expand Down
11 changes: 11 additions & 0 deletions src/wp-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@ export const enum PostStatus {
Publish = 'publish',
// Future = 'future'
}

export interface Term {
term_id: string;
name: string;
slug: string;
term_group: string;
taxonomy: string;
description: string;
parent: string;
count: string;
}
16 changes: 15 additions & 1 deletion src/wp-publish-modal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { App, Modal, Setting } from 'obsidian';
import WordpressPlugin from './main';
import { WordPressPostParams } from './wp-client';
import { PostStatus } from './wp-api';
import { PostStatus, Term } from './wp-api';

/**
* WordPress publish modal.
Expand All @@ -11,6 +11,7 @@ export class WpPublishModal extends Modal {
constructor(
app: App,
private readonly plugin: WordpressPlugin,
private readonly categories: Term[],
private readonly onSubmit: (params: WordPressPostParams, modal: Modal) => void
) {
super(app);
Expand All @@ -37,6 +38,19 @@ export class WpPublishModal extends Modal {
params.status = value;
});
});
if (this.categories.length > 0) {
new Setting(contentEl)
.setName('Category')
.addDropdown((dropdown) => {
this.categories.forEach(it => {
dropdown.addOption(it.term_id, it.name);
});
dropdown
.onChange(async (value: PostStatus) => {
params.status = value;
});
});
}
new Setting(contentEl)
.addButton(button => button
.setButtonText('Publish')
Expand Down
5 changes: 5 additions & 0 deletions src/wp-rest-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { App, request } from 'obsidian';
import { WordPressClientResult, WordPressClientReturnCode, WordPressPostParams } from './wp-client';
import { AbstractWordPressClient } from './abstract-wp-client';
import WordpressPlugin from './main';
import { Term } from './wp-api';

interface RestOptions {
url: URL;
Expand Down Expand Up @@ -38,6 +39,10 @@ export class WpRestClient extends AbstractWordPressClient {
});
}

getCategories(wp: { userName: string; password: string }): Promise<Term[]> {
return Promise.resolve([]);
}

protected httpPost(
path: string,
body: unknown,
Expand Down
11 changes: 11 additions & 0 deletions src/wp-xml-rpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import WordpressPlugin from './main';
import { WordPressClientResult, WordPressClientReturnCode, WordPressPostParams } from './wp-client';
import { XmlRpcClient } from './xmlrpc-client';
import { AbstractWordPressClient } from './abstract-wp-client';
import { Term } from './wp-api';

export class WpXmlRpcClient extends AbstractWordPressClient {

Expand Down Expand Up @@ -52,4 +53,14 @@ export class WpXmlRpcClient extends AbstractWordPressClient {
});
}

getCategories(wp: { userName: string; password: string }): Promise<Term[]> {
return this.client.methodCall('wp.getTerms', [
0,
wp.userName,
wp.password,
'category'
])
.then(data => (data as Term[] ?? []));
}

}

0 comments on commit 2393092

Please sign in to comment.