web/wp-includes/meta.php
changeset 136 bde1974c263b
child 194 32102edaa81b
equal deleted inserted replaced
135:53cff4b4a802 136:bde1974c263b
       
     1 <?php
       
     2 /**
       
     3  * Meta API
       
     4  *
       
     5  * Functions for retrieving and manipulating metadata
       
     6  *
       
     7  * @package WordPress
       
     8  * @subpackage Meta
       
     9  * @since 2.9.0
       
    10  */
       
    11 
       
    12 function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) {
       
    13 	if ( !$meta_type || !$meta_key )
       
    14 		return false;
       
    15 
       
    16 	if ( ! $table = _get_meta_table($meta_type) )
       
    17 		return false;
       
    18 
       
    19 	global $wpdb;
       
    20 
       
    21 	$column = esc_sql($meta_type . '_id');
       
    22 
       
    23 	// expected_slashed ($meta_key)
       
    24 	$meta_key = stripslashes($meta_key);
       
    25 
       
    26 	if ( $unique && $wpdb->get_var( $wpdb->prepare(
       
    27 		"SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",
       
    28 		$meta_key, $object_id ) ) )
       
    29 		return false;
       
    30 
       
    31 	$meta_value = maybe_serialize( stripslashes_deep($meta_value) );
       
    32 
       
    33 	$wpdb->insert( $table, array(
       
    34 		$column => $object_id,
       
    35 		'meta_key' => $meta_key,
       
    36 		'meta_value' => $meta_value
       
    37 	) );
       
    38 
       
    39 	wp_cache_delete($object_id, $meta_type . '_meta');
       
    40 
       
    41 	do_action( "added_{$meta_type}_meta", $wpdb->insert_id, $object_id, $meta_key, $meta_value );
       
    42 
       
    43 	return true;
       
    44 }
       
    45 
       
    46 function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') {
       
    47 	if ( !$meta_type || !$meta_key )
       
    48 		return false;
       
    49 
       
    50 	if ( ! $table = _get_meta_table($meta_type) )
       
    51 		return false;
       
    52 
       
    53 	global $wpdb;
       
    54 
       
    55 	$column = esc_sql($meta_type . '_id');
       
    56 	$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
       
    57 
       
    58 	// expected_slashed ($meta_key)
       
    59 	$meta_key = stripslashes($meta_key);
       
    60 
       
    61 	if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )
       
    62 		return add_metadata($meta_type, $object_id, $meta_key, $meta_value);
       
    63 
       
    64 	$meta_value = maybe_serialize( stripslashes_deep($meta_value) );
       
    65 
       
    66 	$data  = compact( 'meta_value' );
       
    67 	$where = array( $column => $object_id, 'meta_key' => $meta_key );
       
    68 
       
    69 	if ( !empty( $prev_value ) ) {
       
    70 		$prev_value = maybe_serialize($prev_value);
       
    71 		$where['meta_value'] = $prev_value;
       
    72 	}
       
    73 
       
    74 	do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $meta_value );
       
    75 
       
    76 	$wpdb->update( $table, $data, $where );
       
    77 	wp_cache_delete($object_id, $meta_type . '_meta');
       
    78 
       
    79 	do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $meta_value );
       
    80 
       
    81 	return true;
       
    82 }
       
    83 
       
    84 function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false) {
       
    85 	if ( !$meta_type || !$meta_key || (!$delete_all && ! (int)$object_id) )
       
    86 		return false;
       
    87 
       
    88 	if ( ! $table = _get_meta_table($meta_type) )
       
    89 		return false;
       
    90 
       
    91 	global $wpdb;
       
    92 
       
    93 	$type_column = esc_sql($meta_type . '_id');
       
    94 	$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
       
    95 	// expected_slashed ($meta_key)
       
    96 	$meta_key = stripslashes($meta_key);
       
    97 	$meta_value = maybe_serialize( stripslashes_deep($meta_value) );
       
    98 
       
    99 	$query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
       
   100 
       
   101 	if ( !$delete_all )
       
   102 		$query .= $wpdb->prepare(" AND $type_column = %d", $object_id );
       
   103 
       
   104 	if ( $meta_value )
       
   105 		$query .= $wpdb->prepare(" AND meta_value = %s", $meta_value );
       
   106 
       
   107 	$meta_ids = $wpdb->get_col( $query );
       
   108 	if ( !count( $meta_ids ) )
       
   109 		return false;
       
   110 
       
   111 	$query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )";
       
   112 
       
   113 	$count = $wpdb->query($query);
       
   114 
       
   115 	if ( !$count )
       
   116 		return false;
       
   117 
       
   118 	wp_cache_delete($object_id, $meta_type . '_meta');
       
   119 
       
   120 	do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $meta_value );
       
   121 
       
   122 	return true;
       
   123 }
       
   124 
       
   125 function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
       
   126 	if ( !$meta_type )
       
   127 		return false;
       
   128 
       
   129 	$meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
       
   130 
       
   131 	if ( !$meta_cache ) {
       
   132 		update_meta_cache($meta_type, $object_id);
       
   133 		$meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
       
   134 	}
       
   135 
       
   136 	if ( ! $meta_key )
       
   137 		return $meta_cache;
       
   138 
       
   139 	if ( isset($meta_cache[$meta_key]) ) {
       
   140 		if ( $single ) {
       
   141 			return maybe_unserialize( $meta_cache[$meta_key][0] );
       
   142 		} else {
       
   143 			return array_map('maybe_unserialize', $meta_cache[$meta_key]);
       
   144 		}
       
   145 	}
       
   146 
       
   147 	if ($single)
       
   148 		return '';
       
   149 	else
       
   150 		return array();
       
   151 }
       
   152 
       
   153 function update_meta_cache($meta_type, $object_ids) {
       
   154 	if ( empty( $meta_type ) || empty( $object_ids ) )
       
   155 		return false;
       
   156 
       
   157 	if ( ! $table = _get_meta_table($meta_type) )
       
   158 		return false;
       
   159 
       
   160 	$column = esc_sql($meta_type . '_id');
       
   161 
       
   162 	global $wpdb;
       
   163 
       
   164 	if ( !is_array($object_ids) ) {
       
   165 		$object_ids = preg_replace('|[^0-9,]|', '', $object_ids);
       
   166 		$object_ids = explode(',', $object_ids);
       
   167 	}
       
   168 
       
   169 	$object_ids = array_map('intval', $object_ids);
       
   170 
       
   171 	$cache_key = $meta_type . '_meta';
       
   172 	$ids = array();
       
   173 	foreach ( $object_ids as $id ) {
       
   174 		if ( false === wp_cache_get($id, $cache_key) )
       
   175 			$ids[] = $id;
       
   176 	}
       
   177 
       
   178 	if ( empty( $ids ) )
       
   179 		return false;
       
   180 
       
   181 	// Get meta info
       
   182 	$id_list = join(',', $ids);
       
   183 	$cache = array();
       
   184 	$meta_list = $wpdb->get_results( $wpdb->prepare("SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list)",
       
   185 		$meta_type), ARRAY_A );
       
   186 
       
   187 	if ( !empty($meta_list) ) {
       
   188 		foreach ( $meta_list as $metarow) {
       
   189 			$mpid = intval($metarow[$column]);
       
   190 			$mkey = $metarow['meta_key'];
       
   191 			$mval = $metarow['meta_value'];
       
   192 
       
   193 			// Force subkeys to be array type:
       
   194 			if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
       
   195 				$cache[$mpid] = array();
       
   196 			if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
       
   197 				$cache[$mpid][$mkey] = array();
       
   198 
       
   199 			// Add a value to the current pid/key:
       
   200 			$cache[$mpid][$mkey][] = $mval;
       
   201 		}
       
   202 	}
       
   203 
       
   204 	foreach ( $ids as $id ) {
       
   205 		if ( ! isset($cache[$id]) )
       
   206 			$cache[$id] = array();
       
   207 	}
       
   208 
       
   209 	foreach ( array_keys($cache) as $object)
       
   210 		wp_cache_set($object, $cache[$object], $cache_key);
       
   211 
       
   212 	return $cache;
       
   213 }
       
   214 
       
   215 function _get_meta_table($type) {
       
   216 	global $wpdb;
       
   217 
       
   218 	$table_name = $type . 'meta';
       
   219 
       
   220 	if ( empty($wpdb->$table_name) )
       
   221 		return false;
       
   222 
       
   223 	return $wpdb->$table_name;
       
   224 }
       
   225 ?>