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