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