// 1. Fetch + cache live Google rating 
function bali_eco_deli_get_google_rating() {
	$cache_key = 'bali_eco_deli_google_rating'; 
	$cache_ttl = 24 * HOUR_IN_SECONDS; 	// refresh daily 
	$cached = get_transient($cache_key);
	if ($cached) { 
        return $cached; 
	} 
	
    // CONFIG 
	$api_key = 'AIzaSyDYicYrnTLXYzeXBdbP2ifv46yFa1AeqLI'; 
	$place_id = 'ChIJtyxZygpt0i0RnOnelbdsHh0'; 
	
	if (!$api_key || !$place_id) return false; 
	
	$url = add_query_arg([ 
		'place_id' => $place_id,
		'fields' => 'rating,user_ratings_total', 
		'key' => $api_key 
	], 'https://maps.googleapis.com/maps/api/place/details/json'); 
	
	$response = wp_remote_get($url, ['timeout' => 8]); 
	if (is_wp_error($response)) return false; 
	
	$body = json_decode(wp_remote_retrieve_body($response), true); 
	if (empty($body['result']['rating'])) return false;
	
	$data = [ 
		'ratingValue' => (string) $body['result']['rating'], 
		'reviewCount' => (string) $body['result']['user_ratings_total']
	]; 
	
	set_transient($cache_key, $data, $cache_ttl); 
	return $data; 
} 

// 2. Inject schema (WITH EXCLUSION FOR ABOUT PAGE)
add_action('wp_head', function () {
    
    // --- ADDED EXCLUSION LOGIC HERE ---
    // This tells WordPress: "If this is the about-us page, stop and don't print this code."
    if ( is_page('about-bali-eco-deli-nusa-lembongan') ) {
        return;
    }
    // ----------------------------------

	$rating = bali_eco_deli_get_google_rating(); 
	
    // Fallback values if API fails 
	$ratingValue = $rating ? $rating['ratingValue'] : "4.7";
	$reviewCount = $rating ? $rating['reviewCount'] : "1880"; 
?> 

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@graph": [
    {
      "@type": "Restaurant",
      "@id": "https://baliecodeli.net/#restaurant",
      "name": "Bali ECO Deli",
      "url": "https://baliecodeli.net/restaurant-location-nusa-lembongan/",
      "logo": "https://baliecodeli.net/wp-content/uploads/2026/01/Logo-Bali-ECO-Deli-600.webp",
      "image": "https://baliecodeli.net/wp-content/uploads/2026/02/hero_home.webp",
      "description": "Healthy and eco-conscious café and restaurant in Jungut Batu, Nusa Lembongan, serving fresh, nourishing food with vegetarian and vegan options.",
      "telephone": "0821-3732-7521",
      "priceRange": "$$",
      "servesCuisine": ["Healthy","Cafe","Brunch","Vegetarian","Vegan"],
      "amenityFeature": [
        {
          "@type": "LocationFeatureSpecification",
          "name": "High-Speed Starlink WiFi",
          "value": true
        },
        {
          "@type": "LocationFeatureSpecification",
          "name": "Solar Powered",
          "value": true
        }
      ],
      "knowsAbout": [
        "Sustainability",
        "Plastic Recycling Program",
        "Composting",
        "Palm Oil Free Cooking"
      ],
      "keywords": "Starlink WiFi, Healthy Cafe Lembongan, Solar Powered Restaurant, Plastic Recycling Bali",
      "address": {
        "@type": "PostalAddress",
        "streetAddress": "Jl. Jungutbatu",
        "addressLocality": "Nusa Lembongan",
        "addressRegion": "Kabupaten Klungkung, Bali",
        "postalCode": "80771",
        "addressCountry": "ID"
      },
      "containedInPlace": {
        "@type": "Place",
        "name": "Kec. Nusa Penida"
      },
      "geo": {
        "@type": "GeoCoordinates",
        "latitude": -8.671957,
        "longitude": 115.4493599
      },
      "openingHoursSpecification": {
        "@type": "OpeningHoursSpecification",
        "dayOfWeek": ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
        "opens": "07:00",
        "closes": "21:00"
      },
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "<?php echo esc_js($ratingValue); ?>",
        "reviewCount": "<?php echo esc_js($reviewCount); ?>"
      },
      "sameAs": [
        "https://www.instagram.com/baliecodeli",
        "https://www.facebook.com/baliecodeli"
      ],
	  "hasMenu": { "@id": "https://baliecodeli.net/bali-eco-deli-menu-nusa-lembongan/#menu"  },
      "hasMap": "https://www.google.com/maps/place/?q=place_id:ChIJtyxZygpt0i0RnOnelbdsHh0",
      "mainEntityOfPage": "https://baliecodeli.net/restaurant-location-nusa-lembongan/"
    },
    {
      "@type": "WebSite",
      "@id": "https://baliecodeli.net/#website",
      "url": "https://baliecodeli.net/",
      "name": "Bali ECO Deli",
      "publisher": { "@id": "https://baliecodeli.net/#restaurant" }
    }
  ]
}
</script>

<?php });