| [ Index ] |
PHP Cross Reference of zeList |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Links Metas functions 4 * @from WordPress 2.7.1 5 */ 6 7 /** 8 * Add meta data field to a link. 9 * 10 * link meta data is called "Custom Fields" on the Administration Panels. 11 * 12 * @since 1.5.0 13 * @uses $wpdb 14 * @link http://codex.wordpress.org/Function_Reference/add_link_meta 15 * 16 * @param int $link_id link ID. 17 * @param string $key Metadata name. 18 * @param mixed $value Metadata value. 19 * @param bool $unique Optional, default is false. Whether the same key should not be added. 20 * @return bool False for failure. True for success. 21 */ 22 function add_link_meta($link_id, $meta_key, $meta_value, $unique = false) { 23 global $wpdb; 24 25 // expected_slashed ($meta_key) 26 $meta_key = stripslashes($meta_key); 27 28 if ( $unique && $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->linkmeta WHERE meta_key = %s AND link_id = %d", $meta_key, $link_id ) ) ) 29 return false; 30 31 $meta_value = maybe_serialize( stripslashes_deep($meta_value) ); 32 33 $wpdb->insert( $wpdb->linkmeta, compact( 'link_id', 'meta_key', 'meta_value' ) ); 34 35 wp_cache_delete($link_id, 'link_meta'); 36 37 return true; 38 } 39 40 /** 41 * Remove metadata matching criteria from a link. 42 * 43 * You can match based on the key, or key and value. Removing based on key and 44 * value, will keep from removing duplicate metadata with the same key. It also 45 * allows removing all metadata matching key, if needed. 46 * 47 * @since 1.5.0 48 * @uses $wpdb 49 * @link http://codex.wordpress.org/Function_Reference/delete_link_meta 50 * 51 * @param int $link_id link ID 52 * @param string $meta_key Metadata name. 53 * @param mixed $meta_value Optional. Metadata value. 54 * @return bool False for failure. True for success. 55 */ 56 function delete_link_meta($link_id, $meta_key, $meta_value = '') { 57 global $wpdb; 58 59 // expected_slashed ($meta_key, $meta_value) 60 $meta_key = stripslashes( $meta_key ); 61 $meta_value = maybe_serialize( stripslashes_deep($meta_value) ); 62 63 if ( empty( $meta_value ) ) 64 $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->linkmeta WHERE link_id = %d AND meta_key = %s", $link_id, $meta_key ) ); 65 else 66 $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM $wpdb->linkmeta WHERE link_id = %d AND meta_key = %s AND meta_value = %s", $link_id, $meta_key, $meta_value ) ); 67 68 if ( !$meta_id ) 69 return false; 70 71 if ( empty( $meta_value ) ) 72 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->linkmeta WHERE link_id = %d AND meta_key = %s", $link_id, $meta_key ) ); 73 else 74 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->linkmeta WHERE link_id = %d AND meta_key = %s AND meta_value = %s", $link_id, $meta_key, $meta_value ) ); 75 76 wp_cache_delete($link_id, 'link_meta'); 77 78 return true; 79 } 80 81 /** 82 * Retrieve link meta field for a link. 83 * 84 * @since 1.5.0 85 * @uses $wpdb 86 * @link http://codex.wordpress.org/Function_Reference/get_link_meta 87 * 88 * @param int $link_id link ID. 89 * @param string $key The meta key to retrieve. 90 * @param bool $single Whether to return a single value. 91 * @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. 92 */ 93 function get_link_meta($link_id, $key, $single = false) { 94 $link_id = (int) $link_id; 95 96 $meta_cache = wp_cache_get($link_id, 'link_meta'); 97 98 if ( !$meta_cache ) { 99 update_linkmeta_cache($link_id); 100 $meta_cache = wp_cache_get($link_id, 'link_meta'); 101 } 102 103 if ( isset($meta_cache[$key]) ) { 104 if ( $single ) { 105 return maybe_unserialize( $meta_cache[$key][0] ); 106 } else { 107 return array_map('maybe_unserialize', $meta_cache[$key]); 108 } 109 } 110 111 return ''; 112 } 113 114 /** 115 * Update link meta field based on link ID. 116 * 117 * Use the $prev_value parameter to differentiate between meta fields with the 118 * same key and link ID. 119 * 120 * If the meta field for the link does not exist, it will be added. 121 * 122 * @since 1.5 123 * @uses $wpdb 124 * @link http://codex.wordpress.org/Function_Reference/update_link_meta 125 * 126 * @param int $link_id link ID. 127 * @param string $key Metadata key. 128 * @param mixed $value Metadata value. 129 * @param mixed $prev_value Optional. Previous value to check before removing. 130 * @return bool False on failure, true if success. 131 */ 132 function update_link_meta($link_id, $meta_key, $meta_value, $prev_value = '') { 133 global $wpdb; 134 135 // expected_slashed ($meta_key) 136 $meta_key = stripslashes($meta_key); 137 138 if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->linkmeta WHERE meta_key = %s AND link_id = %d", $meta_key, $link_id ) ) ) { 139 return add_link_meta($link_id, $meta_key, $meta_value); 140 } 141 142 $meta_value = maybe_serialize( stripslashes_deep($meta_value) ); 143 144 $data = compact( 'meta_value' ); 145 $where = compact( 'meta_key', 'link_id' ); 146 147 if ( !empty( $prev_value ) ) { 148 $prev_value = maybe_serialize($prev_value); 149 $where['meta_value'] = $prev_value; 150 } 151 152 $wpdb->update( $wpdb->linkmeta, $data, $where ); 153 wp_cache_delete($link_id, 'link_meta'); 154 return true; 155 } 156 157 /** 158 * Delete everything from link meta matching meta key. 159 * 160 * @since 2.3.0 161 * @uses $wpdb 162 * 163 * @param string $link_meta_key Key to search for when deleting. 164 * @return bool Whether the link meta key was deleted from the database 165 */ 166 function delete_link_meta_by_key($link_meta_key) { 167 global $wpdb; 168 if ( $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->linkmeta WHERE meta_key = %s", $link_meta_key)) ) { 169 /** @todo Get link_ids and delete cache */ 170 // wp_cache_delete($link_id, 'link_meta'); 171 return true; 172 } 173 return false; 174 } 175 176 /** 177 * Retrieve link meta fields, based on link ID. 178 * 179 * The link meta fields are retrieved from the cache, so the function is 180 * optimized to be called more than once. It also applies to the functions, that 181 * use this function. 182 * 183 * @since 1.2.0 184 * @link http://codex.wordpress.org/Function_Reference/get_link_custom 185 * 186 * @uses $link_id Current Loop link ID 187 * 188 * @param int $link_id link ID 189 * @return array 190 */ 191 function get_link_custom($link_id = 0) { 192 global $link_id; 193 194 if ( !$link_id ) 195 $link_id = (int) $link_id; 196 197 $link_id = (int) $link_id; 198 199 if ( ! wp_cache_get($link_id, 'link_meta') ) 200 update_linkmeta_cache($link_id); 201 202 return wp_cache_get($link_id, 'link_meta'); 203 } 204 205 206 /** 207 * Retrieve meta field names for a link. 208 * 209 * If there are no meta fields, then nothing (null) will be returned. 210 * 211 * @since 1.2.0 212 * @link http://codex.wordpress.org/Function_Reference/get_link_custom_keys 213 * 214 * @param int $link_id link ID 215 * @return array|null Either array of the keys, or null if keys could not be retrieved. 216 */ 217 function get_link_custom_keys( $link_id = 0 ) { 218 $custom = get_link_custom( $link_id ); 219 220 if ( !is_array($custom) ) 221 return; 222 223 if ( $keys = array_keys($custom) ) 224 return $keys; 225 } 226 227 /** 228 * Retrieve values for a custom link field. 229 * 230 * The parameters must not be considered optional. All of the link meta fields 231 * will be retrieved and only the meta field key values returned. 232 * 233 * @since 1.2.0 234 * @link http://codex.wordpress.org/Function_Reference/get_link_custom_values 235 * 236 * @param string $key Meta field key. 237 * @param int $link_id link ID 238 * @return array Meta field values. 239 */ 240 function get_link_custom_values( $key = '', $link_id = 0 ) { 241 $custom = get_link_custom($link_id); 242 243 return isset($custom[$key]) ? $custom[$key] : null; 244 } 245 246 247 /** 248 * Updates metadata cache for list of link IDs. 249 * 250 * Performs SQL query to retrieve the metadata for the link IDs and updates the 251 * metadata cache for the links. Therefore, the functions, which call this 252 * function, do not need to perform SQL queries on their own. 253 * 254 * @package WordPress 255 * @subpackage Cache 256 * @since 2.1.0 257 * 258 * @uses $wpdb 259 * 260 * @param array $link_ids List of link IDs. 261 * @return bool|array Returns false if there is nothing to update or an array of metadata. 262 */ 263 function update_linkmeta_cache($link_ids) { 264 global $wpdb; 265 266 if ( empty( $link_ids ) ) 267 return false; 268 269 if ( !is_array($link_ids) ) { 270 $link_ids = preg_replace('|[^0-9,]|', '', $link_ids); 271 $link_ids = explode(',', $link_ids); 272 } 273 274 $link_ids = array_map('intval', $link_ids); 275 276 $ids = array(); 277 foreach ( (array) $link_ids as $id ) { 278 if ( false === wp_cache_get($id, 'link_meta') ) 279 $ids[] = $id; 280 } 281 282 if ( empty( $ids ) ) 283 return false; 284 285 // Get link-meta info 286 $id_list = join(',', $ids); 287 $cache = array(); 288 if ( $meta_list = $wpdb->get_results("SELECT link_id, meta_key, meta_value FROM $wpdb->linkmeta WHERE link_id IN ($id_list)", ARRAY_A) ) { 289 foreach ( (array) $meta_list as $metarow) { 290 $mpid = (int) $metarow['link_id']; 291 $mkey = $metarow['meta_key']; 292 $mval = $metarow['meta_value']; 293 294 // Force subkeys to be array type: 295 if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) ) 296 $cache[$mpid] = array(); 297 if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) ) 298 $cache[$mpid][$mkey] = array(); 299 300 // Add a value to the current pid/key: 301 $cache[$mpid][$mkey][] = $mval; 302 } 303 } 304 305 foreach ( (array) $ids as $id ) { 306 if ( ! isset($cache[$id]) ) 307 $cache[$id] = array(); 308 } 309 310 foreach ( (array) array_keys($cache) as $link) 311 wp_cache_set($link, $cache[$link], 'link_meta'); 312 313 return $cache; 314 } 315 316
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat May 30 23:51:06 2009 | Cross-referenced by PHPXref 0.7 |