public function getUserMeta($UserID, $Key = null, $Default = null)
{
if (Gdn::cache()->activeEnabled()) {
if (is_array($UserID)) {
$Result = array();
foreach ($UserID as $ID) {
$Meta = $this->GetUserMeta($ID, $Key, $Default);
$Result[$ID] = $Meta;
}
return $Result;
}
// Try and grab the user meta from the cache.
$CacheKey = 'UserMeta_' . $UserID;
$UserMeta = Gdn::cache()->get($CacheKey);
if ($UserMeta === Gdn_Cache::CACHEOP_FAILURE) {
$UserMeta = $this->getWhere(array('UserID' => $UserID), 'Name')->resultArray();
$UserMeta = array_column($UserMeta, 'Value', 'Name');
Gdn::cache()->store($CacheKey, $UserMeta);
}
if ($Key === null) {
return $UserMeta;
}
if (strpos($Key, '%') === false) {
$Result = val($Key, $UserMeta, $Default);
return array($Key => $Result);
}
$Regex = '`' . str_replace('%', '.*', preg_quote($Key)) . '`i';
$Result = array();
foreach ($UserMeta as $Name => $Value) {
if (preg_match($Regex, $Name)) {
$Result[$Name] = $Value;
}
}
return $Result;
}
$Sql = clone Gdn::sql();
$Sql->reset();
$UserMetaQuery = $Sql->select('*')->from('UserMeta u');
if (is_array($UserID)) {
$UserMetaQuery->whereIn('u.UserID', $UserID);
} else {
$UserMetaQuery->where('u.UserID', $UserID);
}
if (stristr($Key, '%')) {
$UserMetaQuery->where('u.Name like', $Key);
} else {
$UserMetaQuery->where('u.Name', $Key);
}
$UserMetaData = $UserMetaQuery->get();
$UserMeta = array();
if ($UserMetaData->numRows()) {
if (is_array($UserID)) {
while ($MetaRow = $UserMetaData->NextRow()) {
$UserMeta[$MetaRow->UserID][$MetaRow->Name] = $MetaRow->Value;
}
} else {
while ($MetaRow = $UserMetaData->NextRow()) {
$UserMeta[$MetaRow->Name] = $MetaRow->Value;
}
}
} else {
self::$MemoryCache[$Key] = $Default;
$UserMeta[$Key] = $Default;
}
unset($UserMetaData);
return $UserMeta;
}