9 * @package WordPress |
9 * @package WordPress |
10 * @subpackage Meta |
10 * @subpackage Meta |
11 */ |
11 */ |
12 |
12 |
13 /** |
13 /** |
14 * Add metadata for the specified object. |
14 * Adds metadata for the specified object. |
15 * |
15 * |
16 * @since 2.9.0 |
16 * @since 2.9.0 |
17 * |
17 * |
18 * @global wpdb $wpdb WordPress database abstraction object. |
18 * @global wpdb $wpdb WordPress database abstraction object. |
19 * |
19 * |
20 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
20 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
21 * @param int $object_id ID of the object metadata is for |
21 * or any other object type with an associated meta table. |
22 * @param string $meta_key Metadata key |
22 * @param int $object_id ID of the object metadata is for. |
|
23 * @param string $meta_key Metadata key. |
23 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
24 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
24 * @param bool $unique Optional, default is false. |
25 * @param bool $unique Optional. Whether the specified metadata key should be unique for the object. |
25 * Whether the specified metadata key should be unique for the object. |
|
26 * If true, and the object already has a value for the specified metadata key, |
26 * If true, and the object already has a value for the specified metadata key, |
27 * no change will be made. |
27 * no change will be made. Default false. |
28 * @return int|false The meta ID on success, false on failure. |
28 * @return int|false The meta ID on success, false on failure. |
29 */ |
29 */ |
30 function add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique = false ) { |
30 function add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique = false ) { |
31 global $wpdb; |
31 global $wpdb; |
32 |
32 |
52 $meta_key = wp_unslash( $meta_key ); |
52 $meta_key = wp_unslash( $meta_key ); |
53 $meta_value = wp_unslash( $meta_value ); |
53 $meta_value = wp_unslash( $meta_value ); |
54 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); |
54 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); |
55 |
55 |
56 /** |
56 /** |
57 * Filters whether to add metadata of a specific type. |
57 * Short-circuits adding metadata of a specific type. |
58 * |
58 * |
59 * The dynamic portion of the hook, `$meta_type`, refers to the meta |
59 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type |
60 * object type (comment, post, term, or user). Returning a non-null value |
60 * (post, comment, term, user, or any other type with an associated meta table). |
61 * will effectively short-circuit the function. |
61 * Returning a non-null value will effectively short-circuit the function. |
62 * |
62 * |
63 * @since 3.1.0 |
63 * @since 3.1.0 |
64 * |
64 * |
65 * @param null|bool $check Whether to allow adding metadata for the given type. |
65 * @param null|bool $check Whether to allow adding metadata for the given type. |
66 * @param int $object_id Object ID. |
66 * @param int $object_id ID of the object metadata is for. |
67 * @param string $meta_key Meta key. |
67 * @param string $meta_key Metadata key. |
68 * @param mixed $meta_value Meta value. Must be serializable if non-scalar. |
68 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
69 * @param bool $unique Whether the specified meta key should be unique |
69 * @param bool $unique Whether the specified meta key should be unique for the object. |
70 * for the object. Optional. Default false. |
|
71 */ |
70 */ |
72 $check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique ); |
71 $check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique ); |
73 if ( null !== $check ) { |
72 if ( null !== $check ) { |
74 return $check; |
73 return $check; |
75 } |
74 } |
119 wp_cache_delete( $object_id, $meta_type . '_meta' ); |
118 wp_cache_delete( $object_id, $meta_type . '_meta' ); |
120 |
119 |
121 /** |
120 /** |
122 * Fires immediately after meta of a specific type is added. |
121 * Fires immediately after meta of a specific type is added. |
123 * |
122 * |
124 * The dynamic portion of the hook, `$meta_type`, refers to the meta |
123 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type |
125 * object type (comment, post, term, or user). |
124 * (post, comment, term, user, or any other type with an associated meta table). |
126 * |
125 * |
127 * @since 2.9.0 |
126 * @since 2.9.0 |
128 * |
127 * |
129 * @param int $mid The meta ID after successful update. |
128 * @param int $mid The meta ID after successful update. |
130 * @param int $object_id Object ID. |
129 * @param int $object_id ID of the object metadata is for. |
131 * @param string $meta_key Meta key. |
130 * @param string $meta_key Metadata key. |
132 * @param mixed $_meta_value Meta value. |
131 * @param mixed $_meta_value Metadata value. Serialized if non-scalar. |
133 */ |
132 */ |
134 do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value ); |
133 do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value ); |
135 |
134 |
136 return $mid; |
135 return $mid; |
137 } |
136 } |
138 |
137 |
139 /** |
138 /** |
140 * Update metadata for the specified object. If no value already exists for the specified object |
139 * Updates metadata for the specified object. If no value already exists for the specified object |
141 * ID and metadata key, the metadata will be added. |
140 * ID and metadata key, the metadata will be added. |
142 * |
141 * |
143 * @since 2.9.0 |
142 * @since 2.9.0 |
144 * |
143 * |
145 * @global wpdb $wpdb WordPress database abstraction object. |
144 * @global wpdb $wpdb WordPress database abstraction object. |
146 * |
145 * |
147 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
146 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
148 * @param int $object_id ID of the object metadata is for |
147 * or any other object type with an associated meta table. |
149 * @param string $meta_key Metadata key |
148 * @param int $object_id ID of the object metadata is for. |
|
149 * @param string $meta_key Metadata key. |
150 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
150 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
151 * @param mixed $prev_value Optional. If specified, only update existing metadata entries with |
151 * @param mixed $prev_value Optional. Previous value to check before updating. |
152 * the specified value. Otherwise, update all entries. |
152 * If specified, only update existing metadata entries with |
153 * @return int|bool The new meta field ID if a field with the given key didn't exist and was |
153 * this value. Otherwise, update all entries. Default empty. |
154 * therefore added, true on successful update, false on failure. |
154 * @return int|bool The new meta field ID if a field with the given key didn't exist |
|
155 * and was therefore added, true on successful update, |
|
156 * false on failure or if the value passed to the function |
|
157 * is the same as the one that is already in the database. |
155 */ |
158 */ |
156 function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_value = '' ) { |
159 function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_value = '' ) { |
157 global $wpdb; |
160 global $wpdb; |
158 |
161 |
159 if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) { |
162 if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) { |
171 } |
174 } |
172 |
175 |
173 $meta_subtype = get_object_subtype( $meta_type, $object_id ); |
176 $meta_subtype = get_object_subtype( $meta_type, $object_id ); |
174 |
177 |
175 $column = sanitize_key( $meta_type . '_id' ); |
178 $column = sanitize_key( $meta_type . '_id' ); |
176 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
179 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; |
177 |
180 |
178 // expected_slashed ($meta_key) |
181 // expected_slashed ($meta_key) |
179 $raw_meta_key = $meta_key; |
182 $raw_meta_key = $meta_key; |
180 $meta_key = wp_unslash( $meta_key ); |
183 $meta_key = wp_unslash( $meta_key ); |
181 $passed_value = $meta_value; |
184 $passed_value = $meta_value; |
182 $meta_value = wp_unslash( $meta_value ); |
185 $meta_value = wp_unslash( $meta_value ); |
183 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); |
186 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); |
184 |
187 |
185 /** |
188 /** |
186 * Filters whether to update metadata of a specific type. |
189 * Short-circuits updating metadata of a specific type. |
187 * |
190 * |
188 * The dynamic portion of the hook, `$meta_type`, refers to the meta |
191 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type |
189 * object type (comment, post, term, or user). Returning a non-null value |
192 * (post, comment, term, user, or any other type with an associated meta table). |
190 * will effectively short-circuit the function. |
193 * Returning a non-null value will effectively short-circuit the function. |
191 * |
194 * |
192 * @since 3.1.0 |
195 * @since 3.1.0 |
193 * |
196 * |
194 * @param null|bool $check Whether to allow updating metadata for the given type. |
197 * @param null|bool $check Whether to allow updating metadata for the given type. |
195 * @param int $object_id Object ID. |
198 * @param int $object_id ID of the object metadata is for. |
196 * @param string $meta_key Meta key. |
199 * @param string $meta_key Metadata key. |
197 * @param mixed $meta_value Meta value. Must be serializable if non-scalar. |
200 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
198 * @param mixed $prev_value Optional. If specified, only update existing |
201 * @param mixed $prev_value Optional. Previous value to check before updating. |
199 * metadata entries with the specified value. |
202 * If specified, only update existing metadata entries with |
200 * Otherwise, update all entries. |
203 * this value. Otherwise, update all entries. |
201 */ |
204 */ |
202 $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value ); |
205 $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value ); |
203 if ( null !== $check ) { |
206 if ( null !== $check ) { |
204 return (bool) $check; |
207 return (bool) $check; |
205 } |
208 } |
206 |
209 |
207 // Compare existing value to new value if no prev value given and the key exists only once. |
210 // Compare existing value to new value if no prev value given and the key exists only once. |
208 if ( empty( $prev_value ) ) { |
211 if ( empty( $prev_value ) ) { |
209 $old_value = get_metadata( $meta_type, $object_id, $meta_key ); |
212 $old_value = get_metadata_raw( $meta_type, $object_id, $meta_key ); |
210 if ( count( $old_value ) == 1 ) { |
213 if ( is_countable( $old_value ) && count( $old_value ) === 1 ) { |
211 if ( $old_value[0] === $meta_value ) { |
214 if ( $old_value[0] === $meta_value ) { |
212 return false; |
215 return false; |
213 } |
216 } |
214 } |
217 } |
215 } |
218 } |
235 |
238 |
236 foreach ( $meta_ids as $meta_id ) { |
239 foreach ( $meta_ids as $meta_id ) { |
237 /** |
240 /** |
238 * Fires immediately before updating metadata of a specific type. |
241 * Fires immediately before updating metadata of a specific type. |
239 * |
242 * |
240 * The dynamic portion of the hook, `$meta_type`, refers to the meta |
243 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type |
241 * object type (comment, post, term, or user). |
244 * (post, comment, term, user, or any other type with an associated meta table). |
242 * |
245 * |
243 * @since 2.9.0 |
246 * @since 2.9.0 |
244 * |
247 * |
245 * @param int $meta_id ID of the metadata entry to update. |
248 * @param int $meta_id ID of the metadata entry to update. |
246 * @param int $object_id Object ID. |
249 * @param int $object_id ID of the object metadata is for. |
247 * @param string $meta_key Meta key. |
250 * @param string $meta_key Metadata key. |
248 * @param mixed $_meta_value Meta value. |
251 * @param mixed $_meta_value Metadata value. Serialized if non-scalar. |
249 */ |
252 */ |
250 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); |
253 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); |
251 |
254 |
252 if ( 'post' == $meta_type ) { |
255 if ( 'post' === $meta_type ) { |
253 /** |
256 /** |
254 * Fires immediately before updating a post's metadata. |
257 * Fires immediately before updating a post's metadata. |
255 * |
258 * |
256 * @since 2.9.0 |
259 * @since 2.9.0 |
257 * |
260 * |
258 * @param int $meta_id ID of metadata entry to update. |
261 * @param int $meta_id ID of metadata entry to update. |
259 * @param int $object_id Post ID. |
262 * @param int $object_id Post ID. |
260 * @param string $meta_key Meta key. |
263 * @param string $meta_key Metadata key. |
261 * @param mixed $meta_value Meta value. This will be a PHP-serialized string representation of the value if |
264 * @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value |
262 * the value is an array, an object, or itself a PHP-serialized string. |
265 * if the value is an array, an object, or itself a PHP-serialized string. |
263 */ |
266 */ |
264 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); |
267 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); |
265 } |
268 } |
266 } |
269 } |
267 |
270 |
274 |
277 |
275 foreach ( $meta_ids as $meta_id ) { |
278 foreach ( $meta_ids as $meta_id ) { |
276 /** |
279 /** |
277 * Fires immediately after updating metadata of a specific type. |
280 * Fires immediately after updating metadata of a specific type. |
278 * |
281 * |
279 * The dynamic portion of the hook, `$meta_type`, refers to the meta |
282 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type |
280 * object type (comment, post, term, or user). |
283 * (post, comment, term, user, or any other type with an associated meta table). |
281 * |
284 * |
282 * @since 2.9.0 |
285 * @since 2.9.0 |
283 * |
286 * |
284 * @param int $meta_id ID of updated metadata entry. |
287 * @param int $meta_id ID of updated metadata entry. |
285 * @param int $object_id Object ID. |
288 * @param int $object_id ID of the object metadata is for. |
286 * @param string $meta_key Meta key. |
289 * @param string $meta_key Metadata key. |
287 * @param mixed $_meta_value Meta value. |
290 * @param mixed $_meta_value Metadata value. Serialized if non-scalar. |
288 */ |
291 */ |
289 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); |
292 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); |
290 |
293 |
291 if ( 'post' == $meta_type ) { |
294 if ( 'post' === $meta_type ) { |
292 /** |
295 /** |
293 * Fires immediately after updating a post's metadata. |
296 * Fires immediately after updating a post's metadata. |
294 * |
297 * |
295 * @since 2.9.0 |
298 * @since 2.9.0 |
296 * |
299 * |
297 * @param int $meta_id ID of updated metadata entry. |
300 * @param int $meta_id ID of updated metadata entry. |
298 * @param int $object_id Post ID. |
301 * @param int $object_id Post ID. |
299 * @param string $meta_key Meta key. |
302 * @param string $meta_key Metadata key. |
300 * @param mixed $meta_value Meta value. This will be a PHP-serialized string representation of the value if |
303 * @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value |
301 * the value is an array, an object, or itself a PHP-serialized string. |
304 * if the value is an array, an object, or itself a PHP-serialized string. |
302 */ |
305 */ |
303 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); |
306 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); |
304 } |
307 } |
305 } |
308 } |
306 |
309 |
307 return true; |
310 return true; |
308 } |
311 } |
309 |
312 |
310 /** |
313 /** |
311 * Delete metadata for the specified object. |
314 * Deletes metadata for the specified object. |
312 * |
315 * |
313 * @since 2.9.0 |
316 * @since 2.9.0 |
314 * |
317 * |
315 * @global wpdb $wpdb WordPress database abstraction object. |
318 * @global wpdb $wpdb WordPress database abstraction object. |
316 * |
319 * |
317 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
320 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
318 * @param int $object_id ID of the object metadata is for |
321 * or any other object type with an associated meta table. |
319 * @param string $meta_key Metadata key |
322 * @param int $object_id ID of the object metadata is for. |
320 * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete |
323 * @param string $meta_key Metadata key. |
321 * metadata entries with this value. Otherwise, delete all entries with the specified meta_key. |
324 * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. |
322 * Pass `null`, `false`, or an empty string to skip this check. (For backward compatibility, |
325 * If specified, only delete metadata entries with this value. |
323 * it is not possible to pass an empty string to delete those entries with an empty string |
326 * Otherwise, delete all entries with the specified meta_key. |
324 * for a value.) |
327 * Pass `null`, `false`, or an empty string to skip this check. |
325 * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries for all objects, |
328 * (For backward compatibility, it is not possible to pass an empty string |
326 * ignoring the specified object_id. Otherwise, only delete matching metadata entries for |
329 * to delete those entries with an empty string for a value.) |
327 * the specified object_id. |
330 * @param bool $delete_all Optional. If true, delete matching metadata entries for all objects, |
|
331 * ignoring the specified object_id. Otherwise, only delete |
|
332 * matching metadata entries for the specified object_id. Default false. |
328 * @return bool True on successful delete, false on failure. |
333 * @return bool True on successful delete, false on failure. |
329 */ |
334 */ |
330 function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) { |
335 function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) { |
331 global $wpdb; |
336 global $wpdb; |
332 |
337 |
343 if ( ! $table ) { |
348 if ( ! $table ) { |
344 return false; |
349 return false; |
345 } |
350 } |
346 |
351 |
347 $type_column = sanitize_key( $meta_type . '_id' ); |
352 $type_column = sanitize_key( $meta_type . '_id' ); |
348 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
353 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; |
|
354 |
349 // expected_slashed ($meta_key) |
355 // expected_slashed ($meta_key) |
350 $meta_key = wp_unslash( $meta_key ); |
356 $meta_key = wp_unslash( $meta_key ); |
351 $meta_value = wp_unslash( $meta_value ); |
357 $meta_value = wp_unslash( $meta_value ); |
352 |
358 |
353 /** |
359 /** |
354 * Filters whether to delete metadata of a specific type. |
360 * Short-circuits deleting metadata of a specific type. |
355 * |
361 * |
356 * The dynamic portion of the hook, `$meta_type`, refers to the meta |
362 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type |
357 * object type (comment, post, term, or user). Returning a non-null value |
363 * (post, comment, term, user, or any other type with an associated meta table). |
358 * will effectively short-circuit the function. |
364 * Returning a non-null value will effectively short-circuit the function. |
359 * |
365 * |
360 * @since 3.1.0 |
366 * @since 3.1.0 |
361 * |
367 * |
362 * @param null|bool $delete Whether to allow metadata deletion of the given type. |
368 * @param null|bool $delete Whether to allow metadata deletion of the given type. |
363 * @param int $object_id Object ID. |
369 * @param int $object_id ID of the object metadata is for. |
364 * @param string $meta_key Meta key. |
370 * @param string $meta_key Metadata key. |
365 * @param mixed $meta_value Meta value. Must be serializable if non-scalar. |
371 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
366 * @param bool $delete_all Whether to delete the matching metadata entries |
372 * @param bool $delete_all Whether to delete the matching metadata entries |
367 * for all objects, ignoring the specified $object_id. |
373 * for all objects, ignoring the specified $object_id. |
368 * Default false. |
374 * Default false. |
369 */ |
375 */ |
370 $check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all ); |
376 $check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all ); |
399 } |
405 } |
400 |
406 |
401 /** |
407 /** |
402 * Fires immediately before deleting metadata of a specific type. |
408 * Fires immediately before deleting metadata of a specific type. |
403 * |
409 * |
404 * The dynamic portion of the hook, `$meta_type`, refers to the meta |
410 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type |
405 * object type (comment, post, term, or user). |
411 * (post, comment, term, user, or any other type with an associated meta table). |
406 * |
412 * |
407 * @since 3.1.0 |
413 * @since 3.1.0 |
408 * |
414 * |
409 * @param array $meta_ids An array of metadata entry IDs to delete. |
415 * @param string[] $meta_ids An array of metadata entry IDs to delete. |
410 * @param int $object_id Object ID. |
416 * @param int $object_id ID of the object metadata is for. |
411 * @param string $meta_key Meta key. |
417 * @param string $meta_key Metadata key. |
412 * @param mixed $_meta_value Meta value. |
418 * @param mixed $_meta_value Metadata value. Serialized if non-scalar. |
413 */ |
419 */ |
414 do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); |
420 do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); |
415 |
421 |
416 // Old-style action. |
422 // Old-style action. |
417 if ( 'post' == $meta_type ) { |
423 if ( 'post' === $meta_type ) { |
418 /** |
424 /** |
419 * Fires immediately before deleting metadata for a post. |
425 * Fires immediately before deleting metadata for a post. |
420 * |
426 * |
421 * @since 2.9.0 |
427 * @since 2.9.0 |
422 * |
428 * |
423 * @param array $meta_ids An array of post metadata entry IDs to delete. |
429 * @param string[] $meta_ids An array of metadata entry IDs to delete. |
424 */ |
430 */ |
425 do_action( 'delete_postmeta', $meta_ids ); |
431 do_action( 'delete_postmeta', $meta_ids ); |
426 } |
432 } |
427 |
433 |
428 $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . ' )'; |
434 $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . ' )'; |
442 } |
448 } |
443 |
449 |
444 /** |
450 /** |
445 * Fires immediately after deleting metadata of a specific type. |
451 * Fires immediately after deleting metadata of a specific type. |
446 * |
452 * |
447 * The dynamic portion of the hook name, `$meta_type`, refers to the meta |
453 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type |
448 * object type (comment, post, term, or user). |
454 * (post, comment, term, user, or any other type with an associated meta table). |
449 * |
455 * |
450 * @since 2.9.0 |
456 * @since 2.9.0 |
451 * |
457 * |
452 * @param array $meta_ids An array of deleted metadata entry IDs. |
458 * @param string[] $meta_ids An array of metadata entry IDs to delete. |
453 * @param int $object_id Object ID. |
459 * @param int $object_id ID of the object metadata is for. |
454 * @param string $meta_key Meta key. |
460 * @param string $meta_key Metadata key. |
455 * @param mixed $_meta_value Meta value. |
461 * @param mixed $_meta_value Metadata value. Serialized if non-scalar. |
456 */ |
462 */ |
457 do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); |
463 do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); |
458 |
464 |
459 // Old-style action. |
465 // Old-style action. |
460 if ( 'post' == $meta_type ) { |
466 if ( 'post' === $meta_type ) { |
461 /** |
467 /** |
462 * Fires immediately after deleting metadata for a post. |
468 * Fires immediately after deleting metadata for a post. |
463 * |
469 * |
464 * @since 2.9.0 |
470 * @since 2.9.0 |
465 * |
471 * |
466 * @param array $meta_ids An array of deleted post metadata entry IDs. |
472 * @param string[] $meta_ids An array of metadata entry IDs to delete. |
467 */ |
473 */ |
468 do_action( 'deleted_postmeta', $meta_ids ); |
474 do_action( 'deleted_postmeta', $meta_ids ); |
469 } |
475 } |
470 |
476 |
471 return true; |
477 return true; |
472 } |
478 } |
473 |
479 |
474 /** |
480 /** |
475 * Retrieve metadata for the specified object. |
481 * Retrieves the value of a metadata field for the specified object type and ID. |
|
482 * |
|
483 * If the meta field exists, a single value is returned if `$single` is true, |
|
484 * or an array of values if it's false. |
|
485 * |
|
486 * If the meta field does not exist, the result depends on get_metadata_default(). |
|
487 * By default, an empty string is returned if `$single` is true, or an empty array |
|
488 * if it's false. |
476 * |
489 * |
477 * @since 2.9.0 |
490 * @since 2.9.0 |
478 * |
491 * |
479 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
492 * @see get_metadata_raw() |
480 * @param int $object_id ID of the object metadata is for |
493 * @see get_metadata_default() |
|
494 * |
|
495 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
|
496 * or any other object type with an associated meta table. |
|
497 * @param int $object_id ID of the object metadata is for. |
481 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for |
498 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for |
482 * the specified object. |
499 * the specified object. Default empty. |
483 * @param bool $single Optional, default is false. |
500 * @param bool $single Optional. If true, return only the first value of the specified meta_key. |
484 * If true, return only the first value of the specified meta_key. |
501 * This parameter has no effect if meta_key is not specified. Default false. |
485 * This parameter has no effect if meta_key is not specified. |
502 * @return mixed Single metadata value, or array of values. |
486 * @return mixed Single metadata value, or array of values |
503 * False if there's a problem with the parameters passed to the function. |
487 */ |
504 */ |
488 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) { |
505 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) { |
|
506 $value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single ); |
|
507 if ( ! is_null( $value ) ) { |
|
508 return $value; |
|
509 } |
|
510 |
|
511 return get_metadata_default( $meta_type, $object_id, $meta_key, $single ); |
|
512 } |
|
513 |
|
514 /** |
|
515 * Retrieves raw metadata value for the specified object. |
|
516 * |
|
517 * @since 5.5.0 |
|
518 * |
|
519 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
|
520 * or any other object type with an associated meta table. |
|
521 * @param int $object_id ID of the object metadata is for. |
|
522 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for |
|
523 * the specified object. Default empty. |
|
524 * @param bool $single Optional. If true, return only the first value of the specified meta_key. |
|
525 * This parameter has no effect if meta_key is not specified. Default false. |
|
526 * @return mixed Single metadata value, or array of values. Null if the value does not exist. |
|
527 * False if there's a problem with the parameters passed to the function. |
|
528 */ |
|
529 function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) { |
489 if ( ! $meta_type || ! is_numeric( $object_id ) ) { |
530 if ( ! $meta_type || ! is_numeric( $object_id ) ) { |
490 return false; |
531 return false; |
491 } |
532 } |
492 |
533 |
493 $object_id = absint( $object_id ); |
534 $object_id = absint( $object_id ); |
494 if ( ! $object_id ) { |
535 if ( ! $object_id ) { |
495 return false; |
536 return false; |
496 } |
537 } |
497 |
538 |
498 /** |
539 /** |
499 * Filters whether to retrieve metadata of a specific type. |
540 * Short-circuits the return value of a meta field. |
500 * |
541 * |
501 * The dynamic portion of the hook, `$meta_type`, refers to the meta |
542 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type |
502 * object type (comment, post, term, or user). Returning a non-null value |
543 * (post, comment, term, user, or any other type with an associated meta table). |
503 * will effectively short-circuit the function. |
544 * Returning a non-null value will effectively short-circuit the function. |
|
545 * |
|
546 * Possible filter names include: |
|
547 * |
|
548 * - `get_post_metadata` |
|
549 * - `get_comment_metadata` |
|
550 * - `get_term_metadata` |
|
551 * - `get_user_metadata` |
504 * |
552 * |
505 * @since 3.1.0 |
553 * @since 3.1.0 |
506 * |
554 * @since 5.5.0 Added the `$meta_type` parameter. |
507 * @param null|array|string $value The value get_metadata() should return - a single metadata value, |
555 * |
508 * or an array of values. |
556 * @param mixed $value The value to return, either a single metadata value or an array |
509 * @param int $object_id Object ID. |
557 * of values depending on the value of `$single`. Default null. |
510 * @param string $meta_key Meta key. |
558 * @param int $object_id ID of the object metadata is for. |
511 * @param bool $single Whether to return only the first value of the specified $meta_key. |
559 * @param string $meta_key Metadata key. |
|
560 * @param bool $single Whether to return only the first value of the specified `$meta_key`. |
|
561 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
|
562 * or any other object type with an associated meta table. |
512 */ |
563 */ |
513 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single ); |
564 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single, $meta_type ); |
514 if ( null !== $check ) { |
565 if ( null !== $check ) { |
515 if ( $single && is_array( $check ) ) { |
566 if ( $single && is_array( $check ) ) { |
516 return $check[0]; |
567 return $check[0]; |
517 } else { |
568 } else { |
518 return $check; |
569 return $check; |
536 } else { |
591 } else { |
537 return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] ); |
592 return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] ); |
538 } |
593 } |
539 } |
594 } |
540 |
595 |
|
596 return null; |
|
597 } |
|
598 |
|
599 /** |
|
600 * Retrieves default metadata value for the specified meta key and object. |
|
601 * |
|
602 * By default, an empty string is returned if `$single` is true, or an empty array |
|
603 * if it's false. |
|
604 * |
|
605 * @since 5.5.0 |
|
606 * |
|
607 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
|
608 * or any other object type with an associated meta table. |
|
609 * @param int $object_id ID of the object metadata is for. |
|
610 * @param string $meta_key Metadata key. |
|
611 * @param bool $single Optional. If true, return only the first value of the specified meta_key. |
|
612 * This parameter has no effect if meta_key is not specified. Default false. |
|
613 * @return mixed Single metadata value, or array of values. |
|
614 */ |
|
615 function get_metadata_default( $meta_type, $object_id, $meta_key, $single = false ) { |
541 if ( $single ) { |
616 if ( $single ) { |
542 return ''; |
617 $value = ''; |
543 } else { |
618 } else { |
544 return array(); |
619 $value = array(); |
545 } |
620 } |
546 } |
621 |
547 |
622 /** |
548 /** |
623 * Filters the default metadata value for a specified meta key and object. |
549 * Determine if a meta key is set for a given object |
624 * |
|
625 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type |
|
626 * (post, comment, term, user, or any other type with an associated meta table). |
|
627 * |
|
628 * Possible filter names include: |
|
629 * |
|
630 * - `default_post_metadata` |
|
631 * - `default_comment_metadata` |
|
632 * - `default_term_metadata` |
|
633 * - `default_user_metadata` |
|
634 * |
|
635 * @since 5.5.0 |
|
636 * |
|
637 * @param mixed $value The value to return, either a single metadata value or an array |
|
638 * of values depending on the value of `$single`. |
|
639 * @param int $object_id ID of the object metadata is for. |
|
640 * @param string $meta_key Metadata key. |
|
641 * @param bool $single Whether to return only the first value of the specified `$meta_key`. |
|
642 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
|
643 * or any other object type with an associated meta table. |
|
644 */ |
|
645 $value = apply_filters( "default_{$meta_type}_metadata", $value, $object_id, $meta_key, $single, $meta_type ); |
|
646 |
|
647 if ( ! $single && ! wp_is_numeric_array( $value ) ) { |
|
648 $value = array( $value ); |
|
649 } |
|
650 |
|
651 return $value; |
|
652 } |
|
653 |
|
654 /** |
|
655 * Determines if a meta field with the given key exists for the given object ID. |
550 * |
656 * |
551 * @since 3.3.0 |
657 * @since 3.3.0 |
552 * |
658 * |
553 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
659 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
554 * @param int $object_id ID of the object metadata is for |
660 * or any other object type with an associated meta table. |
|
661 * @param int $object_id ID of the object metadata is for. |
555 * @param string $meta_key Metadata key. |
662 * @param string $meta_key Metadata key. |
556 * @return bool True of the key is set, false if not. |
663 * @return bool Whether a meta field with the given key exists. |
557 */ |
664 */ |
558 function metadata_exists( $meta_type, $object_id, $meta_key ) { |
665 function metadata_exists( $meta_type, $object_id, $meta_key ) { |
559 if ( ! $meta_type || ! is_numeric( $object_id ) ) { |
666 if ( ! $meta_type || ! is_numeric( $object_id ) ) { |
560 return false; |
667 return false; |
561 } |
668 } |
584 |
691 |
585 return false; |
692 return false; |
586 } |
693 } |
587 |
694 |
588 /** |
695 /** |
589 * Get meta data by meta ID |
696 * Retrieves metadata by meta ID. |
590 * |
697 * |
591 * @since 3.3.0 |
698 * @since 3.3.0 |
592 * |
699 * |
593 * @global wpdb $wpdb WordPress database abstraction object. |
700 * @global wpdb $wpdb WordPress database abstraction object. |
594 * |
701 * |
595 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
702 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
596 * @param int $meta_id ID for a specific meta row |
703 * or any other object type with an associated meta table. |
597 * @return object|false Meta object or false. |
704 * @param int $meta_id ID for a specific meta row. |
|
705 * @return stdClass|false { |
|
706 * Metadata object, or boolean `false` if the metadata doesn't exist. |
|
707 * |
|
708 * @type string $meta_key The meta key. |
|
709 * @type mixed $meta_value The unserialized meta value. |
|
710 * @type string $meta_id Optional. The meta ID when the meta type is any value except 'user'. |
|
711 * @type string $umeta_id Optional. The meta ID when the meta type is 'user'. |
|
712 * @type string $post_id Optional. The object ID when the meta type is 'post'. |
|
713 * @type string $comment_id Optional. The object ID when the meta type is 'comment'. |
|
714 * @type string $term_id Optional. The object ID when the meta type is 'term'. |
|
715 * @type string $user_id Optional. The object ID when the meta type is 'user'. |
|
716 * } |
598 */ |
717 */ |
599 function get_metadata_by_mid( $meta_type, $meta_id ) { |
718 function get_metadata_by_mid( $meta_type, $meta_id ) { |
600 global $wpdb; |
719 global $wpdb; |
601 |
720 |
602 if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) { |
721 if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) { |
611 $table = _get_meta_table( $meta_type ); |
730 $table = _get_meta_table( $meta_type ); |
612 if ( ! $table ) { |
731 if ( ! $table ) { |
613 return false; |
732 return false; |
614 } |
733 } |
615 |
734 |
616 $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id'; |
|
617 |
|
618 /** |
735 /** |
619 * Filters whether to retrieve metadata of a specific type by meta ID. |
736 * Short-circuits the return value when fetching a meta field by meta ID. |
620 * |
737 * |
621 * The dynamic portion of the hook, `$meta_type`, refers to the meta |
738 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type |
622 * object type (comment, post, term, or user). Returning a non-null value |
739 * (post, comment, term, user, or any other type with an associated meta table). |
623 * will effectively short-circuit the function. |
740 * Returning a non-null value will effectively short-circuit the function. |
624 * |
741 * |
625 * @since 5.0.0 |
742 * @since 5.0.0 |
626 * |
743 * |
627 * @param mixed $value The value get_metadata_by_mid() should return. |
744 * @param stdClass|null $value The value to return. |
628 * @param int $meta_id Meta ID. |
745 * @param int $meta_id Meta ID. |
629 */ |
746 */ |
630 $check = apply_filters( "get_{$meta_type}_metadata_by_mid", null, $meta_id ); |
747 $check = apply_filters( "get_{$meta_type}_metadata_by_mid", null, $meta_id ); |
631 if ( null !== $check ) { |
748 if ( null !== $check ) { |
632 return $check; |
749 return $check; |
633 } |
750 } |
634 |
751 |
|
752 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; |
|
753 |
635 $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) ); |
754 $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) ); |
636 |
755 |
637 if ( empty( $meta ) ) { |
756 if ( empty( $meta ) ) { |
638 return false; |
757 return false; |
639 } |
758 } |
830 * @param int $meta_id ID of the metadata entry to delete. |
953 * @param int $meta_id ID of the metadata entry to delete. |
831 */ |
954 */ |
832 do_action( "delete_{$meta_type}meta", $meta_id ); |
955 do_action( "delete_{$meta_type}meta", $meta_id ); |
833 } |
956 } |
834 |
957 |
835 // Run the query, will return true if deleted, false otherwise |
958 // Run the query, will return true if deleted, false otherwise. |
836 $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) ); |
959 $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) ); |
837 |
960 |
838 // Clear the caches. |
961 // Clear the caches. |
839 wp_cache_delete( $object_id, $meta_type . '_meta' ); |
962 wp_cache_delete( $object_id, $meta_type . '_meta' ); |
840 |
963 |
841 /** This action is documented in wp-includes/meta.php */ |
964 /** This action is documented in wp-includes/meta.php */ |
842 do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); |
965 do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); |
843 |
966 |
844 // Old-style action. |
967 // Old-style action. |
845 if ( 'post' == $meta_type || 'comment' == $meta_type ) { |
968 if ( 'post' === $meta_type || 'comment' === $meta_type ) { |
846 /** |
969 /** |
847 * Fires immediately after deleting post or comment metadata of a specific type. |
970 * Fires immediately after deleting post or comment metadata of a specific type. |
848 * |
971 * |
849 * The dynamic portion of the hook, `$meta_type`, refers to the meta |
972 * The dynamic portion of the hook, `$meta_type`, refers to the meta |
850 * object type (post or comment). |
973 * object type (post or comment). |
895 } |
1019 } |
896 |
1020 |
897 $object_ids = array_map( 'intval', $object_ids ); |
1021 $object_ids = array_map( 'intval', $object_ids ); |
898 |
1022 |
899 /** |
1023 /** |
900 * Filters whether to update the metadata cache of a specific type. |
1024 * Short-circuits updating the metadata cache of a specific type. |
901 * |
1025 * |
902 * The dynamic portion of the hook, `$meta_type`, refers to the meta |
1026 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type |
903 * object type (comment, post, term, or user). Returning a non-null value |
1027 * (post, comment, term, user, or any other type with an associated meta table). |
904 * will effectively short-circuit the function. |
1028 * Returning a non-null value will effectively short-circuit the function. |
905 * |
1029 * |
906 * @since 5.0.0 |
1030 * @since 5.0.0 |
907 * |
1031 * |
908 * @param mixed $check Whether to allow updating the meta cache of the given type. |
1032 * @param mixed $check Whether to allow updating the meta cache of the given type. |
909 * @param array $object_ids Array of object IDs to update the meta cache for. |
1033 * @param int[] $object_ids Array of object IDs to update the meta cache for. |
910 */ |
1034 */ |
911 $check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids ); |
1035 $check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids ); |
912 if ( null !== $check ) { |
1036 if ( null !== $check ) { |
913 return (bool) $check; |
1037 return (bool) $check; |
914 } |
1038 } |
915 |
1039 |
916 $cache_key = $meta_type . '_meta'; |
1040 $cache_key = $meta_type . '_meta'; |
917 $ids = array(); |
1041 $non_cached_ids = array(); |
918 $cache = array(); |
1042 $cache = array(); |
919 foreach ( $object_ids as $id ) { |
1043 $cache_values = wp_cache_get_multiple( $object_ids, $cache_key ); |
920 $cached_object = wp_cache_get( $id, $cache_key ); |
1044 |
|
1045 foreach ( $cache_values as $id => $cached_object ) { |
921 if ( false === $cached_object ) { |
1046 if ( false === $cached_object ) { |
922 $ids[] = $id; |
1047 $non_cached_ids[] = $id; |
923 } else { |
1048 } else { |
924 $cache[ $id ] = $cached_object; |
1049 $cache[ $id ] = $cached_object; |
925 } |
1050 } |
926 } |
1051 } |
927 |
1052 |
928 if ( empty( $ids ) ) { |
1053 if ( empty( $non_cached_ids ) ) { |
929 return $cache; |
1054 return $cache; |
930 } |
1055 } |
931 |
1056 |
932 // Get meta info |
1057 // Get meta info. |
933 $id_list = join( ',', $ids ); |
1058 $id_list = join( ',', $non_cached_ids ); |
934 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
1059 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; |
|
1060 |
935 $meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A ); |
1061 $meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A ); |
936 |
1062 |
937 if ( ! empty( $meta_list ) ) { |
1063 if ( ! empty( $meta_list ) ) { |
938 foreach ( $meta_list as $metarow ) { |
1064 foreach ( $meta_list as $metarow ) { |
939 $mpid = intval( $metarow[ $column ] ); |
1065 $mpid = intval( $metarow[ $column ] ); |
940 $mkey = $metarow['meta_key']; |
1066 $mkey = $metarow['meta_key']; |
941 $mval = $metarow['meta_value']; |
1067 $mval = $metarow['meta_value']; |
942 |
1068 |
943 // Force subkeys to be array type: |
1069 // Force subkeys to be array type. |
944 if ( ! isset( $cache[ $mpid ] ) || ! is_array( $cache[ $mpid ] ) ) { |
1070 if ( ! isset( $cache[ $mpid ] ) || ! is_array( $cache[ $mpid ] ) ) { |
945 $cache[ $mpid ] = array(); |
1071 $cache[ $mpid ] = array(); |
946 } |
1072 } |
947 if ( ! isset( $cache[ $mpid ][ $mkey ] ) || ! is_array( $cache[ $mpid ][ $mkey ] ) ) { |
1073 if ( ! isset( $cache[ $mpid ][ $mkey ] ) || ! is_array( $cache[ $mpid ][ $mkey ] ) ) { |
948 $cache[ $mpid ][ $mkey ] = array(); |
1074 $cache[ $mpid ][ $mkey ] = array(); |
949 } |
1075 } |
950 |
1076 |
951 // Add a value to the current pid/key: |
1077 // Add a value to the current pid/key. |
952 $cache[ $mpid ][ $mkey ][] = $mval; |
1078 $cache[ $mpid ][ $mkey ][] = $mval; |
953 } |
1079 } |
954 } |
1080 } |
955 |
1081 |
956 foreach ( $ids as $id ) { |
1082 foreach ( $non_cached_ids as $id ) { |
957 if ( ! isset( $cache[ $id ] ) ) { |
1083 if ( ! isset( $cache[ $id ] ) ) { |
958 $cache[ $id ] = array(); |
1084 $cache[ $id ] = array(); |
959 } |
1085 } |
960 wp_cache_add( $id, $cache[ $id ], $cache_key ); |
1086 wp_cache_add( $id, $cache[ $id ], $cache_key ); |
961 } |
1087 } |
1024 /** |
1151 /** |
1025 * Determines whether a meta key is considered protected. |
1152 * Determines whether a meta key is considered protected. |
1026 * |
1153 * |
1027 * @since 3.1.3 |
1154 * @since 3.1.3 |
1028 * |
1155 * |
1029 * @param string $meta_key Meta key. |
1156 * @param string $meta_key Metadata key. |
1030 * @param string|null $meta_type Optional. Type of object metadata is for (e.g., comment, post, term, or user). |
1157 * @param string $meta_type Optional. Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
|
1158 * or any other object type with an associated meta table. Default empty. |
1031 * @return bool Whether the meta key is considered protected. |
1159 * @return bool Whether the meta key is considered protected. |
1032 */ |
1160 */ |
1033 function is_protected_meta( $meta_key, $meta_type = null ) { |
1161 function is_protected_meta( $meta_key, $meta_type = '' ) { |
1034 $protected = ( '_' == $meta_key[0] ); |
1162 $protected = ( '_' === $meta_key[0] ); |
1035 |
1163 |
1036 /** |
1164 /** |
1037 * Filters whether a meta key is considered protected. |
1165 * Filters whether a meta key is considered protected. |
1038 * |
1166 * |
1039 * @since 3.2.0 |
1167 * @since 3.2.0 |
1040 * |
1168 * |
1041 * @param bool $protected Whether the key is considered protected. |
1169 * @param bool $protected Whether the key is considered protected. |
1042 * @param string $meta_key Meta key. |
1170 * @param string $meta_key Metadata key. |
1043 * @param string|null $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
1171 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
|
1172 * or any other object type with an associated meta table. |
1044 */ |
1173 */ |
1045 return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type ); |
1174 return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type ); |
1046 } |
1175 } |
1047 |
1176 |
1048 /** |
1177 /** |
1049 * Sanitize meta value. |
1178 * Sanitizes meta value. |
1050 * |
1179 * |
1051 * @since 3.1.3 |
1180 * @since 3.1.3 |
1052 * @since 4.9.8 The `$object_subtype` parameter was added. |
1181 * @since 4.9.8 The `$object_subtype` parameter was added. |
1053 * |
1182 * |
1054 * @param string $meta_key Meta key. |
1183 * @param string $meta_key Metadata key. |
1055 * @param mixed $meta_value Meta value to sanitize. |
1184 * @param mixed $meta_value Metadata value to sanitize. |
1056 * @param string $object_type Type of object the meta is registered to. |
1185 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
|
1186 * or any other object type with an associated meta table. |
1057 * @param string $object_subtype Optional. The subtype of the object type. |
1187 * @param string $object_subtype Optional. The subtype of the object type. |
1058 * |
|
1059 * @return mixed Sanitized $meta_value. |
1188 * @return mixed Sanitized $meta_value. |
1060 */ |
1189 */ |
1061 function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) { |
1190 function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) { |
1062 if ( ! empty( $object_subtype ) && has_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) { |
1191 if ( ! empty( $object_subtype ) && has_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) { |
1063 |
1192 |
1107 * @since 3.3.0 |
1238 * @since 3.3.0 |
1108 * @since 4.6.0 {@link https://core.trac.wordpress.org/ticket/35658 Modified |
1239 * @since 4.6.0 {@link https://core.trac.wordpress.org/ticket/35658 Modified |
1109 * to support an array of data to attach to registered meta keys}. Previous arguments for |
1240 * to support an array of data to attach to registered meta keys}. Previous arguments for |
1110 * `$sanitize_callback` and `$auth_callback` have been folded into this array. |
1241 * `$sanitize_callback` and `$auth_callback` have been folded into this array. |
1111 * @since 4.9.8 The `$object_subtype` argument was added to the arguments array. |
1242 * @since 4.9.8 The `$object_subtype` argument was added to the arguments array. |
1112 * |
1243 * @since 5.3.0 Valid meta types expanded to include "array" and "object". |
1113 * @param string $object_type Type of object this meta is registered to. |
1244 * @since 5.5.0 The `$default` argument was added to the arguments array. |
1114 * @param string $meta_key Meta key to register. |
1245 * |
1115 * @param array $args { |
1246 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
|
1247 * or any other object type with an associated meta table. |
|
1248 * @param string $meta_key Meta key to register. |
|
1249 * @param array $args { |
1116 * Data used to describe the meta key when registered. |
1250 * Data used to describe the meta key when registered. |
1117 * |
1251 * |
1118 * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type. If left empty, |
1252 * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type. If left empty, |
1119 * the meta key will be registered on the entire object type. Default empty. |
1253 * the meta key will be registered on the entire object type. Default empty. |
1120 * @type string $type The type of data associated with this meta key. |
1254 * @type string $type The type of data associated with this meta key. |
1121 * Valid values are 'string', 'boolean', 'integer', and 'number'. |
1255 * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'. |
1122 * @type string $description A description of the data attached to this meta key. |
1256 * @type string $description A description of the data attached to this meta key. |
1123 * @type bool $single Whether the meta key has one value per object, or an array of values per object. |
1257 * @type bool $single Whether the meta key has one value per object, or an array of values per object. |
1124 * @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data. |
1258 * @type mixed $default The default value returned from get_metadata() if no value has been set yet. |
1125 * @type string $auth_callback Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks. |
1259 * When using a non-single meta key, the default value is for the first entry. |
1126 * @type bool $show_in_rest Whether data associated with this meta key can be considered public. |
1260 * In other words, when calling get_metadata() with `$single` set to `false`, |
|
1261 * the default value given here will be wrapped in an array. |
|
1262 * @type callable $sanitize_callback A function or method to call when sanitizing `$meta_key` data. |
|
1263 * @type callable $auth_callback Optional. A function or method to call when performing edit_post_meta, |
|
1264 * add_post_meta, and delete_post_meta capability checks. |
|
1265 * @type bool|array $show_in_rest Whether data associated with this meta key can be considered public and |
|
1266 * should be accessible via the REST API. A custom post type must also declare |
|
1267 * support for custom fields for registered meta to be accessible via REST. |
|
1268 * When registering complex meta values this argument may optionally be an |
|
1269 * array with 'schema' or 'prepare_callback' keys instead of a boolean. |
1127 * } |
1270 * } |
1128 * @param string|array $deprecated Deprecated. Use `$args` instead. |
1271 * @param string|array $deprecated Deprecated. Use `$args` instead. |
1129 * |
|
1130 * @return bool True if the meta key was successfully registered in the global array, false if not. |
1272 * @return bool True if the meta key was successfully registered in the global array, false if not. |
1131 * Registering a meta key with distinct sanitize and auth callbacks will fire those |
1273 * Registering a meta key with distinct sanitize and auth callbacks will fire those callbacks, |
1132 * callbacks, but will not add to the global registry. |
1274 * but will not add to the global registry. |
1133 */ |
1275 */ |
1134 function register_meta( $object_type, $meta_key, $args, $deprecated = null ) { |
1276 function register_meta( $object_type, $meta_key, $args, $deprecated = null ) { |
1135 global $wp_meta_keys; |
1277 global $wp_meta_keys; |
1136 |
1278 |
1137 if ( ! is_array( $wp_meta_keys ) ) { |
1279 if ( ! is_array( $wp_meta_keys ) ) { |
1172 * |
1315 * |
1173 * @since 4.6.0 |
1316 * @since 4.6.0 |
1174 * |
1317 * |
1175 * @param array $args Array of meta registration arguments. |
1318 * @param array $args Array of meta registration arguments. |
1176 * @param array $defaults Array of default arguments. |
1319 * @param array $defaults Array of default arguments. |
1177 * @param string $object_type Object type. |
1320 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
|
1321 * or any other object type with an associated meta table. |
1178 * @param string $meta_key Meta key. |
1322 * @param string $meta_key Meta key. |
1179 */ |
1323 */ |
1180 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); |
1324 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); |
|
1325 unset( $defaults['default'] ); |
1181 $args = wp_parse_args( $args, $defaults ); |
1326 $args = wp_parse_args( $args, $defaults ); |
|
1327 |
|
1328 // Require an item schema when registering array meta. |
|
1329 if ( false !== $args['show_in_rest'] && 'array' === $args['type'] ) { |
|
1330 if ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) { |
|
1331 _doing_it_wrong( __FUNCTION__, __( 'When registering an "array" meta type to show in the REST API, you must specify the schema for each array item in "show_in_rest.schema.items".' ), '5.3.0' ); |
|
1332 |
|
1333 return false; |
|
1334 } |
|
1335 } |
1182 |
1336 |
1183 $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : ''; |
1337 $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : ''; |
1184 |
1338 |
1185 // If `auth_callback` is not provided, fall back to `is_protected_meta()`. |
1339 // If `auth_callback` is not provided, fall back to `is_protected_meta()`. |
1186 if ( empty( $args['auth_callback'] ) ) { |
1340 if ( empty( $args['auth_callback'] ) ) { |
1219 |
1391 |
1220 return false; |
1392 return false; |
1221 } |
1393 } |
1222 |
1394 |
1223 /** |
1395 /** |
|
1396 * Filters into default_{$object_type}_metadata and adds in default value. |
|
1397 * |
|
1398 * @since 5.5.0 |
|
1399 * |
|
1400 * @param mixed $value Current value passed to filter. |
|
1401 * @param int $object_id ID of the object metadata is for. |
|
1402 * @param string $meta_key Metadata key. |
|
1403 * @param bool $single If true, return only the first value of the specified meta_key. |
|
1404 * This parameter has no effect if meta_key is not specified. |
|
1405 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
|
1406 * or any other object type with an associated meta table. |
|
1407 * @return mixed Single metadata default, or array of defaults. |
|
1408 */ |
|
1409 function filter_default_metadata( $value, $object_id, $meta_key, $single, $meta_type ) { |
|
1410 global $wp_meta_keys; |
|
1411 |
|
1412 if ( wp_installing() ) { |
|
1413 return $value; |
|
1414 } |
|
1415 |
|
1416 if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $meta_type ] ) ) { |
|
1417 return $value; |
|
1418 } |
|
1419 |
|
1420 $defaults = array(); |
|
1421 foreach ( $wp_meta_keys[ $meta_type ] as $sub_type => $meta_data ) { |
|
1422 foreach ( $meta_data as $_meta_key => $args ) { |
|
1423 if ( $_meta_key === $meta_key && array_key_exists( 'default', $args ) ) { |
|
1424 $defaults[ $sub_type ] = $args; |
|
1425 } |
|
1426 } |
|
1427 } |
|
1428 |
|
1429 if ( ! $defaults ) { |
|
1430 return $value; |
|
1431 } |
|
1432 |
|
1433 // If this meta type does not have subtypes, then the default is keyed as an empty string. |
|
1434 if ( isset( $defaults[''] ) ) { |
|
1435 $metadata = $defaults['']; |
|
1436 } else { |
|
1437 $sub_type = get_object_subtype( $meta_type, $object_id ); |
|
1438 if ( ! isset( $defaults[ $sub_type ] ) ) { |
|
1439 return $value; |
|
1440 } |
|
1441 $metadata = $defaults[ $sub_type ]; |
|
1442 } |
|
1443 |
|
1444 if ( $single ) { |
|
1445 $value = $metadata['default']; |
|
1446 } else { |
|
1447 $value = array( $metadata['default'] ); |
|
1448 } |
|
1449 |
|
1450 return $value; |
|
1451 } |
|
1452 |
|
1453 /** |
1224 * Checks if a meta key is registered. |
1454 * Checks if a meta key is registered. |
1225 * |
1455 * |
1226 * @since 4.6.0 |
1456 * @since 4.6.0 |
1227 * @since 4.9.8 The `$object_subtype` parameter was added. |
1457 * @since 4.9.8 The `$object_subtype` parameter was added. |
1228 * |
1458 * |
1229 * @param string $object_type The type of object. |
1459 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
1230 * @param string $meta_key The meta key. |
1460 * or any other object type with an associated meta table. |
|
1461 * @param string $meta_key Metadata key. |
1231 * @param string $object_subtype Optional. The subtype of the object type. |
1462 * @param string $object_subtype Optional. The subtype of the object type. |
1232 * |
|
1233 * @return bool True if the meta key is registered to the object type and, if provided, |
1463 * @return bool True if the meta key is registered to the object type and, if provided, |
1234 * the object subtype. False if not. |
1464 * the object subtype. False if not. |
1235 */ |
1465 */ |
1236 function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) { |
1466 function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) { |
1237 $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); |
1467 $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); |
1355 |
1588 |
1356 return array_intersect_key( $data, $meta_keys ); |
1589 return array_intersect_key( $data, $meta_keys ); |
1357 } |
1590 } |
1358 |
1591 |
1359 /** |
1592 /** |
1360 * Filter out `register_meta()` args based on a whitelist. |
1593 * Filters out `register_meta()` args based on an allowed list. |
1361 * `register_meta()` args may change over time, so requiring the whitelist |
1594 * |
|
1595 * `register_meta()` args may change over time, so requiring the allowed list |
1362 * to be explicitly turned off is a warranty seal of sorts. |
1596 * to be explicitly turned off is a warranty seal of sorts. |
1363 * |
1597 * |
1364 * @access private |
1598 * @access private |
1365 * @since 4.6.0 |
1599 * @since 5.5.0 |
1366 * |
1600 * |
1367 * @param array $args Arguments from `register_meta()`. |
1601 * @param array $args Arguments from `register_meta()`. |
1368 * @param array $default_args Default arguments for `register_meta()`. |
1602 * @param array $default_args Default arguments for `register_meta()`. |
1369 * |
|
1370 * @return array Filtered arguments. |
1603 * @return array Filtered arguments. |
1371 */ |
1604 */ |
1372 function _wp_register_meta_args_whitelist( $args, $default_args ) { |
1605 function _wp_register_meta_args_allowed_list( $args, $default_args ) { |
1373 return array_intersect_key( $args, $default_args ); |
1606 return array_intersect_key( $args, $default_args ); |
1374 } |
1607 } |
1375 |
1608 |
1376 /** |
1609 /** |
1377 * Returns the object subtype for a given object ID of a specific type. |
1610 * Returns the object subtype for a given object ID of a specific type. |
1378 * |
1611 * |
1379 * @since 4.9.8 |
1612 * @since 4.9.8 |
1380 * |
1613 * |
1381 * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user) |
1614 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', |
|
1615 * or any other object type with an associated meta table. |
1382 * @param int $object_id ID of the object to retrieve its subtype. |
1616 * @param int $object_id ID of the object to retrieve its subtype. |
1383 * @return string The object subtype or an empty string if unspecified subtype. |
1617 * @return string The object subtype or an empty string if unspecified subtype. |
1384 */ |
1618 */ |
1385 function get_object_subtype( $object_type, $object_id ) { |
1619 function get_object_subtype( $object_type, $object_id ) { |
1386 $object_id = (int) $object_id; |
1620 $object_id = (int) $object_id; |