/
wp.xmlrpc.php
407 lines (393 loc) · 13.2 KB
/
wp.xmlrpc.php
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
/**
* Wordpress XML-RPC Class
*
* XML-RPCによる通信を簡単に行うためのラッパークラスです。
*
* PHP versions 5.3+
* Wordpress version 3.4+
*
* @category Web Services
* @author moi <twitter: @moi_fc2>
* @copyright 2012 moi
* @license MIT Licence
* @version v1.0.2
*/
require_once('XML/RPC.php');
class wpXMLRPC {
protected $client, $appkey, $user, $password, $blog_id;
/**
* コンストラクタ
*
* @param string $path xmlrpc.phpのURL
* @param string $host ホスト名
* @param string $user Wordpressユーザー名
* @param string $password Wordpressパスワード
* @return void
*/
public function __construct($path, $host, $user, $password) {
$this->client = new XML_RPC_client($path, $host, 80);
$this->appkey = new XML_RPC_Value('', 'string');
$this->user = new XML_RPC_Value($user, 'string');
$this->password = new XML_RPC_Value($password, 'string');
$this->blog_id = new XML_RPC_Value(1, 'int');
}
/**
* XML-RPC送信
*
* @param struct $message XML_RPC_Message
* @return struct
*/
protected function sendXMLRPC($message) {
if ( !($res = $this->client->send($message)) ) {
return array(
'error' => 'サーバーに接続できません。'
);
} else if ( $res->faultCode() ) {
return array(
'error' => $res->faultString()
);
}
return XML_RPC_decode($res->value());
}
/**
* 新規記事投稿
*
* @param struct $data 投稿内容のデータ
* @return struct
*/
public function newPost(array $data) {
$content = array();
// 投稿タイプ (post|page|attachment)
if ( $post_type = $data['post_type'] ) {
$content['post_type'] = new XML_RPC_Value($post_type, 'string');
}
// 公開ステータス (publish|pending|draft|private|static|object|attachment|inherit|future)
if ( $post_status = $data['post_status'] ) {
$content['post_status'] = new XML_RPC_Value($post_status, 'string');
}
// タイトル
if ( $post_title = $data['post_title'] ) {
$content['post_title'] = new XML_RPC_Value($post_title, 'string');
} else {
return array(
'error' => 'タイトル(post_title)が設定されていません。'
);
}
// 筆者
if ( $post_author = $data['post_author'] ) {
$content['post_author'] = new XML_RPC_Value($post_author, 'int');
}
// 概要
if ( $post_excerpt = $data['post_excerpt'] ) {
$content['post_excerpt'] = new XML_RPC_Value($post_excerpt, 'string');
}
// 内容
if ( $post_content = $data['post_content'] ) {
$content['post_content'] = new XML_RPC_Value($post_content, 'string');
}
if ( !$post_content && !$post_excerpt ) {
return array(
'error' => '内容(post_content)も概要(post_excerpt)も設定されていません。'
);
}
// 投稿時間
if ( $post_date_gmt = $data['post_date_gmt'] ) {
$content['post_date_gmt'] = new XML_RPC_Value($post_date_dmt, 'dateTime.iso8601');
} else if ( $post_date = $data['post_date'] ) {
$content['post_date'] = new XML_RPC_Value($post_date, 'dateTime.iso8601');
} else {
$content['post_date'] = new XML_RPC_Value(time(), 'dateTime.iso8601');
}
// 投稿形式
if ( $post_format = $data['post_format'] ) {
$content['post_format'] = new XML_RPC_Value($post_format, 'string');
}
// 記事パスワード
if ( $post_password = $data['post_password'] ) {
$content['post_password'] = new XML_RPC_Value($post_password, 'string');
}
// コメント状態 (open|closed|registered_only)
if ( $comment_status = $data['comment_status'] ) {
$content['comment_status'] = new XML_RPC_Value($comment_status, 'string');
}
// トラックバック状態 (open|closed)
if ( $ping_status = $data['ping_status'] ) {
$content['ping_status'] = new XML_RPC_Value($ping_status, 'string');
}
// Sticky
if ( $sticky = $data['sticky'] ) {
$content['sticky'] = new XML_RPC_Value($sticky, 'bool');
}
// アイキャッチ画像
if ( $post_thumbnail = $data['post_thumbnail'] ) {
if ( is_numeric($post_thumbnail) ) {
$res = $post_thumbnail;
} else if ( is_array($post_thumbnail) ) {
$res = $this->uploadFile($post_thumbnail['path'], $post_thumbnail['name']);
} else {
$res = $this->uploadFile($post_thumbnail);
}
if ( $res ) {
$content['post_thumbnail'] = new XML_RPC_Value($res['id'], 'int');
} else {
return array(
'error' => 'アイキャッチ画像のアップロードに失敗しました。'
);
}
}
// 親記事
if ( $post_parent = $data['post_parent'] ) {
$content['post_parent'] = new XML_RPC_Value($post_parent, 'int');
}
// カスタムフィールド
if ( $custom_fields = $data['custom_fields'] ) {
$content['custom_fields'] = array();
foreach($custom_fields as $field) {
$content['custom_fields'][] = new XML_RPC_Value(
array(
'key' => new XML_RPC_Value($field['key'], 'string'),
'value' => new XML_RPC_Value($field['value'], 'string')
),
'struct'
);
}
$content['custom_fields'] = new XML_RPC_Value($content['custom_fields'], 'struct');
}
// スラッグ名
if ( $post_name = $data['post_name'] ) {
$content['post_name'] = new XML_RPC_Value($post_name, 'string');
}
// terms
if ( $terms = $data['terms'] ) {
$content['terms'] = array();
foreach($terms as $key => $value) {
$content['terms'][$key] = array();
$termList = $this->createHash( $this->getTerms($key), 'slug' );
foreach($value as $term) {
$id = '';
if (is_array($term)) {
if ($term['slug'] != '') {
$t = $termList[mb_strtolower($term['slug'])];
$id = $t['term_id'];
} else {
$term['taxonomy'] = $key;
$id = newTerm($term);
}
} else {
$id = $term;
}
$content['terms'][$key][] = new XML_RPC_Value($id, 'int');
}
$content['terms'][$key] = new XML_RPC_Value($content['terms'][$key], 'struct');
}
$content['terms'] = new XML_RPC_Value($content['terms'], 'struct');
}
// terms_names
if ( $terms_names = $data['terms_names'] ) {
$content['terms_names'] = array();
foreach($terms_names as $key => $value) {
$termList = $this->createHash( $this->getTerms($key), 'slug' );
$content['terms_names'][$key] = array();
foreach($value as $term) {
$name = '';
$slug = '';
$termKey = '';
$termValue = array();
$term_id = '';
if (is_array($term)) {
if (!isset($term['name'])) continue;
$name = $term['name'];
$termKey = ($term['slug']) ? $term['slug'] : $term['name'];
$termValue = $term;
$termValue['taxonomy'] = $key;
} else {
$name = $term;
$termKey = $term;
$termValue = array(
'name' => $term,
'taxonomy' => $key
);
}
$termKey = mb_strtolower($termKey);
$term_id = (!$termList[$termKey]) ? $this->newTerm($termValue) : $termList[$termKey]['term_id'];
$content['terms'][$key][] = new XML_RPC_Value($term_id, 'int');
}
$content['terms'][$key] = new XML_RPC_Value($content['terms'][$key], 'struct');
}
$content['terms'] = new XML_RPC_Value($content['terms'], 'struct');
}
// terms_slugs
if ( $terms_slugs = $data['terms_slugs'] ) {
$content['terms'] = (is_array($content['terms'])) ? $content['terms'] : array();
foreach($terms_slugs as $key => $value) {
$content['terms'][$key] = ($content['terms'][$key]) ? $content['terms'][$key] : array();
$termList = $this->createHash( $this->getTerms($key), 'slug' );
foreach($value as $term) {
$is_array = is_array($term);
$slug = ($is_array) ? $term['slug'] : $term;
if ($slug == '') continue;
// slug から term_id 取得
$term_id = '';
$t = $termList[mb_strtolower($slug)];
// $term_id が空の場合は新規作成
if ($t != '') {
$term_id = $t['term_id'];
if (!isset($term['name'])) continue;
$termValue = array();
if ($is_array) {
$termValue = $term;
$termValue['taxonomy'] = $key;
} else {
$termValue = array(
'name' => $term['name'],
'slug' => $slug,
'taxonomy' => $key
);
}
$term_id = $this->newTerm($termValue);
}
$content['terms'][$key][] = new XML_RPC_Value($term_id, 'int');
}
$content['terms'][$key] = new XML_RPC_Value($content['terms'][$key], 'struct');
}
$content['terms'] = new XML_RPC_Value($content['terms'], 'struct');
}
// enclosure
if ( $enclosure = $data['enclosure'] ) {
$content['enclosure'] = new XML_RPC_Value(
array(
'url' => new XML_RPC_Value($enclosure['url'], 'string'),
'length' => new XML_RPC_Value($enclosure['length'], 'int'),
'type' => new XML_RPC_Value($enclosure['type'], 'string')
),
'struct'
);
}
$content = new XML_RPC_Value($content, 'struct');
$publish = new XML_RPC_Value(1, 'boolean');
$message = new XML_RPC_Message(
'wp.newPost',
array($this->blog_id, $this->user, $this->password, $content, $publish)
);
return $this->sendXMLRPC($message);
}
/**
* Term一覧取得
*
* @param string $taxonomy タクソノミー
* @param struct $filter Term ID
* @return struct
*/
public function getTerms($taxonomy = null, array $filter = array()) {
$data = array(
$this->blog_id,
$this->user,
$this->password
);
if ( $taxonomy === null ) {
return array(
'error' => 'タクソノミーを指定してください。'
);
}
$data[3] = new XML_RPC_Value($taxonomy, 'string');
if ( count($filter) ) {
$filterData = array();
if ( $number = $filter['number'] ) {
$filterData['number'] = new XML_RPC_Value($number, 'int');
}
if ( $offset = $filter['offset'] ) {
$filterData['offset'] = new XML_RPC_Value($offset, 'int');
}
if ( $orderby = $filter['orderby'] ) {
$filterData['orderby'] = new XML_RPC_Value($orderby, 'string');
}
if ( $order = $filter['order'] ) {
$filterData['order'] = new XML_RPC_Value($order, 'string');
}
if ( $hide_empty = $filter['hide_empty'] ) {
$filterData['hide_empty'] = new XML_RPC_Value($hide_empty, 'bool');
}
if ( $search = $filter['search'] ) {
$filterData['search'] = new XML_RPC_Value($search, 'string');
}
$data[4] = new XML_RPC_Value($filterData, 'struct');
}
$message = new XML_RPC_Message(
'wp.getTerms', $data
);
return $this->sendXMLRPC($message);
}
/**
* Term作成
*
* @param struct $content Termデータ
* @return struct
*/
public function newTerm(array $content) {
$data = array(
$this->blog_id,
$this->user,
$this->password
);
$contentData = array(
'name' => new XML_RPC_Value($content['name'], 'string'),
'taxonomy' => new XML_RPC_Value($content['taxonomy'], 'string')
);
if ( $slug = $content['slug'] ) {
$contentData['slug'] = new XML_RPC_Value($slug, 'string');
}
if ( $description = $content['description'] ) {
$contentData['description'] = new XML_RPC_Value($description, 'string');
}
if ( $parent = $content['parent'] ) {
$contentData['parent'] = new XML_RPC_Value($parent, 'int');
}
$data[3] = new XML_RPC_Value($contentData, 'struct');
$message = new XML_RPC_Message(
'wp.newTerm', $data
);
return $this->sendXMLRPC($message);
}
/**
* ファイルアップロード
*
* @param string $path ファイルパス
* @param string $name ファイル名
* @return struct
*/
public function uploadFile($path, $name = null) {
$data = file_get_contents($path);
$info = new FInfo(FILEINFO_MIME_TYPE);
$mimeType = $info->buffer($data);
$fileName = $name ? $name : basename($path);
$file = new XML_RPC_Value(
array(
'type' => new XML_RPC_Value($mimeType, 'string'),
'bits' => new XML_RPC_Value($data, 'base64'),
'name' => new XML_RPC_Value($fileName, 'string')
),
'struct'
);
$message = new XML_RPC_Message(
'wp.uploadFile',
array($this->blog_id, $this->user, $this->password, $file)
);
return $this->sendXMLRPC($message);
}
/**
* ハッシュ生成
*
* @param struct $terms Term一覧
* @param string $key ファイルパス
* @return struct
*/
protected function createHash(array $terms, $key) {
$hash = array();
foreach($terms as $term) {
$hash[ mb_strtolower($term[$key]) ] = $term;
}
return $hash;
}
}
?>