author | ymh <ymh.work@gmail.com> |
Tue, 22 Oct 2019 16:11:46 +0200 | |
changeset 15 | 3d4e9c994f10 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Core Metadata API |
0 | 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 |
*/ |
|
12 |
||
13 |
/** |
|
14 |
* Add metadata for the specified object. |
|
15 |
* |
|
16 |
* @since 2.9.0 |
|
5 | 17 |
* |
18 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 19 |
* |
9 | 20 |
* @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
* @param int $object_id ID of the object metadata is for |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
* @param string $meta_key Metadata key |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
24 |
* @param bool $unique Optional, default is false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
* Whether the specified metadata key should be unique for the object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
26 |
* If true, and the object already has a value for the specified metadata key, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
* no change will be made. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
* @return int|false The meta ID on success, false on failure. |
0 | 29 |
*/ |
9 | 30 |
function add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique = false ) { |
5 | 31 |
global $wpdb; |
0 | 32 |
|
5 | 33 |
if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) { |
0 | 34 |
return false; |
5 | 35 |
} |
0 | 36 |
|
5 | 37 |
$object_id = absint( $object_id ); |
38 |
if ( ! $object_id ) { |
|
0 | 39 |
return false; |
5 | 40 |
} |
0 | 41 |
|
5 | 42 |
$table = _get_meta_table( $meta_type ); |
43 |
if ( ! $table ) { |
|
44 |
return false; |
|
45 |
} |
|
0 | 46 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
47 |
$meta_subtype = get_object_subtype( $meta_type, $object_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
|
9 | 49 |
$column = sanitize_key( $meta_type . '_id' ); |
0 | 50 |
|
51 |
// expected_slashed ($meta_key) |
|
9 | 52 |
$meta_key = wp_unslash( $meta_key ); |
53 |
$meta_value = wp_unslash( $meta_value ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); |
0 | 55 |
|
5 | 56 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
* Filters whether to add metadata of a specific type. |
5 | 58 |
* |
59 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
9 | 60 |
* object type (comment, post, term, or user). Returning a non-null value |
5 | 61 |
* will effectively short-circuit the function. |
62 |
* |
|
63 |
* @since 3.1.0 |
|
64 |
* |
|
65 |
* @param null|bool $check Whether to allow adding metadata for the given type. |
|
66 |
* @param int $object_id Object ID. |
|
67 |
* @param string $meta_key Meta key. |
|
68 |
* @param mixed $meta_value Meta value. Must be serializable if non-scalar. |
|
69 |
* @param bool $unique Whether the specified meta key should be unique |
|
70 |
* for the object. Optional. Default false. |
|
71 |
*/ |
|
0 | 72 |
$check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique ); |
9 | 73 |
if ( null !== $check ) { |
0 | 74 |
return $check; |
9 | 75 |
} |
0 | 76 |
|
9 | 77 |
if ( $unique && $wpdb->get_var( |
78 |
$wpdb->prepare( |
|
79 |
"SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d", |
|
80 |
$meta_key, |
|
81 |
$object_id |
|
82 |
) |
|
83 |
) ) { |
|
0 | 84 |
return false; |
9 | 85 |
} |
0 | 86 |
|
87 |
$_meta_value = $meta_value; |
|
9 | 88 |
$meta_value = maybe_serialize( $meta_value ); |
0 | 89 |
|
5 | 90 |
/** |
91 |
* Fires immediately before meta of a specific type is added. |
|
92 |
* |
|
93 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
9 | 94 |
* object type (comment, post, term, or user). |
5 | 95 |
* |
96 |
* @since 3.1.0 |
|
97 |
* |
|
9 | 98 |
* @param int $object_id Object ID. |
99 |
* @param string $meta_key Meta key. |
|
100 |
* @param mixed $_meta_value Meta value. |
|
5 | 101 |
*/ |
0 | 102 |
do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value ); |
103 |
||
9 | 104 |
$result = $wpdb->insert( |
105 |
$table, |
|
106 |
array( |
|
107 |
$column => $object_id, |
|
108 |
'meta_key' => $meta_key, |
|
109 |
'meta_value' => $meta_value, |
|
110 |
) |
|
111 |
); |
|
0 | 112 |
|
9 | 113 |
if ( ! $result ) { |
0 | 114 |
return false; |
9 | 115 |
} |
0 | 116 |
|
117 |
$mid = (int) $wpdb->insert_id; |
|
118 |
||
9 | 119 |
wp_cache_delete( $object_id, $meta_type . '_meta' ); |
0 | 120 |
|
5 | 121 |
/** |
122 |
* Fires immediately after meta of a specific type is added. |
|
123 |
* |
|
124 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
9 | 125 |
* object type (comment, post, term, or user). |
5 | 126 |
* |
127 |
* @since 2.9.0 |
|
128 |
* |
|
9 | 129 |
* @param int $mid The meta ID after successful update. |
130 |
* @param int $object_id Object ID. |
|
131 |
* @param string $meta_key Meta key. |
|
132 |
* @param mixed $_meta_value Meta value. |
|
5 | 133 |
*/ |
0 | 134 |
do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value ); |
135 |
||
136 |
return $mid; |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* Update metadata for the specified object. If no value already exists for the specified object |
|
141 |
* ID and metadata key, the metadata will be added. |
|
142 |
* |
|
143 |
* @since 2.9.0 |
|
5 | 144 |
* |
145 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 146 |
* |
9 | 147 |
* @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
148 |
* @param int $object_id ID of the object metadata is for |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
* @param string $meta_key Metadata key |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
151 |
* @param mixed $prev_value Optional. If specified, only update existing metadata entries with |
9 | 152 |
* the specified value. Otherwise, update all entries. |
153 |
* @return int|bool The new meta field ID if a field with the given key didn't exist and was |
|
154 |
* therefore added, true on successful update, false on failure. |
|
0 | 155 |
*/ |
9 | 156 |
function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_value = '' ) { |
5 | 157 |
global $wpdb; |
0 | 158 |
|
5 | 159 |
if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) { |
0 | 160 |
return false; |
5 | 161 |
} |
0 | 162 |
|
5 | 163 |
$object_id = absint( $object_id ); |
164 |
if ( ! $object_id ) { |
|
0 | 165 |
return false; |
5 | 166 |
} |
0 | 167 |
|
5 | 168 |
$table = _get_meta_table( $meta_type ); |
169 |
if ( ! $table ) { |
|
170 |
return false; |
|
171 |
} |
|
0 | 172 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
$meta_subtype = get_object_subtype( $meta_type, $object_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
|
9 | 175 |
$column = sanitize_key( $meta_type . '_id' ); |
0 | 176 |
$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
177 |
||
178 |
// expected_slashed ($meta_key) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
179 |
$raw_meta_key = $meta_key; |
9 | 180 |
$meta_key = wp_unslash( $meta_key ); |
0 | 181 |
$passed_value = $meta_value; |
9 | 182 |
$meta_value = wp_unslash( $meta_value ); |
183 |
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); |
|
0 | 184 |
|
5 | 185 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
186 |
* Filters whether to update metadata of a specific type. |
5 | 187 |
* |
188 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
9 | 189 |
* object type (comment, post, term, or user). Returning a non-null value |
5 | 190 |
* will effectively short-circuit the function. |
191 |
* |
|
192 |
* @since 3.1.0 |
|
193 |
* |
|
194 |
* @param null|bool $check Whether to allow updating metadata for the given type. |
|
195 |
* @param int $object_id Object ID. |
|
196 |
* @param string $meta_key Meta key. |
|
197 |
* @param mixed $meta_value Meta value. Must be serializable if non-scalar. |
|
198 |
* @param mixed $prev_value Optional. If specified, only update existing |
|
199 |
* metadata entries with the specified value. |
|
200 |
* Otherwise, update all entries. |
|
201 |
*/ |
|
0 | 202 |
$check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value ); |
9 | 203 |
if ( null !== $check ) { |
0 | 204 |
return (bool) $check; |
9 | 205 |
} |
0 | 206 |
|
207 |
// Compare existing value to new value if no prev value given and the key exists only once. |
|
9 | 208 |
if ( empty( $prev_value ) ) { |
209 |
$old_value = get_metadata( $meta_type, $object_id, $meta_key ); |
|
210 |
if ( count( $old_value ) == 1 ) { |
|
211 |
if ( $old_value[0] === $meta_value ) { |
|
0 | 212 |
return false; |
9 | 213 |
} |
0 | 214 |
} |
215 |
} |
|
216 |
||
5 | 217 |
$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ); |
218 |
if ( empty( $meta_ids ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
219 |
return add_metadata( $meta_type, $object_id, $raw_meta_key, $passed_value ); |
5 | 220 |
} |
0 | 221 |
|
222 |
$_meta_value = $meta_value; |
|
9 | 223 |
$meta_value = maybe_serialize( $meta_value ); |
0 | 224 |
|
225 |
$data = compact( 'meta_value' ); |
|
9 | 226 |
$where = array( |
227 |
$column => $object_id, |
|
228 |
'meta_key' => $meta_key, |
|
229 |
); |
|
0 | 230 |
|
9 | 231 |
if ( ! empty( $prev_value ) ) { |
232 |
$prev_value = maybe_serialize( $prev_value ); |
|
0 | 233 |
$where['meta_value'] = $prev_value; |
234 |
} |
|
235 |
||
5 | 236 |
foreach ( $meta_ids as $meta_id ) { |
237 |
/** |
|
238 |
* Fires immediately before updating metadata of a specific type. |
|
239 |
* |
|
240 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
9 | 241 |
* object type (comment, post, term, or user). |
5 | 242 |
* |
243 |
* @since 2.9.0 |
|
244 |
* |
|
9 | 245 |
* @param int $meta_id ID of the metadata entry to update. |
246 |
* @param int $object_id Object ID. |
|
247 |
* @param string $meta_key Meta key. |
|
248 |
* @param mixed $_meta_value Meta value. |
|
5 | 249 |
*/ |
250 |
do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); |
|
0 | 251 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
if ( 'post' == $meta_type ) { |
5 | 253 |
/** |
254 |
* Fires immediately before updating a post's metadata. |
|
255 |
* |
|
256 |
* @since 2.9.0 |
|
257 |
* |
|
258 |
* @param int $meta_id ID of metadata entry to update. |
|
9 | 259 |
* @param int $object_id Post ID. |
5 | 260 |
* @param string $meta_key Meta key. |
9 | 261 |
* @param mixed $meta_value Meta value. This will be a PHP-serialized string representation of the value if |
262 |
* the value is an array, an object, or itself a PHP-serialized string. |
|
5 | 263 |
*/ |
264 |
do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); |
|
265 |
} |
|
266 |
} |
|
0 | 267 |
|
268 |
$result = $wpdb->update( $table, $data, $where ); |
|
9 | 269 |
if ( ! $result ) { |
0 | 270 |
return false; |
9 | 271 |
} |
0 | 272 |
|
9 | 273 |
wp_cache_delete( $object_id, $meta_type . '_meta' ); |
0 | 274 |
|
5 | 275 |
foreach ( $meta_ids as $meta_id ) { |
276 |
/** |
|
277 |
* Fires immediately after updating metadata of a specific type. |
|
278 |
* |
|
279 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
9 | 280 |
* object type (comment, post, term, or user). |
5 | 281 |
* |
282 |
* @since 2.9.0 |
|
283 |
* |
|
9 | 284 |
* @param int $meta_id ID of updated metadata entry. |
285 |
* @param int $object_id Object ID. |
|
286 |
* @param string $meta_key Meta key. |
|
287 |
* @param mixed $_meta_value Meta value. |
|
5 | 288 |
*/ |
289 |
do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); |
|
0 | 290 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
291 |
if ( 'post' == $meta_type ) { |
5 | 292 |
/** |
293 |
* Fires immediately after updating a post's metadata. |
|
294 |
* |
|
295 |
* @since 2.9.0 |
|
296 |
* |
|
297 |
* @param int $meta_id ID of updated metadata entry. |
|
9 | 298 |
* @param int $object_id Post ID. |
5 | 299 |
* @param string $meta_key Meta key. |
9 | 300 |
* @param mixed $meta_value Meta value. This will be a PHP-serialized string representation of the value if |
301 |
* the value is an array, an object, or itself a PHP-serialized string. |
|
5 | 302 |
*/ |
303 |
do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); |
|
304 |
} |
|
305 |
} |
|
0 | 306 |
|
307 |
return true; |
|
308 |
} |
|
309 |
||
310 |
/** |
|
311 |
* Delete metadata for the specified object. |
|
312 |
* |
|
313 |
* @since 2.9.0 |
|
5 | 314 |
* |
315 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 316 |
* |
9 | 317 |
* @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
318 |
* @param int $object_id ID of the object metadata is for |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
319 |
* @param string $meta_key Metadata key |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
320 |
* @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
* metadata entries with this value. Otherwise, delete all entries with the specified meta_key. |
9 | 322 |
* Pass `null`, `false`, or an empty string to skip this check. (For backward compatibility, |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
* it is not possible to pass an empty string to delete those entries with an empty string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
* for a value.) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
* @param bool $delete_all Optional, default is false. If true, delete matching metadata entries for all objects, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
* ignoring the specified object_id. Otherwise, only delete matching metadata entries for |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
* the specified object_id. |
0 | 328 |
* @return bool True on successful delete, false on failure. |
329 |
*/ |
|
9 | 330 |
function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) { |
5 | 331 |
global $wpdb; |
0 | 332 |
|
5 | 333 |
if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) && ! $delete_all ) { |
0 | 334 |
return false; |
5 | 335 |
} |
0 | 336 |
|
5 | 337 |
$object_id = absint( $object_id ); |
338 |
if ( ! $object_id && ! $delete_all ) { |
|
0 | 339 |
return false; |
5 | 340 |
} |
0 | 341 |
|
5 | 342 |
$table = _get_meta_table( $meta_type ); |
343 |
if ( ! $table ) { |
|
344 |
return false; |
|
345 |
} |
|
0 | 346 |
|
9 | 347 |
$type_column = sanitize_key( $meta_type . '_id' ); |
348 |
$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
|
0 | 349 |
// expected_slashed ($meta_key) |
9 | 350 |
$meta_key = wp_unslash( $meta_key ); |
351 |
$meta_value = wp_unslash( $meta_value ); |
|
0 | 352 |
|
5 | 353 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
* Filters whether to delete metadata of a specific type. |
5 | 355 |
* |
356 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
9 | 357 |
* object type (comment, post, term, or user). Returning a non-null value |
5 | 358 |
* will effectively short-circuit the function. |
359 |
* |
|
360 |
* @since 3.1.0 |
|
361 |
* |
|
362 |
* @param null|bool $delete Whether to allow metadata deletion of the given type. |
|
363 |
* @param int $object_id Object ID. |
|
364 |
* @param string $meta_key Meta key. |
|
365 |
* @param mixed $meta_value Meta value. Must be serializable if non-scalar. |
|
366 |
* @param bool $delete_all Whether to delete the matching metadata entries |
|
367 |
* for all objects, ignoring the specified $object_id. |
|
368 |
* Default false. |
|
369 |
*/ |
|
0 | 370 |
$check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all ); |
9 | 371 |
if ( null !== $check ) { |
0 | 372 |
return (bool) $check; |
9 | 373 |
} |
0 | 374 |
|
375 |
$_meta_value = $meta_value; |
|
9 | 376 |
$meta_value = maybe_serialize( $meta_value ); |
0 | 377 |
|
378 |
$query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key ); |
|
379 |
||
9 | 380 |
if ( ! $delete_all ) { |
381 |
$query .= $wpdb->prepare( " AND $type_column = %d", $object_id ); |
|
382 |
} |
|
0 | 383 |
|
9 | 384 |
if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) { |
385 |
$query .= $wpdb->prepare( ' AND meta_value = %s', $meta_value ); |
|
386 |
} |
|
0 | 387 |
|
388 |
$meta_ids = $wpdb->get_col( $query ); |
|
9 | 389 |
if ( ! count( $meta_ids ) ) { |
0 | 390 |
return false; |
9 | 391 |
} |
0 | 392 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
if ( $delete_all ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
395 |
$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s AND meta_value = %s", $meta_key, $meta_value ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
396 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
} |
0 | 400 |
|
5 | 401 |
/** |
402 |
* Fires immediately before deleting metadata of a specific type. |
|
403 |
* |
|
404 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
9 | 405 |
* object type (comment, post, term, or user). |
5 | 406 |
* |
407 |
* @since 3.1.0 |
|
408 |
* |
|
9 | 409 |
* @param array $meta_ids An array of metadata entry IDs to delete. |
410 |
* @param int $object_id Object ID. |
|
411 |
* @param string $meta_key Meta key. |
|
412 |
* @param mixed $_meta_value Meta value. |
|
5 | 413 |
*/ |
0 | 414 |
do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); |
415 |
||
416 |
// Old-style action. |
|
5 | 417 |
if ( 'post' == $meta_type ) { |
418 |
/** |
|
419 |
* Fires immediately before deleting metadata for a post. |
|
420 |
* |
|
421 |
* @since 2.9.0 |
|
422 |
* |
|
423 |
* @param array $meta_ids An array of post metadata entry IDs to delete. |
|
424 |
*/ |
|
0 | 425 |
do_action( 'delete_postmeta', $meta_ids ); |
5 | 426 |
} |
0 | 427 |
|
9 | 428 |
$query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . ' )'; |
429 |
||
430 |
$count = $wpdb->query( $query ); |
|
0 | 431 |
|
9 | 432 |
if ( ! $count ) { |
0 | 433 |
return false; |
9 | 434 |
} |
0 | 435 |
|
436 |
if ( $delete_all ) { |
|
437 |
foreach ( (array) $object_ids as $o_id ) { |
|
9 | 438 |
wp_cache_delete( $o_id, $meta_type . '_meta' ); |
0 | 439 |
} |
440 |
} else { |
|
9 | 441 |
wp_cache_delete( $object_id, $meta_type . '_meta' ); |
0 | 442 |
} |
443 |
||
5 | 444 |
/** |
445 |
* Fires immediately after deleting metadata of a specific type. |
|
446 |
* |
|
447 |
* The dynamic portion of the hook name, `$meta_type`, refers to the meta |
|
9 | 448 |
* object type (comment, post, term, or user). |
5 | 449 |
* |
450 |
* @since 2.9.0 |
|
451 |
* |
|
9 | 452 |
* @param array $meta_ids An array of deleted metadata entry IDs. |
453 |
* @param int $object_id Object ID. |
|
454 |
* @param string $meta_key Meta key. |
|
455 |
* @param mixed $_meta_value Meta value. |
|
5 | 456 |
*/ |
0 | 457 |
do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); |
458 |
||
459 |
// Old-style action. |
|
5 | 460 |
if ( 'post' == $meta_type ) { |
461 |
/** |
|
462 |
* Fires immediately after deleting metadata for a post. |
|
463 |
* |
|
464 |
* @since 2.9.0 |
|
465 |
* |
|
466 |
* @param array $meta_ids An array of deleted post metadata entry IDs. |
|
467 |
*/ |
|
0 | 468 |
do_action( 'deleted_postmeta', $meta_ids ); |
5 | 469 |
} |
0 | 470 |
|
471 |
return true; |
|
472 |
} |
|
473 |
||
474 |
/** |
|
475 |
* Retrieve metadata for the specified object. |
|
476 |
* |
|
477 |
* @since 2.9.0 |
|
478 |
* |
|
9 | 479 |
* @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
480 |
* @param int $object_id ID of the object metadata is for |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
481 |
* @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for |
9 | 482 |
* the specified object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
483 |
* @param bool $single Optional, default is false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
484 |
* If true, return only the first value of the specified meta_key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
485 |
* This parameter has no effect if meta_key is not specified. |
5 | 486 |
* @return mixed Single metadata value, or array of values |
0 | 487 |
*/ |
9 | 488 |
function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) { |
5 | 489 |
if ( ! $meta_type || ! is_numeric( $object_id ) ) { |
0 | 490 |
return false; |
5 | 491 |
} |
492 |
||
493 |
$object_id = absint( $object_id ); |
|
494 |
if ( ! $object_id ) { |
|
495 |
return false; |
|
496 |
} |
|
0 | 497 |
|
5 | 498 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
499 |
* Filters whether to retrieve metadata of a specific type. |
5 | 500 |
* |
501 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
9 | 502 |
* object type (comment, post, term, or user). Returning a non-null value |
5 | 503 |
* will effectively short-circuit the function. |
504 |
* |
|
505 |
* @since 3.1.0 |
|
506 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
507 |
* @param null|array|string $value The value get_metadata() should return - a single metadata value, |
5 | 508 |
* or an array of values. |
509 |
* @param int $object_id Object ID. |
|
510 |
* @param string $meta_key Meta key. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
511 |
* @param bool $single Whether to return only the first value of the specified $meta_key. |
5 | 512 |
*/ |
0 | 513 |
$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single ); |
514 |
if ( null !== $check ) { |
|
9 | 515 |
if ( $single && is_array( $check ) ) { |
0 | 516 |
return $check[0]; |
9 | 517 |
} else { |
0 | 518 |
return $check; |
9 | 519 |
} |
0 | 520 |
} |
521 |
||
9 | 522 |
$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' ); |
0 | 523 |
|
9 | 524 |
if ( ! $meta_cache ) { |
0 | 525 |
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); |
9 | 526 |
$meta_cache = $meta_cache[ $object_id ]; |
0 | 527 |
} |
528 |
||
5 | 529 |
if ( ! $meta_key ) { |
0 | 530 |
return $meta_cache; |
5 | 531 |
} |
0 | 532 |
|
9 | 533 |
if ( isset( $meta_cache[ $meta_key ] ) ) { |
534 |
if ( $single ) { |
|
535 |
return maybe_unserialize( $meta_cache[ $meta_key ][0] ); |
|
536 |
} else { |
|
537 |
return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] ); |
|
538 |
} |
|
0 | 539 |
} |
540 |
||
9 | 541 |
if ( $single ) { |
0 | 542 |
return ''; |
9 | 543 |
} else { |
0 | 544 |
return array(); |
9 | 545 |
} |
0 | 546 |
} |
547 |
||
548 |
/** |
|
549 |
* Determine if a meta key is set for a given object |
|
550 |
* |
|
551 |
* @since 3.3.0 |
|
552 |
* |
|
9 | 553 |
* @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
554 |
* @param int $object_id ID of the object metadata is for |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
555 |
* @param string $meta_key Metadata key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
* @return bool True of the key is set, false if not. |
0 | 557 |
*/ |
558 |
function metadata_exists( $meta_type, $object_id, $meta_key ) { |
|
5 | 559 |
if ( ! $meta_type || ! is_numeric( $object_id ) ) { |
0 | 560 |
return false; |
5 | 561 |
} |
0 | 562 |
|
5 | 563 |
$object_id = absint( $object_id ); |
564 |
if ( ! $object_id ) { |
|
0 | 565 |
return false; |
5 | 566 |
} |
0 | 567 |
|
5 | 568 |
/** This filter is documented in wp-includes/meta.php */ |
0 | 569 |
$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true ); |
9 | 570 |
if ( null !== $check ) { |
5 | 571 |
return (bool) $check; |
9 | 572 |
} |
0 | 573 |
|
574 |
$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' ); |
|
575 |
||
9 | 576 |
if ( ! $meta_cache ) { |
0 | 577 |
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); |
9 | 578 |
$meta_cache = $meta_cache[ $object_id ]; |
0 | 579 |
} |
580 |
||
9 | 581 |
if ( isset( $meta_cache[ $meta_key ] ) ) { |
0 | 582 |
return true; |
9 | 583 |
} |
0 | 584 |
|
585 |
return false; |
|
586 |
} |
|
587 |
||
588 |
/** |
|
589 |
* Get meta data by meta ID |
|
590 |
* |
|
591 |
* @since 3.3.0 |
|
592 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
593 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
594 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
595 |
* @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
596 |
* @param int $meta_id ID for a specific meta row |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
597 |
* @return object|false Meta object or false. |
0 | 598 |
*/ |
599 |
function get_metadata_by_mid( $meta_type, $meta_id ) { |
|
600 |
global $wpdb; |
|
601 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
602 |
if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) { |
0 | 603 |
return false; |
5 | 604 |
} |
0 | 605 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
606 |
$meta_id = intval( $meta_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
607 |
if ( $meta_id <= 0 ) { |
0 | 608 |
return false; |
5 | 609 |
} |
0 | 610 |
|
5 | 611 |
$table = _get_meta_table( $meta_type ); |
612 |
if ( ! $table ) { |
|
0 | 613 |
return false; |
5 | 614 |
} |
0 | 615 |
|
616 |
$id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id'; |
|
617 |
||
9 | 618 |
/** |
619 |
* Filters whether to retrieve metadata of a specific type by meta ID. |
|
620 |
* |
|
621 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
622 |
* object type (comment, post, term, or user). Returning a non-null value |
|
623 |
* will effectively short-circuit the function. |
|
624 |
* |
|
625 |
* @since 5.0.0 |
|
626 |
* |
|
627 |
* @param mixed $value The value get_metadata_by_mid() should return. |
|
628 |
* @param int $meta_id Meta ID. |
|
629 |
*/ |
|
630 |
$check = apply_filters( "get_{$meta_type}_metadata_by_mid", null, $meta_id ); |
|
631 |
if ( null !== $check ) { |
|
632 |
return $check; |
|
633 |
} |
|
634 |
||
0 | 635 |
$meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) ); |
636 |
||
9 | 637 |
if ( empty( $meta ) ) { |
0 | 638 |
return false; |
9 | 639 |
} |
0 | 640 |
|
9 | 641 |
if ( isset( $meta->meta_value ) ) { |
0 | 642 |
$meta->meta_value = maybe_unserialize( $meta->meta_value ); |
9 | 643 |
} |
0 | 644 |
|
645 |
return $meta; |
|
646 |
} |
|
647 |
||
648 |
/** |
|
649 |
* Update meta data by meta ID |
|
650 |
* |
|
651 |
* @since 3.3.0 |
|
652 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
653 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
654 |
* |
9 | 655 |
* @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
656 |
* @param int $meta_id ID for a specific meta row |
0 | 657 |
* @param string $meta_value Metadata value |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
658 |
* @param string $meta_key Optional, you can provide a meta key to update it |
0 | 659 |
* @return bool True on successful update, false on failure. |
660 |
*/ |
|
661 |
function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) { |
|
662 |
global $wpdb; |
|
663 |
||
664 |
// Make sure everything is valid. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
665 |
if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) { |
0 | 666 |
return false; |
5 | 667 |
} |
0 | 668 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
669 |
$meta_id = intval( $meta_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
670 |
if ( $meta_id <= 0 ) { |
0 | 671 |
return false; |
5 | 672 |
} |
0 | 673 |
|
5 | 674 |
$table = _get_meta_table( $meta_type ); |
675 |
if ( ! $table ) { |
|
0 | 676 |
return false; |
5 | 677 |
} |
0 | 678 |
|
9 | 679 |
$column = sanitize_key( $meta_type . '_id' ); |
0 | 680 |
$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
681 |
||
9 | 682 |
/** |
683 |
* Filters whether to update metadata of a specific type by meta ID. |
|
684 |
* |
|
685 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
686 |
* object type (comment, post, term, or user). Returning a non-null value |
|
687 |
* will effectively short-circuit the function. |
|
688 |
* |
|
689 |
* @since 5.0.0 |
|
690 |
* |
|
691 |
* @param null|bool $check Whether to allow updating metadata for the given type. |
|
692 |
* @param int $meta_id Meta ID. |
|
693 |
* @param mixed $meta_value Meta value. Must be serializable if non-scalar. |
|
694 |
* @param string|bool $meta_key Meta key, if provided. |
|
695 |
*/ |
|
696 |
$check = apply_filters( "update_{$meta_type}_metadata_by_mid", null, $meta_id, $meta_value, $meta_key ); |
|
697 |
if ( null !== $check ) { |
|
698 |
return (bool) $check; |
|
699 |
} |
|
700 |
||
0 | 701 |
// Fetch the meta and go on if it's found. |
702 |
if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { |
|
703 |
$original_key = $meta->meta_key; |
|
9 | 704 |
$object_id = $meta->{$column}; |
0 | 705 |
|
706 |
// If a new meta_key (last parameter) was specified, change the meta key, |
|
707 |
// otherwise use the original key in the update statement. |
|
708 |
if ( false === $meta_key ) { |
|
709 |
$meta_key = $original_key; |
|
710 |
} elseif ( ! is_string( $meta_key ) ) { |
|
711 |
return false; |
|
712 |
} |
|
713 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
714 |
$meta_subtype = get_object_subtype( $meta_type, $object_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
715 |
|
0 | 716 |
// Sanitize the meta |
717 |
$_meta_value = $meta_value; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
718 |
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
719 |
$meta_value = maybe_serialize( $meta_value ); |
0 | 720 |
|
721 |
// Format the data query arguments. |
|
722 |
$data = array( |
|
9 | 723 |
'meta_key' => $meta_key, |
724 |
'meta_value' => $meta_value, |
|
0 | 725 |
); |
726 |
||
727 |
// Format the where query arguments. |
|
9 | 728 |
$where = array(); |
729 |
$where[ $id_column ] = $meta_id; |
|
0 | 730 |
|
5 | 731 |
/** This action is documented in wp-includes/meta.php */ |
0 | 732 |
do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); |
733 |
||
5 | 734 |
if ( 'post' == $meta_type ) { |
735 |
/** This action is documented in wp-includes/meta.php */ |
|
0 | 736 |
do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); |
5 | 737 |
} |
0 | 738 |
|
739 |
// Run the update query, all fields in $data are %s, $where is a %d. |
|
740 |
$result = $wpdb->update( $table, $data, $where, '%s', '%d' ); |
|
9 | 741 |
if ( ! $result ) { |
0 | 742 |
return false; |
9 | 743 |
} |
0 | 744 |
|
745 |
// Clear the caches. |
|
9 | 746 |
wp_cache_delete( $object_id, $meta_type . '_meta' ); |
0 | 747 |
|
5 | 748 |
/** This action is documented in wp-includes/meta.php */ |
0 | 749 |
do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); |
750 |
||
5 | 751 |
if ( 'post' == $meta_type ) { |
752 |
/** This action is documented in wp-includes/meta.php */ |
|
0 | 753 |
do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); |
5 | 754 |
} |
0 | 755 |
|
756 |
return true; |
|
757 |
} |
|
758 |
||
759 |
// And if the meta was not found. |
|
760 |
return false; |
|
761 |
} |
|
762 |
||
763 |
/** |
|
764 |
* Delete meta data by meta ID |
|
765 |
* |
|
766 |
* @since 3.3.0 |
|
767 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
768 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
769 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
770 |
* @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
771 |
* @param int $meta_id ID for a specific meta row |
0 | 772 |
* @return bool True on successful delete, false on failure. |
773 |
*/ |
|
774 |
function delete_metadata_by_mid( $meta_type, $meta_id ) { |
|
775 |
global $wpdb; |
|
776 |
||
777 |
// Make sure everything is valid. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
778 |
if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) { |
0 | 779 |
return false; |
5 | 780 |
} |
0 | 781 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
782 |
$meta_id = intval( $meta_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
783 |
if ( $meta_id <= 0 ) { |
0 | 784 |
return false; |
5 | 785 |
} |
0 | 786 |
|
5 | 787 |
$table = _get_meta_table( $meta_type ); |
788 |
if ( ! $table ) { |
|
0 | 789 |
return false; |
5 | 790 |
} |
0 | 791 |
|
792 |
// object and id columns |
|
9 | 793 |
$column = sanitize_key( $meta_type . '_id' ); |
0 | 794 |
$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
795 |
||
9 | 796 |
/** |
797 |
* Filters whether to delete metadata of a specific type by meta ID. |
|
798 |
* |
|
799 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
800 |
* object type (comment, post, term, or user). Returning a non-null value |
|
801 |
* will effectively short-circuit the function. |
|
802 |
* |
|
803 |
* @since 5.0.0 |
|
804 |
* |
|
805 |
* @param null|bool $delete Whether to allow metadata deletion of the given type. |
|
806 |
* @param int $meta_id Meta ID. |
|
807 |
*/ |
|
808 |
$check = apply_filters( "delete_{$meta_type}_metadata_by_mid", null, $meta_id ); |
|
809 |
if ( null !== $check ) { |
|
810 |
return (bool) $check; |
|
811 |
} |
|
812 |
||
0 | 813 |
// Fetch the meta and go on if it's found. |
814 |
if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { |
|
9 | 815 |
$object_id = (int) $meta->{$column}; |
0 | 816 |
|
5 | 817 |
/** This action is documented in wp-includes/meta.php */ |
0 | 818 |
do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); |
819 |
||
820 |
// Old-style action. |
|
5 | 821 |
if ( 'post' == $meta_type || 'comment' == $meta_type ) { |
822 |
/** |
|
823 |
* Fires immediately before deleting post or comment metadata of a specific type. |
|
824 |
* |
|
825 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
826 |
* object type (post or comment). |
|
827 |
* |
|
828 |
* @since 3.4.0 |
|
829 |
* |
|
830 |
* @param int $meta_id ID of the metadata entry to delete. |
|
831 |
*/ |
|
0 | 832 |
do_action( "delete_{$meta_type}meta", $meta_id ); |
5 | 833 |
} |
0 | 834 |
|
835 |
// Run the query, will return true if deleted, false otherwise |
|
836 |
$result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) ); |
|
837 |
||
838 |
// Clear the caches. |
|
9 | 839 |
wp_cache_delete( $object_id, $meta_type . '_meta' ); |
0 | 840 |
|
5 | 841 |
/** This action is documented in wp-includes/meta.php */ |
0 | 842 |
do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); |
843 |
||
844 |
// Old-style action. |
|
5 | 845 |
if ( 'post' == $meta_type || 'comment' == $meta_type ) { |
846 |
/** |
|
847 |
* Fires immediately after deleting post or comment metadata of a specific type. |
|
848 |
* |
|
849 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
850 |
* object type (post or comment). |
|
851 |
* |
|
852 |
* @since 3.4.0 |
|
853 |
* |
|
854 |
* @param int $meta_ids Deleted metadata entry ID. |
|
855 |
*/ |
|
0 | 856 |
do_action( "deleted_{$meta_type}meta", $meta_id ); |
5 | 857 |
} |
0 | 858 |
|
859 |
return $result; |
|
860 |
||
861 |
} |
|
862 |
||
863 |
// Meta id was not found. |
|
864 |
return false; |
|
865 |
} |
|
866 |
||
867 |
/** |
|
868 |
* Update the metadata cache for the specified objects. |
|
869 |
* |
|
870 |
* @since 2.9.0 |
|
5 | 871 |
* |
872 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 873 |
* |
9 | 874 |
* @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
875 |
* @param int|array $object_ids Array or comma delimited list of object IDs to update cache for |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
876 |
* @return array|false Metadata cache for the specified objects, or false on failure. |
0 | 877 |
*/ |
9 | 878 |
function update_meta_cache( $meta_type, $object_ids ) { |
5 | 879 |
global $wpdb; |
0 | 880 |
|
5 | 881 |
if ( ! $meta_type || ! $object_ids ) { |
0 | 882 |
return false; |
5 | 883 |
} |
884 |
||
885 |
$table = _get_meta_table( $meta_type ); |
|
886 |
if ( ! $table ) { |
|
887 |
return false; |
|
888 |
} |
|
0 | 889 |
|
9 | 890 |
$column = sanitize_key( $meta_type . '_id' ); |
0 | 891 |
|
9 | 892 |
if ( ! is_array( $object_ids ) ) { |
893 |
$object_ids = preg_replace( '|[^0-9,]|', '', $object_ids ); |
|
894 |
$object_ids = explode( ',', $object_ids ); |
|
0 | 895 |
} |
896 |
||
9 | 897 |
$object_ids = array_map( 'intval', $object_ids ); |
898 |
||
899 |
/** |
|
900 |
* Filters whether to update the metadata cache of a specific type. |
|
901 |
* |
|
902 |
* The dynamic portion of the hook, `$meta_type`, refers to the meta |
|
903 |
* object type (comment, post, term, or user). Returning a non-null value |
|
904 |
* will effectively short-circuit the function. |
|
905 |
* |
|
906 |
* @since 5.0.0 |
|
907 |
* |
|
908 |
* @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. |
|
910 |
*/ |
|
911 |
$check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids ); |
|
912 |
if ( null !== $check ) { |
|
913 |
return (bool) $check; |
|
914 |
} |
|
0 | 915 |
|
916 |
$cache_key = $meta_type . '_meta'; |
|
9 | 917 |
$ids = array(); |
918 |
$cache = array(); |
|
0 | 919 |
foreach ( $object_ids as $id ) { |
920 |
$cached_object = wp_cache_get( $id, $cache_key ); |
|
9 | 921 |
if ( false === $cached_object ) { |
0 | 922 |
$ids[] = $id; |
9 | 923 |
} else { |
924 |
$cache[ $id ] = $cached_object; |
|
925 |
} |
|
0 | 926 |
} |
927 |
||
9 | 928 |
if ( empty( $ids ) ) { |
0 | 929 |
return $cache; |
9 | 930 |
} |
0 | 931 |
|
932 |
// Get meta info |
|
9 | 933 |
$id_list = join( ',', $ids ); |
5 | 934 |
$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; |
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 ); |
|
0 | 936 |
|
9 | 937 |
if ( ! empty( $meta_list ) ) { |
938 |
foreach ( $meta_list as $metarow ) { |
|
939 |
$mpid = intval( $metarow[ $column ] ); |
|
0 | 940 |
$mkey = $metarow['meta_key']; |
941 |
$mval = $metarow['meta_value']; |
|
942 |
||
943 |
// Force subkeys to be array type: |
|
9 | 944 |
if ( ! isset( $cache[ $mpid ] ) || ! is_array( $cache[ $mpid ] ) ) { |
945 |
$cache[ $mpid ] = array(); |
|
946 |
} |
|
947 |
if ( ! isset( $cache[ $mpid ][ $mkey ] ) || ! is_array( $cache[ $mpid ][ $mkey ] ) ) { |
|
948 |
$cache[ $mpid ][ $mkey ] = array(); |
|
949 |
} |
|
0 | 950 |
|
951 |
// Add a value to the current pid/key: |
|
9 | 952 |
$cache[ $mpid ][ $mkey ][] = $mval; |
0 | 953 |
} |
954 |
} |
|
955 |
||
956 |
foreach ( $ids as $id ) { |
|
9 | 957 |
if ( ! isset( $cache[ $id ] ) ) { |
958 |
$cache[ $id ] = array(); |
|
959 |
} |
|
960 |
wp_cache_add( $id, $cache[ $id ], $cache_key ); |
|
0 | 961 |
} |
962 |
||
963 |
return $cache; |
|
964 |
} |
|
965 |
||
966 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
967 |
* Retrieves the queue for lazy-loading metadata. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
968 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
969 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
970 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
971 |
* @return WP_Metadata_Lazyloader $lazyloader Metadata lazyloader queue. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
973 |
function wp_metadata_lazyloader() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
974 |
static $wp_metadata_lazyloader; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
975 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
976 |
if ( null === $wp_metadata_lazyloader ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
977 |
$wp_metadata_lazyloader = new WP_Metadata_Lazyloader(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
978 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
979 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
980 |
return $wp_metadata_lazyloader; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
981 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
982 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
983 |
/** |
5 | 984 |
* Given a meta query, generates SQL clauses to be appended to a main query. |
0 | 985 |
* |
986 |
* @since 3.2.0 |
|
987 |
* |
|
988 |
* @see WP_Meta_Query |
|
989 |
* |
|
5 | 990 |
* @param array $meta_query A meta query. |
991 |
* @param string $type Type of meta. |
|
992 |
* @param string $primary_table Primary database table name. |
|
993 |
* @param string $primary_id_column Primary ID column name. |
|
994 |
* @param object $context Optional. The main query object |
|
995 |
* @return array Associative array of `JOIN` and `WHERE` SQL. |
|
0 | 996 |
*/ |
997 |
function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) { |
|
998 |
$meta_query_obj = new WP_Meta_Query( $meta_query ); |
|
999 |
return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context ); |
|
1000 |
} |
|
1001 |
||
1002 |
/** |
|
1003 |
* Retrieve the name of the metadata table for the specified object type. |
|
1004 |
* |
|
1005 |
* @since 2.9.0 |
|
5 | 1006 |
* |
1007 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 1008 |
* |
9 | 1009 |
* @param string $type Type of object to get metadata table for (e.g., comment, post, term, or user). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1010 |
* @return string|false Metadata table name, or false if no metadata table exists |
0 | 1011 |
*/ |
9 | 1012 |
function _get_meta_table( $type ) { |
0 | 1013 |
global $wpdb; |
1014 |
||
1015 |
$table_name = $type . 'meta'; |
|
1016 |
||
9 | 1017 |
if ( empty( $wpdb->$table_name ) ) { |
0 | 1018 |
return false; |
9 | 1019 |
} |
0 | 1020 |
|
1021 |
return $wpdb->$table_name; |
|
1022 |
} |
|
1023 |
||
1024 |
/** |
|
9 | 1025 |
* Determines whether a meta key is considered protected. |
0 | 1026 |
* |
1027 |
* @since 3.1.3 |
|
1028 |
* |
|
9 | 1029 |
* @param string $meta_key Meta key. |
1030 |
* @param string|null $meta_type Optional. Type of object metadata is for (e.g., comment, post, term, or user). |
|
1031 |
* @return bool Whether the meta key is considered protected. |
|
0 | 1032 |
*/ |
1033 |
function is_protected_meta( $meta_key, $meta_type = null ) { |
|
1034 |
$protected = ( '_' == $meta_key[0] ); |
|
1035 |
||
5 | 1036 |
/** |
9 | 1037 |
* Filters whether a meta key is considered protected. |
5 | 1038 |
* |
1039 |
* @since 3.2.0 |
|
1040 |
* |
|
9 | 1041 |
* @param bool $protected Whether the key is considered protected. |
1042 |
* @param string $meta_key Meta key. |
|
1043 |
* @param string|null $meta_type Type of object metadata is for (e.g., comment, post, term, or user). |
|
5 | 1044 |
*/ |
0 | 1045 |
return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type ); |
1046 |
} |
|
1047 |
||
1048 |
/** |
|
5 | 1049 |
* Sanitize meta value. |
0 | 1050 |
* |
1051 |
* @since 3.1.3 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1052 |
* @since 4.9.8 The `$object_subtype` parameter was added. |
0 | 1053 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1054 |
* @param string $meta_key Meta key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1055 |
* @param mixed $meta_value Meta value to sanitize. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1056 |
* @param string $object_type Type of object the meta is registered to. |
9 | 1057 |
* @param string $object_subtype Optional. The subtype of the object type. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1058 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1059 |
* @return mixed Sanitized $meta_value. |
0 | 1060 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1061 |
function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1062 |
if ( ! empty( $object_subtype ) && has_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1063 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1064 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1065 |
* Filters the sanitization of a specific meta key of a specific meta type and subtype. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1066 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1067 |
* The dynamic portions of the hook name, `$object_type`, `$meta_key`, |
9 | 1068 |
* and `$object_subtype`, refer to the metadata object type (comment, post, term, or user), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1069 |
* the meta key value, and the object subtype respectively. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1070 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1071 |
* @since 4.9.8 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1072 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1073 |
* @param mixed $meta_value Meta value to sanitize. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1074 |
* @param string $meta_key Meta key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1075 |
* @param string $object_type Object type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1076 |
* @param string $object_subtype Object subtype. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1077 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1078 |
return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $meta_value, $meta_key, $object_type, $object_subtype ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1079 |
} |
5 | 1080 |
|
1081 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1082 |
* Filters the sanitization of a specific meta key of a specific meta type. |
5 | 1083 |
* |
1084 |
* The dynamic portions of the hook name, `$meta_type`, and `$meta_key`, |
|
9 | 1085 |
* refer to the metadata object type (comment, post, term, or user) and the meta |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1086 |
* key value, respectively. |
5 | 1087 |
* |
1088 |
* @since 3.3.0 |
|
1089 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1090 |
* @param mixed $meta_value Meta value to sanitize. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1091 |
* @param string $meta_key Meta key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1092 |
* @param string $object_type Object type. |
5 | 1093 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1094 |
return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1095 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1096 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1097 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1098 |
* Registers a meta key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1099 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1100 |
* It is recommended to register meta keys for a specific combination of object type and object subtype. If passing |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1101 |
* an object subtype is omitted, the meta key will be registered for the entire object type, however it can be partly |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1102 |
* overridden in case a more specific meta key of the same name exists for the same object type and a subtype. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1103 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1104 |
* If an object type does not support any subtypes, such as users or comments, you should commonly call this function |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1105 |
* without passing a subtype. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1106 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1107 |
* @since 3.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1108 |
* @since 4.6.0 {@link https://core.trac.wordpress.org/ticket/35658 Modified |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1109 |
* to support an array of data to attach to registered meta keys}. Previous arguments for |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1110 |
* `$sanitize_callback` and `$auth_callback` have been folded into this array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1111 |
* @since 4.9.8 The `$object_subtype` argument was added to the arguments array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1112 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1113 |
* @param string $object_type Type of object this meta is registered to. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1114 |
* @param string $meta_key Meta key to register. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1115 |
* @param array $args { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1116 |
* Data used to describe the meta key when registered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1117 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1118 |
* @type string $object_subtype A subtype; e.g. if the object type is "post", the post type. If left empty, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1119 |
* the meta key will be registered on the entire object type. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1120 |
* @type string $type The type of data associated with this meta key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1121 |
* Valid values are 'string', 'boolean', 'integer', and 'number'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1122 |
* @type string $description A description of the data attached to this meta key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1123 |
* @type bool $single Whether the meta key has one value per object, or an array of values per object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1124 |
* @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
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. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1126 |
* @type bool $show_in_rest Whether data associated with this meta key can be considered public. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1127 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1128 |
* @param string|array $deprecated Deprecated. Use `$args` instead. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1129 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1130 |
* @return bool True if the meta key was successfully registered in the global array, false if not. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1131 |
* Registering a meta key with distinct sanitize and auth callbacks will fire those |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1132 |
* callbacks, but will not add to the global registry. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1133 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1134 |
function register_meta( $object_type, $meta_key, $args, $deprecated = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1135 |
global $wp_meta_keys; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1136 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1137 |
if ( ! is_array( $wp_meta_keys ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1138 |
$wp_meta_keys = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1139 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1140 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1141 |
$defaults = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1142 |
'object_subtype' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1143 |
'type' => 'string', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1144 |
'description' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1145 |
'single' => false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1146 |
'sanitize_callback' => null, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1147 |
'auth_callback' => null, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1148 |
'show_in_rest' => false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1149 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1150 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1151 |
// There used to be individual args for sanitize and auth callbacks |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1152 |
$has_old_sanitize_cb = false; |
9 | 1153 |
$has_old_auth_cb = false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1154 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1155 |
if ( is_callable( $args ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1156 |
$args = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1157 |
'sanitize_callback' => $args, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1158 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1159 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1160 |
$has_old_sanitize_cb = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1161 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1162 |
$args = (array) $args; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1163 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1164 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1165 |
if ( is_callable( $deprecated ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1166 |
$args['auth_callback'] = $deprecated; |
9 | 1167 |
$has_old_auth_cb = true; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1168 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1169 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1170 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1171 |
* Filters the registration arguments when registering meta. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1172 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1173 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1174 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1175 |
* @param array $args Array of meta registration arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1176 |
* @param array $defaults Array of default arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1177 |
* @param string $object_type Object type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1178 |
* @param string $meta_key Meta key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1179 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1180 |
$args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1181 |
$args = wp_parse_args( $args, $defaults ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1182 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1183 |
$object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1184 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1185 |
// If `auth_callback` is not provided, fall back to `is_protected_meta()`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1186 |
if ( empty( $args['auth_callback'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1187 |
if ( is_protected_meta( $meta_key, $object_type ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1188 |
$args['auth_callback'] = '__return_false'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1189 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1190 |
$args['auth_callback'] = '__return_true'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1191 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1192 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1193 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1194 |
// Back-compat: old sanitize and auth callbacks are applied to all of an object type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1195 |
if ( is_callable( $args['sanitize_callback'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1196 |
if ( ! empty( $object_subtype ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1197 |
add_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'], 10, 4 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1198 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1199 |
add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1200 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1201 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1202 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1203 |
if ( is_callable( $args['auth_callback'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1204 |
if ( ! empty( $object_subtype ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1205 |
add_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'], 10, 6 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1206 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1207 |
add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1208 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1209 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1210 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1211 |
// Global registry only contains meta keys registered with the array of arguments added in 4.6.0. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1212 |
if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1213 |
unset( $args['object_subtype'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1214 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1215 |
$wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1216 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1217 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1218 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1219 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1220 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1221 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1222 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1223 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1224 |
* Checks if a meta key is registered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1225 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1226 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1227 |
* @since 4.9.8 The `$object_subtype` parameter was added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1228 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1229 |
* @param string $object_type The type of object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1230 |
* @param string $meta_key The meta key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1231 |
* @param string $object_subtype Optional. The subtype of the object type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1232 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1233 |
* @return bool True if the meta key is registered to the object type and, if provided, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1234 |
* the object subtype. False if not. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1235 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1236 |
function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1237 |
$meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1238 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1239 |
return isset( $meta_keys[ $meta_key ] ); |
0 | 1240 |
} |
1241 |
||
1242 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1243 |
* Unregisters a meta key from the list of registered keys. |
0 | 1244 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1245 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1246 |
* @since 4.9.8 The `$object_subtype` parameter was added. |
0 | 1247 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1248 |
* @param string $object_type The type of object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1249 |
* @param string $meta_key The meta key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1250 |
* @param string $object_subtype Optional. The subtype of the object type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1251 |
* @return bool True if successful. False if the meta key was not registered. |
0 | 1252 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1253 |
function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1254 |
global $wp_meta_keys; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1255 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1256 |
if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1257 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1258 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1259 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1260 |
$args = $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1261 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1262 |
if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1263 |
if ( ! empty( $object_subtype ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1264 |
remove_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1265 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1266 |
remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1267 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1268 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1269 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1270 |
if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1271 |
if ( ! empty( $object_subtype ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1272 |
remove_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1273 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1274 |
remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1275 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1276 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1277 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1278 |
unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] ); |
0 | 1279 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1280 |
// Do some clean up |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1281 |
if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1282 |
unset( $wp_meta_keys[ $object_type ][ $object_subtype ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1283 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1284 |
if ( empty( $wp_meta_keys[ $object_type ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1285 |
unset( $wp_meta_keys[ $object_type ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1286 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1287 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1288 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1289 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1290 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1291 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1292 |
* Retrieves a list of registered meta keys for an object type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1293 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1294 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1295 |
* @since 4.9.8 The `$object_subtype` parameter was added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1296 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1297 |
* @param string $object_type The type of object. Post, comment, user, term. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1298 |
* @param string $object_subtype Optional. The subtype of the object type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1299 |
* @return array List of registered meta keys. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1300 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1301 |
function get_registered_meta_keys( $object_type, $object_subtype = '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1302 |
global $wp_meta_keys; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1303 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1304 |
if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1305 |
return array(); |
0 | 1306 |
} |
1307 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1308 |
return $wp_meta_keys[ $object_type ][ $object_subtype ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1309 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1310 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1311 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1312 |
* Retrieves registered metadata for a specified object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1313 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1314 |
* The results include both meta that is registered specifically for the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1315 |
* object's subtype and meta that is registered for the entire object type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1316 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1317 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1318 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1319 |
* @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1320 |
* @param int $object_id ID of the object the metadata is for. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1321 |
* @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1322 |
* metadata for the specified object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1323 |
* @return mixed A single value or array of values for a key if specified. An array of all registered keys |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1324 |
* and values for an object ID if not. False if a given $meta_key is not registered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1325 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1326 |
function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1327 |
$object_subtype = get_object_subtype( $object_type, $object_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1328 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1329 |
if ( ! empty( $meta_key ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1330 |
if ( ! empty( $object_subtype ) && ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1331 |
$object_subtype = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1332 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1333 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1334 |
if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1335 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1336 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1337 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1338 |
$meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1339 |
$meta_key_data = $meta_keys[ $meta_key ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1340 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1341 |
$data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1342 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1343 |
return $data; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1344 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1345 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1346 |
$data = get_metadata( $object_type, $object_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1347 |
if ( ! $data ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1348 |
return array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1349 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1350 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1351 |
$meta_keys = get_registered_meta_keys( $object_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1352 |
if ( ! empty( $object_subtype ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1353 |
$meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $object_type, $object_subtype ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1354 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1355 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1356 |
return array_intersect_key( $data, $meta_keys ); |
0 | 1357 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1358 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1359 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1360 |
* Filter out `register_meta()` args based on a whitelist. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1361 |
* `register_meta()` args may change over time, so requiring the whitelist |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1362 |
* to be explicitly turned off is a warranty seal of sorts. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1363 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1364 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1365 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1366 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1367 |
* @param array $args Arguments from `register_meta()`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1368 |
* @param array $default_args Default arguments for `register_meta()`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1369 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1370 |
* @return array Filtered arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1371 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1372 |
function _wp_register_meta_args_whitelist( $args, $default_args ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1373 |
return array_intersect_key( $args, $default_args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1374 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1375 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1376 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1377 |
* Returns the object subtype for a given object ID of a specific type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1378 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1379 |
* @since 4.9.8 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1380 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1381 |
* @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1382 |
* @param int $object_id ID of the object to retrieve its subtype. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1383 |
* @return string The object subtype or an empty string if unspecified subtype. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1384 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1385 |
function get_object_subtype( $object_type, $object_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1386 |
$object_id = (int) $object_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1387 |
$object_subtype = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1388 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1389 |
switch ( $object_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1390 |
case 'post': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1391 |
$post_type = get_post_type( $object_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1392 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1393 |
if ( ! empty( $post_type ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1394 |
$object_subtype = $post_type; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1395 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1397 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1398 |
case 'term': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1399 |
$term = get_term( $object_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1400 |
if ( ! $term instanceof WP_Term ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1401 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1402 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1403 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1404 |
$object_subtype = $term->taxonomy; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1405 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1406 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1407 |
case 'comment': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1408 |
$comment = get_comment( $object_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1409 |
if ( ! $comment ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1410 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1411 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1412 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1413 |
$object_subtype = 'comment'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1414 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1415 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1416 |
case 'user': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1417 |
$user = get_user_by( 'id', $object_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1418 |
if ( ! $user ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1419 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1420 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1421 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1422 |
$object_subtype = 'user'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1423 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1424 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1425 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1426 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1427 |
* Filters the object subtype identifier for a non standard object type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1428 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1429 |
* The dynamic portion of the hook, `$object_type`, refers to the object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1430 |
* type (post, comment, term, or user). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1431 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1432 |
* @since 4.9.8 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1433 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1434 |
* @param string $object_subtype Empty string to override. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1435 |
* @param int $object_id ID of the object to get the subtype for. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1436 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1437 |
return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1438 |
} |