Files
theoldfitz/inc/google-reviews.php

89 lines
2.8 KiB
PHP

<?php
/**
* Pulls the average star rating and review count from The Old Fitz's
* Google Business Profile via the Places API (New), cached to a local
* JSON file so we don't hit Google on every page load. Falls back to
* the numbers in config.php whenever there's no API key/Place ID yet,
* or the API call fails for any reason — the homepage never breaks
* because of this.
*/
function fritz_google_reviews(): array {
$fallback = [
'rating' => GOOGLE_FALLBACK_RATING,
'review_count' => GOOGLE_FALLBACK_REVIEW_COUNT,
'source' => 'fallback',
'fetched_at' => null,
];
if (GOOGLE_PLACES_API_KEY === '' || GOOGLE_PLACE_ID === '') {
return $fallback;
}
// In Docker, FITZ_CACHE_DIR points into the writable data/ mount, outside
// the web root, so deploys don't wipe it and www/ can stay read-only to
// www-data. Local dev falls back to cache/ in the project root.
$cacheDir = getenv('FITZ_CACHE_DIR') ?: __DIR__ . '/../cache';
$cacheFile = $cacheDir . '/google-reviews.json';
if (is_file($cacheFile)) {
$cached = json_decode((string) file_get_contents($cacheFile), true);
if (is_array($cached) && (time() - ($cached['fetched_at'] ?? 0)) < GOOGLE_STATS_CACHE_TTL) {
$cached['source'] = 'cache';
return $cached;
}
}
$fresh = fritz_fetch_google_reviews($fallback);
$fresh['fetched_at'] = time();
if (!is_dir($cacheDir)) {
@mkdir($cacheDir, 0775, true);
}
@file_put_contents($cacheFile, json_encode($fresh));
return $fresh;
}
function fritz_fetch_google_reviews(array $fallback): array {
if (!function_exists('curl_init')) {
return $fallback;
}
$url = sprintf(
'https://places.googleapis.com/v1/places/%s',
rawurlencode(GOOGLE_PLACE_ID)
);
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5,
CURLOPT_HTTPHEADER => [
'X-Goog-Api-Key: ' . GOOGLE_PLACES_API_KEY,
// Field mask is required by the New Places API (400 without
// it) and also determines the billing SKU — rating and
// userRatingCount are "Place Details (New) - Atmosphere".
'X-Goog-FieldMask: rating,userRatingCount',
],
]);
$body = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($body === false || $httpCode !== 200) {
return $fallback;
}
$data = json_decode($body, true);
if (!is_array($data) || isset($data['error'])) {
return $fallback;
}
return [
'rating' => $data['rating'] ?? $fallback['rating'],
'review_count' => $data['userRatingCount'] ?? $fallback['review_count'],
'source' => 'live',
];
}