|
1 <?php |
|
2 /** |
|
3 * Metadata API |
|
4 * |
|
5 * Functions for retrieving and manipulating metadata of various WordPress object types. Metadata |
|
6 * for an object is a represented by a simple key-value pair. Objects may contain multiple |
|
7 * metadata entries that share the same key and differ only in their value. |
|
8 * |
|
9 * @package WordPress |
|
10 * @subpackage Meta |
|
11 * @since 2.9.0 |
|
12 */ |
|
13 |
|
14 /** |
|
15 * Add metadata for the specified object. |
|
16 * |
|
17 * @since 2.9.0 |
|
18 * @uses $wpdb WordPress database object for queries. |
|
19 * @uses do_action() Calls 'added_{$meta_type}_meta' with meta_id of added metadata entry, |
|
20 * object ID, meta key, and meta value |
|
21 * |
|
22 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) |
|
23 * @param int $object_id ID of the object metadata is for |
|
24 * @param string $meta_key Metadata key |
|
25 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
|
26 * @param bool $unique Optional, default is false. Whether the specified metadata key should be |
|
27 * unique for the object. If true, and the object already has a value for the specified |
|
28 * metadata key, no change will be made |
|
29 * @return int|bool The meta ID on successful update, false on failure. |
|
30 */ |
|
31 function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) { |
|
32 if ( !$meta_type || !$meta_key ) |
|
33 return false; |
|
34 |
|
35 if ( !$object_id = absint($object_id) ) |
|
36 return false; |
|
37 |
|
38 if ( ! $table = _get_meta_table($meta_type) ) |
|
39 return false; |
|
40 |
|
41 global $wpdb; |
|
42 |
|
43 $column = sanitize_key($meta_type . '_id'); |
|
44 |
|
45 // expected_slashed ($meta_key) |
|
46 $meta_key = wp_unslash($meta_key); |
|
47 $meta_value = wp_unslash($meta_value); |
|
48 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type ); |
|
49 |
|
50 $check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique ); |
|
51 if ( null !== $check ) |
|
52 return $check; |
|
53 |
|
54 if ( $unique && $wpdb->get_var( $wpdb->prepare( |
|
55 "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d", |
|
56 $meta_key, $object_id ) ) ) |
|
57 return false; |
|
58 |
|
59 $_meta_value = $meta_value; |
|
60 $meta_value = maybe_serialize( $meta_value ); |
|
61 |
|
62 do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value ); |
|
63 |
|
64 $result = $wpdb->insert( $table, array( |
|
65 $column => $object_id, |
|
66 'meta_key' => $meta_key, |
|
67 'meta_value' => $meta_value |
|
68 ) ); |
|
69 |
|
70 if ( ! $result ) |
|
71 return false; |
|
72 |
|
73 $mid = (int) $wpdb->insert_id; |
|
74 |
|
75 wp_cache_delete($object_id, $meta_type . '_meta'); |
|
76 |
|
77 do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value ); |
|
78 |
|
79 return $mid; |
|
80 } |
|
81 |
|
82 /** |
|
83 * Update metadata for the specified object. If no value already exists for the specified object |
|
84 * ID and metadata key, the metadata will be added. |
|
85 * |
|
86 * @since 2.9.0 |
|
87 * @uses $wpdb WordPress database object for queries. |
|
88 * @uses do_action() Calls 'update_{$meta_type}_meta' before updating metadata with meta_id of |
|
89 * metadata entry to update, object ID, meta key, and meta value |
|
90 * @uses do_action() Calls 'updated_{$meta_type}_meta' after updating metadata with meta_id of |
|
91 * updated metadata entry, object ID, meta key, and meta value |
|
92 * |
|
93 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) |
|
94 * @param int $object_id ID of the object metadata is for |
|
95 * @param string $meta_key Metadata key |
|
96 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
|
97 * @param mixed $prev_value Optional. If specified, only update existing metadata entries with |
|
98 * the specified value. Otherwise, update all entries. |
|
99 * @return bool True on successful update, false on failure. |
|
100 */ |
|
101 function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') { |
|
102 if ( !$meta_type || !$meta_key ) |
|
103 return false; |
|
104 |
|
105 if ( !$object_id = absint($object_id) ) |
|
106 return false; |
|
107 |
|
108 if ( ! $table = _get_meta_table($meta_type) ) |
|
109 return false; |
|
110 |
|
111 global $wpdb; |
|
112 |
|
113 $column = sanitize_key($meta_type . '_id'); |
|
114 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
|
115 |
|
116 // expected_slashed ($meta_key) |
|
117 $meta_key = wp_unslash($meta_key); |
|
118 $passed_value = $meta_value; |
|
119 $meta_value = wp_unslash($meta_value); |
|
120 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type ); |
|
121 |
|
122 $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value ); |
|
123 if ( null !== $check ) |
|
124 return (bool) $check; |
|
125 |
|
126 // Compare existing value to new value if no prev value given and the key exists only once. |
|
127 if ( empty($prev_value) ) { |
|
128 $old_value = get_metadata($meta_type, $object_id, $meta_key); |
|
129 if ( count($old_value) == 1 ) { |
|
130 if ( $old_value[0] === $meta_value ) |
|
131 return false; |
|
132 } |
|
133 } |
|
134 |
|
135 if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) ) |
|
136 return add_metadata($meta_type, $object_id, $meta_key, $passed_value); |
|
137 |
|
138 $_meta_value = $meta_value; |
|
139 $meta_value = maybe_serialize( $meta_value ); |
|
140 |
|
141 $data = compact( 'meta_value' ); |
|
142 $where = array( $column => $object_id, 'meta_key' => $meta_key ); |
|
143 |
|
144 if ( !empty( $prev_value ) ) { |
|
145 $prev_value = maybe_serialize($prev_value); |
|
146 $where['meta_value'] = $prev_value; |
|
147 } |
|
148 |
|
149 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); |
|
150 |
|
151 if ( 'post' == $meta_type ) |
|
152 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); |
|
153 |
|
154 $result = $wpdb->update( $table, $data, $where ); |
|
155 if ( ! $result ) |
|
156 return false; |
|
157 |
|
158 wp_cache_delete($object_id, $meta_type . '_meta'); |
|
159 |
|
160 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); |
|
161 |
|
162 if ( 'post' == $meta_type ) |
|
163 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); |
|
164 |
|
165 return true; |
|
166 } |
|
167 |
|
168 /** |
|
169 * Delete metadata for the specified object. |
|
170 * |
|
171 * @since 2.9.0 |
|
172 * @uses $wpdb WordPress database object for queries. |
|
173 * @uses do_action() Calls 'deleted_{$meta_type}_meta' after deleting with meta_id of |
|
174 * deleted metadata entries, object ID, meta key, and meta value |
|
175 * |
|
176 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) |
|
177 * @param int $object_id ID of the object metadata is for |
|
178 * @param string $meta_key Metadata key |
|
179 * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete metadata entries |
|
180 * with this value. Otherwise, delete all entries with the specified meta_key. |
|
181 * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries |
|
182 * for all objects, ignoring the specified object_id. Otherwise, only delete matching |
|
183 * metadata entries for the specified object_id. |
|
184 * @return bool True on successful delete, false on failure. |
|
185 */ |
|
186 function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false) { |
|
187 if ( !$meta_type || !$meta_key ) |
|
188 return false; |
|
189 |
|
190 if ( (!$object_id = absint($object_id)) && !$delete_all ) |
|
191 return false; |
|
192 |
|
193 if ( ! $table = _get_meta_table($meta_type) ) |
|
194 return false; |
|
195 |
|
196 global $wpdb; |
|
197 |
|
198 $type_column = sanitize_key($meta_type . '_id'); |
|
199 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
|
200 // expected_slashed ($meta_key) |
|
201 $meta_key = wp_unslash($meta_key); |
|
202 $meta_value = wp_unslash($meta_value); |
|
203 |
|
204 $check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all ); |
|
205 if ( null !== $check ) |
|
206 return (bool) $check; |
|
207 |
|
208 $_meta_value = $meta_value; |
|
209 $meta_value = maybe_serialize( $meta_value ); |
|
210 |
|
211 $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key ); |
|
212 |
|
213 if ( !$delete_all ) |
|
214 $query .= $wpdb->prepare(" AND $type_column = %d", $object_id ); |
|
215 |
|
216 if ( $meta_value ) |
|
217 $query .= $wpdb->prepare(" AND meta_value = %s", $meta_value ); |
|
218 |
|
219 $meta_ids = $wpdb->get_col( $query ); |
|
220 if ( !count( $meta_ids ) ) |
|
221 return false; |
|
222 |
|
223 if ( $delete_all ) |
|
224 $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) ); |
|
225 |
|
226 do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); |
|
227 |
|
228 // Old-style action. |
|
229 if ( 'post' == $meta_type ) |
|
230 do_action( 'delete_postmeta', $meta_ids ); |
|
231 |
|
232 $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )"; |
|
233 |
|
234 $count = $wpdb->query($query); |
|
235 |
|
236 if ( !$count ) |
|
237 return false; |
|
238 |
|
239 if ( $delete_all ) { |
|
240 foreach ( (array) $object_ids as $o_id ) { |
|
241 wp_cache_delete($o_id, $meta_type . '_meta'); |
|
242 } |
|
243 } else { |
|
244 wp_cache_delete($object_id, $meta_type . '_meta'); |
|
245 } |
|
246 |
|
247 do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); |
|
248 |
|
249 // Old-style action. |
|
250 if ( 'post' == $meta_type ) |
|
251 do_action( 'deleted_postmeta', $meta_ids ); |
|
252 |
|
253 return true; |
|
254 } |
|
255 |
|
256 /** |
|
257 * Retrieve metadata for the specified object. |
|
258 * |
|
259 * @since 2.9.0 |
|
260 * |
|
261 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) |
|
262 * @param int $object_id ID of the object metadata is for |
|
263 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for |
|
264 * the specified object. |
|
265 * @param bool $single Optional, default is false. If true, return only the first value of the |
|
266 * specified meta_key. This parameter has no effect if meta_key is not specified. |
|
267 * @return string|array Single metadata value, or array of values |
|
268 */ |
|
269 function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) { |
|
270 if ( !$meta_type ) |
|
271 return false; |
|
272 |
|
273 if ( !$object_id = absint($object_id) ) |
|
274 return false; |
|
275 |
|
276 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single ); |
|
277 if ( null !== $check ) { |
|
278 if ( $single && is_array( $check ) ) |
|
279 return $check[0]; |
|
280 else |
|
281 return $check; |
|
282 } |
|
283 |
|
284 $meta_cache = wp_cache_get($object_id, $meta_type . '_meta'); |
|
285 |
|
286 if ( !$meta_cache ) { |
|
287 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); |
|
288 $meta_cache = $meta_cache[$object_id]; |
|
289 } |
|
290 |
|
291 if ( !$meta_key ) |
|
292 return $meta_cache; |
|
293 |
|
294 if ( isset($meta_cache[$meta_key]) ) { |
|
295 if ( $single ) |
|
296 return maybe_unserialize( $meta_cache[$meta_key][0] ); |
|
297 else |
|
298 return array_map('maybe_unserialize', $meta_cache[$meta_key]); |
|
299 } |
|
300 |
|
301 if ($single) |
|
302 return ''; |
|
303 else |
|
304 return array(); |
|
305 } |
|
306 |
|
307 /** |
|
308 * Determine if a meta key is set for a given object |
|
309 * |
|
310 * @since 3.3.0 |
|
311 * |
|
312 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) |
|
313 * @param int $object_id ID of the object metadata is for |
|
314 * @param string $meta_key Metadata key. |
|
315 * @return boolean true of the key is set, false if not. |
|
316 */ |
|
317 function metadata_exists( $meta_type, $object_id, $meta_key ) { |
|
318 if ( ! $meta_type ) |
|
319 return false; |
|
320 |
|
321 if ( ! $object_id = absint( $object_id ) ) |
|
322 return false; |
|
323 |
|
324 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true ); |
|
325 if ( null !== $check ) |
|
326 return true; |
|
327 |
|
328 $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' ); |
|
329 |
|
330 if ( !$meta_cache ) { |
|
331 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); |
|
332 $meta_cache = $meta_cache[$object_id]; |
|
333 } |
|
334 |
|
335 if ( isset( $meta_cache[ $meta_key ] ) ) |
|
336 return true; |
|
337 |
|
338 return false; |
|
339 } |
|
340 |
|
341 /** |
|
342 * Get meta data by meta ID |
|
343 * |
|
344 * @since 3.3.0 |
|
345 * |
|
346 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) |
|
347 * @param int $meta_id ID for a specific meta row |
|
348 * @return object Meta object or false. |
|
349 */ |
|
350 function get_metadata_by_mid( $meta_type, $meta_id ) { |
|
351 global $wpdb; |
|
352 |
|
353 if ( ! $meta_type ) |
|
354 return false; |
|
355 |
|
356 if ( !$meta_id = absint( $meta_id ) ) |
|
357 return false; |
|
358 |
|
359 if ( ! $table = _get_meta_table($meta_type) ) |
|
360 return false; |
|
361 |
|
362 $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id'; |
|
363 |
|
364 $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) ); |
|
365 |
|
366 if ( empty( $meta ) ) |
|
367 return false; |
|
368 |
|
369 if ( isset( $meta->meta_value ) ) |
|
370 $meta->meta_value = maybe_unserialize( $meta->meta_value ); |
|
371 |
|
372 return $meta; |
|
373 } |
|
374 |
|
375 /** |
|
376 * Update meta data by meta ID |
|
377 * |
|
378 * @since 3.3.0 |
|
379 * |
|
380 * @uses get_metadata_by_mid() Calls get_metadata_by_mid() to fetch the meta key, value |
|
381 * and object_id of the given meta_id. |
|
382 * |
|
383 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) |
|
384 * @param int $meta_id ID for a specific meta row |
|
385 * @param string $meta_value Metadata value |
|
386 * @param string $meta_key Optional, you can provide a meta key to update it |
|
387 * @return bool True on successful update, false on failure. |
|
388 */ |
|
389 function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) { |
|
390 global $wpdb; |
|
391 |
|
392 // Make sure everything is valid. |
|
393 if ( ! $meta_type ) |
|
394 return false; |
|
395 |
|
396 if ( ! $meta_id = absint( $meta_id ) ) |
|
397 return false; |
|
398 |
|
399 if ( ! $table = _get_meta_table( $meta_type ) ) |
|
400 return false; |
|
401 |
|
402 $column = sanitize_key($meta_type . '_id'); |
|
403 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
|
404 |
|
405 // Fetch the meta and go on if it's found. |
|
406 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { |
|
407 $original_key = $meta->meta_key; |
|
408 $original_value = $meta->meta_value; |
|
409 $object_id = $meta->{$column}; |
|
410 |
|
411 // If a new meta_key (last parameter) was specified, change the meta key, |
|
412 // otherwise use the original key in the update statement. |
|
413 if ( false === $meta_key ) { |
|
414 $meta_key = $original_key; |
|
415 } elseif ( ! is_string( $meta_key ) ) { |
|
416 return false; |
|
417 } |
|
418 |
|
419 // Sanitize the meta |
|
420 $_meta_value = $meta_value; |
|
421 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type ); |
|
422 $meta_value = maybe_serialize( $meta_value ); |
|
423 |
|
424 // Format the data query arguments. |
|
425 $data = array( |
|
426 'meta_key' => $meta_key, |
|
427 'meta_value' => $meta_value |
|
428 ); |
|
429 |
|
430 // Format the where query arguments. |
|
431 $where = array(); |
|
432 $where[$id_column] = $meta_id; |
|
433 |
|
434 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); |
|
435 |
|
436 if ( 'post' == $meta_type ) |
|
437 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); |
|
438 |
|
439 // Run the update query, all fields in $data are %s, $where is a %d. |
|
440 $result = $wpdb->update( $table, $data, $where, '%s', '%d' ); |
|
441 if ( ! $result ) |
|
442 return false; |
|
443 |
|
444 // Clear the caches. |
|
445 wp_cache_delete($object_id, $meta_type . '_meta'); |
|
446 |
|
447 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); |
|
448 |
|
449 if ( 'post' == $meta_type ) |
|
450 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); |
|
451 |
|
452 return true; |
|
453 } |
|
454 |
|
455 // And if the meta was not found. |
|
456 return false; |
|
457 } |
|
458 |
|
459 /** |
|
460 * Delete meta data by meta ID |
|
461 * |
|
462 * @since 3.3.0 |
|
463 * |
|
464 * @uses get_metadata_by_mid() Calls get_metadata_by_mid() to fetch the meta key, value |
|
465 * and object_id of the given meta_id. |
|
466 * |
|
467 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) |
|
468 * @param int $meta_id ID for a specific meta row |
|
469 * @return bool True on successful delete, false on failure. |
|
470 */ |
|
471 function delete_metadata_by_mid( $meta_type, $meta_id ) { |
|
472 global $wpdb; |
|
473 |
|
474 // Make sure everything is valid. |
|
475 if ( ! $meta_type ) |
|
476 return false; |
|
477 |
|
478 if ( ! $meta_id = absint( $meta_id ) ) |
|
479 return false; |
|
480 |
|
481 if ( ! $table = _get_meta_table( $meta_type ) ) |
|
482 return false; |
|
483 |
|
484 // object and id columns |
|
485 $column = sanitize_key($meta_type . '_id'); |
|
486 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
|
487 |
|
488 // Fetch the meta and go on if it's found. |
|
489 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { |
|
490 $object_id = $meta->{$column}; |
|
491 |
|
492 do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); |
|
493 |
|
494 // Old-style action. |
|
495 if ( 'post' == $meta_type || 'comment' == $meta_type ) |
|
496 do_action( "delete_{$meta_type}meta", $meta_id ); |
|
497 |
|
498 // Run the query, will return true if deleted, false otherwise |
|
499 $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) ); |
|
500 |
|
501 // Clear the caches. |
|
502 wp_cache_delete($object_id, $meta_type . '_meta'); |
|
503 |
|
504 do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); |
|
505 |
|
506 // Old-style action. |
|
507 if ( 'post' == $meta_type || 'comment' == $meta_type ) |
|
508 do_action( "deleted_{$meta_type}meta", $meta_id ); |
|
509 |
|
510 return $result; |
|
511 |
|
512 } |
|
513 |
|
514 // Meta id was not found. |
|
515 return false; |
|
516 } |
|
517 |
|
518 /** |
|
519 * Update the metadata cache for the specified objects. |
|
520 * |
|
521 * @since 2.9.0 |
|
522 * @uses $wpdb WordPress database object for queries. |
|
523 * |
|
524 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) |
|
525 * @param int|array $object_ids array or comma delimited list of object IDs to update cache for |
|
526 * @return mixed Metadata cache for the specified objects, or false on failure. |
|
527 */ |
|
528 function update_meta_cache($meta_type, $object_ids) { |
|
529 if ( empty( $meta_type ) || empty( $object_ids ) ) |
|
530 return false; |
|
531 |
|
532 if ( ! $table = _get_meta_table($meta_type) ) |
|
533 return false; |
|
534 |
|
535 $column = sanitize_key($meta_type . '_id'); |
|
536 |
|
537 global $wpdb; |
|
538 |
|
539 if ( !is_array($object_ids) ) { |
|
540 $object_ids = preg_replace('|[^0-9,]|', '', $object_ids); |
|
541 $object_ids = explode(',', $object_ids); |
|
542 } |
|
543 |
|
544 $object_ids = array_map('intval', $object_ids); |
|
545 |
|
546 $cache_key = $meta_type . '_meta'; |
|
547 $ids = array(); |
|
548 $cache = array(); |
|
549 foreach ( $object_ids as $id ) { |
|
550 $cached_object = wp_cache_get( $id, $cache_key ); |
|
551 if ( false === $cached_object ) |
|
552 $ids[] = $id; |
|
553 else |
|
554 $cache[$id] = $cached_object; |
|
555 } |
|
556 |
|
557 if ( empty( $ids ) ) |
|
558 return $cache; |
|
559 |
|
560 // Get meta info |
|
561 $id_list = join(',', $ids); |
|
562 $meta_list = $wpdb->get_results( $wpdb->prepare("SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list)", |
|
563 $meta_type), ARRAY_A ); |
|
564 |
|
565 if ( !empty($meta_list) ) { |
|
566 foreach ( $meta_list as $metarow) { |
|
567 $mpid = intval($metarow[$column]); |
|
568 $mkey = $metarow['meta_key']; |
|
569 $mval = $metarow['meta_value']; |
|
570 |
|
571 // Force subkeys to be array type: |
|
572 if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) ) |
|
573 $cache[$mpid] = array(); |
|
574 if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) ) |
|
575 $cache[$mpid][$mkey] = array(); |
|
576 |
|
577 // Add a value to the current pid/key: |
|
578 $cache[$mpid][$mkey][] = $mval; |
|
579 } |
|
580 } |
|
581 |
|
582 foreach ( $ids as $id ) { |
|
583 if ( ! isset($cache[$id]) ) |
|
584 $cache[$id] = array(); |
|
585 wp_cache_add( $id, $cache[$id], $cache_key ); |
|
586 } |
|
587 |
|
588 return $cache; |
|
589 } |
|
590 |
|
591 /** |
|
592 * Given a meta query, generates SQL clauses to be appended to a main query |
|
593 * |
|
594 * @since 3.2.0 |
|
595 * |
|
596 * @see WP_Meta_Query |
|
597 * |
|
598 * @param array $meta_query A meta query |
|
599 * @param string $type Type of meta |
|
600 * @param string $primary_table |
|
601 * @param string $primary_id_column |
|
602 * @param object $context (optional) The main query object |
|
603 * @return array( 'join' => $join_sql, 'where' => $where_sql ) |
|
604 */ |
|
605 function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) { |
|
606 $meta_query_obj = new WP_Meta_Query( $meta_query ); |
|
607 return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context ); |
|
608 } |
|
609 |
|
610 /** |
|
611 * Container class for a multiple metadata query |
|
612 * |
|
613 * @since 3.2.0 |
|
614 */ |
|
615 class WP_Meta_Query { |
|
616 /** |
|
617 * List of metadata queries. A single query is an associative array: |
|
618 * - 'key' string The meta key |
|
619 * - 'value' string|array The meta value |
|
620 * - 'compare' (optional) string How to compare the key to the value. |
|
621 * Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', |
|
622 * 'BETWEEN', 'NOT BETWEEN', 'REGEXP', 'NOT REGEXP', 'RLIKE'. |
|
623 * Default: '=' |
|
624 * - 'type' string (optional) The type of the value. |
|
625 * Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. |
|
626 * Default: 'CHAR' |
|
627 * |
|
628 * @since 3.2.0 |
|
629 * @access public |
|
630 * @var array |
|
631 */ |
|
632 public $queries = array(); |
|
633 |
|
634 /** |
|
635 * The relation between the queries. Can be one of 'AND' or 'OR'. |
|
636 * |
|
637 * @since 3.2.0 |
|
638 * @access public |
|
639 * @var string |
|
640 */ |
|
641 public $relation; |
|
642 |
|
643 /** |
|
644 * Constructor |
|
645 * |
|
646 * @param array $meta_query (optional) A meta query |
|
647 */ |
|
648 function __construct( $meta_query = false ) { |
|
649 if ( !$meta_query ) |
|
650 return; |
|
651 |
|
652 if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) { |
|
653 $this->relation = 'OR'; |
|
654 } else { |
|
655 $this->relation = 'AND'; |
|
656 } |
|
657 |
|
658 $this->queries = array(); |
|
659 |
|
660 foreach ( $meta_query as $key => $query ) { |
|
661 if ( ! is_array( $query ) ) |
|
662 continue; |
|
663 |
|
664 $this->queries[] = $query; |
|
665 } |
|
666 } |
|
667 |
|
668 /** |
|
669 * Constructs a meta query based on 'meta_*' query vars |
|
670 * |
|
671 * @since 3.2.0 |
|
672 * @access public |
|
673 * |
|
674 * @param array $qv The query variables |
|
675 */ |
|
676 function parse_query_vars( $qv ) { |
|
677 $meta_query = array(); |
|
678 |
|
679 // Simple query needs to be first for orderby=meta_value to work correctly |
|
680 foreach ( array( 'key', 'compare', 'type' ) as $key ) { |
|
681 if ( !empty( $qv[ "meta_$key" ] ) ) |
|
682 $meta_query[0][ $key ] = $qv[ "meta_$key" ]; |
|
683 } |
|
684 |
|
685 // WP_Query sets 'meta_value' = '' by default |
|
686 if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] && ( ! is_array( $qv[ 'meta_value' ] ) || $qv[ 'meta_value' ] ) ) |
|
687 $meta_query[0]['value'] = $qv[ 'meta_value' ]; |
|
688 |
|
689 if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) { |
|
690 $meta_query = array_merge( $meta_query, $qv['meta_query'] ); |
|
691 } |
|
692 |
|
693 $this->__construct( $meta_query ); |
|
694 } |
|
695 |
|
696 /** |
|
697 * Given a meta type, return the appropriate alias if applicable |
|
698 * |
|
699 * @since 3.7.0 |
|
700 * |
|
701 * @param string $type MySQL type to cast meta_value |
|
702 * @return string MySQL type |
|
703 */ |
|
704 function get_cast_for_type( $type = '' ) { |
|
705 if ( empty( $type ) ) |
|
706 return 'CHAR'; |
|
707 |
|
708 $meta_type = strtoupper( $type ); |
|
709 |
|
710 if ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED', 'NUMERIC' ) ) ) |
|
711 return 'CHAR'; |
|
712 |
|
713 if ( 'NUMERIC' == $meta_type ) |
|
714 $meta_type = 'SIGNED'; |
|
715 |
|
716 return $meta_type; |
|
717 } |
|
718 |
|
719 /** |
|
720 * Generates SQL clauses to be appended to a main query. |
|
721 * |
|
722 * @since 3.2.0 |
|
723 * @access public |
|
724 * |
|
725 * @param string $type Type of meta |
|
726 * @param string $primary_table |
|
727 * @param string $primary_id_column |
|
728 * @param object $context (optional) The main query object |
|
729 * @return array( 'join' => $join_sql, 'where' => $where_sql ) |
|
730 */ |
|
731 function get_sql( $type, $primary_table, $primary_id_column, $context = null ) { |
|
732 global $wpdb; |
|
733 |
|
734 if ( ! $meta_table = _get_meta_table( $type ) ) |
|
735 return false; |
|
736 |
|
737 $meta_id_column = sanitize_key( $type . '_id' ); |
|
738 |
|
739 $join = array(); |
|
740 $where = array(); |
|
741 |
|
742 $key_only_queries = array(); |
|
743 $queries = array(); |
|
744 |
|
745 // Split out the queries with empty arrays as value |
|
746 foreach ( $this->queries as $k => $q ) { |
|
747 if ( isset( $q['value'] ) && is_array( $q['value'] ) && empty( $q['value'] ) ) { |
|
748 $key_only_queries[$k] = $q; |
|
749 unset( $this->queries[$k] ); |
|
750 } |
|
751 } |
|
752 |
|
753 // Split out the meta_key only queries (we can only do this for OR) |
|
754 if ( 'OR' == $this->relation ) { |
|
755 foreach ( $this->queries as $k => $q ) { |
|
756 if ( ! isset( $q['value'] ) && ! empty( $q['key'] ) ) |
|
757 $key_only_queries[$k] = $q; |
|
758 else |
|
759 $queries[$k] = $q; |
|
760 } |
|
761 } else { |
|
762 $queries = $this->queries; |
|
763 } |
|
764 |
|
765 // Specify all the meta_key only queries in one go |
|
766 if ( $key_only_queries ) { |
|
767 $join[] = "INNER JOIN $meta_table ON $primary_table.$primary_id_column = $meta_table.$meta_id_column"; |
|
768 |
|
769 foreach ( $key_only_queries as $key => $q ) |
|
770 $where["key-only-$key"] = $wpdb->prepare( "$meta_table.meta_key = %s", trim( $q['key'] ) ); |
|
771 } |
|
772 |
|
773 foreach ( $queries as $k => $q ) { |
|
774 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : ''; |
|
775 $meta_type = $this->get_cast_for_type( isset( $q['type'] ) ? $q['type'] : '' ); |
|
776 |
|
777 $meta_value = isset( $q['value'] ) ? $q['value'] : null; |
|
778 |
|
779 if ( isset( $q['compare'] ) ) |
|
780 $meta_compare = strtoupper( $q['compare'] ); |
|
781 else |
|
782 $meta_compare = is_array( $meta_value ) ? 'IN' : '='; |
|
783 |
|
784 if ( ! in_array( $meta_compare, array( |
|
785 '=', '!=', '>', '>=', '<', '<=', |
|
786 'LIKE', 'NOT LIKE', |
|
787 'IN', 'NOT IN', |
|
788 'BETWEEN', 'NOT BETWEEN', |
|
789 'NOT EXISTS', |
|
790 'REGEXP', 'NOT REGEXP', 'RLIKE' |
|
791 ) ) ) |
|
792 $meta_compare = '='; |
|
793 |
|
794 $i = count( $join ); |
|
795 $alias = $i ? 'mt' . $i : $meta_table; |
|
796 |
|
797 if ( 'NOT EXISTS' == $meta_compare ) { |
|
798 $join[$i] = "LEFT JOIN $meta_table"; |
|
799 $join[$i] .= $i ? " AS $alias" : ''; |
|
800 $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column AND $alias.meta_key = '$meta_key')"; |
|
801 |
|
802 $where[$k] = ' ' . $alias . '.' . $meta_id_column . ' IS NULL'; |
|
803 |
|
804 continue; |
|
805 } |
|
806 |
|
807 $join[$i] = "INNER JOIN $meta_table"; |
|
808 $join[$i] .= $i ? " AS $alias" : ''; |
|
809 $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)"; |
|
810 |
|
811 $where[$k] = ''; |
|
812 if ( !empty( $meta_key ) ) |
|
813 $where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key ); |
|
814 |
|
815 if ( is_null( $meta_value ) ) { |
|
816 if ( empty( $where[$k] ) ) |
|
817 unset( $join[$i] ); |
|
818 continue; |
|
819 } |
|
820 |
|
821 if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { |
|
822 if ( ! is_array( $meta_value ) ) |
|
823 $meta_value = preg_split( '/[,\s]+/', $meta_value ); |
|
824 |
|
825 if ( empty( $meta_value ) ) { |
|
826 unset( $join[$i] ); |
|
827 continue; |
|
828 } |
|
829 } else { |
|
830 $meta_value = trim( $meta_value ); |
|
831 } |
|
832 |
|
833 if ( 'IN' == substr( $meta_compare, -2) ) { |
|
834 $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')'; |
|
835 } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) { |
|
836 $meta_value = array_slice( $meta_value, 0, 2 ); |
|
837 $meta_compare_string = '%s AND %s'; |
|
838 } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) { |
|
839 $meta_value = '%' . like_escape( $meta_value ) . '%'; |
|
840 $meta_compare_string = '%s'; |
|
841 } else { |
|
842 $meta_compare_string = '%s'; |
|
843 } |
|
844 |
|
845 if ( ! empty( $where[$k] ) ) |
|
846 $where[$k] .= ' AND '; |
|
847 |
|
848 $where[$k] = ' (' . $where[$k] . $wpdb->prepare( "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string})", $meta_value ); |
|
849 } |
|
850 |
|
851 $where = array_filter( $where ); |
|
852 |
|
853 if ( empty( $where ) ) |
|
854 $where = ''; |
|
855 else |
|
856 $where = ' AND (' . implode( "\n{$this->relation} ", $where ) . ' )'; |
|
857 |
|
858 $join = implode( "\n", $join ); |
|
859 if ( ! empty( $join ) ) |
|
860 $join = ' ' . $join; |
|
861 |
|
862 return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) ); |
|
863 } |
|
864 } |
|
865 |
|
866 /** |
|
867 * Retrieve the name of the metadata table for the specified object type. |
|
868 * |
|
869 * @since 2.9.0 |
|
870 * @uses $wpdb WordPress database object for queries. |
|
871 * |
|
872 * @param string $type Type of object to get metadata table for (e.g., comment, post, or user) |
|
873 * @return mixed Metadata table name, or false if no metadata table exists |
|
874 */ |
|
875 function _get_meta_table($type) { |
|
876 global $wpdb; |
|
877 |
|
878 $table_name = $type . 'meta'; |
|
879 |
|
880 if ( empty($wpdb->$table_name) ) |
|
881 return false; |
|
882 |
|
883 return $wpdb->$table_name; |
|
884 } |
|
885 |
|
886 /** |
|
887 * Determine whether a meta key is protected |
|
888 * |
|
889 * @since 3.1.3 |
|
890 * |
|
891 * @param string $meta_key Meta key |
|
892 * @return bool True if the key is protected, false otherwise. |
|
893 */ |
|
894 function is_protected_meta( $meta_key, $meta_type = null ) { |
|
895 $protected = ( '_' == $meta_key[0] ); |
|
896 |
|
897 return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type ); |
|
898 } |
|
899 |
|
900 /** |
|
901 * Sanitize meta value |
|
902 * |
|
903 * @since 3.1.3 |
|
904 * |
|
905 * @param string $meta_key Meta key |
|
906 * @param mixed $meta_value Meta value to sanitize |
|
907 * @param string $meta_type Type of meta |
|
908 * @return mixed Sanitized $meta_value |
|
909 */ |
|
910 function sanitize_meta( $meta_key, $meta_value, $meta_type ) { |
|
911 return apply_filters( "sanitize_{$meta_type}_meta_{$meta_key}", $meta_value, $meta_key, $meta_type ); |
|
912 } |
|
913 |
|
914 /** |
|
915 * Register meta key |
|
916 * |
|
917 * @since 3.3.0 |
|
918 * |
|
919 * @param string $meta_type Type of meta |
|
920 * @param string $meta_key Meta key |
|
921 * @param string|array $sanitize_callback A function or method to call when sanitizing the value of $meta_key. |
|
922 * @param string|array $auth_callback Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks. |
|
923 * @param array $args Arguments |
|
924 */ |
|
925 function register_meta( $meta_type, $meta_key, $sanitize_callback, $auth_callback = null ) { |
|
926 if ( is_callable( $sanitize_callback ) ) |
|
927 add_filter( "sanitize_{$meta_type}_meta_{$meta_key}", $sanitize_callback, 10, 3 ); |
|
928 |
|
929 if ( empty( $auth_callback ) ) { |
|
930 if ( is_protected_meta( $meta_key, $meta_type ) ) |
|
931 $auth_callback = '__return_false'; |
|
932 else |
|
933 $auth_callback = '__return_true'; |
|
934 } |
|
935 |
|
936 if ( is_callable( $auth_callback ) ) |
|
937 add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 ); |
|
938 } |