Nessuna descrizione

meta.php 59KB

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