meta.php 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  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_id 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 string[]|false {
  1088. * Array containing JOIN and WHERE SQL clauses to append to the main query,
  1089. * or false if no table exists for the requested meta type.
  1090. *
  1091. * @type string $join SQL fragment to append to the main JOIN clause.
  1092. * @type string $where SQL fragment to append to the main WHERE clause.
  1093. * }
  1094. */
  1095. function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) {
  1096. $meta_query_obj = new WP_Meta_Query( $meta_query );
  1097. return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
  1098. }
  1099. /**
  1100. * Retrieves the name of the metadata table for the specified object type.
  1101. *
  1102. * @since 2.9.0
  1103. *
  1104. * @global wpdb $wpdb WordPress database abstraction object.
  1105. *
  1106. * @param string $type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1107. * or any other object type with an associated meta table.
  1108. * @return string|false Metadata table name, or false if no metadata table exists
  1109. */
  1110. function _get_meta_table( $type ) {
  1111. global $wpdb;
  1112. $table_name = $type . 'meta';
  1113. if ( empty( $wpdb->$table_name ) ) {
  1114. return false;
  1115. }
  1116. return $wpdb->$table_name;
  1117. }
  1118. /**
  1119. * Determines whether a meta key is considered protected.
  1120. *
  1121. * @since 3.1.3
  1122. *
  1123. * @param string $meta_key Metadata key.
  1124. * @param string $meta_type Optional. Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1125. * or any other object type with an associated meta table. Default empty.
  1126. * @return bool Whether the meta key is considered protected.
  1127. */
  1128. function is_protected_meta( $meta_key, $meta_type = '' ) {
  1129. $sanitized_key = preg_replace( "/[^\x20-\x7E\p{L}]/", '', $meta_key );
  1130. $protected = strlen( $sanitized_key ) > 0 && ( '_' === $sanitized_key[0] );
  1131. /**
  1132. * Filters whether a meta key is considered protected.
  1133. *
  1134. * @since 3.2.0
  1135. *
  1136. * @param bool $protected Whether the key is considered protected.
  1137. * @param string $meta_key Metadata key.
  1138. * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1139. * or any other object type with an associated meta table.
  1140. */
  1141. return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
  1142. }
  1143. /**
  1144. * Sanitizes meta value.
  1145. *
  1146. * @since 3.1.3
  1147. * @since 4.9.8 The `$object_subtype` parameter was added.
  1148. *
  1149. * @param string $meta_key Metadata key.
  1150. * @param mixed $meta_value Metadata value to sanitize.
  1151. * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1152. * or any other object type with an associated meta table.
  1153. * @param string $object_subtype Optional. The subtype of the object type.
  1154. * @return mixed Sanitized $meta_value.
  1155. */
  1156. function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) {
  1157. if ( ! empty( $object_subtype ) && has_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) {
  1158. /**
  1159. * Filters the sanitization of a specific meta key of a specific meta type and subtype.
  1160. *
  1161. * The dynamic portions of the hook name, `$object_type`, `$meta_key`,
  1162. * and `$object_subtype`, refer to the metadata object type (comment, post, term, or user),
  1163. * the meta key value, and the object subtype respectively.
  1164. *
  1165. * @since 4.9.8
  1166. *
  1167. * @param mixed $meta_value Metadata value to sanitize.
  1168. * @param string $meta_key Metadata key.
  1169. * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1170. * or any other object type with an associated meta table.
  1171. * @param string $object_subtype Object subtype.
  1172. */
  1173. return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $meta_value, $meta_key, $object_type, $object_subtype );
  1174. }
  1175. /**
  1176. * Filters the sanitization of a specific meta key of a specific meta type.
  1177. *
  1178. * The dynamic portions of the hook name, `$meta_type`, and `$meta_key`,
  1179. * refer to the metadata object type (comment, post, term, or user) and the meta
  1180. * key value, respectively.
  1181. *
  1182. * @since 3.3.0
  1183. *
  1184. * @param mixed $meta_value Metadata value to sanitize.
  1185. * @param string $meta_key Metadata key.
  1186. * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1187. * or any other object type with an associated meta table.
  1188. */
  1189. return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type );
  1190. }
  1191. /**
  1192. * Registers a meta key.
  1193. *
  1194. * It is recommended to register meta keys for a specific combination of object type and object subtype. If passing
  1195. * an object subtype is omitted, the meta key will be registered for the entire object type, however it can be partly
  1196. * overridden in case a more specific meta key of the same name exists for the same object type and a subtype.
  1197. *
  1198. * If an object type does not support any subtypes, such as users or comments, you should commonly call this function
  1199. * without passing a subtype.
  1200. *
  1201. * @since 3.3.0
  1202. * @since 4.6.0 {@link https://core.trac.wordpress.org/ticket/35658 Modified
  1203. * to support an array of data to attach to registered meta keys}. Previous arguments for
  1204. * `$sanitize_callback` and `$auth_callback` have been folded into this array.
  1205. * @since 4.9.8 The `$object_subtype` argument was added to the arguments array.
  1206. * @since 5.3.0 Valid meta types expanded to include "array" and "object".
  1207. * @since 5.5.0 The `$default` argument was added to the arguments array.
  1208. *
  1209. * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1210. * or any other object type with an associated meta table.
  1211. * @param string $meta_key Meta key to register.
  1212. * @param array $args {
  1213. * Data used to describe the meta key when registered.
  1214. *
  1215. * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type. If left empty,
  1216. * the meta key will be registered on the entire object type. Default empty.
  1217. * @type string $type The type of data associated with this meta key.
  1218. * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
  1219. * @type string $description A description of the data attached to this meta key.
  1220. * @type bool $single Whether the meta key has one value per object, or an array of values per object.
  1221. * @type mixed $default The default value returned from get_metadata() if no value has been set yet.
  1222. * When using a non-single meta key, the default value is for the first entry.
  1223. * In other words, when calling get_metadata() with `$single` set to `false`,
  1224. * the default value given here will be wrapped in an array.
  1225. * @type callable $sanitize_callback A function or method to call when sanitizing `$meta_key` data.
  1226. * @type callable $auth_callback Optional. A function or method to call when performing edit_post_meta,
  1227. * add_post_meta, and delete_post_meta capability checks.
  1228. * @type bool|array $show_in_rest Whether data associated with this meta key can be considered public and
  1229. * should be accessible via the REST API. A custom post type must also declare
  1230. * support for custom fields for registered meta to be accessible via REST.
  1231. * When registering complex meta values this argument may optionally be an
  1232. * array with 'schema' or 'prepare_callback' keys instead of a boolean.
  1233. * }
  1234. * @param string|array $deprecated Deprecated. Use `$args` instead.
  1235. * @return bool True if the meta key was successfully registered in the global array, false if not.
  1236. * Registering a meta key with distinct sanitize and auth callbacks will fire those callbacks,
  1237. * but will not add to the global registry.
  1238. */
  1239. function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
  1240. global $wp_meta_keys;
  1241. if ( ! is_array( $wp_meta_keys ) ) {
  1242. $wp_meta_keys = array();
  1243. }
  1244. $defaults = array(
  1245. 'object_subtype' => '',
  1246. 'type' => 'string',
  1247. 'description' => '',
  1248. 'default' => '',
  1249. 'single' => false,
  1250. 'sanitize_callback' => null,
  1251. 'auth_callback' => null,
  1252. 'show_in_rest' => false,
  1253. );
  1254. // There used to be individual args for sanitize and auth callbacks.
  1255. $has_old_sanitize_cb = false;
  1256. $has_old_auth_cb = false;
  1257. if ( is_callable( $args ) ) {
  1258. $args = array(
  1259. 'sanitize_callback' => $args,
  1260. );
  1261. $has_old_sanitize_cb = true;
  1262. } else {
  1263. $args = (array) $args;
  1264. }
  1265. if ( is_callable( $deprecated ) ) {
  1266. $args['auth_callback'] = $deprecated;
  1267. $has_old_auth_cb = true;
  1268. }
  1269. /**
  1270. * Filters the registration arguments when registering meta.
  1271. *
  1272. * @since 4.6.0
  1273. *
  1274. * @param array $args Array of meta registration arguments.
  1275. * @param array $defaults Array of default arguments.
  1276. * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1277. * or any other object type with an associated meta table.
  1278. * @param string $meta_key Meta key.
  1279. */
  1280. $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
  1281. unset( $defaults['default'] );
  1282. $args = wp_parse_args( $args, $defaults );
  1283. // Require an item schema when registering array meta.
  1284. if ( false !== $args['show_in_rest'] && 'array' === $args['type'] ) {
  1285. if ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) {
  1286. _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' );
  1287. return false;
  1288. }
  1289. }
  1290. $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : '';
  1291. // If `auth_callback` is not provided, fall back to `is_protected_meta()`.
  1292. if ( empty( $args['auth_callback'] ) ) {
  1293. if ( is_protected_meta( $meta_key, $object_type ) ) {
  1294. $args['auth_callback'] = '__return_false';
  1295. } else {
  1296. $args['auth_callback'] = '__return_true';
  1297. }
  1298. }
  1299. // Back-compat: old sanitize and auth callbacks are applied to all of an object type.
  1300. if ( is_callable( $args['sanitize_callback'] ) ) {
  1301. if ( ! empty( $object_subtype ) ) {
  1302. add_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'], 10, 4 );
  1303. } else {
  1304. add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 );
  1305. }
  1306. }
  1307. if ( is_callable( $args['auth_callback'] ) ) {
  1308. if ( ! empty( $object_subtype ) ) {
  1309. add_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'], 10, 6 );
  1310. } else {
  1311. add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
  1312. }
  1313. }
  1314. if ( array_key_exists( 'default', $args ) ) {
  1315. $schema = $args;
  1316. if ( is_array( $args['show_in_rest'] ) && isset( $args['show_in_rest']['schema'] ) ) {
  1317. $schema = array_merge( $schema, $args['show_in_rest']['schema'] );
  1318. }
  1319. $check = rest_validate_value_from_schema( $args['default'], $schema );
  1320. if ( is_wp_error( $check ) ) {
  1321. _doing_it_wrong( __FUNCTION__, __( 'When registering a default meta value the data must match the type provided.' ), '5.5.0' );
  1322. return false;
  1323. }
  1324. if ( ! has_filter( "default_{$object_type}_metadata", 'filter_default_metadata' ) ) {
  1325. add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 );
  1326. }
  1327. }
  1328. // Global registry only contains meta keys registered with the array of arguments added in 4.6.0.
  1329. if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
  1330. unset( $args['object_subtype'] );
  1331. $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;
  1332. return true;
  1333. }
  1334. return false;
  1335. }
  1336. /**
  1337. * Filters into default_{$object_type}_metadata and adds in default value.
  1338. *
  1339. * @since 5.5.0
  1340. *
  1341. * @param mixed $value Current value passed to filter.
  1342. * @param int $object_id ID of the object metadata is for.
  1343. * @param string $meta_key Metadata key.
  1344. * @param bool $single If true, return only the first value of the specified `$meta_key`.
  1345. * This parameter has no effect if `$meta_key` is not specified.
  1346. * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1347. * or any other object type with an associated meta table.
  1348. * @return mixed An array of default values if `$single` is false.
  1349. * The default value of the meta field if `$single` is true.
  1350. */
  1351. function filter_default_metadata( $value, $object_id, $meta_key, $single, $meta_type ) {
  1352. global $wp_meta_keys;
  1353. if ( wp_installing() ) {
  1354. return $value;
  1355. }
  1356. if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $meta_type ] ) ) {
  1357. return $value;
  1358. }
  1359. $defaults = array();
  1360. foreach ( $wp_meta_keys[ $meta_type ] as $sub_type => $meta_data ) {
  1361. foreach ( $meta_data as $_meta_key => $args ) {
  1362. if ( $_meta_key === $meta_key && array_key_exists( 'default', $args ) ) {
  1363. $defaults[ $sub_type ] = $args;
  1364. }
  1365. }
  1366. }
  1367. if ( ! $defaults ) {
  1368. return $value;
  1369. }
  1370. // If this meta type does not have subtypes, then the default is keyed as an empty string.
  1371. if ( isset( $defaults[''] ) ) {
  1372. $metadata = $defaults[''];
  1373. } else {
  1374. $sub_type = get_object_subtype( $meta_type, $object_id );
  1375. if ( ! isset( $defaults[ $sub_type ] ) ) {
  1376. return $value;
  1377. }
  1378. $metadata = $defaults[ $sub_type ];
  1379. }
  1380. if ( $single ) {
  1381. $value = $metadata['default'];
  1382. } else {
  1383. $value = array( $metadata['default'] );
  1384. }
  1385. return $value;
  1386. }
  1387. /**
  1388. * Checks if a meta key is registered.
  1389. *
  1390. * @since 4.6.0
  1391. * @since 4.9.8 The `$object_subtype` parameter was added.
  1392. *
  1393. * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1394. * or any other object type with an associated meta table.
  1395. * @param string $meta_key Metadata key.
  1396. * @param string $object_subtype Optional. The subtype of the object type.
  1397. * @return bool True if the meta key is registered to the object type and, if provided,
  1398. * the object subtype. False if not.
  1399. */
  1400. function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) {
  1401. $meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
  1402. return isset( $meta_keys[ $meta_key ] );
  1403. }
  1404. /**
  1405. * Unregisters a meta key from the list of registered keys.
  1406. *
  1407. * @since 4.6.0
  1408. * @since 4.9.8 The `$object_subtype` parameter was added.
  1409. *
  1410. * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1411. * or any other object type with an associated meta table.
  1412. * @param string $meta_key Metadata key.
  1413. * @param string $object_subtype Optional. The subtype of the object type.
  1414. * @return bool True if successful. False if the meta key was not registered.
  1415. */
  1416. function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) {
  1417. global $wp_meta_keys;
  1418. if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
  1419. return false;
  1420. }
  1421. $args = $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ];
  1422. if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) {
  1423. if ( ! empty( $object_subtype ) ) {
  1424. remove_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'] );
  1425. } else {
  1426. remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] );
  1427. }
  1428. }
  1429. if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) {
  1430. if ( ! empty( $object_subtype ) ) {
  1431. remove_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'] );
  1432. } else {
  1433. remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] );
  1434. }
  1435. }
  1436. unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] );
  1437. // Do some clean up.
  1438. if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
  1439. unset( $wp_meta_keys[ $object_type ][ $object_subtype ] );
  1440. }
  1441. if ( empty( $wp_meta_keys[ $object_type ] ) ) {
  1442. unset( $wp_meta_keys[ $object_type ] );
  1443. }
  1444. return true;
  1445. }
  1446. /**
  1447. * Retrieves a list of registered metadata args for an object type, keyed by their meta keys.
  1448. *
  1449. * @since 4.6.0
  1450. * @since 4.9.8 The `$object_subtype` parameter was added.
  1451. *
  1452. * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1453. * or any other object type with an associated meta table.
  1454. * @param string $object_subtype Optional. The subtype of the object type.
  1455. * @return array[] List of registered metadata args, keyed by their meta keys.
  1456. */
  1457. function get_registered_meta_keys( $object_type, $object_subtype = '' ) {
  1458. global $wp_meta_keys;
  1459. if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
  1460. return array();
  1461. }
  1462. return $wp_meta_keys[ $object_type ][ $object_subtype ];
  1463. }
  1464. /**
  1465. * Retrieves registered metadata for a specified object.
  1466. *
  1467. * The results include both meta that is registered specifically for the
  1468. * object's subtype and meta that is registered for the entire object type.
  1469. *
  1470. * @since 4.6.0
  1471. *
  1472. * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1473. * or any other object type with an associated meta table.
  1474. * @param int $object_id ID of the object the metadata is for.
  1475. * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered
  1476. * metadata for the specified object.
  1477. * @return mixed A single value or array of values for a key if specified. An array of all registered keys
  1478. * and values for an object ID if not. False if a given $meta_key is not registered.
  1479. */
  1480. function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
  1481. $object_subtype = get_object_subtype( $object_type, $object_id );
  1482. if ( ! empty( $meta_key ) ) {
  1483. if ( ! empty( $object_subtype ) && ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
  1484. $object_subtype = '';
  1485. }
  1486. if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
  1487. return false;
  1488. }
  1489. $meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
  1490. $meta_key_data = $meta_keys[ $meta_key ];
  1491. $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] );
  1492. return $data;
  1493. }
  1494. $data = get_metadata( $object_type, $object_id );
  1495. if ( ! $data ) {
  1496. return array();
  1497. }
  1498. $meta_keys = get_registered_meta_keys( $object_type );
  1499. if ( ! empty( $object_subtype ) ) {
  1500. $meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $object_type, $object_subtype ) );
  1501. }
  1502. return array_intersect_key( $data, $meta_keys );
  1503. }
  1504. /**
  1505. * Filters out `register_meta()` args based on an allowed list.
  1506. *
  1507. * `register_meta()` args may change over time, so requiring the allowed list
  1508. * to be explicitly turned off is a warranty seal of sorts.
  1509. *
  1510. * @access private
  1511. * @since 5.5.0
  1512. *
  1513. * @param array $args Arguments from `register_meta()`.
  1514. * @param array $default_args Default arguments for `register_meta()`.
  1515. * @return array Filtered arguments.
  1516. */
  1517. function _wp_register_meta_args_allowed_list( $args, $default_args ) {
  1518. return array_intersect_key( $args, $default_args );
  1519. }
  1520. /**
  1521. * Returns the object subtype for a given object ID of a specific type.
  1522. *
  1523. * @since 4.9.8
  1524. *
  1525. * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1526. * or any other object type with an associated meta table.
  1527. * @param int $object_id ID of the object to retrieve its subtype.
  1528. * @return string The object subtype or an empty string if unspecified subtype.
  1529. */
  1530. function get_object_subtype( $object_type, $object_id ) {
  1531. $object_id = (int) $object_id;
  1532. $object_subtype = '';
  1533. switch ( $object_type ) {
  1534. case 'post':
  1535. $post_type = get_post_type( $object_id );
  1536. if ( ! empty( $post_type ) ) {
  1537. $object_subtype = $post_type;
  1538. }
  1539. break;
  1540. case 'term':
  1541. $term = get_term( $object_id );
  1542. if ( ! $term instanceof WP_Term ) {
  1543. break;
  1544. }
  1545. $object_subtype = $term->taxonomy;
  1546. break;
  1547. case 'comment':
  1548. $comment = get_comment( $object_id );
  1549. if ( ! $comment ) {
  1550. break;
  1551. }
  1552. $object_subtype = 'comment';
  1553. break;
  1554. case 'user':
  1555. $user = get_user_by( 'id', $object_id );
  1556. if ( ! $user ) {
  1557. break;
  1558. }
  1559. $object_subtype = 'user';
  1560. break;
  1561. }
  1562. /**
  1563. * Filters the object subtype identifier for a non-standard object type.
  1564. *
  1565. * The dynamic portion of the hook name, `$object_type`, refers to the meta object type
  1566. * (post, comment, term, user, or any other type with an associated meta table).
  1567. *
  1568. * Possible hook names include:
  1569. *
  1570. * - `get_object_subtype_post`
  1571. * - `get_object_subtype_comment`
  1572. * - `get_object_subtype_term`
  1573. * - `get_object_subtype_user`
  1574. *
  1575. * @since 4.9.8
  1576. *
  1577. * @param string $object_subtype Empty string to override.
  1578. * @param int $object_id ID of the object to get the subtype for.
  1579. */
  1580. return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id );
  1581. }