meta.php 42 KB

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