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