meta.php 47 KB

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