meta.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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
  784. * @return bool True if the key is protected, false otherwise.
  785. */
  786. function is_protected_meta( $meta_key, $meta_type = null ) {
  787. $protected = ( '_' == $meta_key[0] );
  788. /**
  789. * Filters whether a meta key is protected.
  790. *
  791. * @since 3.2.0
  792. *
  793. * @param bool $protected Whether the key is protected. Default false.
  794. * @param string $meta_key Meta key.
  795. * @param string $meta_type Meta type.
  796. */
  797. return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
  798. }
  799. /**
  800. * Sanitize meta value.
  801. *
  802. * @since 3.1.3
  803. * @since 4.9.8 The `$object_subtype` parameter was added.
  804. *
  805. * @param string $meta_key Meta key.
  806. * @param mixed $meta_value Meta value to sanitize.
  807. * @param string $object_type Type of object the meta is registered to.
  808. *
  809. * @return mixed Sanitized $meta_value.
  810. */
  811. function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) {
  812. if ( ! empty( $object_subtype ) && has_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) {
  813. /**
  814. * Filters the sanitization of a specific meta key of a specific meta type and subtype.
  815. *
  816. * The dynamic portions of the hook name, `$object_type`, `$meta_key`,
  817. * and `$object_subtype`, refer to the metadata object type (comment, post, term or user),
  818. * the meta key value, and the object subtype respectively.
  819. *
  820. * @since 4.9.8
  821. *
  822. * @param mixed $meta_value Meta value to sanitize.
  823. * @param string $meta_key Meta key.
  824. * @param string $object_type Object type.
  825. * @param string $object_subtype Object subtype.
  826. */
  827. return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $meta_value, $meta_key, $object_type, $object_subtype );
  828. }
  829. /**
  830. * Filters the sanitization of a specific meta key of a specific meta type.
  831. *
  832. * The dynamic portions of the hook name, `$meta_type`, and `$meta_key`,
  833. * refer to the metadata object type (comment, post, or user) and the meta
  834. * key value, respectively.
  835. *
  836. * @since 3.3.0
  837. *
  838. * @param mixed $meta_value Meta value to sanitize.
  839. * @param string $meta_key Meta key.
  840. * @param string $object_type Object type.
  841. */
  842. return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type );
  843. }
  844. /**
  845. * Registers a meta key.
  846. *
  847. * It is recommended to register meta keys for a specific combination of object type and object subtype. If passing
  848. * an object subtype is omitted, the meta key will be registered for the entire object type, however it can be partly
  849. * overridden in case a more specific meta key of the same name exists for the same object type and a subtype.
  850. *
  851. * If an object type does not support any subtypes, such as users or comments, you should commonly call this function
  852. * without passing a subtype.
  853. *
  854. * @since 3.3.0
  855. * @since 4.6.0 {@link https://core.trac.wordpress.org/ticket/35658 Modified
  856. * to support an array of data to attach to registered meta keys}. Previous arguments for
  857. * `$sanitize_callback` and `$auth_callback` have been folded into this array.
  858. * @since 4.9.8 The `$object_subtype` argument was added to the arguments array.
  859. *
  860. * @param string $object_type Type of object this meta is registered to.
  861. * @param string $meta_key Meta key to register.
  862. * @param array $args {
  863. * Data used to describe the meta key when registered.
  864. *
  865. * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type. If left empty,
  866. * the meta key will be registered on the entire object type. Default empty.
  867. * @type string $type The type of data associated with this meta key.
  868. * Valid values are 'string', 'boolean', 'integer', and 'number'.
  869. * @type string $description A description of the data attached to this meta key.
  870. * @type bool $single Whether the meta key has one value per object, or an array of values per object.
  871. * @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data.
  872. * @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.
  873. * @type bool $show_in_rest Whether data associated with this meta key can be considered public.
  874. * }
  875. * @param string|array $deprecated Deprecated. Use `$args` instead.
  876. *
  877. * @return bool True if the meta key was successfully registered in the global array, false if not.
  878. * Registering a meta key with distinct sanitize and auth callbacks will fire those
  879. * callbacks, but will not add to the global registry.
  880. */
  881. function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
  882. global $wp_meta_keys;
  883. if ( ! is_array( $wp_meta_keys ) ) {
  884. $wp_meta_keys = array();
  885. }
  886. $defaults = array(
  887. 'object_subtype' => '',
  888. 'type' => 'string',
  889. 'description' => '',
  890. 'single' => false,
  891. 'sanitize_callback' => null,
  892. 'auth_callback' => null,
  893. 'show_in_rest' => false,
  894. );
  895. // There used to be individual args for sanitize and auth callbacks
  896. $has_old_sanitize_cb = false;
  897. $has_old_auth_cb = false;
  898. if ( is_callable( $args ) ) {
  899. $args = array(
  900. 'sanitize_callback' => $args,
  901. );
  902. $has_old_sanitize_cb = true;
  903. } else {
  904. $args = (array) $args;
  905. }
  906. if ( is_callable( $deprecated ) ) {
  907. $args['auth_callback'] = $deprecated;
  908. $has_old_auth_cb = true;
  909. }
  910. /**
  911. * Filters the registration arguments when registering meta.
  912. *
  913. * @since 4.6.0
  914. *
  915. * @param array $args Array of meta registration arguments.
  916. * @param array $defaults Array of default arguments.
  917. * @param string $object_type Object type.
  918. * @param string $meta_key Meta key.
  919. */
  920. $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
  921. $args = wp_parse_args( $args, $defaults );
  922. $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : '';
  923. // If `auth_callback` is not provided, fall back to `is_protected_meta()`.
  924. if ( empty( $args['auth_callback'] ) ) {
  925. if ( is_protected_meta( $meta_key, $object_type ) ) {
  926. $args['auth_callback'] = '__return_false';
  927. } else {
  928. $args['auth_callback'] = '__return_true';
  929. }
  930. }
  931. // Back-compat: old sanitize and auth callbacks are applied to all of an object type.
  932. if ( is_callable( $args['sanitize_callback'] ) ) {
  933. if ( ! empty( $object_subtype ) ) {
  934. add_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'], 10, 4 );
  935. } else {
  936. add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 );
  937. }
  938. }
  939. if ( is_callable( $args['auth_callback'] ) ) {
  940. if ( ! empty( $object_subtype ) ) {
  941. add_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'], 10, 6 );
  942. } else {
  943. add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
  944. }
  945. }
  946. // Global registry only contains meta keys registered with the array of arguments added in 4.6.0.
  947. if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
  948. unset( $args['object_subtype'] );
  949. $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;
  950. return true;
  951. }
  952. return false;
  953. }
  954. /**
  955. * Checks if a meta key is registered.
  956. *
  957. * @since 4.6.0
  958. * @since 4.9.8 The `$object_subtype` parameter was added.
  959. *
  960. * @param string $object_type The type of object.
  961. * @param string $meta_key The meta key.
  962. * @param string $object_subtype Optional. The subtype of the object type.
  963. *
  964. * @return bool True if the meta key is registered to the object type and, if provided,
  965. * the object subtype. False if not.
  966. */
  967. function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) {
  968. $meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
  969. return isset( $meta_keys[ $meta_key ] );
  970. }
  971. /**
  972. * Unregisters a meta key from the list of registered keys.
  973. *
  974. * @since 4.6.0
  975. * @since 4.9.8 The `$object_subtype` parameter was added.
  976. *
  977. * @param string $object_type The type of object.
  978. * @param string $meta_key The meta key.
  979. * @param string $object_subtype Optional. The subtype of the object type.
  980. * @return bool True if successful. False if the meta key was not registered.
  981. */
  982. function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) {
  983. global $wp_meta_keys;
  984. if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
  985. return false;
  986. }
  987. $args = $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ];
  988. if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) {
  989. if ( ! empty( $object_subtype ) ) {
  990. remove_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'] );
  991. } else {
  992. remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] );
  993. }
  994. }
  995. if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) {
  996. if ( ! empty( $object_subtype ) ) {
  997. remove_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'] );
  998. } else {
  999. remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] );
  1000. }
  1001. }
  1002. unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] );
  1003. // Do some clean up
  1004. if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
  1005. unset( $wp_meta_keys[ $object_type ][ $object_subtype ] );
  1006. }
  1007. if ( empty( $wp_meta_keys[ $object_type ] ) ) {
  1008. unset( $wp_meta_keys[ $object_type ] );
  1009. }
  1010. return true;
  1011. }
  1012. /**
  1013. * Retrieves a list of registered meta keys for an object type.
  1014. *
  1015. * @since 4.6.0
  1016. * @since 4.9.8 The `$object_subtype` parameter was added.
  1017. *
  1018. * @param string $object_type The type of object. Post, comment, user, term.
  1019. * @param string $object_subtype Optional. The subtype of the object type.
  1020. * @return array List of registered meta keys.
  1021. */
  1022. function get_registered_meta_keys( $object_type, $object_subtype = '' ) {
  1023. global $wp_meta_keys;
  1024. if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
  1025. return array();
  1026. }
  1027. return $wp_meta_keys[ $object_type ][ $object_subtype ];
  1028. }
  1029. /**
  1030. * Retrieves registered metadata for a specified object.
  1031. *
  1032. * The results include both meta that is registered specifically for the
  1033. * object's subtype and meta that is registered for the entire object type.
  1034. *
  1035. * @since 4.6.0
  1036. *
  1037. * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user)
  1038. * @param int $object_id ID of the object the metadata is for.
  1039. * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered
  1040. * metadata for the specified object.
  1041. * @return mixed A single value or array of values for a key if specified. An array of all registered keys
  1042. * and values for an object ID if not. False if a given $meta_key is not registered.
  1043. */
  1044. function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
  1045. $object_subtype = get_object_subtype( $object_type, $object_id );
  1046. if ( ! empty( $meta_key ) ) {
  1047. if ( ! empty( $object_subtype ) && ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
  1048. $object_subtype = '';
  1049. }
  1050. if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
  1051. return false;
  1052. }
  1053. $meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
  1054. $meta_key_data = $meta_keys[ $meta_key ];
  1055. $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] );
  1056. return $data;
  1057. }
  1058. $data = get_metadata( $object_type, $object_id );
  1059. if ( ! $data ) {
  1060. return array();
  1061. }
  1062. $meta_keys = get_registered_meta_keys( $object_type );
  1063. if ( ! empty( $object_subtype ) ) {
  1064. $meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $object_type, $object_subtype ) );
  1065. }
  1066. return array_intersect_key( $data, $meta_keys );
  1067. }
  1068. /**
  1069. * Filter out `register_meta()` args based on a whitelist.
  1070. * `register_meta()` args may change over time, so requiring the whitelist
  1071. * to be explicitly turned off is a warranty seal of sorts.
  1072. *
  1073. * @access private
  1074. * @since 4.6.0
  1075. *
  1076. * @param array $args Arguments from `register_meta()`.
  1077. * @param array $default_args Default arguments for `register_meta()`.
  1078. *
  1079. * @return array Filtered arguments.
  1080. */
  1081. function _wp_register_meta_args_whitelist( $args, $default_args ) {
  1082. return array_intersect_key( $args, $default_args );
  1083. }
  1084. /**
  1085. * Returns the object subtype for a given object ID of a specific type.
  1086. *
  1087. * @since 4.9.8
  1088. *
  1089. * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user)
  1090. * @param int $object_id ID of the object to retrieve its subtype.
  1091. * @return string The object subtype or an empty string if unspecified subtype.
  1092. */
  1093. function get_object_subtype( $object_type, $object_id ) {
  1094. $object_id = (int) $object_id;
  1095. $object_subtype = '';
  1096. switch ( $object_type ) {
  1097. case 'post':
  1098. $post_type = get_post_type( $object_id );
  1099. if ( ! empty( $post_type ) ) {
  1100. $object_subtype = $post_type;
  1101. }
  1102. break;
  1103. case 'term':
  1104. $term = get_term( $object_id );
  1105. if ( ! $term instanceof WP_Term ) {
  1106. break;
  1107. }
  1108. $object_subtype = $term->taxonomy;
  1109. break;
  1110. case 'comment':
  1111. $comment = get_comment( $object_id );
  1112. if ( ! $comment ) {
  1113. break;
  1114. }
  1115. $object_subtype = 'comment';
  1116. break;
  1117. case 'user':
  1118. $user = get_user_by( 'id', $object_id );
  1119. if ( ! $user ) {
  1120. break;
  1121. }
  1122. $object_subtype = 'user';
  1123. break;
  1124. }
  1125. /**
  1126. * Filters the object subtype identifier for a non standard object type.
  1127. *
  1128. * The dynamic portion of the hook, `$object_type`, refers to the object
  1129. * type (post, comment, term, or user).
  1130. *
  1131. * @since 4.9.8
  1132. *
  1133. * @param string $object_subtype Empty string to override.
  1134. * @param int $object_id ID of the object to get the subtype for.
  1135. */
  1136. return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id );
  1137. }