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