public function getCommentCounts()
{
$this->AllowJSONP(true);
$vanilla_identifier = val('vanilla_identifier', $_GET);
if (!is_array($vanilla_identifier)) {
$vanilla_identifier = array($vanilla_identifier);
}
$vanilla_identifier = array_unique($vanilla_identifier);
$FinalData = array_fill_keys($vanilla_identifier, 0);
$Misses = array();
$CacheKey = 'embed.comments.count.%s';
$OriginalIDs = array();
foreach ($vanilla_identifier as $ForeignID) {
$HashedForeignID = ForeignIDHash($ForeignID);
// Keep record of non-hashed identifiers for the reply
$OriginalIDs[$HashedForeignID] = $ForeignID;
$RealCacheKey = sprintf($CacheKey, $HashedForeignID);
$Comments = Gdn::cache()->get($RealCacheKey);
if ($Comments !== Gdn_Cache::CACHEOP_FAILURE) {
$FinalData[$ForeignID] = $Comments;
} else {
$Misses[] = $HashedForeignID;
}
}
if (sizeof($Misses)) {
$CountData = Gdn::sql()->select('ForeignID, CountComments')->from('Discussion')->where('Type', 'page')->whereIn('ForeignID', $Misses)->get()->resultArray();
foreach ($CountData as $Row) {
// Get original identifier to send back
$ForeignID = $OriginalIDs[$Row['ForeignID']];
$FinalData[$ForeignID] = $Row['CountComments'];
// Cache using the hashed identifier
$RealCacheKey = sprintf($CacheKey, $Row['ForeignID']);
Gdn::cache()->store($RealCacheKey, $Row['CountComments'], array(Gdn_Cache::FEATURE_EXPIRY => 60));
}
}
$this->setData('CountData', $FinalData);
$this->DeliveryMethod = DELIVERY_METHOD_JSON;
$this->DeliveryType = DELIVERY_TYPE_DATA;
$this->render();
}