Skip to content

Commit

Permalink
feat: You can add multiple profiles of WordPress.
Browse files Browse the repository at this point in the history
  • Loading branch information
devbean committed Mar 27, 2023
1 parent 15fb5bc commit af74d11
Show file tree
Hide file tree
Showing 20 changed files with 2,407 additions and 737 deletions.
1,786 changes: 1,374 additions & 412 deletions main.js

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@
"tslib": "2.4.0",
"typescript": "4.7.4",
"xmlbuilder2": "^3.0.2"
},
"dependencies": {
"solid-js": "^1.6.14"
}
}
42 changes: 26 additions & 16 deletions src/abstract-wp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import { ERROR_NOTICE_TIMEOUT } from './consts';
import matter from 'gray-matter';
import { isPromiseFulfilledResult, openWithBrowser, SafeAny } from './utils';
import { PostPublishedModal } from './post-published-modal';
import { WpProfile } from './wp-profile';


export abstract class AbstractWordPressClient implements WordPressClient {

protected constructor(
protected readonly app: App,
protected readonly plugin: WordpressPlugin
protected readonly plugin: WordpressPlugin,
protected readonly profile: WpProfile
) { }

abstract publish(
Expand Down Expand Up @@ -51,7 +53,7 @@ export abstract class AbstractWordPressClient implements WordPressClient {

publishPost(defaultPostParams?: WordPressPostParams): Promise<WordPressClientResult> {
return new Promise<WordPressClientResult>((resolve, reject) => {
if (!this.plugin.settings.endpoint || this.plugin.settings.endpoint.length === 0) {
if (!this.profile.endpoint || this.profile.endpoint.length === 0) {
new Notice(this.plugin.i18n.t('error_noEndpoint'), ERROR_NOTICE_TIMEOUT);
reject({
code: WordPressClientReturnCode.Error,
Expand Down Expand Up @@ -89,7 +91,7 @@ export abstract class AbstractWordPressClient implements WordPressClient {
username,
password
});
const selectedCategories = matterData.categories as number[] ?? this.plugin.settings.lastSelectedCategories;
const selectedCategories = matterData.categories as number[] ?? this.profile.lastSelectedCategories;
new WpPublishModal(
this.app,
this.plugin,
Expand All @@ -115,15 +117,23 @@ export abstract class AbstractWordPressClient implements WordPressClient {
};

if (this.openLoginModal()) {
new WpLoginModal(
this.app,
this.plugin,
(username, password, loginModal) => {
publishToWordPress(username, password, loginModal).then(() => {
// make compiler happy
});
}
).open();
if (this.profile.username && this.profile.password) {
// saved username and password found
publishToWordPress(this.profile.username, this.profile.password).then(() => {
// make compiler happy
});
} else {
new WpLoginModal(
this.app,
this.plugin,
this.profile,
(username, password, loginModal) => {
publishToWordPress(username, password, loginModal).then(() => {
// make compiler happy
});
}
).open();
}
} else {
publishToWordPress(null, null).then(() => {
// make compiler happy
Expand Down Expand Up @@ -170,7 +180,7 @@ export abstract class AbstractWordPressClient implements WordPressClient {
new Notice(this.plugin.i18n.t('error_publishFailed', {
code: data.code,
message: data.message
}), 0);
}), ERROR_NOTICE_TIMEOUT);
} else {
new Notice(this.plugin.i18n.t('message_publishSuccessfully'));
publishModal?.close();
Expand All @@ -186,13 +196,13 @@ export abstract class AbstractWordPressClient implements WordPressClient {
this.updateFrontMatter(modified);

if (this.plugin.settings.rememberLastSelectedCategories) {
this.plugin.settings.lastSelectedCategories = (result.data as SafeAny).categories;
this.profile.lastSelectedCategories = (result.data as SafeAny).categories;
await this.plugin.saveSettings();
}

if (this.plugin.settings.showWordPressEditConfirm) {
new PostPublishedModal(this.app, this.plugin, (modal: Modal) => {
openWithBrowser(`${this.plugin.settings.endpoint}/wp-admin/post.php`, {
openWithBrowser(`${this.profile.endpoint}/wp-admin/post.php`, {
action: 'edit',
post: postId
});
Expand Down Expand Up @@ -237,7 +247,7 @@ export abstract class AbstractWordPressClient implements WordPressClient {
postParams.postId = matterData.postId;
}
if (matterData.categories) {
postParams.categories = matterData.categories as number[] ?? this.plugin.settings.lastSelectedCategories;
postParams.categories = matterData.categories as number[] ?? this.profile.lastSelectedCategories;
}
if (matterData.tags) {
postParams.tags = matterData.tags as string[];
Expand Down
24 changes: 24 additions & 0 deletions src/app-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { WpProfile } from './wp-profile';
import { WordPressOAuth2Token } from './oauth2-client';
import { createSignal } from 'solid-js';

export class AppData {
private static instance: AppData;


/**
* Code verifier between classes.
*/
codeVerifier: string | undefined;
oauth2Token = createSignal<WordPressOAuth2Token | undefined>(undefined);

private constructor() { }

static getInstance(): AppData {
if (!AppData.instance) {
AppData.instance = new AppData();
}
return AppData.instance;
}

}
2 changes: 2 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export const WP_OAUTH2_CLIENT_SECRET = 'zg4mKy9O1mc1mmynShJTVxs8r1k3X4e3g1sv5URl
export const WP_OAUTH2_TOKEN_ENDPOINT = 'https://public-api.wordpress.com/oauth2/token';
export const WP_OAUTH2_AUTHORIZE_ENDPOINT = 'https://public-api.wordpress.com/oauth2/authorize';
export const WP_OAUTH2_VALIDATE_TOKEN_ENDPOINT = 'https://public-api.wordpress.com/oauth2/token-info';
export const WP_OAUTH2_URL_ACTION = 'wordpress-plugin-oauth';
export const WP_OAUTH2_REDIRECT_URI = `obsidian://${WP_OAUTH2_URL_ACTION}`;
20 changes: 19 additions & 1 deletion src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
"error_wpComAuthFailed": "WordPress authorize failed!\n<%= error %>: <%= desc %>",
"error_invalidWpComToken": "Invalid wordpress.com token",
"error_cannotParseResponse": "Cannot parse WordPress server response.",
"error_noDefaultProfile": "No default profile found.",
"message_publishSuccessfully": "Post published successfully!",
"message_wpComTokenValidated": "Wordpress.com token validated successfully!",
"ribbon_iconTitle": "WordPress Publish",
"command_publish": "Publish current note",
"command_publishWithDefault": "Publish current note with default options",
"settings_title": "WordPress Publish",
"settings_profiles": "Profiles",
"settings_profilesDesc": "Manage WordPress profiles.",
"settings_profilesModal": "Open",
"settings_url": "WordPress URL",
"settings_urlDesc": "Full path of installed WordPress, for example, https://example.com/wordpress",
"settings_urlPlaceholder": "https://example.com/wordpress",
Expand Down Expand Up @@ -75,5 +79,19 @@
"publishedModal_title": "Post published successfully!",
"publishedModal_confirmEditInWP": "Do you want to open WordPress edit page in browser?",
"publishedModal_cancel": "Cancel",
"publishedModal_open": "Open"
"publishedModal_open": "Open",
"profilesManageModal_setDefault": "Set Default",
"profilesManageModal_showDetails": "Edit",
"profilesManageModal_deleteTooltip": "Delete",
"profilesManageModal_title": "Profiles",
"profilesManageModal_create": "Create",
"profilesManageModal_createDesc": "Creates a new WordPress profile.",
"profileModal_title": "Profile",
"profileModal_Save": "Save",
"profileModal_name": "Name",
"profileModal_nameDesc": "WordPress name of this profile.",
"profileModal_rememberUsername": "Remember Username",
"profileModal_rememberPassword": "Remember Password",
"profileModal_setDefault": "Set Default",
"profilesChooserModal_choose": "Choose"
}
20 changes: 19 additions & 1 deletion src/i18n/zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
"error_wpComAuthFailed": "WordPress 认证失败!\n<%= error %>: <%= desc %>",
"error_invalidWpComToken": "非法的 wordpress.com 令牌",
"error_cannotParseResponse": "无法识别 WordPress 服务器的响应",
"error_noDefaultProfile": "无法找到默认账号",
"message_publishSuccessfully": "文章发布成功",
"message_wpComTokenValidated": "Wordpress.com 令牌验证通过",
"ribbon_iconTitle": "发布到 WordPress",
"command_publish": "发布当前笔记",
"command_publishWithDefault": "使用默认参数发布当前笔记",
"settings_title": "WordPress 发布插件",
"settings_profiles": "WordPress 账户",
"settings_profilesDesc": "管理 WordPress 账户",
"settings_profilesModal": "打开",
"settings_url": "WordPress URL",
"settings_urlDesc": "WordPress 完整路径,例如 https://example.com/wordpress",
"settings_urlPlaceholder": "https://example.com/wordpress",
Expand Down Expand Up @@ -75,5 +79,19 @@
"publishedModal_title": "文章发布成功",
"publishedModal_confirmEditInWP": "需要使用浏览器打开 WordPress 编辑页面吗?",
"publishedModal_cancel": "取消",
"publishedModal_open": "打开"
"publishedModal_open": "打开",
"profilesManageModal_setDefault": "设为默认",
"profilesManageModal_showDetails": "编辑",
"profilesManageModal_deleteTooltip": "删除",
"profilesManageModal_title": "WordPress 帐户",
"profilesManageModal_create": "创建",
"profilesManageModal_createDesc": "创建新的 WordPress 帐户",
"profileModal_title": "WordPress 帐户",
"profileModal_Save": "保存",
"profileModal_name": "名称",
"profileModal_nameDesc": "WordPress 账户名称",
"profileModal_rememberUsername": "记住用户名",
"profileModal_rememberPassword": "记住密码",
"profileModal_setDefault": "设为默认",
"profilesChooserModal_choose": "选择"
}
Loading

0 comments on commit af74d11

Please sign in to comment.