| 1 |
<?php |
| 2 |
/** |
| 3 |
* Leaflet Map Class File |
| 4 |
* |
| 5 |
* PHP Version 5.5 |
| 6 |
* |
| 7 |
* @category Admin |
| 8 |
* @author Benjamin J DeLong <ben@bozdoz.com> |
| 9 |
*/ |
| 10 |
|
| 11 |
// Exit if accessed directly |
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Leaflet Map Class |
| 18 |
*/ |
| 19 |
class Leaflet_Map |
| 20 |
{ |
| 21 |
|
| 22 |
/** |
| 23 |
* Leaflet version |
| 24 |
* |
| 25 |
* @var string major minor patch version |
| 26 |
*/ |
| 27 |
public static $leaflet_version = '1.3.4'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Number of maps on page; used for unique map ids |
| 31 |
* |
| 32 |
* @var int $map_count |
| 33 |
*/ |
| 34 |
public $map_count = 0; |
| 35 |
|
| 36 |
/** |
| 37 |
* Files to include upon init |
| 38 |
* |
| 39 |
* @var array $_shortcodes |
| 40 |
*/ |
| 41 |
private $_shortcodes = array( |
| 42 |
'leaflet-geojson' => array( |
| 43 |
'file' => 'class.geojson-shortcode.php', |
| 44 |
'class' => 'Leaflet_Geojson_Shortcode' |
| 45 |
), |
| 46 |
'leaflet-image' => array( |
| 47 |
'file' => 'class.image-shortcode.php', |
| 48 |
'class' => 'Leaflet_Image_Shortcode' |
| 49 |
), |
| 50 |
'leaflet-kml' => array( |
| 51 |
'file' => 'class.kml-shortcode.php', |
| 52 |
'class' => 'Leaflet_Kml_Shortcode' |
| 53 |
), |
| 54 |
'leaflet-gpx' => array( |
| 55 |
'file' => 'class.gpx-shortcode.php', |
| 56 |
'class' => 'Leaflet_Gpx_Shortcode' |
| 57 |
), |
| 58 |
'leaflet-line' => array( |
| 59 |
'file' => 'class.line-shortcode.php', |
| 60 |
'class' => 'Leaflet_Line_Shortcode' |
| 61 |
), |
| 62 |
'leaflet-circle' => array( |
| 63 |
'file' => 'class.circle-shortcode.php', |
| 64 |
'class' => 'Leaflet_Circle_Shortcode' |
| 65 |
), |
| 66 |
'leaflet-map' => array( |
| 67 |
'file' => 'class.map-shortcode.php', |
| 68 |
'class' => 'Leaflet_Map_Shortcode' |
| 69 |
), |
| 70 |
'leaflet-marker' => array( |
| 71 |
'file' => 'class.marker-shortcode.php', |
| 72 |
'class' => 'Leaflet_Marker_Shortcode' |
| 73 |
) |
| 74 |
); |
| 75 |
|
| 76 |
/** |
| 77 |
* Singleton Instance of Leaflet Map |
| 78 |
* |
| 79 |
* @var Leaflet_Map |
| 80 |
**/ |
| 81 |
private static $instance = null; |
| 82 |
|
| 83 |
/** |
| 84 |
* Singleton init Function |
| 85 |
* |
| 86 |
* @static |
| 87 |
*/ |
| 88 |
public static function init() { |
| 89 |
if ( !self::$instance ) { |
| 90 |
self::$instance = new self; |
| 91 |
} |
| 92 |
|
| 93 |
return self::$instance; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Leaflet_Map Constructor |
| 98 |
*/ |
| 99 |
private function __construct() { |
| 100 |
$this->init_hooks(); |
| 101 |
$this->add_shortcodes(); |
| 102 |
|
| 103 |
// loaded |
| 104 |
do_action('leaflet_map_loaded'); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Add actions and filters |
| 109 |
*/ |
| 110 |
private function init_hooks() |
| 111 |
{ |
| 112 |
|
| 113 |
// Leaflet_Map_Plugin_Settings |
| 114 |
include_once LEAFLET_MAP__PLUGIN_DIR . 'class.plugin-settings.php'; |
| 115 |
|
| 116 |
// Leaflet_Map_Admin |
| 117 |
include_once LEAFLET_MAP__PLUGIN_DIR . 'class.admin.php'; |
| 118 |
|
| 119 |
// init admin |
| 120 |
Leaflet_Map_Admin::init(); |
| 121 |
|
| 122 |
add_action( 'plugins_loaded', array('Leaflet_Map', 'load_text_domain' )); |
| 123 |
|
| 124 |
add_action( 'wp_enqueue_scripts', array('Leaflet_Map', 'enqueue_and_register') ); |
| 125 |
|
| 126 |
/* |
| 127 |
allows maps on excerpts |
| 128 |
todo? should be optional somehow (admin setting?) |
| 129 |
*/ |
| 130 |
add_filter('the_excerpt', 'do_shortcode'); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Includes and adds shortcodes |
| 135 |
*/ |
| 136 |
private function add_shortcodes() |
| 137 |
{ |
| 138 |
// shortcodes |
| 139 |
$shortcode_dir = LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/'; |
| 140 |
|
| 141 |
foreach ($this->_shortcodes as $shortcode => $details) { |
| 142 |
include_once $shortcode_dir . $details['file']; |
| 143 |
add_shortcode($shortcode, array($details['class'], 'shortcode')); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Triggered when user uninstalls/removes plugin |
| 149 |
*/ |
| 150 |
public static function uninstall() |
| 151 |
{ |
| 152 |
// remove settings in db |
| 153 |
// think it needs to be included again (because __construct |
| 154 |
// won't need to execute) |
| 155 |
include_once LEAFLET_MAP__PLUGIN_DIR . 'class.plugin-settings.php'; |
| 156 |
$settings = Leaflet_Map_Plugin_Settings::init(); |
| 157 |
$settings->reset(); |
| 158 |
|
| 159 |
// remove geocoder locations in db |
| 160 |
include_once LEAFLET_MAP__PLUGIN_DIR . 'class.geocoder.php'; |
| 161 |
Leaflet_Geocoder::remove_caches(); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Loads Translations |
| 166 |
*/ |
| 167 |
public static function load_text_domain() |
| 168 |
{ |
| 169 |
load_plugin_textdomain( 'leaflet-map', false, dirname( plugin_basename( LEAFLET_MAP__PLUGIN_FILE ) ) . '/languages/' ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Enqueue and register styles and scripts (called in __construct) |
| 174 |
*/ |
| 175 |
public static function enqueue_and_register() |
| 176 |
{ |
| 177 |
/* defaults from db */ |
| 178 |
// Leaflet_Map_Plugin_Settings |
| 179 |
include_once LEAFLET_MAP__PLUGIN_DIR . 'class.plugin-settings.php'; |
| 180 |
$settings = Leaflet_Map_Plugin_Settings::init(); |
| 181 |
|
| 182 |
$js_url = $settings->get('js_url'); |
| 183 |
$css_url = $settings->get('css_url'); |
| 184 |
|
| 185 |
wp_register_style('leaflet_stylesheet', $css_url, Array(), null, false); |
| 186 |
wp_register_script('leaflet_js', $js_url, Array(), null, true); |
| 187 |
|
| 188 |
// new required MapQuest javascript file |
| 189 |
$tiling_service = $settings->get('default_tiling_service'); |
| 190 |
|
| 191 |
if ($tiling_service == 'mapquest') { |
| 192 |
$mapquest_js_url = 'https://www.mapquestapi.com/sdk/leaflet/v2.2/mq-map.js?key=%s'; |
| 193 |
$mq_appkey = $settings->get('mapquest_appkey'); |
| 194 |
$mapquest_js_url = sprintf($mapquest_js_url, $mq_appkey); |
| 195 |
|
| 196 |
wp_register_script('leaflet_mapquest_plugin', $mapquest_js_url, Array('leaflet_js'), '2.0', true); |
| 197 |
} |
| 198 |
|
| 199 |
// optional ajax geojson plugin |
| 200 |
wp_register_script('tmcw_togeojson', 'https://cdn.rawgit.com/mapbox/togeojson/master/togeojson.js', Array('jquery'), LEAFLET_MAP__PLUGIN_VERSION, false); |
| 201 |
|
| 202 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 203 |
$minified = ''; |
| 204 |
} else { |
| 205 |
$minified = '.min'; |
| 206 |
} |
| 207 |
|
| 208 |
wp_register_script('leaflet_ajax_geojson_js', plugins_url(sprintf('scripts/leaflet-ajax-geojson%s.js', $minified), __FILE__), Array('tmcw_togeojson', 'leaflet_js'), LEAFLET_MAP__PLUGIN_VERSION, false); |
| 209 |
|
| 210 |
wp_register_script('leaflet_svg_icon_js', plugins_url(sprintf('scripts/leaflet-svg-icon%s.js', $minified), __FILE__), Array('leaflet_js'), LEAFLET_MAP__PLUGIN_VERSION, false); |
| 211 |
|
| 212 |
/* run a construct function in the document head for subsequent functions to use (it is lightweight) */ |
| 213 |
wp_enqueue_script('wp_leaflet_map', plugins_url(sprintf('scripts/construct-leaflet-map%s.js', $minified), __FILE__), Array('leaflet_js'), LEAFLET_MAP__PLUGIN_VERSION, false); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Filter for removing nulls from array |
| 218 |
* |
| 219 |
* @param array $arr |
| 220 |
* |
| 221 |
* @return array with nulls removed |
| 222 |
*/ |
| 223 |
public function filter_null($arr) |
| 224 |
{ |
| 225 |
if (!function_exists('remove_null')) { |
| 226 |
function remove_null ($var) { |
| 227 |
return $var !== null; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
return array_filter($arr, 'remove_null'); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Sanitize JSON |
| 236 |
* |
| 237 |
* Takes options for filtering/correcting inputs for use in JavaScript |
| 238 |
* |
| 239 |
* @param array $arr user-input array |
| 240 |
* @param array $args array with key-value definitions on how to convert values |
| 241 |
* @return array corrected for JavaScript |
| 242 |
*/ |
| 243 |
public function json_sanitize($arr, $args) |
| 244 |
{ |
| 245 |
// remove nulls |
| 246 |
$arr = self::filter_null($arr); |
| 247 |
|
| 248 |
// sanitize output |
| 249 |
$args = array_intersect_key($args, $arr); |
| 250 |
$arr = filter_var_array($arr, $args); |
| 251 |
|
| 252 |
return json_encode($arr); |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Get Style JSON for map shapes/geojson (svg or canvas) |
| 257 |
* |
| 258 |
* Takes atts for creating shapes on the map |
| 259 |
* |
| 260 |
* @param array $atts user-input array |
| 261 |
* |
| 262 |
* @return array corrected for JavaScript |
| 263 |
*/ |
| 264 |
public function get_style_json($atts) |
| 265 |
{ |
| 266 |
if ($atts) { |
| 267 |
extract($atts); |
| 268 |
} |
| 269 |
|
| 270 |
// from http://leafletjs.com/reference-1.0.3.html#path |
| 271 |
$style = array( |
| 272 |
'stroke' => isset($stroke) ? $stroke : null, |
| 273 |
'color' => isset($color) ? $color : null, |
| 274 |
'weight' => isset($weight) ? $weight : null, |
| 275 |
'opacity' => isset($opacity) ? $opacity : null, |
| 276 |
'lineCap' => isset($linecap) ? $linecap : null, |
| 277 |
'lineJoin' => isset($linejoin) ? $linejoin : null, |
| 278 |
'dashArray' => isset($dasharray) ? $dasharray : null, |
| 279 |
'dashOffset' => isset($dashoffset) ? $dashoffset : null, |
| 280 |
'fill' => isset($fill) ? $fill : null, |
| 281 |
'fillColor' => isset($fillcolor) ? $fillcolor : null, |
| 282 |
'fillOpacity' => isset($fillopacity) ? $fillopacity : null, |
| 283 |
'fillRule' => isset($fillrule) ? $fillrule : null, |
| 284 |
'className' => isset($classname) ? $classname : null, |
| 285 |
); |
| 286 |
|
| 287 |
$args = array( |
| 288 |
'stroke' => FILTER_VALIDATE_BOOLEAN, |
| 289 |
'color' => FILTER_SANITIZE_STRING, |
| 290 |
'weight' => FILTER_VALIDATE_FLOAT, |
| 291 |
'opacity' => FILTER_VALIDATE_FLOAT, |
| 292 |
'lineCap' => FILTER_SANITIZE_STRING, |
| 293 |
'lineJoin' => FILTER_SANITIZE_STRING, |
| 294 |
'dashArray' => FILTER_SANITIZE_STRING, |
| 295 |
'dashOffset' => FILTER_SANITIZE_STRING, |
| 296 |
'fill' => FILTER_VALIDATE_BOOLEAN, |
| 297 |
'fillColor' => FILTER_SANITIZE_STRING, |
| 298 |
'fillOpacity' => FILTER_VALIDATE_FLOAT, |
| 299 |
'fillRule' => FILTER_SANITIZE_STRING, |
| 300 |
'className' => FILTER_SANITIZE_STRING |
| 301 |
); |
| 302 |
|
| 303 |
return $this->json_sanitize($style, $args); |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Add Popups to Shapes |
| 308 |
* |
| 309 |
* used by leaflet-marker, leaflet-line and leaflet-circle |
| 310 |
* |
| 311 |
* @param array $atts user-input array |
| 312 |
* @param string $content text to display |
| 313 |
* @param string $shape JavaScript variable for shape |
| 314 |
* |
| 315 |
* @return null |
| 316 |
*/ |
| 317 |
public function add_popup_to_shape($atts, $content, $shape) |
| 318 |
{ |
| 319 |
if (!empty($atts)) { |
| 320 |
extract($atts); |
| 321 |
} |
| 322 |
|
| 323 |
$message = empty($message) ? |
| 324 |
(empty($content) ? '' : $content) : $message; |
| 325 |
$message = str_replace(array("\r\n", "\n", "\r"), '<br>', $message); |
| 326 |
$message = htmlspecialchars($message); |
| 327 |
$visible = empty($visible) ? false : ($visible == 'true'); |
| 328 |
|
| 329 |
if (!empty($message)) { |
| 330 |
echo "{$shape}.bindPopup(window.WPLeafletMapPlugin.unescape('{$message}'))"; |
| 331 |
if ($visible) { |
| 332 |
echo ".openPopup()"; |
| 333 |
} |
| 334 |
echo ";"; |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|