|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The WP eCommerce Base Query Class |
|
4
|
|
|
* |
|
5
|
|
|
* @package wp-e-commerce |
|
6
|
|
|
* @since 4.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
abstract class WPSC_Query_Base { |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* An array of arrays of cache keys. Allows versioning the cached values, |
|
13
|
|
|
* and busting cache for a group if needed (by incrementing the version). |
|
14
|
|
|
* |
|
15
|
|
|
* Should be structured like: |
|
16
|
|
|
* |
|
17
|
|
|
* protected $group_ids = array( |
|
18
|
|
|
* 'group_id' => array( |
|
19
|
|
|
* 'group' => 'full_group_id_key', |
|
20
|
|
|
* 'version' => 1, |
|
21
|
|
|
* ), |
|
22
|
|
|
* 'group2_id' => array( |
|
23
|
|
|
* 'group' => 'full_group2_id_key', |
|
24
|
|
|
* 'version' => 1, |
|
25
|
|
|
* ), |
|
26
|
|
|
* ); |
|
27
|
|
|
* |
|
28
|
|
|
* And fetched like: |
|
29
|
|
|
* |
|
30
|
|
|
* $value = $this->cache_get( $cache_key, 'group_id' ); |
|
31
|
|
|
* |
|
32
|
|
|
* And set like: |
|
33
|
|
|
* |
|
34
|
|
|
* $this->cache_set( $cache_key, $value, 'group_id' ); |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $group_ids = array(); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Contains the values fetched from the DB |
|
42
|
|
|
* |
|
43
|
|
|
* @access protected |
|
44
|
|
|
* @since 4.0 |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $data = array(); |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Data that is not directly stored inside the DB but is inferred. Optional. |
|
52
|
|
|
* |
|
53
|
|
|
* @access protected |
|
54
|
|
|
* @since 4.0 |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $meta_data = array(); |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* True if the DB row is fetched into the $data array. |
|
60
|
|
|
* |
|
61
|
|
|
* @access protected |
|
62
|
|
|
* @since 4.0 |
|
63
|
|
|
* |
|
64
|
|
|
* @var boolean |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $fetched = false; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* True if the row exists in DB |
|
70
|
|
|
* |
|
71
|
|
|
* @access protected |
|
72
|
|
|
* @since 4.0 |
|
73
|
|
|
* |
|
74
|
|
|
* @var boolean |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $exists = false; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Fetches the actual $data array. |
|
80
|
|
|
* Should set $this->fetched to true, and $this->exists if row is found. |
|
81
|
|
|
* Should return $this; |
|
82
|
|
|
* |
|
83
|
|
|
* @access protected |
|
84
|
|
|
* @since 4.0 |
|
85
|
|
|
* |
|
86
|
|
|
* @return WPSC_Query_Base |
|
87
|
|
|
*/ |
|
88
|
|
|
abstract protected function fetch(); |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Whether the DB row for this purchase log exists |
|
92
|
|
|
* |
|
93
|
|
|
* @access public |
|
94
|
|
|
* @since 4.0 |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool True if it exists. Otherwise false. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function exists() { |
|
99
|
|
|
$this->fetch(); |
|
100
|
|
|
return $this->exists; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Resets properties so any subsequent requests will be refreshed. |
|
105
|
|
|
* |
|
106
|
|
|
* @since 4.0 |
|
107
|
|
|
* |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function reset() { |
|
111
|
|
|
$this->data = array(); |
|
112
|
|
|
$this->fetched = false; |
|
113
|
|
|
$this->exists = false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns the value of the specified property of the $data array if it exists. |
|
118
|
|
|
* |
|
119
|
|
|
* @access public |
|
120
|
|
|
* @since 4.0 |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $key Name of the property (column) |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
*/ |
|
125
|
|
|
public function get( $key ) { |
|
126
|
|
|
|
|
127
|
|
|
// lazy load the row if it's not fetched from the database yet |
|
128
|
|
|
if ( empty( $this->data ) || ! array_key_exists( $key, $this->data ) ) { |
|
129
|
|
|
$this->fetch(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ( isset( $this->data[ $key ] ) ) { |
|
133
|
|
|
$value = $this->data[ $key ]; |
|
134
|
|
|
} else if ( isset( $this->meta_data[ $key ] ) ) { |
|
135
|
|
|
$value = $this->meta_data[ $key ]; |
|
136
|
|
|
} else { |
|
137
|
|
|
$value = null; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $this->prepare_get( $value, $key ); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Prepares the return value for get() (apply_filters, etc). |
|
145
|
|
|
* |
|
146
|
|
|
* @access protected |
|
147
|
|
|
* @since 4.0 |
|
148
|
|
|
* |
|
149
|
|
|
* @param mixed $value Value fetched |
|
150
|
|
|
* @param string $key Key for $data. |
|
151
|
|
|
* |
|
152
|
|
|
* @return mixed |
|
153
|
|
|
*/ |
|
154
|
|
|
abstract protected function prepare_get( $value, $key ); |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Returns the entire $data array. |
|
158
|
|
|
* |
|
159
|
|
|
* @access public |
|
160
|
|
|
* @since 4.0 |
|
161
|
|
|
* |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
|
|
public function get_data() { |
|
165
|
|
|
if ( empty( $this->data ) ) { |
|
166
|
|
|
$this->fetch(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $this->prepare_get_data(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Prepares the return value for get_data() (apply_filters, etc). |
|
174
|
|
|
* |
|
175
|
|
|
* @access protected |
|
176
|
|
|
* @since 4.0 |
|
177
|
|
|
* |
|
178
|
|
|
* @return mixed |
|
179
|
|
|
*/ |
|
180
|
|
|
abstract protected function prepare_get_data(); |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Returns the entire $meta_data array. |
|
184
|
|
|
* |
|
185
|
|
|
* @access public |
|
186
|
|
|
* @since 4.0 |
|
187
|
|
|
* |
|
188
|
|
|
* @return array |
|
189
|
|
|
*/ |
|
190
|
|
|
public function get_meta() { |
|
191
|
|
|
|
|
192
|
|
|
// lazy load the row if it's not fetched from the database yet |
|
193
|
|
|
if ( empty( $this->data ) && empty( $this->meta_data ) ) { |
|
194
|
|
|
$this->fetch(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $this->prepare_get_meta(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Prepares the return value for get_meta() (apply_filters, etc). |
|
202
|
|
|
* |
|
203
|
|
|
* @access protected |
|
204
|
|
|
* @since 4.0 |
|
205
|
|
|
* |
|
206
|
|
|
* @return mixed |
|
207
|
|
|
*/ |
|
208
|
|
|
protected function prepare_get_meta() { |
|
209
|
|
|
return $this->meta_data; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Sets a property to a certain value. This function accepts a key and a value |
|
214
|
|
|
* as arguments, or an associative array containing key value pairs. |
|
215
|
|
|
* |
|
216
|
|
|
* @access public |
|
217
|
|
|
* @since 4.0 |
|
218
|
|
|
* |
|
219
|
|
|
* @param mixed $key Name of the property (column), or an array containing |
|
220
|
|
|
* key value pairs |
|
221
|
|
|
* @param string|int|null $value Optional. Defaults to null. In case $key is a string, |
|
222
|
|
|
* this should be specified. |
|
223
|
|
|
* @return WPSC_Query_Base The current object (for method chaining) |
|
224
|
|
|
*/ |
|
225
|
|
|
abstract public function set( $key, $value = null ); |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Saves the object back to the database. |
|
229
|
|
|
* |
|
230
|
|
|
* @access public |
|
231
|
|
|
* @since 4.0 |
|
232
|
|
|
* |
|
233
|
|
|
* @return mixed |
|
234
|
|
|
*/ |
|
235
|
|
|
abstract public function save(); |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Sets a meta property to a certain value. This function should accept a key |
|
239
|
|
|
* and a value as arguments, or an associative array containing key value pairs. |
|
240
|
|
|
* |
|
241
|
|
|
* @access public |
|
242
|
|
|
* @since 4.0 |
|
243
|
|
|
* |
|
244
|
|
|
* @param mixed $key Name of the property (column), or an array containing |
|
245
|
|
|
* key value pairs |
|
246
|
|
|
* @param string|int|null $value Optional. Defaults to null. In case $key is a string, |
|
247
|
|
|
* this should be specified. |
|
248
|
|
|
* @return WPSC_Query_Base The current object (for method chaining) |
|
249
|
|
|
*/ |
|
250
|
|
|
public function set_meta( $key, $value = null ) { |
|
251
|
|
|
if ( is_array( $key ) ) { |
|
252
|
|
|
foreach ( $key as $index => $value) { |
|
253
|
|
|
$this->set_meta( $index, $value ); |
|
254
|
|
|
} |
|
255
|
|
|
return; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
// lazy load the row if it's not fetched from the database yet |
|
259
|
|
|
if ( empty( $this->data ) && empty( $this->meta_data ) ) { |
|
260
|
|
|
$this->fetch(); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
$this->meta_data[ $key ] = $value; |
|
264
|
|
|
|
|
265
|
|
|
return $this; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Saves the meta data back to the database. |
|
270
|
|
|
* |
|
271
|
|
|
* @access public |
|
272
|
|
|
* @since 4.0 |
|
273
|
|
|
* |
|
274
|
|
|
* @return WPSC_Query_Base The current object (for method chaining) |
|
275
|
|
|
*/ |
|
276
|
|
|
public function save_meta() { |
|
277
|
|
|
return $this; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Wrapper for wp_cache_get. |
|
282
|
|
|
* |
|
283
|
|
|
* @access public |
|
284
|
|
|
* @since 4.0 |
|
285
|
|
|
* |
|
286
|
|
|
* @see wp_cache_get() |
|
287
|
|
|
* |
|
288
|
|
|
* @param int|string $key The key under which the cache contents are stored. |
|
289
|
|
|
* @param string $group_id The key for the group_ids array to compile the group |
|
290
|
|
|
* from version/key. |
|
291
|
|
|
* Default 0 (no expiration). |
|
292
|
|
|
* @return bool|mixed False on failure to retrieve contents or the cache |
|
293
|
|
|
* contents on success |
|
294
|
|
|
*/ |
|
295
|
|
|
public function cache_get( $key, $group_id ) { |
|
296
|
|
|
return wp_cache_get( $key, $this->get_group_id( $group_id ) ); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Wrapper for wp_cache_set. |
|
301
|
|
|
* |
|
302
|
|
|
* @access public |
|
303
|
|
|
* @since 4.0 |
|
304
|
|
|
* |
|
305
|
|
|
* @see wp_cache_set() |
|
306
|
|
|
* |
|
307
|
|
|
* @param int|string $key The cache key to use for retrieval later. |
|
308
|
|
|
* @param mixed $data The contents to store in the cache. |
|
309
|
|
|
* @param string $group_id The key for the group_ids array to compile the group |
|
310
|
|
|
* from version/key. |
|
311
|
|
|
* @param int $expire Optional. When to expire the cache contents, in seconds. |
|
312
|
|
|
* Default 0 (no expiration). |
|
313
|
|
|
* @return bool False on failure, true on success |
|
314
|
|
|
*/ |
|
315
|
|
|
public function cache_set( $key, $data, $group_id, $expire = 0 ) { |
|
316
|
|
|
return wp_cache_set( $key, $data, $this->get_group_id( $group_id ), $expire ); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Wrapper for wp_cache_delete. |
|
321
|
|
|
* |
|
322
|
|
|
* @access public |
|
323
|
|
|
* @since 4.0 |
|
324
|
|
|
* |
|
325
|
|
|
* @see wp_cache_delete() |
|
326
|
|
|
* |
|
327
|
|
|
* @param int|string $key What the contents in the cache are called. |
|
328
|
|
|
* @param string $group_id The key for the group_ids array to compile the group |
|
329
|
|
|
* from version/key. |
|
330
|
|
|
* @return bool True on successful removal, false on failure. |
|
331
|
|
|
*/ |
|
332
|
|
|
public function cache_delete( $key, $group_id ) { |
|
333
|
|
|
return wp_cache_delete( $key, $this->get_group_id( $group_id ) ); |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* Get the versioned group id from the $group_ids array. |
|
338
|
|
|
* |
|
339
|
|
|
* @since 4.0 |
|
340
|
|
|
* |
|
341
|
|
|
* @param string $group_id The key for the group_ids array to compile the group |
|
342
|
|
|
* from version/key. |
|
343
|
|
|
* |
|
344
|
|
|
* @return string The full group key. |
|
345
|
|
|
*/ |
|
346
|
|
|
public function get_group_id( $group_id ) { |
|
347
|
|
|
$group = $this->group_ids[ $group_id ]['group']; |
|
348
|
|
|
|
|
349
|
|
|
if ( isset( $this->group_ids[ $group_id ]['version'] ) && $this->group_ids[ $group_id ]['version'] ) { |
|
350
|
|
|
$group .= '_' . $this->group_ids[ $group_id ]['version']; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
return $group; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
} |
|
357
|
|
|
|