Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
# Conflicts:
#	main.js
  • Loading branch information
devbean committed Apr 10, 2023
2 parents 955fec8 + 15e2d15 commit e5ad368
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"error_notWpCom": "The URL is not wordpress.com, please check again.",
"error_noUsername": "No username",
"error_noPassword": "No password",
"error_noProfile": "No profile, please add one at least",
"error_noProfileName": "No profile name",
"error_invalidUrl": "Invalid URL",
"error_invalidUser": "Invalid username or password",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"error_notWpCom": "不是合法的 wordpress.com 地址,请检查",
"error_noUsername": "没有设置用户名",
"error_noPassword": "没有设置密码",
"error_noProfile": "没有账号,请至少添加一个 WordPress 账号",
"error_noProfileName": "没有设置账号名",
"error_invalidUrl": "URL 格式错误",
"error_invalidUser": "用户名或密码错误",
Expand Down
14 changes: 10 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,16 @@ export default class WordpressPlugin extends Plugin {
}

private openProfileChooser(): void {
new WpProfileChooserModal(this.app, this, (profile) => {
console.log(profile);
this.doClientPublish(profile);
}).open();
if (this.settings.profiles.length === 1) {
this.doClientPublish(this.settings.profiles[0]);
} else if (this.settings.profiles.length > 1) {
new WpProfileChooserModal(this.app, this, (profile) => {
console.log(profile);
this.doClientPublish(profile);
}).open();
} else {
new Notice(this.i18n.t('error_noProfile'));
}
}

private doClientPublish(profile: WpProfile, defaultPostParams?: WordPressPostParams): void {
Expand Down
57 changes: 31 additions & 26 deletions src/xmlrpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ export class XmlRpcClient {
method: string,
params: unknown
): Promise<unknown> {
console.log(`Endpoint: ${this.endpoint}, ${method}`, params);

const xml = this.objectToXml(method, params);
console.log(`Endpoint: ${this.endpoint}, ${method}, request: ${xml}`, params);
return request({
url: this.endpoint,
method: 'POST',
Expand Down Expand Up @@ -136,27 +135,22 @@ export class XmlRpcClient {
const doc = parser.parseFromString(xml, 'application/xml');
const methodResponse = doc.getElementsByTagName('methodResponse')[0];
const faults = methodResponse.getElementsByTagName('fault');
let response: unknown;
if (faults.length > 0) {
const faultValue = faults[0]
.getElementsByTagName('value')[0]
.children;
return this.fromValue(faultValue);
.children[0] // <value>
.children[0];
response = this.fromElement(faultValue);
} else {
const responseValue = methodResponse
.getElementsByTagName('params')[0]
.getElementsByTagName('param')[0]
.getElementsByTagName('value')[0]
.children;
return this.fromValue(responseValue);
}
}

private fromValue(collection: HTMLCollection): unknown {
for (let i = 0; i < collection.length; i++) {
const element = collection[i];
this.fromElement(element);
.children[0] // <params>
.children[0] // <param>
.children[0] // <value>
.children[0];
response = this.fromElement(responseValue);
}
return {};
console.log(`response: ${xml}`, response);
return response;
}

private fromElement(element: Element): unknown {
Expand All @@ -179,21 +173,32 @@ export class XmlRpcClient {
} else if (tagName === 'array') {
const array = [];
const arrayValues = element
.getElementsByTagName('data')[0]
.getElementsByTagName('value');
.children[0] // <data>
.children; // <value>s
for (let i = 0; i < arrayValues.length; i++) {
const arrayElement = arrayValues[i].children[0];
array.push(this.fromElement(arrayElement));
array.push(this.fromElement(arrayValues[i].children[0]));
}
return array;
} else if (tagName === 'struct') {
const struct: SafeAny = {};
const members = element.getElementsByTagName('member');
const members = element.children; // <member>s
for (let i = 0; i < members.length; i++) {
const member = members[i];
const name = member.getElementsByTagName('name')[0];
const value = member.getElementsByTagName('value')[0].children[0];
struct[name.getText()] = this.fromElement(value);
let name;
let value;
for (let memberIndex = 0; memberIndex < member.children.length; memberIndex++) {
const prop = member.children[memberIndex];
if (prop.tagName === 'name') {
name = prop;
} else if (prop.tagName === 'value') {
value = prop.children[0];
}
}
// const name = member.getElementsByTagName('name')[0];
// const value = member.getElementsByTagName('value')[0].children[0];
if (name && value) {
struct[name.getText()] = this.fromElement(value);
}
}
return struct;
}
Expand Down

0 comments on commit e5ad368

Please sign in to comment.