-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathmeta.functions.php
More file actions
executable file
·387 lines (317 loc) · 11.3 KB
/
meta.functions.php
File metadata and controls
executable file
·387 lines (317 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/**
* Some parts of this code were copied from functions.bb-meta.php in bbpress
*/
function wpsc_sanitize_meta_key( $key ) {
return preg_replace( '|[^a-z0-9_]|i', '', $key );
}
/**
* Get meta data from the database
*
* Gets and caches an object's meta using the WordPress Object Cache API
* and returns meta for a specific key.
*
* @internal
*
* @param integer $object_id Object ID.
* @param string $meta_key Meta key.
* @param string $object_type Object type.
* @return mixed Meta value.
*/
<<<<<<< HEAD
function wpsc_get_meta( $object_id, $meta_key, $object_type ) {
=======
function wpsc_get_meta( $object_id = 0, $meta_key, $object_type ) {
>>>>>>> parent of eb244fad (PHP 8 Compatibility)
global $wpdb;
$cache_object_id = $object_id = (int) $object_id;
$meta_key = wpsc_sanitize_meta_key( $meta_key );
$meta_tuple = compact( 'object_type', 'object_id', 'meta_key' );
$meta_tuple = apply_filters( 'wpsc_get_meta', $meta_tuple );
// Get cached meta
$meta_value = wp_cache_get( $cache_object_id, $meta_tuple['object_type'] );
// If not cached, get and cache all object meta
if ( $meta_value === false ) {
$meta_values = wpsc_update_meta_cache( $meta_tuple['object_type'], $meta_tuple['object_id'] );
$meta_value = $meta_values[ $meta_tuple['object_id'] ];
}
if ( isset( $meta_value[ $meta_tuple['meta_key'] ] ) ) {
return maybe_unserialize( $meta_value[ $meta_tuple['meta_key'] ] );
}
return '';
}
/**
* Adds and updates meta data in the database
*
* @internal
*
* @param integer $object_id Object ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
* @param string $object_type Object type.
* @param boolean $global ?
* @return boolean
*/
function wpsc_update_meta( $object_id = 0, $meta_key, $meta_value, $object_type, $global = false ) {
global $wpdb;
if ( ! is_numeric( $object_id ) || empty( $object_id ) && ! $global ) {
return false;
}
$cache_object_id = $object_id = (int) $object_id;
$meta_key = wpsc_sanitize_meta_key( $meta_key );
$meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value' );
$meta_tuple = apply_filters( 'wpsc_update_meta', $meta_tuple );
$meta_value = $_meta_value = maybe_serialize( $meta_tuple['meta_value'] );
$meta_value = maybe_unserialize( $meta_value );
$cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `" . WPSC_TABLE_META . "` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s", $meta_tuple['object_type'], $meta_tuple['object_id'], $meta_tuple['meta_key'] ) );
if ( ! $cur ) {
$wpdb->insert( WPSC_TABLE_META, array( 'object_type' => $meta_tuple['object_type'], 'object_id' => $meta_tuple['object_id'], 'meta_key' => $meta_tuple['meta_key'], 'meta_value' => $_meta_value ) );
} elseif ( $cur->meta_value != $meta_value ) {
$wpdb->update( WPSC_TABLE_META, array( 'meta_value' => $_meta_value ), array( 'object_type' => $meta_tuple['object_type'], 'object_id' => $meta_tuple['object_id'], 'meta_key' => $meta_tuple['meta_key'] ) );
}
wp_cache_delete( $cache_object_id, $meta_tuple['object_type'] );
if ( ! $cur ) {
return true;
}
}
/**
* Deletes meta data from the database
*
* @internal
*
* @param integer $object_id Object ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
* @param string $object_type Object type.
* @param boolean $global ?
* @return boolean
*/
function wpsc_delete_meta( $object_id = 0, $meta_key, $meta_value, $object_type, $global = false ) {
global $wpdb;
if ( ! is_numeric( $object_id ) || empty( $object_id ) && ! $global ) {
return false;
}
$cache_object_id = $object_id = (int) $object_id;
$meta_key = wpsc_sanitize_meta_key( $meta_key );
$meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value' );
$meta_tuple = apply_filters( 'wpsc_delete_meta', $meta_tuple );
$meta_value = maybe_serialize( $meta_tuple['meta_value'] );
if ( empty( $meta_value ) ) {
$meta_sql = $wpdb->prepare( "SELECT `meta_id` FROM `" . WPSC_TABLE_META . "` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s", $meta_tuple['object_type'], $meta_tuple['object_id'], $meta_tuple['meta_key'] );
} else {
$meta_sql = $wpdb->prepare( "SELECT `meta_id` FROM `" . WPSC_TABLE_META . "` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s AND `meta_value` = %s", $meta_tuple['object_type'], $meta_tuple['object_id'], $meta_tuple['meta_key'], $meta_value );
}
if ( ! $meta_id = $wpdb->get_var( $meta_sql ) ) {
return false;
}
$wpdb->query( $wpdb->prepare( "DELETE FROM `" . WPSC_TABLE_META . "` WHERE `meta_id` = %d", $meta_id ) );
wp_cache_delete( $cache_object_id, $meta_tuple['object_type'] );
return true;
}
/**
* Update Meta Cache
*
* Query database to get meta for objects, update the cache and return the object meta.
*
* @param string $object_type Object type.
* @param int|array $object_ids Object ID or IDs.
* @return array Array of objects and cached values.
*/
function wpsc_update_meta_cache( $object_type, $object_ids ) {
global $wpdb;
if ( ! $object_type || ! $object_ids ) {
return false;
}
// If $object_ids is a string, convert to array
if ( ! is_array( $object_ids ) ) {
$object_ids = preg_replace( '|[^0-9,]|', '', $object_ids );
$object_ids = explode( ',', $object_ids );
}
$object_ids = array_map( 'intval', $object_ids );
$ids = array();
$cache = array();
// Only need to retrieve objects that aren't already cached
foreach ( $object_ids as $id ) {
$cached_object = wp_cache_get( $id, $object_type );
if ( false === $cached_object ) {
$ids[] = $id;
} else {
$cache[ $id ] = $cached_object;
}
}
if ( empty( $ids ) ) {
return $cache;
}
$id_list = join( ',', $ids );
$meta_list = $wpdb->get_results( $wpdb->prepare( "SELECT object_id, meta_key, meta_value FROM " . WPSC_TABLE_META . " WHERE `object_type` = '%s' AND `object_id` IN ( " . $id_list . " )", $object_type ), ARRAY_A );
// Add results to cache array
if ( ! empty( $meta_list ) ) {
foreach ( $meta_list as $metarow ) {
$mpid = intval( $metarow[ 'object_id' ] );
$mkey = $metarow['meta_key'];
$mval = $metarow['meta_value'];
// Add a value to the current pid/key:
$cache[ $mpid ][ $mkey ] = $mval;
}
}
// Update cache
foreach ( $ids as $id ) {
if ( ! isset( $cache[ $id ] ) ) {
$cache[ $id ] = array();
}
wp_cache_add( $id, $cache[ $id ], $object_type );
}
return $cache;
}
/**
* category meta functions are as follows:
*/
/**
* Retrieve meta field for a category
*
* @param int $cat_id Category ID.
* @param string $meta_key The meta key to retrieve.
* @return mixed Will be value of meta data field
*/
function wpsc_get_categorymeta( $cat_id, $meta_key ) {
return wpsc_get_meta( $cat_id, $meta_key, 'wpsc_category' );
}
/**
* Update meta field for a category
*
* @param int $cat_id Category ID.
* @param string $meta_key The meta key to retrieve.
* @param string $meta_value The value to be stored.
* @return mixed True if updated
*/
function wpsc_update_categorymeta( $cat_id, $meta_key, $meta_value ) {
return wpsc_update_meta( $cat_id, $meta_key, $meta_value, 'wpsc_category' );
}
/**
* Delete meta field for a category
*
* @param int $cat_id Category ID.
* @param string $meta_key The meta key to retrieve.
* @param string $meta_value Value to be compared before deleting.
* @return mixed True if updated
*/
function wpsc_delete_categorymeta( $cat_id, $meta_key, $meta_value = '' ) {
return wpsc_delete_meta( $cat_id, $meta_key, $meta_value, 'wpsc_category' );
}
/**
* category meta functions end here.
*/
/**
* product meta functions start here
* all these functions just prefix the key with the meta prefix, and pass the values through to the equivalent post meta function.
*/
/**
* add_product_meta function.
*
* @access public
* @param int $product_id Unique product identifier
* @param string $key Metadata name.
* @param mixed $value Metadata value. Must be serializable if non-scalar.
* @param bool $unique - obsolete
* @param bool $custom - obsolete
* @return int|bool WordPress Meta ID on success, false on failure.
*/
function add_product_meta( $product_id, $key, $value, $unique = false, $custom = false ) {
$key = WPSC_META_PREFIX.$key;
return add_post_meta($product_id, $key, $value);
}
/**
* delete_product_meta function.
*
* @access public
* @param int $product_id
* @param string $key
* @param mixed $value. Optional. Metadata value. Must be serializable if non-scalar. Default empty
* @return bool True on success, false on failure.
*/
function delete_product_meta($product_id, $key, $value = '') {
$key = WPSC_META_PREFIX.$key;
return delete_post_meta($product_id, $key, $value);
}
/**
* get_product_meta function.
*
* @access public
* @param int $product_id
* @param string $key
* @param bool $single Optional. Whether to return a single value. Default false.
* @return mixed Will be an array if $single is false. Will be value of meta data
* field if $single is true.
*/
function get_product_meta($product_id, $key, $single = false) {
$key = WPSC_META_PREFIX.$key;
return get_post_meta($product_id, $key, $single);
}
/**
* update_product_meta function.
*
* @access public
* @param int $product_id
* @param string $key
* @param mixed Metadata value. Must be serializable if non-scalar.
* @param string Optional. Previous value to check before removing.
* Defaults to empty. (default: '')
* @return int|bool Meta ID if the key didn't exist, true on successful update,
* false on failure.
*/
function update_product_meta($product_id, $key, $value, $prev_value = '') {
$key = WPSC_META_PREFIX.$key;
return update_post_meta($product_id, $key, $value, $prev_value);
}
/**
* product meta functions end here
*/
class wpsc_custom_meta {
// Custom meta values
var $custom_meta;
var $custom_meta_count = 0;
var $current_custom_meta = -1;
var $custom_meta_values;
function __construct( $post_id ) {
$cleaned_metas = array();
if ( ! empty( $post_id ) ) {
$meta_values = get_post_meta( $post_id );
foreach ( $meta_values as $key => $values ) {
if ( ! is_protected_meta( $key, 'wpsc-product' ) ) {
if ( is_array( $values ) ) {
foreach ( $values as $value ) {
$cleaned_metas[] = array( 'meta_key' => $key, 'meta_value' => $value );
}
}
}
}
}
$this->custom_meta = $cleaned_metas;
$this->custom_meta_count = count( $this->custom_meta );
}
function have_custom_meta() {
if (($this->current_custom_meta + 1) < $this->custom_meta_count) {
return true;
} else if ($this->current_custom_meta + 1 == $this->custom_meta_count && $this->custom_meta_count > 0) {
$this->rewind_custom_meta();
}
return false;
}
/*
* Custom Meta Loop Code Starts here
*/
function next_custom_meta() {
$this->current_custom_meta++;
$this->custom_meta_values = $this->custom_meta[$this->current_custom_meta];
return $this->custom_meta_values;
}
function the_custom_meta() {
$this->custom_meta_values = $this->next_custom_meta();
return $this->custom_meta_values;
}
function rewind_custom_meta() {
if ($this->custom_meta_count > 0) {
$this->custom_meta_values = $this->custom_meta[0];
}
}
}