| 1 |
<?php |
| 2 |
/** |
| 3 |
* Line Shortcode |
| 4 |
* |
| 5 |
* Use with [leaflet-line ...] |
| 6 |
* |
| 7 |
* @param array $atts user-input array |
| 8 |
* @param string $content user-input content (allows HTML) |
| 9 |
* @return string content for post/page |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly |
| 13 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 14 |
|
| 15 |
include_once(LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/class.shortcode.php'); |
| 16 |
|
| 17 |
class Leaflet_Line_Shortcode extends Leaflet_Shortcode { |
| 18 |
protected function getHTML ($atts='', $content=null) { |
| 19 |
if (!empty($atts)) extract($atts); |
| 20 |
|
| 21 |
$style_json = $this->LM->get_style_json( $atts ); |
| 22 |
|
| 23 |
$fitbounds = empty($fitbounds) ? 0 : $fitbounds; |
| 24 |
|
| 25 |
// backwards compatible `fitline` |
| 26 |
if (!empty($fitline)) { |
| 27 |
$fitbounds = $fitline; |
| 28 |
} |
| 29 |
|
| 30 |
$locations = Array(); |
| 31 |
|
| 32 |
if (!empty($addresses)) { |
| 33 |
include_once(LEAFLET_MAP__PLUGIN_DIR . 'class.geocoder.php'); |
| 34 |
$addresses = preg_split('/\s?[;|\/]\s?/', $addresses); |
| 35 |
foreach ($addresses as $address) { |
| 36 |
if (trim($address)) { |
| 37 |
$location = new Leaflet_Geocoder( $address ); |
| 38 |
$locations[] = Array($location->lat, $location->lng); |
| 39 |
} |
| 40 |
} |
| 41 |
} else if (!empty($latlngs)) { |
| 42 |
$latlngs = preg_split('/\s?[;|\/]\s?/', $latlngs); |
| 43 |
foreach ($latlngs as $latlng) { |
| 44 |
if (trim($latlng)) { |
| 45 |
$locations[] = array_map('floatval', preg_split('/\s?,\s?/', $latlng)); |
| 46 |
} |
| 47 |
} |
| 48 |
} else if (!empty($coordinates)) { |
| 49 |
$coordinates = preg_split('/\s?[;|\/]\s?/', $coordinates); |
| 50 |
foreach ($coordinates as $xy) { |
| 51 |
if (trim($xy)) { |
| 52 |
$locations[] = array_map('floatval', preg_split('/\s?,\s?/', $xy)); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
$location_json = json_encode($locations); |
| 58 |
ob_start(); |
| 59 |
?> |
| 60 |
<script> |
| 61 |
WPLeafletMapPlugin.add(function () { |
| 62 |
var previous_map = WPLeafletMapPlugin.getCurrentMap(), |
| 63 |
line = L.polyline(<?php echo $location_json; ?>, <?php echo $style_json; ?>), |
| 64 |
fitbounds = <?php echo $fitbounds; ?>; |
| 65 |
line.addTo( previous_map ); |
| 66 |
if (fitbounds) { |
| 67 |
// zoom the map to the polyline |
| 68 |
previous_map.fitBounds( line.getBounds() ); |
| 69 |
} |
| 70 |
<?php |
| 71 |
$this->LM->add_popup_to_shape($atts, $content, 'line'); |
| 72 |
?> |
| 73 |
WPLeafletMapPlugin.lines.push( line ); |
| 74 |
}); |
| 75 |
</script> |
| 76 |
<?php |
| 77 |
|
| 78 |
return ob_get_clean(); |
| 79 |
} |
| 80 |
} |