|
3 | 3 | * Plugin Name: CMB2 Mapbox
|
4 | 4 | * Plugin URI:
|
5 | 5 | * Description: This plugin adds a new CMB2 fieldtype for adding a single point to a Mapbox map. This plugin requires CMB2 and a Mapbox access token.
|
6 |
| - * Version: 1.0.3 |
| 6 | + * Version: 1.5.0 |
7 | 7 | * Author: Rob Clark
|
8 | 8 | * Author URI: https://robclark.io
|
9 | 9 | * License: GPLv2 or later
|
@@ -59,11 +59,11 @@ function cmb2_mapbox_options_metabox() {
|
59 | 59 |
|
60 | 60 |
|
61 | 61 | function cmb2_mapbox_scripts() {
|
62 |
| - wp_enqueue_style( 'mapbox-gl', 'https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.css', array(), null ); |
63 |
| - wp_enqueue_script( 'mapbox-gl', 'https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.js', array(), null ); |
| 62 | + wp_enqueue_style( 'mapbox-gl', 'https://api.mapbox.com/mapbox-gl-js/v2.13.0/mapbox-gl.css', array(), null ); |
| 63 | + wp_enqueue_script( 'mapbox-gl', 'https://api.mapbox.com/mapbox-gl-js/v2.13.0/mapbox-gl.js', array(), null ); |
64 | 64 | if ( is_admin() ) {
|
65 |
| - wp_enqueue_style( 'mapbox-gl-draw', 'https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.2.2/mapbox-gl-draw.css', array(), null ); |
66 |
| - wp_enqueue_script( 'mapbox-gl-draw', 'https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.2.2/mapbox-gl-draw.js', array( 'mapbox-gl' ), null ); |
| 65 | + wp_enqueue_style( 'mapbox-gl-draw', 'https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.4.1/mapbox-gl-draw.css', array(), null ); |
| 66 | + wp_enqueue_script( 'mapbox-gl-draw', 'https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.4.1/mapbox-gl-draw.js', array( 'mapbox-gl' ), null ); |
67 | 67 | }
|
68 | 68 | }
|
69 | 69 |
|
@@ -93,7 +93,8 @@ function cmb2_render_mapbox_map_callback( $field, $value, $object_id, $object_ty
|
93 | 93 | zoom: 13,
|
94 | 94 | center: [<?php echo $value['lnglat']; ?>]
|
95 | 95 | <?php } else { ?>
|
96 |
| - zoom: 11<?php echo ( isset( $options['map_center'] ) ? ',center: [' . $options['map_center'] . ']' : '' ); ?> |
| 96 | + zoom: <?php echo ( cmb2_mapbox_check_array_key( $field->args, 'default_zoom' ) ? $field->args['default_zoom'] : '11' ); ?>, |
| 97 | + center: [<?php echo ( cmb2_mapbox_check_array_key( $options, 'map_center' ) ? $options['map_center'] : '-95.7129,37.0902' ); ?>] |
97 | 98 | <?php } ?>
|
98 | 99 | });
|
99 | 100 | map.addControl(new mapboxgl.NavigationControl());
|
@@ -216,3 +217,127 @@ function updateEditor(e) {
|
216 | 217 | }
|
217 | 218 |
|
218 | 219 | add_filter( 'cmb2_render_mapbox_map', 'cmb2_render_mapbox_map_callback', 10, 5 );
|
| 220 | + |
| 221 | +function cmb2_mapbox_check_array_key( $item, $key ) { |
| 222 | + $output = false; |
| 223 | + if ( is_array( $item ) ) { |
| 224 | + if ( array_key_exists( $key, $item ) ) { |
| 225 | + if ( ! empty( $item["{$key}"] ) ) { |
| 226 | + $output = true; |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + return $output; |
| 231 | +} |
| 232 | + |
| 233 | +if ( ! class_exists( 'CMB2_MB_Map' ) ) { |
| 234 | + class CMB2_MB_Map { |
| 235 | + protected $plugin_options = array(); |
| 236 | + protected $map_options = array(); |
| 237 | + protected $geo = array(); |
| 238 | + |
| 239 | + function __construct() { |
| 240 | + $this->set_options(); |
| 241 | + |
| 242 | + // Set up arrays for geo |
| 243 | + $this->geo['markers'] = array(); |
| 244 | + } |
| 245 | + |
| 246 | + private function set_options() { |
| 247 | + // Set plugin options |
| 248 | + $defaults = array( |
| 249 | + 'api_token' => '', |
| 250 | + 'map_center' => '-95.7129,37.0902', |
| 251 | + ); |
| 252 | + $this->plugin_options = wp_parse_args( get_option( 'cmb2_mapbox' ), $defaults ); |
| 253 | + } |
| 254 | + |
| 255 | + public function set_map_options( $args ) { |
| 256 | + $defaults = array( |
| 257 | + 'id' => 'map', |
| 258 | + 'class' => 'cmb2-mapbox-map', |
| 259 | + 'width' => '100%', |
| 260 | + 'height' => '400px', |
| 261 | + 'zoom' => '16', |
| 262 | + 'center' => '-95.7129,37.0902', |
| 263 | + 'mapstyle' => 'mapbox://styles/mapbox/streets-v11', |
| 264 | + 'terrain' => false, |
| 265 | + ); |
| 266 | + $this->map_options = wp_parse_args( $args, $defaults ); |
| 267 | + } |
| 268 | + |
| 269 | + public function add_marker( $geo, $tooltip, $color ) { |
| 270 | + $this->geo['markers'][] = array( |
| 271 | + 'geo' => $geo, |
| 272 | + 'tooltip' => $tooltip, |
| 273 | + 'color' => $color, |
| 274 | + ); |
| 275 | + return; |
| 276 | + } |
| 277 | + |
| 278 | + public function build_map() { |
| 279 | + $output = ''; |
| 280 | + $marker_html = ''; |
| 281 | + $markers = array(); |
| 282 | + if ( 0 < count( $this->geo['markers'] ) ) { |
| 283 | + $i = 1; |
| 284 | + foreach( $this->geo['markers'] as $marker ) { |
| 285 | + $html = ' |
| 286 | + const popup' . $i . ' = new mapboxgl.Popup({ offset: 25 }).setMaxWidth("300px").setHTML( |
| 287 | + \'' . '<div class="mapbox-popup-content-wrap">' . $marker['tooltip'] . '</div>\' |
| 288 | + ); |
| 289 | +
|
| 290 | + const marker' . $i . ' = new mapboxgl.Marker({ color: \'' . $marker['color'] . '\' }) |
| 291 | + .setLngLat([' . $marker['geo']['lnglat'] . ']) |
| 292 | + .setPopup(popup' . $i . ') |
| 293 | + .addTo(map); |
| 294 | + '; |
| 295 | + $markers[] = array( |
| 296 | + 'html' => $html, |
| 297 | + 'lat' => $marker['geo']['lat'] |
| 298 | + ); |
| 299 | + $i++; |
| 300 | + } |
| 301 | + array_multisort( array_column( $markers, 'lat' ), SORT_DESC, $markers ); |
| 302 | + foreach ($markers as $marker) { |
| 303 | + $marker_html .= $marker['html']; |
| 304 | + } |
| 305 | + $output .= ' |
| 306 | + <div id="' . $this->map_options['id'] . '" class="' . $this->map_options['class'] . '" style="height: ' . $this->map_options['height'] . '; width: ' . $this->map_options['width'] . ';"></div> |
| 307 | + <script> |
| 308 | + mapboxgl.accessToken = \'' . $this->plugin_options['api_token'] . '\'; |
| 309 | + var map = new mapboxgl.Map({ |
| 310 | + container: \'' . $this->map_options['id'] . '\', |
| 311 | + style: \'' . $this->map_options['mapstyle'] . '\', |
| 312 | + zoom: ' . $this->map_options['zoom'] . ', |
| 313 | + scrollZoom: false, |
| 314 | + center: [' . $this->map_options['center'] . '], |
| 315 | + }); |
| 316 | + map.on(\'load\', function () { |
| 317 | + var nav = new mapboxgl.NavigationControl(); |
| 318 | + map.addControl(nav, \'top-left\'); |
| 319 | + ' . $marker_html . ' |
| 320 | + '; |
| 321 | + if ( $this->map_options['terrain'] ) { |
| 322 | + $output .= ' |
| 323 | + map.addSource(\'dem\', { |
| 324 | + \'type\': \'raster-dem\', |
| 325 | + \'url\': \'mapbox://mapbox.terrain-rgb\' |
| 326 | + }); |
| 327 | + map.addLayer({ |
| 328 | + \'id\': \'hillshading\', |
| 329 | + \'source\': \'dem\', |
| 330 | + \'type\': \'hillshade\' |
| 331 | + },); |
| 332 | + |
| 333 | + '; |
| 334 | + } |
| 335 | + $output .= ' |
| 336 | + }); |
| 337 | + </script> |
| 338 | + '; |
| 339 | + } |
| 340 | + return $output; |
| 341 | + } |
| 342 | + } |
| 343 | +} |
0 commit comments