Keine Beschreibung

meta.php 61KB

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