1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Codexshaper\WooCommerce\Traits; |
4
|
|
|
|
5
|
|
|
trait WooCommerceTrait |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* GET method. |
9
|
|
|
* Retrieve data. |
10
|
|
|
* |
11
|
|
|
* @param string $endpoint API endpoint. |
12
|
|
|
* @param array $options |
13
|
|
|
* |
14
|
|
|
* @return array |
15
|
|
|
*/ |
16
|
|
|
public function all($endpoint = '', $options = []) |
17
|
|
|
{ |
18
|
|
|
self::__construct(); |
19
|
|
|
|
20
|
|
|
return $this->client->get($endpoint, $options); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* GET method. |
25
|
|
|
* Retrieve Single data. |
26
|
|
|
* |
27
|
|
|
* @param string $endpoint API endpoint. |
28
|
|
|
* @param array $options |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
|
public function find($endpoint = '', $options = []) |
33
|
|
|
{ |
34
|
|
|
self::__construct(); |
35
|
|
|
|
36
|
|
|
return $this->client->get($endpoint, $options); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* POST method. |
41
|
|
|
* Insert data. |
42
|
|
|
* |
43
|
|
|
* @param string $endpoint API endpoint. |
44
|
|
|
* @param array $data |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function create($endpoint, $data) |
49
|
|
|
{ |
50
|
|
|
self::__construct(); |
51
|
|
|
|
52
|
|
|
return $this->client->post($endpoint, $data); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* PUT method. |
57
|
|
|
* Update data. |
58
|
|
|
* |
59
|
|
|
* @param string $endpoint API endpoint. |
60
|
|
|
* @param array $data |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function update($endpoint, $data) |
65
|
|
|
{ |
66
|
|
|
self::__construct(); |
67
|
|
|
|
68
|
|
|
return $this->client->put($endpoint, $data); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* DELETE method. |
73
|
|
|
* Remove data. |
74
|
|
|
* |
75
|
|
|
* @param string $endpoint API endpoint. |
76
|
|
|
* @param array $options |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function delete($endpoint, $options = []) |
81
|
|
|
{ |
82
|
|
|
self::__construct(); |
83
|
|
|
|
84
|
|
|
return $this->client->delete($endpoint, $options); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return the last request header. |
89
|
|
|
* |
90
|
|
|
* @return \Automattic\WooCommerce\HttpClient\Request |
91
|
|
|
*/ |
92
|
|
|
public function getRequest() |
93
|
|
|
{ |
94
|
|
|
self::__construct(); |
95
|
|
|
|
96
|
|
|
return $this->client->http->getRequest(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return the http response headers from last request. |
101
|
|
|
* |
102
|
|
|
* @return \Automattic\WooCommerce\HttpClient\Response |
103
|
|
|
*/ |
104
|
|
|
public function getResponse() |
105
|
|
|
{ |
106
|
|
|
self::__construct(); |
107
|
|
|
|
108
|
|
|
return $this->client->http->getResponse(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Count the total results and return it. |
113
|
|
|
* |
114
|
|
|
* @return int |
115
|
|
|
*/ |
116
|
|
|
public function countResults() |
117
|
|
|
{ |
118
|
|
|
return (int) $this->getResponse()->getHeaders()['X-WP-Total']; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Count the total pages and return. |
123
|
|
|
* |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
public function countPages() |
127
|
|
|
{ |
128
|
|
|
return (int) $this->getResponse()->getHeaders()['X-WP-TotalPages']; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Return the current page number. |
133
|
|
|
* |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
|
|
public function current() |
137
|
|
|
{ |
138
|
|
|
return !empty($this->getRequest()->getParameters()['page']) ? $this->getRequest()->getParameters()['page'] : 1; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return the previous page number. |
143
|
|
|
* |
144
|
|
|
* @return int|null |
145
|
|
|
*/ |
146
|
|
|
public function previous() |
147
|
|
|
{ |
148
|
|
|
$currentPage = $this->current(); |
149
|
|
|
|
150
|
|
|
return ($currentPage-- > 1) ? $currentPage : null; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Return the next page number. |
155
|
|
|
* |
156
|
|
|
* @return int|null |
157
|
|
|
*/ |
158
|
|
|
public function next() |
159
|
|
|
{ |
160
|
|
|
$currentPage = $this->current(); |
161
|
|
|
|
162
|
|
|
return ($currentPage++ < $this->countPages()) ? $currentPage : null; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: