forked from ShortCirquit/WordPressApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComWpApi.php
executable file
·204 lines (154 loc) · 6.55 KB
/
ComWpApi.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
<?php
/**
* Created by PhpStorm.
* User: Nabeel
* Date: 2015-09-11
* Time: 9:26 AM
*/
namespace ShortCirquit\WordPressApi;
class ComWpApi extends BaseWpApi
{
public $clientId;
public $clientSecret;
public $redirectUrl;
public $blogId;
public $blogUrl;
public $token;
public $curlOptions = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => 'LinkoScope WordPress.com Client',
CURLOPT_SSL_VERIFYPEER => false,
];
private $requestUrl = '/oauth2/token';
private $authorizeUrl = '/oauth2/authorize';
private $wpBase = 'https://public-api.wordpress.com';
private $selfUrl = '/rest/v1.1/me';
private $listFormat = '/rest/v1.1/sites/{blogId}/{type}';
private $getFormat = '/rest/v1.1/sites/{blogId}/{type}/{id}';
private $addFormat = '/rest/v1.1/sites/{blogId}/{type}/new';
private $updateFormat = '/rest/v1.1/sites/{blogId}/{type}/{id}';
private $deleteFormat = '/rest/v1.1/sites/{blogId}/{type}/{id}/delete';
private $likeFormat = '/rest/v1.1/sites/{blogId}/{type}/{id}/likes/new';
private $unlikeFormat = '/rest/v1.1/sites/{blogId}/{type}/{id}/likes/mine/delete';
private $repliesFormat = '/rest/v1.1/sites/{blogId}/{type}/{id}/replies/';
private $newReplyFormat = '/rest/v1.1/sites/{blogId}/{type}/{id}/replies/new';
public function __construct(array $config = [])
{
$vars = ['clientId', 'clientSecret', 'redirectUrl', 'blogId', 'blogUrl', 'token'];
foreach ($config as $k => $v)
{
if (in_array($k, $vars))
{
$this->$k = $v;
}
}
$this->baseUrl = $this->wpBase;
}
public function getAuthorizeUrl()
{
$url = $this->wpBase . $this->authorizeUrl;
$url .= "?client_id=$this->clientId&redirect_uri=$this->redirectUrl&response_type=code";
$url .= $this->blogId !== null ? "&blog=$this->blogId" : '';
return $url;
}
public function getToken($code)
{
return $this->post(
$this->requestUrl, [], [
'client_id' => $this->clientId,
'redirect_uri' => $this->redirectUrl,
'client_secret' => $this->clientSecret,
'code' => $code,
'grant_type' => 'authorization_code',
]
);
}
public function listPosts($params = []) { return $this->listItems('posts', $params); }
public function getPost($id, $params = []) { return $this->getItem('posts', $id, $params); }
public function addPost($data, $params = []) { return $this->newItem('posts', $data, $params); }
public function updatePost($id, $data, $params = []) { return $this->updateItem('posts', $id, $data, $params); }
public function deletePost($id, $params = []) { return $this->deleteItem('posts', $id, $params); }
public function likePost($id, $params = []) { return $this->likeItem('posts', $id, $params); }
public function unlikePost($id, $params = []) { return $this->unlikeItem('posts', $id, $params); }
public function listComments($postId, $params = []) { return $this->getReplies($postId, $params); }
public function getComment($id, $params = []) { return $this->getItem('comments', $id, $params); }
public function addComment($id, $data, $params = []) { return $this->addReply($id, $data, $params); }
public function updateComment($id, $data, $params = [])
{
return $this->updateItem(
'comments', $id, $data, $params
);
}
public function deleteComment($id, $params = []) { return $this->deleteItem('comments', $id, $params); }
public function likeComment($id, $params = []) { return $this->likeItem('comments', $id, $params); }
public function unlikeComment($id, $params = []) { return $this->unlikeItem('comments', $id, $params); }
public function getSelf() { return $this->get($this->selfUrl); }
public function getUsers($params = []) { return $this->listItems('users', $params); }
public function getUser($id, $params = []) { return $this->getItem('users', $id, $params); }
public function listTypes() { return []; }
protected function requestFilter(ApiRequest $request)
{
$request->headers[] = "Authorization: Bearer $this->token";
if ($request->body != null)
{
if (preg_match('/token$/', $request->url) == 0)
{
$request->headers[] = 'Content-type: application/json';
$request->body = json_encode($request->body);
}
}
return $request;
}
private function listItems($type, $params = [])
{
$url = $this->formatUrl($this->listFormat, $type);
return $this->get($url, $params);
}
private function getItem($type, $id, $params = [])
{
$url = $this->formatUrl($this->getFormat, $type, $id);
return $this->get($url, $params);
}
private function newItem($type, $data, $params = [])
{
$url = $this->formatUrl($this->addFormat, $type);
return $this->post($url, $params, $data);
}
private function getReplies($id, $params = [])
{
$url = $this->formatUrl($this->repliesFormat, 'posts', $id);
return $this->get($url, $params);
}
private function addReply($id, $data, $params = [])
{
$url = $this->formatUrl($this->newReplyFormat, 'posts', $id);
return $this->post($url, $params, $data);
}
private function updateItem($type, $id, $data, $params = [])
{
$url = $this->formatUrl($this->updateFormat, $type, $id);
return $this->post($url, $params, $data);
}
private function deleteItem($type, $id, $params = [])
{
$url = $this->formatUrl($this->deleteFormat, $type, $id);
return $this->post($url, $params);
}
private function likeItem($type, $id, $params = [])
{
$url = $this->formatUrl($this->likeFormat, $type, $id);
return $this->post($url, $params);
}
private function unlikeItem($type, $id, $params = [])
{
$url = $this->formatUrl($this->unlikeFormat, $type, $id);
return $this->post($url, $params);
}
private function formatUrl($fmt, $type = '', $id = '')
{
$fmt = str_replace("{blogId}", $this->blogId, $fmt);
$fmt = str_replace("{type}", $type, $fmt);
$fmt = str_replace("{id}", $id, $fmt);
return $fmt;
}
}