-
Notifications
You must be signed in to change notification settings - Fork 1
/
OrgWpApi.php
executable file
·228 lines (187 loc) · 7.39 KB
/
OrgWpApi.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
<?php
/**
* Created by PhpStorm.
* User: Nabeel
* Date: 2015-09-11
* Time: 9:26 AM
*/
namespace ShortCirquit\WordPressApi;
class OrgWpApi extends BaseWpApi
{
public $blogUrl;
public $consumerKey;
public $consumerSecret;
public $token;
public $tokenSecret;
protected $curlOptions = [
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_USERAGENT => 'LinkoScope WP-API OAuth 1.0 Client',
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false,
];
private $requestUrl = '/oauth1/request';
private $authorizeUrl = '/oauth1/authorize';
private $accessUrl = '/oauth1/access';
private $postUrl = '/wp-json/wp/v2/posts';
private $userUrl = '/wp-json/wp/v2/users';
private $typeUrl = '/wp-json/wp/v2/types';
private $selfUrl = '/wp-json/wp/v2/users/me';
private $commentsUrl = '/wp-json/wp/v2/comments';
private $customBase = '/wp-json/wp/v2/';
public function __construct(array $config = [])
{
$vars = ['blogUrl', 'consumerKey', 'consumerSecret', 'token', 'tokenSecret'];
foreach ($config as $k => $v)
{
if (in_array($k, $vars))
{
$this->$k = $v;
}
}
$this->baseUrl = $this->blogUrl;
}
public function getAuthorizeUrl($returnUrl)
{
$response = $this->get($this->requestUrl, ['oauth_callback' => $returnUrl]);
$params = ['oauth_callback' => $returnUrl, 'oauth_token' => $response['oauth_token'],];
return $this->baseUrl . $this->authorizeUrl . '?' . http_build_query($params, '', '&', PHP_QUERY_RFC3986);
}
public function getAccessToken($token, $verifier)
{
$defaultParams = [
'oauth_consumer_key' => $this->consumerKey,
'oauth_token' => $token,
'oauth_verifier' => $verifier,
];
return $this->get($this->accessUrl, $defaultParams);
}
public function listCustom($type, $params = []) { return $this->get($this->customBase . $type, $params); }
public function getCustom($type, $id, $params = [])
{
return $this->get(
$this->customBase . $type . "/$id", $params
);
}
public function addCustom($type, $data, $params = [])
{
return $this->post(
$this->customBase . $type, $params, $data
);
}
public function updateCustom($type, $id, $data, $params = [])
{
return $this->put(
$this->customBase . $type . "/$id", $params, $data
);
}
public function deleteCustom($type, $id, $params = [])
{
return $this->delete(
$this->customBase . $type . "/$id", $params
);
}
public function listPosts($params = []) { return $this->get($this->postUrl, $params); }
public function getPost($id, $params = []) { return $this->get($this->postUrl . "/$id", $params); }
public function addPost($data, $params = []) { return $this->post($this->postUrl, $params, $data); }
public function updatePost($id, $data, $params = []) { return $this->put($this->postUrl . "/$id", $params, $data); }
public function deletePost($id, $params = []) { return $this->delete($this->postUrl . "/$id", $params); }
public function listComments($postId, $params = [])
{
return $this->get(
$this->commentsUrl, ['post' => $postId] + $params
);
}
public function getComment($id, $params = []) { return $this->get($this->commentsUrl . "/$id", $params); }
public function addComment($data, $params = []) { return $this->post($this->commentsUrl, $params, $data); }
public function updateComment($id, $data, $params = [])
{
return $this->put(
$this->commentsUrl . "/$id", $params, $data
);
}
public function deleteComment($id, $params = []) { return $this->delete($this->commentsUrl . "/$id", $params); }
public function getSelf($params = []) { return $this->get($this->selfUrl, $params + ['_envelope' => 1]); }
public function getUser($id, $params = []) { return $this->get($this->userUrl . "/$id", $params); }
public function getUsers($params = []) { return $this->get($this->userUrl, $params); }
public function listTypes() { return $this->get($this->typeUrl); }
protected function requestFilter(ApiRequest $req)
{
if ($this->token != null || isset($req->params['oauth_token']) || isset($req->params['oauth_callback']))
{
$req->params = $this->signRequest($req->method, $req->url, $req->params);
$req->headers[] = $this->composeAuthorizationHeader($req->params);
}
$req->headers[] = 'Content-type: application/json';
if ($req->body != null)
{
$req->body = json_encode($req->body);
}
return $req;
}
private function composeAuthorizationHeader(array $params)
{
$header = 'Authorization: OAuth';
$headerParams = [];
foreach ($params as $key => $value)
{
if (substr_compare($key, 'oauth', 0, 5))
{
continue;
}
$headerParams[] = rawurlencode($key) . '="' . rawurlencode($value) . '"';
}
if ( ! empty($headerParams))
{
$header .= ' ' . implode(', ', $headerParams);
}
return $header;
}
private function signRequest($method, $url, array $params)
{
$params = array_merge(
$params, [
'oauth_version' => '1.0',
'oauth_nonce' => md5(microtime() . mt_rand()),
'oauth_timestamp' => time(),
'oauth_consumer_key' => $this->consumerKey,
]
);
if ($this->token != null)
{
$params['oauth_token'] = $this->token;
}
$params['oauth_signature_method'] = 'HMAC-SHA1';
$signatureBaseString = $this->composeSignatureBaseString($method, $this->baseUrl . $url, $params);
$signatureKey = $this->composeSignatureKey();
$params['oauth_signature'] = base64_encode(hash_hmac('sha1', $signatureBaseString, $signatureKey, true));
return $params;
}
private function composeSignatureBaseString($method, $url, array $params)
{
unset($params['oauth_signature']);
uksort(
$params, 'strcmp'
); // Parameters are sorted by name, using lexicographical byte value ordering. Ref: Spec: 9.1.1
$parts = [
strtoupper($method),
$url,
http_build_query($params, '', '&', PHP_QUERY_RFC3986),
];
$parts[2] = str_replace('%5B', '[', $parts[2]);
$parts[2] = str_replace('%5D', ']', $parts[2]);
$parts = array_map('rawurlencode', $parts);
return implode('&', $parts);
}
private function composeSignatureKey()
{
$signatureKeyParts = [
$this->consumerSecret,
];
$signatureKeyParts[] = $this->tokenSecret ?: '';
$signatureKeyParts = array_map('rawurlencode', $signatureKeyParts);
return implode('&', $signatureKeyParts);
}
}