55 Description: A plugin for creating a Leaflet JS map with a shortcode. Boasts two free map tile services and three free geocoders.
66 Author: bozdoz
77 Author URI: https://twitter.com/bozdoz/
8- Version: 2.7.8
8+ Version: 2.8.0
99 License: GPL2
1010
1111 Leaflet Map is free software: you can redistribute it and/or modify
2222 along with Leaflet Map. If not, see https://github.com/bozdoz/wp-plugin-leaflet-map/blob/master/LICENSE.
2323 */
2424
25- if (!class_exists ('Leaflet_Map_Plugin ' )) {
26-
25+ // Exit if accessed directly
26+ if ( !defined ( 'ABSPATH ' ) ) exit ;
27+
28+ if ( !class_exists ('Leaflet_Map_Plugin ' ) ) {
29+
2730 class Leaflet_Map_Plugin {
2831
2932 /*
3033 * Number of maps on page
3134 * @var int $leaflet_map_count
3235 */
33- public static $ leaflet_map_count ;
36+ public $ leaflet_map_count = 0 ;
3437
3538 /*
3639 * Default values and admin form information
@@ -62,6 +65,11 @@ class Leaflet_Map_Plugin {
6265 'type ' => 'text ' ,
6366 'helptext ' => 'Can set per map in shortcode or adjust for all maps here. Values can include "px" but it is not necessary. Can also be %; e.g. <br/> <code>[leaflet-map width="100%"]</code> '
6467 ),
68+ 'leaflet_fit_markers ' => array (
69+ 'default ' => '0 ' ,
70+ 'type ' => 'checkbox ' ,
71+ 'helptext ' => 'If enabled, all markers on each map will alter the view of the map; i.e. the map will fit to the bounds of all of the markers on the map. You can also change this per map in the shortcode: e.g. <br /> <code>[leaflet-map fit_markers="1"]</code> '
72+ ),
6573 'leaflet_show_zoom_controls ' => array (
6674 'default ' => '0 ' ,
6775 'type ' => 'checkbox ' ,
@@ -141,31 +149,48 @@ class Leaflet_Map_Plugin {
141149 'leaflet_geocoded_locations ' => array ()
142150 );
143151
152+ /**
153+ * @var Leaflet_Map_Plugin
154+ **/
155+ private static $ instance = null ;
156+
157+ /**
158+ * Singleton
159+ * @static
160+ */
161+ public static function init () {
162+ if ( ! self ::$ instance ) {
163+ self ::$ instance = new Leaflet_Map_Plugin ;
164+ }
165+
166+ return self ::$ instance ;
167+ }
168+
144169 /*
145170 *
146171 * Initialize plugin
147172 *
148173 */
149174
150- public function __construct () {
151- add_action ('admin_init ' , array (& $ this , 'admin_init ' ));
152- add_action ('admin_menu ' , array (& $ this , 'admin_menu ' ));
153- add_action ( 'wp_enqueue_scripts ' , array (& $ this , 'enqueue_and_register ' ) );
154- add_action ( 'admin_enqueue_scripts ' , array (& $ this , 'enqueue_and_register ' ) );
175+ private function __construct () {
176+ add_action ('admin_init ' , array ($ this , 'admin_init ' ));
177+ add_action ('admin_menu ' , array ($ this , 'admin_menu ' ));
178+ add_action ( 'wp_enqueue_scripts ' , array ($ this , 'enqueue_and_register ' ) );
179+ add_action ( 'admin_enqueue_scripts ' , array ($ this , 'enqueue_and_register ' ) );
155180
156- add_shortcode ('leaflet-map ' , array (& $ this , 'map_shortcode ' ));
157- add_shortcode ('leaflet-marker ' , array (& $ this , 'marker_shortcode ' ));
158- add_shortcode ('leaflet-line ' , array (& $ this , 'line_shortcode ' ));
159- add_shortcode ('leaflet-image ' , array (& $ this , 'image_shortcode ' ));
160- add_shortcode ('leaflet-geojson ' , array (& $ this , 'geojson_shortcode ' ));
161- add_shortcode ('leaflet-kml ' , array (& $ this , 'kml_shortcode ' ));
181+ add_shortcode ('leaflet-map ' , array ($ this , 'map_shortcode ' ));
182+ add_shortcode ('leaflet-marker ' , array ($ this , 'marker_shortcode ' ));
183+ add_shortcode ('leaflet-line ' , array ($ this , 'line_shortcode ' ));
184+ add_shortcode ('leaflet-image ' , array ($ this , 'image_shortcode ' ));
185+ add_shortcode ('leaflet-geojson ' , array ($ this , 'geojson_shortcode ' ));
186+ add_shortcode ('leaflet-kml ' , array ($ this , 'kml_shortcode ' ));
162187
163188 /* allow maps on excerpts */
164189 /* should be optional somehow (admin setting?) */
165190 add_filter ('the_excerpt ' , 'do_shortcode ' );
166191
167192 /* add settings to plugin page */
168- add_filter ('plugin_action_links_ ' . plugin_basename (__FILE__ ), array (& $ this , 'plugin_action_links ' ));
193+ add_filter ('plugin_action_links_ ' . plugin_basename (__FILE__ ), array ($ this , 'plugin_action_links ' ));
169194 }
170195
171196 /*
@@ -266,7 +291,7 @@ public function admin_init () {
266291
267292 public function settings_page () {
268293 wp_enqueue_style ( 'leaflet_admin_stylesheet ' );
269- include 'templates/ admin.php ' ;
294+ include 'admin/settings .php ' ;
270295 }
271296
272297 /*
@@ -294,9 +319,9 @@ public function admin_menu () {
294319 $ main_link = 'leaflet-get-shortcode ' ;
295320 }
296321
297- add_menu_page ("Leaflet Map " , "Leaflet Map " , 'manage_options ' , $ main_link , array (& $ this , "settings_page " ), plugins_url ('images/leaf.png ' , __FILE__ ));
298- add_submenu_page ("leaflet-map " , "Default Values " , "Default Values " , 'manage_options ' , "leaflet-map " , array (& $ this , "settings_page " ));
299- add_submenu_page ("leaflet-map " , "Shortcode Helper " , "Shortcode Helper " , 'edit_posts ' , "leaflet-get-shortcode " , array (& $ this , "shortcode_page " ));
322+ add_menu_page ("Leaflet Map " , "Leaflet Map " , 'manage_options ' , $ main_link , array ($ this , "settings_page " ), plugins_url ('images/leaf.png ' , __FILE__ ));
323+ add_submenu_page ("leaflet-map " , "Default Values " , "Default Values " , 'manage_options ' , "leaflet-map " , array ($ this , "settings_page " ));
324+ add_submenu_page ("leaflet-map " , "Shortcode Helper " , "Shortcode Helper " , 'edit_posts ' , "leaflet-get-shortcode " , array ($ this , "shortcode_page " ));
300325 }
301326
302327 /*
@@ -509,13 +534,10 @@ function remove_null ($var) {
509534 */
510535
511536 public function map_shortcode ( $ atts ) {
512-
513- if (!self ::$ leaflet_map_count ) {
514- self ::$ leaflet_map_count = 0 ;
515- }
516- self ::$ leaflet_map_count ++;
517537
518- $ leaflet_map_count = self ::$ leaflet_map_count ;
538+ $ this ->leaflet_map_count ++;
539+
540+ $ leaflet_map_count = $ this ->leaflet_map_count ;
519541
520542 /* defaults from db */
521543 $ defaults = self ::$ defaults ;
@@ -530,6 +552,7 @@ public function map_shortcode ( $atts ) {
530552 $ default_attribution = get_option ('leaflet_default_attribution ' , $ defaults ['leaflet_default_attribution ' ]['default ' ]);
531553 $ default_min_zoom = get_option ('leaflet_default_min_zoom ' , $ defaults ['leaflet_default_min_zoom ' ]['default ' ]);
532554 $ default_max_zoom = get_option ('leaflet_default_max_zoom ' , $ defaults ['leaflet_default_max_zoom ' ]['default ' ]);
555+ $ default_fit_markers = get_option ('leaflet_fit_markers ' , $ defaults ['leaflet_fit_markers ' ]['default ' ]);
533556
534557 if ($ atts ) {
535558 extract ($ atts );
@@ -556,6 +579,7 @@ public function map_shortcode ( $atts ) {
556579 $ doubleclickzoom = empty ($ doubleclickzoom ) ? $ default_doubleclickzoom : $ doubleclickzoom ;
557580 $ height = empty ($ height ) ? $ default_height : $ height ;
558581 $ width = empty ($ width ) ? $ default_width : $ width ;
582+ $ fit_markers = empty ($ fit_markers ) ? $ default_fit_markers : $ fit_markers ;
559583
560584 /* need to allow 0 or empty for removal of attribution */
561585 if (!$ atts ||
@@ -628,7 +652,11 @@ public function map_shortcode ( $atts ) {
628652 doubleClickZoom: {$ doubleclickzoom },
629653 attributionControl: false
630654 }, {$ more_options }),
631- map = L.map('leaflet-wordpress-map- {$ leaflet_map_count }', options).setView([ {$ lat }, {$ lng }], {$ zoom }); " ;
655+ map = L.map('leaflet-wordpress-map- {$ leaflet_map_count }', options).setView([ {$ lat }, {$ lng }], {$ zoom });
656+ if ( {$ fit_markers }) {
657+ map.fit_markers = true;
658+ }
659+ " ;
632660
633661 if ($ attribution ) {
634662 /* add any attributions, semi-colon-separated */
@@ -665,12 +693,9 @@ public function map_shortcode ( $atts ) {
665693 public function image_shortcode ( $ atts ) {
666694
667695 /* get map count for unique id */
668- if (!self ::$ leaflet_map_count ) {
669- self ::$ leaflet_map_count = 0 ;
670- }
671- self ::$ leaflet_map_count ++;
696+ $ this ->leaflet_map_count ++;
672697
673- $ leaflet_map_count = self :: $ leaflet_map_count ;
698+ $ leaflet_map_count = $ this -> leaflet_map_count ;
674699
675700 /* defaults from db */
676701 $ defaults = self ::$ defaults ;
@@ -821,14 +846,15 @@ public function get_style_json ($atts) {
821846 *
822847 * Used for generating shapes from GeoJSON or KML/KMZ
823848 *
824- * @param array $atts user-input array
849+ * @param array $atts user-input attribute array
850+ * @param string $content user-input HTML
825851 * @param string $wp_script script to enqueue (varies)
826852 * @param string $L_method which private function to call
827853 * @param string $default test/example URL (if src is not present in $atts)
828854 * @return string JavaScript
829855 */
830856
831- public function get_shape ( $ atts , $ wp_script , $ L_method , $ default = '' ) {
857+ public function get_shape ( $ atts , $ content , $ wp_script , $ L_method , $ default = '' ) {
832858 wp_enqueue_script ( $ wp_script );
833859
834860 if ($ atts ) {
@@ -842,7 +868,13 @@ public function get_shape ( $atts, $wp_script, $L_method, $default = '' ) {
842868
843869 $ fitbounds = empty ($ fitbounds ) ? 0 : $ fitbounds ;
844870
871+ // shortcode content becomes popup text
872+ $ content_text = empty ($ content ) ? '' : $ content ;
873+ // alternatively, the popup_text attribute works as popup text
845874 $ popup_text = empty ($ popup_text ) ? '' : $ popup_text ;
875+ // choose which one takes priority (content_text)
876+ $ popup_text = empty ($ content_text ) ? $ popup_text : $ content_text ;
877+
846878 $ popup_property = empty ($ popup_property ) ? '' : $ popup_property ;
847879
848880 $ geojson_script = "<script>
@@ -890,7 +922,7 @@ function layerStyle (feature) {
890922 }
891923 function onEachFeature (feature, layer) {
892924 var props = feature.properties || {},
893- text = popup_text || props[ popup_property ];
925+ text = popup_property && props[ popup_property ] || popup_text ;
894926 if (text) {
895927 layer.bindPopup( text );
896928 }
@@ -910,10 +942,8 @@ function onEachFeature (feature, layer) {
910942 * @param array $atts user-input array
911943 * @return string JavaScript
912944 */
913- public function geojson_shortcode ( $ atts ) {
914-
915- return self ::get_shape ( $ atts , 'leaflet_ajax_geojson_js ' , 'ajaxGeoJson ' , 'https://rawgit.com/bozdoz/567817310f102d169510d94306e4f464/raw/2fdb48dafafd4c8304ff051f49d9de03afb1718b/map.geojson ' );
916-
945+ public function geojson_shortcode ( $ atts , $ content = null ) {
946+ return self ::get_shape ( $ atts , $ content , 'leaflet_ajax_geojson_js ' , 'ajaxGeoJson ' , 'https://rawgit.com/bozdoz/567817310f102d169510d94306e4f464/raw/2fdb48dafafd4c8304ff051f49d9de03afb1718b/map.geojson ' );
917947 }
918948
919949 /*
@@ -926,9 +956,9 @@ public function geojson_shortcode ( $atts ) {
926956 * @return string JavaScript
927957 */
928958
929- public function kml_shortcode ( $ atts ) {
959+ public function kml_shortcode ( $ atts, $ content = null ) {
930960
931- return self ::get_shape ( $ atts , 'leaflet_ajax_kml_js ' , 'ajaxKML ' , 'https://cdn.rawgit.com/mapbox/togeojson/master/test/data/polygon.kml ' );
961+ return self ::get_shape ( $ atts , $ content , 'leaflet_ajax_kml_js ' , 'ajaxKML ' , 'https://cdn.rawgit.com/mapbox/togeojson/master/test/data/polygon.kml ' );
932962
933963 }
934964
@@ -1056,7 +1086,8 @@ public function marker_shortcode ( $atts, $content = null ) {
10561086 marker = L.marker([ {$ lat }, {$ lng }], marker_options),
10571087 previous_map = WPLeafletMapPlugin.getCurrentMap(),
10581088 is_image = previous_map.is_image_map,
1059- previous_map_onload;
1089+ previous_map_onload,
1090+ markergroup = WPLeafletMapPlugin.getCurrentMarkerGroup();
10601091 " ;
10611092
10621093 if (empty ($ lat ) && empty ($ lng )) {
@@ -1091,7 +1122,7 @@ public function marker_shortcode ( $atts, $content = null ) {
10911122 });
10921123 }
10931124
1094- marker.addTo( previous_map );
1125+ marker.addTo( markergroup );
10951126 " ;
10961127
10971128 $ marker_script .= self ::add_popup_to_shape ($ atts , $ content , 'marker ' );
@@ -1173,11 +1204,10 @@ public function line_shortcode ( $atts, $content = null ) {
11731204
11741205 return $ line_script ;
11751206 }
1176- }
1207+ } // end class
1208+ } // end if
11771209
1178- register_activation_hook ( __FILE__ , array ('Leaflet_Map_Plugin ' , 'activate ' ));
1179- register_uninstall_hook ( __FILE__ , array ('Leaflet_Map_Plugin ' , 'uninstall ' ) );
1210+ register_activation_hook ( __FILE__ , array ('Leaflet_Map_Plugin ' , 'activate ' ));
1211+ register_uninstall_hook ( __FILE__ , array ('Leaflet_Map_Plugin ' , 'uninstall ' ) );
11801212
1181- $ leaflet_map_plugin = new Leaflet_Map_Plugin ();
1182- }
1183- ?>
1213+ Leaflet_Map_Plugin::init ();
0 commit comments