UserMetaModel::getUserMeta PHP Method

getUserMeta() public method

This method takes a $UserID or array of $UserIDs, and a $Key. It converts the $Key to fully qualified format and then queries for the associated value(s). $Key can contain SQL wildcards, in which case multiple results can be returned. If $UserID is an array, the return value will be a multi dimensional array with the first axis containing UserIDs and the second containing fully qualified UserMetaKeys, associated with their values. If $UserID is a scalar, the return value will be a single dimensional array of $UserMetaKey => $Value pairs.
public getUserMeta ( $UserID, $Key = null, $Default = null ) : array
$UserID integer UserID or array of UserIDs
$Key string relative user meta key
$Default optional default return value if key is not found
return array results or $Default
    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;
    }