1
|
|
|
<?php namespace Pixelpeter\Woocommerce; |
2
|
|
|
|
3
|
|
|
use Automattic\WooCommerce\Client; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @property mixed config |
7
|
|
|
*/ |
8
|
|
|
class WoocommerceClient |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \Automattic\WooCommerce\Client |
12
|
|
|
*/ |
13
|
|
|
protected $client; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
protected $usingLowerCaseHeaders = false; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
*/ |
22
|
|
|
public function __construct(Client $client) |
23
|
|
|
{ |
24
|
|
|
$this->client = $client; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* POST method. |
29
|
|
|
* |
30
|
|
|
* @param string $endpoint API endpoint. |
31
|
|
|
* @param array $data Request data. |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function post($endpoint, $data) |
36
|
|
|
{ |
37
|
|
|
return $this->client->post($endpoint, $data); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* PUT method. |
42
|
|
|
* |
43
|
|
|
* @param string $endpoint API endpoint. |
44
|
|
|
* @param array $data Request data. |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function put($endpoint, $data) |
49
|
|
|
{ |
50
|
|
|
return $this->client->put($endpoint, $data); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* GET method. |
55
|
|
|
* |
56
|
|
|
* @param string $endpoint API endpoint. |
57
|
|
|
* @param array $parameters Request parameters. |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function get($endpoint, $parameters = []) |
62
|
|
|
{ |
63
|
|
|
return $this->client->get($endpoint, $parameters); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* DELETE method. |
68
|
|
|
* |
69
|
|
|
* @param string $endpoint API endpoint. |
70
|
|
|
* @param array $parameters Request parameters. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function delete($endpoint, $parameters = []) |
75
|
|
|
{ |
76
|
|
|
return $this->client->delete($endpoint, $parameters); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the http request header from the last request |
81
|
|
|
* |
82
|
|
|
* @return \Automattic\WooCommerce\HttpClient\Request |
83
|
|
|
*/ |
84
|
|
|
public function getRequest() |
85
|
|
|
{ |
86
|
|
|
return $this->client->http->getRequest(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the http response headers from the last request |
91
|
|
|
* |
92
|
|
|
* @return \Automattic\WooCommerce\HttpClient\Response |
93
|
|
|
*/ |
94
|
|
|
public function getResponse() |
95
|
|
|
{ |
96
|
|
|
return $this->client->http->getResponse(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return the first page number of the result |
101
|
|
|
* |
102
|
|
|
* @return int |
103
|
|
|
*/ |
104
|
|
|
public function firstPage() |
105
|
|
|
{ |
106
|
|
|
return 1; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return the last page number of the result |
111
|
|
|
* |
112
|
|
|
* @return int |
113
|
|
|
*/ |
114
|
|
|
public function lastPage() |
115
|
|
|
{ |
116
|
|
|
return $this->totalPages(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Return the current page number of the result |
121
|
|
|
* |
122
|
|
|
* @return int |
123
|
|
|
*/ |
124
|
|
|
public function currentPage() |
125
|
|
|
{ |
126
|
|
|
return !empty($this->getRequest()->getParameters()['page']) ? $this->getRequest()->getParameters()['page'] : 1; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Return the total number of results |
131
|
|
|
* |
132
|
|
|
* @return int |
133
|
|
|
*/ |
134
|
|
|
public function totalResults() |
135
|
|
|
{ |
136
|
|
|
return (int)$this->getResponse()->getHeaders()[$this->getHeaderWithCase('X-WP-Total')]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Return the total number of pages for this result |
141
|
|
|
* |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
|
|
public function totalPages() |
145
|
|
|
{ |
146
|
|
|
return (int)$this->getResponse()->getHeaders()[$this->getHeaderWithCase('X-WP-TotalPages')]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Return the previous page number for the current page |
151
|
|
|
* Will be null if there's no previous page |
152
|
|
|
* |
153
|
|
|
* @return int|null |
154
|
|
|
*/ |
155
|
|
|
public function previousPage() |
156
|
|
|
{ |
157
|
|
|
$previous_page = $this->currentPage() - 1; |
158
|
|
|
if ($previous_page < 1) { |
159
|
|
|
$previous_page = null; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $previous_page; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Return the next page number for the current page |
167
|
|
|
* Will be null if there's no next page |
168
|
|
|
* |
169
|
|
|
* @return int|null |
170
|
|
|
*/ |
171
|
|
|
public function nextPage() |
172
|
|
|
{ |
173
|
|
|
$next_page = $this->currentPage() + 1; |
174
|
|
|
if ($next_page > $this->totalPages()) { |
175
|
|
|
$next_page = null; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $next_page; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns true if there's a next page |
183
|
|
|
* |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
public function hasNextPage() |
187
|
|
|
{ |
188
|
|
|
return (bool)$this->nextPage(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Returns true if there's a previous page |
193
|
|
|
* |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
|
|
public function hasPreviousPage() |
197
|
|
|
{ |
198
|
|
|
return (bool)$this->previousPage(); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Returns true if there's no next page |
203
|
|
|
* |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
|
|
public function hasNotNextPage() |
207
|
|
|
{ |
208
|
|
|
return (bool)!$this->nextPage(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns true if there's no previous page |
213
|
|
|
* |
214
|
|
|
* @return bool |
215
|
|
|
*/ |
216
|
|
|
public function hasNotPreviousPage() |
217
|
|
|
{ |
218
|
|
|
return (bool)!$this->previousPage(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Set if headers should be indexed using lower case |
223
|
|
|
*/ |
224
|
|
|
public function useLowerCaseHeaders() |
225
|
|
|
{ |
226
|
|
|
$this->usingLowerCaseHeaders = true; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Set if headers should be case sensitive |
231
|
|
|
*/ |
232
|
|
|
public function useCaseSensitiveHeaders() |
233
|
|
|
{ |
234
|
|
|
$this->usingLowerCaseHeaders = false; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Convert header to correct case |
239
|
|
|
* |
240
|
|
|
* @param $header |
241
|
|
|
* |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
|
|
public function getHeaderWithCase($header) { |
245
|
|
|
return $this->usingLowerCaseHeaders ? strtolower($header) : $header; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|