Skip to content

Commit

Permalink
multi-line comments; fixes to geocoder and image maps;
Browse files Browse the repository at this point in the history
  • Loading branch information
bozdoz committed Aug 8, 2017
1 parent 283294d commit 8e1b6a7
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 100 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Leaflet Map WordPress Plugin
========

![Leaflet](https://img.shields.io/badge/leaflet-1.1.0-green.svg?style=flat)
![WordPress](https://img.shields.io/badge/wordpress-4.8-green.svg?style=flat)
![WordPress](https://img.shields.io/badge/wordpress-4.8.1-green.svg?style=flat)

Add a map generated with [LeafletJS](http://leafletjs.com/): an open-source JavaScript library for mobile-friendly interactive maps. Map tiles are provided by default through [OpenStreetMap](http://www.openstreetmap.org/), or [MapQuest](https://www.mapquest.ca/) (with an app key). Can be set per map with shortcode attributes or through the dashboard settings.

Expand Down
59 changes: 39 additions & 20 deletions class.geocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
*
* calls the specific geocoder function (chosen in admin or default: google_geocode)
*
* todo: candidate for separate class
*
* @param string $address the requested address to look up
*/

class Leaflet_Geocoder {
Expand All @@ -26,41 +23,63 @@ class Leaflet_Geocoder {
*/
public $lng = 0;

/**
* new Geocoder from address
*
* handles url encoding and caching
*
* @param string $address the requested address to look up
* @return NOTHING
*/
public function __construct ($address) {
$settings = Leaflet_Map_Plugin_Settings::init();
$address = urlencode( $address );

$geocoder = $settings->get('geocoder');

$cached_address = 'leaflet_' . $address;
$cached_address = 'leaflet_' . $geocoder . '_' . $address;

/* retrieve cached geocoded location */
$found_cache = get_option( $cached_address );

if ( $found_cache ) {
return $found_cache;
$location = $found_cache;
} else {
// try geocoding
$geocoding_method = $geocoder . '_geocode';

try {
$location = (Object) $this->$geocoding_method( $address );

/* add location */
add_option($cached_address, $location);

/* add option key to locations for clean up purposes */
$locations = get_option('leaflet_geocoded_locations', array());
array_push($locations, $cached_address);
update_option('leaflet_geocoded_locations', $locations);
} catch (Exception $e) {
// failed
$location = $this->not_found;
}
}

$geocoding_method = $geocoder . '_geocode';

try {
$location = (Object) $this->$geocoding_method( $address );

/* add location */
add_option($cached_address, $location);

/* add option key to locations for clean up purposes */
$locations = get_option('leaflet_geocoded_locations', array());
array_push($locations, $cached_address);
update_option('leaflet_geocoded_locations', $locations);
} catch (Exception $e) {
$location = $this->not_found;
}
// set variables
$this->lat = $location->lat;
$this->lng = $location->lng;
}

/**
* Removes location caches
*/
public static function remove_caches () {
$addresses = get_option('leaflet_geocoded_locations', array());
foreach ($addresses as $address) {
delete_option($address);
}
delete_option('leaflet_geocoded_locations');
}

/**
* Used by geocoders to make requests via curl or file_get_contents
*
Expand Down
28 changes: 9 additions & 19 deletions class.leaflet-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,14 @@ class Leaflet_Map {
* This plugin version
* @var string major minor patch version
*/
public $version = '2.8.0';
public $version = '2.8.2';

/**
* Number of maps on page; used for unique map ids
* @var int $map_count
*/
public $map_count = 0;

/**
* Holder for WordPress's get_plugin_data
* @var object $plugin_data
*/
public $plugin_data;

/**
* Files to include upon init
* @var array $shortcodes
Expand Down Expand Up @@ -146,15 +140,13 @@ private function add_shortcodes () {
*/

public static function uninstall () {
/* remove geocoded locations */
$locations = get_option('leaflet_geocoded_locations', array());

foreach ($locations as $address => $latlng) {
delete_option('leaflet_' . $address);
}

// remove settings in db
$settings = Leaflet_Map_Plugin_Settings::init();
$settings->reset();

// remove geocoder locations in db
include_once(LEAFLET_MAP__PLUGIN_DIR . 'class.geocoder.php');
Leaflet_Geocoder::remove_caches();
}

/**
Expand All @@ -173,11 +165,11 @@ public function enqueue_and_register () {
wp_register_script('leaflet_js', $js_url, Array(), $this->leaflet_version, true);

// new required MapQuest javascript file
$tiling_service = get_option('leaflet_default_tiling_service','');
$tiling_service = $settings->get('default_tiling_service');

if ($tiling_service == 'mapquest') {
$mapquest_js_url = 'https://www.mapquestapi.com/sdk/leaflet/v2.2/mq-map.js?key=%s';
$mq_appkey = get_option('leaflet_mapquest_appkey','');
$mq_appkey = $settings->get('mapquest_appkey');
$mapquest_js_url = sprintf($mapquest_js_url, $mq_appkey);

wp_register_script('leaflet_mapquest_plugin', $mapquest_js_url, Array('leaflet_js'), '2.0', true);
Expand All @@ -203,8 +195,6 @@ public function enqueue_and_register () {

public function admin_init () {
wp_register_style('leaflet_admin_stylesheet', plugins_url('style.css', __FILE__));

$this->plugin_data = get_plugin_data( LEAFLET_MAP__PLUGIN_FILE );
}

/**
Expand All @@ -217,7 +207,7 @@ public function settings_page () {

$settings = Leaflet_Map_Plugin_Settings::init();
$plugin_data = get_plugin_data(LEAFLET_MAP__PLUGIN_FILE);
include 'admin/settings.php';
include 'templates/settings.php';
}

/**
Expand Down
13 changes: 5 additions & 8 deletions class.plugin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ class Leaflet_Map_Plugin_Settings {
'helptext' => 'Some maps get tiles from multiple servers with subdomains such as a,b,c,d or 1,2,3,4; can be set per map with the shortcode <br/> <code>[leaflet-map subdomains="1234"]</code>',
),
'js_url' => array(
'default'=>'https://unpkg.com/leaflet@#version#/dist/leaflet.js',
'default'=>'https://unpkg.com/leaflet@%s/dist/leaflet.js',
'type' => 'text',
'helptext' => 'If you host your own Leaflet files, specify the URL here.'
),
'css_url' => array(
'default'=>'https://unpkg.com/leaflet@#version#/dist/leaflet.css',
'default'=>'https://unpkg.com/leaflet@%s/dist/leaflet.css',
'type' => 'text',
'helptext' => 'Save as above.'
),
Expand All @@ -135,10 +135,7 @@ class Leaflet_Map_Plugin_Settings {
'dawa' => 'Danmarks Adressers'
),
'helptext' => 'Select the Geocoding provider to use to retrieve addresses defined in shortcode.'
),
// not in admin; geocoder uses this
// so it needs to be here to be deleted on uninstall
'geocoded_locations' => array()
)
);

/**
Expand All @@ -159,8 +156,8 @@ private function __construct () {
$LM = Leaflet_Map::init();
$leaflet_version = $LM->leaflet_version;

$this->options['js_url']['default'] = str_replace('#version#', $leaflet_version, $this->options['js_url']['default']);
$this->options['css_url']['default'] = str_replace('#version#', $leaflet_version, $this->options['css_url']['default']);
$this->options['js_url']['default'] = sprintf($this->options['js_url']['default'], $leaflet_version);
$this->options['css_url']['default'] = sprintf($this->options['css_url']['default'], $leaflet_version);

foreach ($this->options as $name => $details) {
$this->options[ $name ] = new Leaflet_Map_Plugin_Option( $details );
Expand Down
5 changes: 2 additions & 3 deletions leaflet-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Description: A plugin for creating a Leaflet JS map with a shortcode. Boasts two free map tile services and three free geocoders.
Author: bozdoz
Author URI: https://twitter.com/bozdoz/
Version: 2.8.1
Version: 2.8.2
License: GPL2
Leaflet Map is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -34,5 +34,4 @@
// uninstall hook
register_uninstall_hook( __FILE__, array('Leaflet_Map', 'uninstall') );

add_action('init', array('Leaflet_Map', 'init'));
Leaflet_Map::init();
add_action('init', array('Leaflet_Map', 'init'));
26 changes: 21 additions & 5 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ Author: bozdoz
Author URI: https://www.twitter.com/bozdoz/
Plugin URI: https://wordpress.org/plugins/leaflet-map/
Contributors: bozdoz, Remigr, nielsalstrup, jeromelebleu
Donate link: https://www.gittip.com/bozdoz/
Donate link: https://www.paypal.me/bozdoz
Tags: leaflet, map, mobile, javascript, openstreetmap, mapquest, interactive
Requires at least: 3.0.1
Tested up to: 4.8
Version: 2.7.8
Stable tag: 2.7.8
Tested up to: 4.8.1
Version: 2.8.2
Stable tag: 2.8.2
License: GPLv2
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -77,7 +77,7 @@ Yes. Update to the newest plugin version, and reset defaults in settings. You c

= Can I add geojson? =

Yes, just give it a source URL: `[leaflet-geojson src="https://example.com/path/to.geojson"]` It will also support leaflet geojson styles or geojson.io styles. Add a popup message with `[leaflet-geojson popup_text="hello!"]` or identify a geojson property with `popup_property`.
Yes, just give it a source URL: `[leaflet-geojson src="https://example.com/path/to.geojson"]` It will also support leaflet geojson styles or geojson.io styles. Add a popup message with `[leaflet-geojson popup_text="hello!"]`, or add HTML by adding it to the content of the shortcode: `[leaflet-geojson]<a href="#">Link here</a>[/leaflet-geojson]` or identify a geojson property with `popup_property`, and each shape will use its own popup text if available.

= Can I add kml? =

Expand Down Expand Up @@ -120,6 +120,19 @@ Yes: use the keyword `attribution` in your shortcode (semi-colon separated list

== Changelog ==

= 2.8.2 =
* Fix to image maps
* Fixes to geocoder
* Added multi-line popups to markers and geojson/kml

= 2.8.1 =
* Code cleanup
* Added server-side and client-side methods to prevent WordPress from adding paragraph tags within shortcode tags

= 2.8.0 =
* Added Fit Markers to settings and map shortcode: [leaflet-map fit_markers=1]
* Moved geojson/kml popup text to the shortcode content instead of a property so that you can use links or other HTML

= 2.7.8 =
* update default Leaflet version (1.1.0 from 1.0.3)

Expand Down Expand Up @@ -233,6 +246,9 @@ Yes: use the keyword `attribution` in your shortcode (semi-colon separated list

== Upgrade Notice ==

= 2.8.2 =
Fixed issues with image maps and geocoder addresses

= 2.7.6 =
added optional cURL to get geolocations if file_get_contents is not allowed on a server (cURL needs to be enabled, obviously)

Expand Down
33 changes: 17 additions & 16 deletions shortcodes/class.image-shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*
* JavaScript equivalent : L.imageOverlay('path/to/image.jpg');
*
* @param array $atts
* @return string $content produced by adding atts to JavaScript
*/

// Exit if accessed directly
Expand All @@ -16,6 +14,11 @@
include_once(LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/class.map-shortcode.php');

class Leaflet_Image_Shortcode extends Leaflet_Map_Shortcode {
/**
* Get HTML for shortcode
* @param array $atts
* @return string $content produced by adding atts to JavaScript
*/
protected function getHTML ($atts, $content) {
extract($this->getAtts($atts));

Expand All @@ -35,27 +38,25 @@ class="leaflet-map"
img = new Image(),
zoom = <?php echo $zoom; ?>;
img.onload = function() {
var center_h = img.height / (zoom * 4),
center_w = img.width / (zoom * 4);

map.setView([center_h, center_w], zoom);

L.imageOverlay( image_src, [[ center_h * 2, 0 ], [ 0, center_w * 2]] ).addTo( map );

var h = img.height * 2;
var w = img.width * 2;
var southWest = map.unproject([-h/2, h/2], zoom);
var northEast = map.unproject([w/2, -w/2], zoom);
var bounds = new L.LatLngBounds(southWest, northEast);
L.imageOverlay( image_src, bounds ).addTo( map );
map.setMaxBounds(bounds);
img.is_loaded = true;
};
img.src = image_src;
map = L.map('leaflet-map-image-<?php echo $map_count; ?>', {
maxZoom: 10,
minZoom: 1,
map = L.map('leaflet-map-image-<?php echo $this->map_id; ?>', {
maxZoom: <?php echo $max_zoom; ?>,
minZoom: <?php echo $min_zoom; ?>,
crs: L.CRS.Simple,
zoomControl: <?php echo $zoomcontrol ?>,
scrollWheelZoom: <?php echo $scrollwheel ?>
zoomControl: <?php echo $zoomcontrol; ?>,
scrollWheelZoom: <?php echo $scrollwheel; ?>
}).setView([0, 0], zoom);

// make it known that it is an image map
map.is_image_map = true;

WPLeafletMapPlugin.maps.push( map );
WPLeafletMapPlugin.images.push( img );
}); // end add
Expand Down
14 changes: 4 additions & 10 deletions shortcodes/class.marker-shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,11 @@ protected function getHTML ($atts, $content) {
if (empty($lat) && empty($lng)) {
/* update lat lng to previous map's center */
?>
if (is_image &&
!previous_map.is_loaded) {
previous_map_onload = previous_map.onload;
previous_map.onload = function () {
if (typeof(previous_map_onload) === 'function') {
previous_map_onload();
}
marker.setLatLng( previous_map.getCenter() );
};
} else {
if (!is_image) {
marker.setLatLng( previous_map.getCenter() );
} else {
console.warn("hello");
marker.setLatLng( [0, 0] );
}
<?php
}
Expand Down
14 changes: 6 additions & 8 deletions shortcodes/class.shortcode.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php
/**
* Map Shortcode
* Abstract Shortcode Class
*
* Displays map with [leaflet-map ...atts]
* Use with add_shortcode('leaflet-map', array('Leaflet_Shortcode', 'shortcode'))
* extend and manipulate __construct
*
* JavaScript equivalent : L.map("id");
*
* @param array $atts
* @return string $content produced by adding atts to JavaScript
* @var array $atts
* @var string $content
*/
abstract class Leaflet_Shortcode {
protected $LM;
Expand All @@ -23,8 +22,7 @@ abstract class Leaflet_Shortcode {
abstract protected function getHTML($atts, $content);

/**
* Use with add_shortcode('leaflet-map', array('Leaflet_Shortcode', 'shortcode'))
* extend and manipulate __construct
* Instantiate class and get HTML for shortcode
* @var array $atts
* @var string $content
*/
Expand Down
Loading

0 comments on commit 8e1b6a7

Please sign in to comment.