/
wordpress.js
253 lines (206 loc) · 5.64 KB
/
wordpress.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
var url = require( "url" ),
xmlrpc = require( "xmlrpc" ),
fieldMap = require( "./fields" );
// http://codex.wordpress.org/XML-RPC_Support
// http://codex.wordpress.org/XML-RPC_WordPress_API
function extend( a, b ) {
for ( var p in b ) {
a[ p ] = b[ p ];
}
return a;
}
function Client( settings ) {
[ "url", "username", "password" ].forEach(function( prop ) {
if ( !settings.hasOwnProperty( prop ) ) {
throw new Error( "Missing required setting: " + prop );
}
});
var parsedUrl = Client.parseUrl( settings.url );
this.rpc = xmlrpc[ parsedUrl.secure ? "createSecureClient" : "createClient" ]({
host: parsedUrl.host,
port: parsedUrl.port,
path: parsedUrl.path
});
this.blogId = settings.blogId || 0;
this.username = settings.username;
this.password = settings.password;
}
Client.parseUrl = function( wpUrl ) {
var urlParts, secure;
// allow URLs without a protocol
if ( !(/\w+:\/\//.test( wpUrl ) ) ) {
wpUrl = "http://" + wpUrl;
}
urlParts = url.parse( wpUrl );
secure = urlParts.protocol === "https:";
return {
host: urlParts.hostname,
port: urlParts.port || (secure ? 443 : 80),
path: urlParts.path.replace( /\/+$/, "" ) + "/xmlrpc.php",
secure: secure
};
};
extend( Client.prototype, {
call: function( method ) {
var args = [].slice.call( arguments, 1 ),
fn = args.pop();
if ( typeof fn !== "function" ) {
args.push( fn );
fn = null;
}
this.rpc.methodCall( method, args, fn );
},
authenticatedCall: function() {
var args = [].slice.call( arguments );
args.splice( 1, 0, this.blogId, this.username, this.password );
this.call.apply( this, args );
},
listMethods: function( fn ) {
this.call( "system.listMethods", fn );
}
});
extend( Client.prototype, {
getPost: function( id, fields, fn ) {
if ( typeof fields === "function" ) {
fn = fields;
fields = null;
}
if ( fields ) {
fields = fieldMap.array( fields, "post" );
}
this.authenticatedCall( "wp.getPost", id, fields, function( error, post ) {
if ( error ) {
return fn( error );
}
fn( null, fieldMap.from( post, "post" ) );
});
},
getPosts: function( filter, fields, fn ) {
if ( typeof filter === "function" ) {
fn = filter;
fields = null;
filter = {};
}
if ( typeof fields === "function" ) {
fn = fields;
fields = null;
}
if ( filter.type ) {
filter.post_type = filter.type;
delete filter.type;
}
if ( filter.status ) {
filter.post_status = filter.status;
delete filter.status;
}
if ( filter.orderby ) {
filter.orderby = fieldMap.array( [ filter.orderby ], "post" )[ 0 ];
}
if ( fields ) {
fields = fieldMap.array( fields, "post" );
}
this.authenticatedCall( "wp.getPosts", filter, fields, function( error, posts ) {
if ( error ) {
return fn( error );
}
fn( null, posts.map(function( post ) {
return fieldMap.from( post, "post" );
}));
});
},
newPost: function( data, fn ) {
this.authenticatedCall( "wp.newPost", fieldMap.to( data, "post" ), fn );
},
// to remove a term, just set the terms and leave out the id that you want to remove
// to remove a custom field, pass the id with no key or value
editPost: function( id, data, fn ) {
this.authenticatedCall( "wp.editPost", id, fieldMap.to( data, "post" ), fn );
},
deletePost: function( id, fn ) {
this.authenticatedCall( "wp.deletePost", id, fn );
},
getPostType: function( name, fn ) {
this.authenticatedCall( "wp.getPostType", name, function( error, postType ) {
if ( error ) {
return fn( error );
}
fn( null, fieldMap.from( postType, "postType" ) );
});
},
getPostTypes: function( fn ) {
this.authenticatedCall( "wp.getPostTypes", function( error, postTypes ) {
if ( error ) {
return fn( error );
}
Object.keys( postTypes ).forEach(function( postType ) {
postTypes[ postType ] = fieldMap.from( postTypes[ postType ], "postType" );
});
fn( null, postTypes );
});
}
});
extend( Client.prototype, {
getTaxonomy: function( name, fn ) {
this.authenticatedCall( "wp.getTaxonomy", name, function( error, taxonomy ) {
if ( error ) {
return fn( error );
}
fn( null, fieldMap.from( taxonomy, "taxonomy" ) );
});
},
getTaxonomies: function( fn ) {
this.authenticatedCall( "wp.getTaxonomies", function( error, taxonomies ) {
if ( error ) {
return fn( error );
}
fn( null, taxonomies.map(function( taxonomy ) {
return fieldMap.from( taxonomy, "taxonomy" );
}));
});
},
getTerm: function( taxonomy, id, fn ) {
this.authenticatedCall( "wp.getTerm", taxonomy, id, function( error, term ) {
if ( error ) {
return fn( error );
}
fn( null, fieldMap.from( term, "term" ) );
});
},
getTerms: function( taxonomy, filter, fn ) {
if ( typeof filter === "function" ) {
fn = filter;
filter = {};
}
if ( filter.hideEmpty ) {
filter.hide_empty = filter.hideEmpty;
delete filter.hideEmpty;
}
if ( filter.orderby ) {
filter.orderby = fieldMap.array( [ filter.orderby ], "term" )[ 0 ];
}
this.authenticatedCall( "wp.getTerms", taxonomy, filter, function( error, terms ) {
if ( error ) {
return fn( error );
}
fn( null, terms.map(function( term ) {
return fieldMap.from( term, "term" );
}));
});
},
newTerm: function( data, fn ) {
this.authenticatedCall( "wp.newTerm", fieldMap.to( data, "term" ), fn );
},
editTerm: function( id, data, fn ) {
this.authenticatedCall( "wp.editTerm", id, fieldMap.to( data, "term" ), fn );
},
deleteTerm: function( taxonomy, id, fn ) {
this.authenticatedCall( "wp.deleteTerm", taxonomy, id, fn );
}
});
module.exports = {
Client: Client,
createClient: function( settings ) {
return new Client( settings );
},
fieldMap: fieldMap
};