1 | <?php |
||
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() { |
||
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() { |
||
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 ) { |
||
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() { |
||
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() { |
||
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() { |
||
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 ) { |
||
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() { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
355 | |||
356 | } |
||
357 |