From 1fb83fe9a291ed54616552765fb3fb1d18d9ab1f Mon Sep 17 00:00:00 2001 From: Moritz 'e1mo' Fromm Date: Sun, 6 Nov 2022 18:02:07 +0100 Subject: [PATCH 1/3] Feature: custom OSM tile server and attribution --- .../settings/class-sp-settings-general.php | 36 +++++++++++++++++++ modules/sportspress-openstreetmap.php | 8 ++--- templates/venue-map.php | 8 ++++- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/includes/admin/settings/class-sp-settings-general.php b/includes/admin/settings/class-sp-settings-general.php index 37aa0fe4e..5f6d3b6e4 100644 --- a/includes/admin/settings/class-sp-settings-general.php +++ b/includes/admin/settings/class-sp-settings-general.php @@ -247,6 +247,42 @@ public function get_settings() { ); } + $options = array_merge( + $options, + array( + array( + 'type' => 'sectionend', + 'id' => 'general_options', + ), + array( + 'title' => esc_attr__( 'OpenStreetMap', 'sportspress' ), + 'type' => 'title', + 'desc' => '', + 'id' => 'osm_options', + ), + array( + 'title' => esc_attr__( 'Tile server', 'sportspress' ), + 'id' => 'sportspress_osm_tile_server', + 'placeholder' => 'https://tile.openstreetmap.org/{z}/{x}/{y}.png', + 'css' => 'width:100%;', + 'type' => 'text', + 'desc' => esc_attr__('Leave empty to use the default', 'sportspress'), + ), + array( + 'title' => esc_attr__( 'Attributions', 'sportspress' ), + 'id' => 'sportspress_osm_attribution', + 'placeholder' => 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors', + 'css' => 'width:100%;', + 'type' => 'text', + 'desc' => esc_attr__('Leave empty to use the default', 'sportspress'), + ), + array( + 'type' => 'sectionend', + 'id' => 'general_options', + ), + ) + ); + $settings = array_merge( $settings, apply_filters( 'sportspress_script_styling_options', $options ), diff --git a/modules/sportspress-openstreetmap.php b/modules/sportspress-openstreetmap.php index ab37e5b50..6f0384622 100644 --- a/modules/sportspress-openstreetmap.php +++ b/modules/sportspress-openstreetmap.php @@ -36,7 +36,7 @@ public function __construct() { add_action( 'sp_admin_venue_scripts', array( $this, 'admin_venue_scripts' ) ); add_action( 'sp_frontend_venue_scripts', array( $this, 'frontend_venue_scripts' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts' ) ); - add_action( 'sp_venue_show_map', array( $this, 'show_venue_map' ), 10, 5 ); + add_action( 'sp_venue_show_map', array( $this, 'show_venue_map' ), 10, 7 ); add_action( 'sp_admin_geocoder_scripts', array( $this, 'admin_geocoder_scripts' ), 10 ); add_action( 'sp_setup_geocoder_scripts', array( $this, 'setup_geocoder_scripts' ), 10 ); add_action( 'sp_setup_venue_geocoder_scripts', array( $this, 'setup_venue_geocoder_scripts' ), 10 ); @@ -113,7 +113,7 @@ public function frontend_venue_scripts() { * * @return mix */ - public function show_venue_map( $latitude, $longitude, $address, $zoom, $maptype ) { + public function show_venue_map( $latitude, $longitude, $address, $zoom, $maptype, $osm_tile, $osm_attribution ) { $lat = abs( $latitude ); $lat_deg = floor( $lat ); $lat_sec = ( $lat - $lat_deg ) * 3600; @@ -142,8 +142,8 @@ public function show_venue_map( $latitude, $longitude, $address, $zoom, $maptype maxZoom: 18, }).addTo(map); - L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', { - attribution: 'Map data © OpenStreetMap contributors', + L.tileLayer('', { + attribution: '', maxZoom: 18, }).addTo(map); diff --git a/templates/venue-map.php b/templates/venue-map.php index d86250fad..d58fad602 100644 --- a/templates/venue-map.php +++ b/templates/venue-map.php @@ -26,6 +26,11 @@ $zoom = get_option( 'sportspress_map_zoom', 15 ); $maptype = get_option( 'sportspress_map_type', 'roadmap' ); $maptype = strtolower( $maptype ); +$osm_tile = get_option( 'sportspress_osm_tile_server', '' ); +$osm_tile = strtolower( $osm_tile ); +$osm_tile = empty( $osm_tile ) ? 'https://tile.openstreetmap.org/{z}/{x}/{y}.png' : $osm_tile; +$osm_attribution = get_option( 'sportspress_osm_attribution', '' ); +$osm_attribution = empty( $osm_attribution ) ? 'Map data © OpenStreetMap contributors' : $osm_attribution; if ( '' === $address ) { $address = '+'; @@ -34,8 +39,9 @@ $maptype = 'roadmap'; } + if ( $latitude != null && $longitude != null ) { - do_action( 'sp_venue_show_map', $latitude, $longitude, $address, $zoom, $maptype ); + do_action( 'sp_venue_show_map', $latitude, $longitude, $address, $zoom, $maptype, $osm_tile, $osm_attribution ); } if ( is_tax( 'sp_venue' ) ) { do_action( 'sportspress_after_venue_map' ); From d97dc145e5b95ef070559604a8a8684aae30998e Mon Sep 17 00:00:00 2001 From: Moritz 'e1mo' Fromm Date: Mon, 7 Nov 2022 18:15:49 +0100 Subject: [PATCH 2/3] Move satelite map to template instead of module --- modules/sportspress-openstreetmap.php | 11 ++--------- templates/venue-map.php | 7 ++++--- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/modules/sportspress-openstreetmap.php b/modules/sportspress-openstreetmap.php index 6f0384622..69aed7b86 100644 --- a/modules/sportspress-openstreetmap.php +++ b/modules/sportspress-openstreetmap.php @@ -36,7 +36,7 @@ public function __construct() { add_action( 'sp_admin_venue_scripts', array( $this, 'admin_venue_scripts' ) ); add_action( 'sp_frontend_venue_scripts', array( $this, 'frontend_venue_scripts' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts' ) ); - add_action( 'sp_venue_show_map', array( $this, 'show_venue_map' ), 10, 7 ); + add_action( 'sp_venue_show_map', array( $this, 'show_venue_map' ), 10, 6 ); add_action( 'sp_admin_geocoder_scripts', array( $this, 'admin_geocoder_scripts' ), 10 ); add_action( 'sp_setup_geocoder_scripts', array( $this, 'setup_geocoder_scripts' ), 10 ); add_action( 'sp_setup_venue_geocoder_scripts', array( $this, 'setup_venue_geocoder_scripts' ), 10 ); @@ -113,7 +113,7 @@ public function frontend_venue_scripts() { * * @return mix */ - public function show_venue_map( $latitude, $longitude, $address, $zoom, $maptype, $osm_tile, $osm_attribution ) { + public function show_venue_map( $latitude, $longitude, $address, $zoom, $osm_tile, $osm_attribution ) { $lat = abs( $latitude ); $lat_deg = floor( $lat ); $lat_sec = ( $lat - $lat_deg ) * 3600; @@ -136,17 +136,10 @@ public function show_venue_map( $latitude, $longitude, $address, $zoom, $maptype // initialize map map = L.map('sp_openstreetmaps_container', { zoomControl:false }).setView([lat, lon], ); // set map tiles source - - L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', { - attribution: 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community', - maxZoom: 18, - }).addTo(map); - L.tileLayer('', { attribution: '', maxZoom: 18, }).addTo(map); - // add marker to the map marker = L.marker([lat, lon]).addTo(map); map.dragging.disable(); diff --git a/templates/venue-map.php b/templates/venue-map.php index d58fad602..1d4283e42 100644 --- a/templates/venue-map.php +++ b/templates/venue-map.php @@ -35,13 +35,14 @@ if ( '' === $address ) { $address = '+'; } -if ( 'satellite' !== $maptype ) { - $maptype = 'roadmap'; +if ( 'satellite' === $maptype ) { + $osm_tile = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}'; + $osm_attribution = 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community'; } if ( $latitude != null && $longitude != null ) { - do_action( 'sp_venue_show_map', $latitude, $longitude, $address, $zoom, $maptype, $osm_tile, $osm_attribution ); + do_action( 'sp_venue_show_map', $latitude, $longitude, $address, $zoom, $osm_tile, $osm_attribution ); } if ( is_tax( 'sp_venue' ) ) { do_action( 'sportspress_after_venue_map' ); From 1d4a7d6e6486b5611d7959023e8385f672dc132d Mon Sep 17 00:00:00 2001 From: Moritz 'e1mo' Fromm Date: Mon, 7 Nov 2022 19:06:35 +0100 Subject: [PATCH 3/3] Use custom tiles server in admin UI --- assets/js/admin/sp-geocoder.js | 4 ++-- modules/sportspress-openstreetmap.php | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/assets/js/admin/sp-geocoder.js b/assets/js/admin/sp-geocoder.js index 1cb263573..31fd98080 100644 --- a/assets/js/admin/sp-geocoder.js +++ b/assets/js/admin/sp-geocoder.js @@ -16,9 +16,9 @@ var map = L.map( 'sp-location-picker' ).setView( [latitude, longitude], 15 marker = L.marker( [latitude, longitude],{draggable: true, autoPan: true} ).addTo( map ); L.tileLayer( - 'http://{s}.tile.osm.org/{z}/{x}/{y}.png', + sp_osm_settings["tile_server"], { - attribution: '© OpenStreetMap contributors' + attribution: sp_osm_settings["attribution"] } ).addTo( map ); diff --git a/modules/sportspress-openstreetmap.php b/modules/sportspress-openstreetmap.php index 69aed7b86..1d6ebf3f3 100644 --- a/modules/sportspress-openstreetmap.php +++ b/modules/sportspress-openstreetmap.php @@ -70,7 +70,25 @@ public function admin_scripts() { * Enqueue admin venue scripts */ public function admin_venue_scripts() { - $screen = get_current_screen(); + $screen = get_current_screen(); + + $maptype = get_option( 'sportspress_map_type', 'roadmap' ); + $maptype = strtolower( $maptype ); + $osm_tile = get_option( 'sportspress_osm_tile_server', '' ); + $osm_tile = strtolower( $osm_tile ); + $osm_tile = empty( $osm_tile ) ? 'https://tile.openstreetmap.org/{z}/{x}/{y}.png' : $osm_tile; + $osm_attribution = get_option( 'sportspress_osm_attribution', '' ); + $osm_attribution = empty( $osm_attribution ) ? 'Map data © OpenStreetMap contributors' : $osm_attribution; + if ( 'satellite' === $maptype ) { + $osm_tile = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}'; + $osm_attribution = 'Tiles © Esri — Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community'; + } + $osm_config = [ + 'tile_server' => $osm_tile, + 'attribution' => $osm_attribution, + ]; + + if ( in_array( $screen->id, sp_get_screen_ids() ) ) { wp_enqueue_style( 'leaflet_stylesheet', SP()->plugin_url() . '/assets/css/leaflet.css', array(), '1.8.0' ); @@ -81,6 +99,7 @@ public function admin_venue_scripts() { wp_register_script( 'leaflet_js', SP()->plugin_url() . '/assets/js/leaflet.js', array(), '1.8.0' ); wp_register_script( 'control-geocoder', SP()->plugin_url() . '/assets/js/Control.Geocoder.min.js', array( 'leaflet_js' ), '1.13.0' ); wp_register_script( 'sportspress-admin-geocoder', SP()->plugin_url() . '/assets/js/admin/sp-geocoder.js', array( 'leaflet_js', 'control-geocoder' ), SP_VERSION, true ); + wp_localize_script( 'sportspress-admin-geocoder', 'sp_osm_settings', $osm_config ); } if ( in_array( $screen->id, array( 'edit-sp_venue' ) ) ) {