PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/meta.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 344 lines | 169 code | 68 blank | 107 comment | 54 complexity | 3bd52eb2d2afa809cec0d02e2bedd96b MD5 | raw file
  1. <?php
  2. /**
  3. * 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. * @since 2.9.0
  12. */
  13. /**
  14. * Add metadata for the specified object.
  15. *
  16. * @since 2.9.0
  17. * @uses $wpdb WordPress database object for queries.
  18. * @uses do_action() Calls 'added_{$meta_type}_meta' with meta_id of added metadata entry,
  19. * object ID, meta key, and meta value
  20. *
  21. * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
  22. * @param int $object_id ID of the object metadata is for
  23. * @param string $meta_key Metadata key
  24. * @param string $meta_value Metadata value
  25. * @param bool $unique Optional, default is false. Whether the specified metadata key should be
  26. * unique for the object. If true, and the object already has a value for the specified
  27. * metadata key, no change will be made
  28. * @return bool True on successful update, false on failure.
  29. */
  30. function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) {
  31. if ( !$meta_type || !$meta_key )
  32. return false;
  33. if ( !$object_id = absint($object_id) )
  34. return false;
  35. if ( ! $table = _get_meta_table($meta_type) )
  36. return false;
  37. global $wpdb;
  38. $column = esc_sql($meta_type . '_id');
  39. // expected_slashed ($meta_key)
  40. $meta_key = stripslashes($meta_key);
  41. if ( $unique && $wpdb->get_var( $wpdb->prepare(
  42. "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",
  43. $meta_key, $object_id ) ) )
  44. return false;
  45. $_meta_value = $meta_value;
  46. $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
  47. $wpdb->insert( $table, array(
  48. $column => $object_id,
  49. 'meta_key' => $meta_key,
  50. 'meta_value' => $meta_value
  51. ) );
  52. wp_cache_delete($object_id, $meta_type . '_meta');
  53. // users cache stores usermeta that must be cleared.
  54. if ( 'user' == $meta_type )
  55. clean_user_cache($object_id);
  56. do_action( "added_{$meta_type}_meta", $wpdb->insert_id, $object_id, $meta_key, $_meta_value );
  57. return true;
  58. }
  59. /**
  60. * Update metadata for the specified object. If no value already exists for the specified object
  61. * ID and metadata key, the metadata will be added.
  62. *
  63. * @since 2.9.0
  64. * @uses $wpdb WordPress database object for queries.
  65. * @uses do_action() Calls 'update_{$meta_type}_meta' before updating metadata with meta_id of
  66. * metadata entry to update, object ID, meta key, and meta value
  67. * @uses do_action() Calls 'updated_{$meta_type}_meta' after updating metadata with meta_id of
  68. * updated metadata entry, object ID, meta key, and meta value
  69. *
  70. * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
  71. * @param int $object_id ID of the object metadata is for
  72. * @param string $meta_key Metadata key
  73. * @param string $meta_value Metadata value
  74. * @param string $prev_value Optional. If specified, only update existing metadata entries with
  75. * the specified value. Otherwise, update all entries.
  76. * @return bool True on successful update, false on failure.
  77. */
  78. function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') {
  79. if ( !$meta_type || !$meta_key )
  80. return false;
  81. if ( !$object_id = absint($object_id) )
  82. return false;
  83. if ( ! $table = _get_meta_table($meta_type) )
  84. return false;
  85. global $wpdb;
  86. $column = esc_sql($meta_type . '_id');
  87. $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
  88. // expected_slashed ($meta_key)
  89. $meta_key = stripslashes($meta_key);
  90. if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )
  91. return add_metadata($meta_type, $object_id, $meta_key, $meta_value);
  92. // Compare existing value to new value if no prev value given and the key exists only once.
  93. if ( empty($prev_value) ) {
  94. $old_value = get_metadata($meta_type, $object_id, $meta_key);
  95. if ( count($old_value) == 1 ) {
  96. if ( $old_value[0] == $meta_value )
  97. return false;
  98. }
  99. }
  100. $_meta_value = $meta_value;
  101. $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
  102. $data = compact( 'meta_value' );
  103. $where = array( $column => $object_id, 'meta_key' => $meta_key );
  104. if ( !empty( $prev_value ) ) {
  105. $prev_value = maybe_serialize($prev_value);
  106. $where['meta_value'] = $prev_value;
  107. }
  108. do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
  109. $wpdb->update( $table, $data, $where );
  110. wp_cache_delete($object_id, $meta_type . '_meta');
  111. // users cache stores usermeta that must be cleared.
  112. if ( 'user' == $meta_type )
  113. clean_user_cache($object_id);
  114. do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
  115. return true;
  116. }
  117. /**
  118. * Delete metadata for the specified object.
  119. *
  120. * @since 2.9.0
  121. * @uses $wpdb WordPress database object for queries.
  122. * @uses do_action() Calls 'deleted_{$meta_type}_meta' after deleting with meta_id of
  123. * deleted metadata entries, object ID, meta key, and meta value
  124. *
  125. * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
  126. * @param int $object_id ID of the object metadata is for
  127. * @param string $meta_key Metadata key
  128. * @param string $meta_value Optional. Metadata value. If specified, only delete metadata entries
  129. * with this value. Otherwise, delete all entries with the specified meta_key.
  130. * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries
  131. * for all objects, ignoring the specified object_id. Otherwise, only delete matching
  132. * metadata entries for the specified object_id.
  133. * @return bool True on successful delete, false on failure.
  134. */
  135. function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false) {
  136. if ( !$meta_type || !$meta_key )
  137. return false;
  138. if ( (!$object_id = absint($object_id)) && !$delete_all )
  139. return false;
  140. if ( ! $table = _get_meta_table($meta_type) )
  141. return false;
  142. global $wpdb;
  143. $type_column = esc_sql($meta_type . '_id');
  144. $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
  145. // expected_slashed ($meta_key)
  146. $meta_key = stripslashes($meta_key);
  147. $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
  148. $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
  149. if ( !$delete_all )
  150. $query .= $wpdb->prepare(" AND $type_column = %d", $object_id );
  151. if ( $meta_value )
  152. $query .= $wpdb->prepare(" AND meta_value = %s", $meta_value );
  153. $meta_ids = $wpdb->get_col( $query );
  154. if ( !count( $meta_ids ) )
  155. return false;
  156. $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )";
  157. $count = $wpdb->query($query);
  158. if ( !$count )
  159. return false;
  160. wp_cache_delete($object_id, $meta_type . '_meta');
  161. // users cache stores usermeta that must be cleared.
  162. if ( 'user' == $meta_type )
  163. clean_user_cache($object_id);
  164. do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $meta_value );
  165. return true;
  166. }
  167. /**
  168. * Retrieve metadata for the specified object.
  169. *
  170. * @since 2.9.0
  171. *
  172. * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
  173. * @param int $object_id ID of the object metadata is for
  174. * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
  175. * the specified object.
  176. * @param bool $single Optional, default is false. If true, return only the first value of the
  177. * specified meta_key. This parameter has no effect if meta_key is not specified.
  178. * @return string|array Single metadata value, or array of values
  179. */
  180. function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
  181. if ( !$meta_type )
  182. return false;
  183. if ( !$object_id = absint($object_id) )
  184. return false;
  185. $meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
  186. if ( !$meta_cache ) {
  187. update_meta_cache($meta_type, $object_id);
  188. $meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
  189. }
  190. if ( ! $meta_key )
  191. return $meta_cache;
  192. if ( isset($meta_cache[$meta_key]) ) {
  193. if ( $single )
  194. return maybe_unserialize( $meta_cache[$meta_key][0] );
  195. else
  196. return array_map('maybe_unserialize', $meta_cache[$meta_key]);
  197. }
  198. if ($single)
  199. return '';
  200. else
  201. return array();
  202. }
  203. /**
  204. * Update the metadata cache for the specified objects.
  205. *
  206. * @since 2.9.0
  207. * @uses $wpdb WordPress database object for queries.
  208. *
  209. * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
  210. * @param int|array $object_ids array or comma delimited list of object IDs to update cache for
  211. * @return mixed Metadata cache for the specified objects, or false on failure.
  212. */
  213. function update_meta_cache($meta_type, $object_ids) {
  214. if ( empty( $meta_type ) || empty( $object_ids ) )
  215. return false;
  216. if ( ! $table = _get_meta_table($meta_type) )
  217. return false;
  218. $column = esc_sql($meta_type . '_id');
  219. global $wpdb;
  220. if ( !is_array($object_ids) ) {
  221. $object_ids = preg_replace('|[^0-9,]|', '', $object_ids);
  222. $object_ids = explode(',', $object_ids);
  223. }
  224. $object_ids = array_map('intval', $object_ids);
  225. $cache_key = $meta_type . '_meta';
  226. $ids = array();
  227. foreach ( $object_ids as $id ) {
  228. if ( false === wp_cache_get($id, $cache_key) )
  229. $ids[] = $id;
  230. }
  231. if ( empty( $ids ) )
  232. return false;
  233. // Get meta info
  234. $id_list = join(',', $ids);
  235. $cache = array();
  236. $meta_list = $wpdb->get_results( $wpdb->prepare("SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list)",
  237. $meta_type), ARRAY_A );
  238. if ( !empty($meta_list) ) {
  239. foreach ( $meta_list as $metarow) {
  240. $mpid = intval($metarow[$column]);
  241. $mkey = $metarow['meta_key'];
  242. $mval = $metarow['meta_value'];
  243. // Force subkeys to be array type:
  244. if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
  245. $cache[$mpid] = array();
  246. if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
  247. $cache[$mpid][$mkey] = array();
  248. // Add a value to the current pid/key:
  249. $cache[$mpid][$mkey][] = $mval;
  250. }
  251. }
  252. foreach ( $ids as $id ) {
  253. if ( ! isset($cache[$id]) )
  254. $cache[$id] = array();
  255. }
  256. foreach ( array_keys($cache) as $object)
  257. wp_cache_set($object, $cache[$object], $cache_key);
  258. return $cache;
  259. }
  260. /**
  261. * Retrieve the name of the metadata table for the specified object type.
  262. *
  263. * @since 2.9.0
  264. * @uses $wpdb WordPress database object for queries.
  265. *
  266. * @param string $meta_type Type of object to get metadata table for (e.g., comment, post, or user)
  267. * @return mixed Metadata table name, or false if no metadata table exists
  268. */
  269. function _get_meta_table($type) {
  270. global $wpdb;
  271. $table_name = $type . 'meta';
  272. if ( empty($wpdb->$table_name) )
  273. return false;
  274. return $wpdb->$table_name;
  275. }
  276. ?>