-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwpclass.php
90 lines (76 loc) · 2.82 KB
/
wpclass.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
<?php
class WordPress {
private $username;
private $password;
private $endpoint;
private $blogid;
private $ch;
public function __construct($username, $password, $endpoint, $blogid = 1) {
$this->username = $username;
$this->password = $password;
$this->endpoint = $endpoint;
$this->blogid = $blogid;
$this->ch = curl_init($this->endpoint);
curl_setopt($this->ch, CURLOPT_HEADER, false);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
}
private function execute($request) {
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request);
//Disable displaying responce
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($this->ch);
$result = xmlrpc_decode($response);
if (is_array($result) && xmlrpc_is_fault($result)) {
throw new Exception($result['faultString'], $result['faultCode']);
}
else {
return $result;
}
}
public function publish_post($title, $content, array $tags, array $categories, $status = 'publish', $date = Null, $post_thumbnail = Null) {
// Set datetime for post
if ($date == Null) {
$post_date = date("Ymd\TH:i:s", time());
}
else {
$post_date = $date;
}
xmlrpc_set_type($post_date, 'datetime');
$params = array(
$this->blogid,
$this->username,
$this->password,
array(
'post_type' => 'post',
'post_status' => $status,
'post_title' => $title,
'post_content' => $content,
'post_date' => $post_date,
'terms_names' => array('category' => $categories, 'post_tag' => $tags),
'post_thumbnail' => $post_thumbnail,
)
);
$request = xmlrpc_encode_request('wp.newPost', $params, array('encoding'=>'UTF-8','escaping'=>'markup'));
$response = $this->execute($request);
return $response;
}
public function upload_image($image) {
//Send binary data and label as Base64
$bits = file_get_contents($image["fullpath"]);
xmlrpc_set_type($bits, 'base64');
$params = array(
$this->blogid,
$this->username,
$this->password,
array(
'name' => $image["filename"],
'type' => $image["type"],
'bits' => $bits,
'overwrite' => true,
),
);
$request = xmlrpc_encode_request('wp.uploadFile', $params, array('encoding'=>'UTF-8','escaping'=>'markup'));
$response = $this->execute($request);
return $response;
}
}