rendered_maps, true)) { $this->rendered_maps[] = $map_id; } } /** * Output Schema.org JSON-LD */ public function output_schema() { if (empty($this->rendered_maps)) { return; } $all_places = array(); foreach ($this->rendered_maps as $map_id) { $places = $this->get_map_places($map_id); $all_places = array_merge($all_places, $places); } if (empty($all_places)) { return; } $schema = array( '@context' => 'https://schema.org', '@type' => 'ItemList', 'name' => get_bloginfo('name') . ' - Locations', 'itemListElement' => array(), ); foreach ($all_places as $index => $place) { $schema['itemListElement'][] = array( '@type' => 'ListItem', 'position' => $index + 1, 'item' => $place, ); } echo '' . "\n"; } /** * Get places from a map */ private function get_map_places($map_id) { $pins = MXIMAP_Post_Type::readable_pins(get_post_meta($map_id, '_mximap_pins', true)); if (empty($pins) || !is_array($pins)) { return array(); } $places = array(); foreach ($pins as $pin) { // Only add pins with labels (meaningful locations) if (empty($pin['label'])) { continue; } $place = array( '@type' => 'Place', 'name' => sanitize_text_field($pin['label']), ); // Add URL if available if (!empty($pin['url'])) { $place['url'] = esc_url($pin['url']); } // Add geo coordinates if available if (!empty($pin['latitude']) && !empty($pin['longitude']) && is_numeric($pin['latitude']) && is_numeric($pin['longitude']) && is_finite((float) $pin['latitude']) && is_finite((float) $pin['longitude'])) { $place['geo'] = array( '@type' => 'GeoCoordinates', 'latitude' => floatval($pin['latitude']), 'longitude' => floatval($pin['longitude']), ); } $places[] = $place; } return $places; } } /** * Helper function to trigger map rendered action */ function mximap_map_rendered($map_id) { do_action('mximap_map_rendered', $map_id); }