From 03b67a21733ee44dc3b70180fed48726577f98da Mon Sep 17 00:00:00 2001 From: savvasha Date: Thu, 21 Feb 2019 13:44:20 +0200 Subject: [PATCH] Avoid using duplicate code --- assets/js/admin/sp-geocoder.js | 54 +++++++ assets/js/admin/sp-setup-geocoder.js | 54 +++++++ includes/admin/class-sp-admin-assets.php | 4 +- .../admin/class-sp-admin-setup-wizard.php | 29 +++- includes/admin/class-sp-admin-taxonomies.php | 25 +++- modules/sportspress-openstreetmap.php | 139 +----------------- 6 files changed, 157 insertions(+), 148 deletions(-) create mode 100644 assets/js/admin/sp-geocoder.js create mode 100644 assets/js/admin/sp-setup-geocoder.js diff --git a/assets/js/admin/sp-geocoder.js b/assets/js/admin/sp-geocoder.js new file mode 100644 index 00000000..00ce4039 --- /dev/null +++ b/assets/js/admin/sp-geocoder.js @@ -0,0 +1,54 @@ +//Get variables form input values +latitude = document.getElementById('term_meta[sp_latitude]').value; +longitude = document.getElementById('term_meta[sp_longitude]').value; + +//Initialize the map and add the Search control box +var map = L.map('sp-location-picker').setView([latitude, longitude], 15), + geocoder = L.Control.Geocoder.nominatim(), + control = L.Control.geocoder({ + geocoder: geocoder, + collapsed: false, + defaultMarkGeocode: false + }).addTo(map), + //Add a marker to use from the begining + marker = L.marker([latitude, longitude],{draggable: true, autoPan: true}).addTo(map); + +L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', { + attribution: '© OpenStreetMap contributors' +}).addTo(map); + +//Pass the values to the fields after dragging +marker.on('dragend', function (e) { + document.getElementById('term_meta[sp_latitude]').value = marker.getLatLng().lat; + document.getElementById('term_meta[sp_longitude]').value = marker.getLatLng().lng; + geocoder.reverse(marker.getLatLng(), map.options.crs.scale(map.getZoom()), function(results) { + var r = results[0]; + if (r) { + document.getElementById('term_meta[sp_address]').value = r.name; + } + }) +}); + +//After searching +control.on('markgeocode', function(e) { + var center = e.geocode.center; + var address = e.geocode.name; + map.setView([center.lat, center.lng], 15); //Center map to the new place + map.removeLayer(marker); //Remove previous marker + marker = L.marker([center.lat, center.lng],{draggable: true, autoPan: true}).addTo(map); //Add new marker to use + //Pass the values to the fields after searching + document.getElementById('term_meta[sp_latitude]').value = center.lat; + document.getElementById('term_meta[sp_longitude]').value = center.lng; + document.getElementById('term_meta[sp_address]').value = address; + //Pass the values to the fields after dragging + marker.on('dragend', function (e) { + document.getElementById('term_meta[sp_latitude]').value = marker.getLatLng().lat; + document.getElementById('term_meta[sp_longitude]').value = marker.getLatLng().lng; + geocoder.reverse(marker.getLatLng(), map.options.crs.scale(map.getZoom()), function(results) { + var r = results[0]; + if (r) { + document.getElementById('term_meta[sp_address]').value = r.name; + } + }) + }); +}).addTo(map); \ No newline at end of file diff --git a/assets/js/admin/sp-setup-geocoder.js b/assets/js/admin/sp-setup-geocoder.js new file mode 100644 index 00000000..504b6329 --- /dev/null +++ b/assets/js/admin/sp-setup-geocoder.js @@ -0,0 +1,54 @@ +//Get variables form input values +latitude = document.getElementById('sp_latitude').value; +longitude = document.getElementById('sp_longitude').value; + +//Initialize the map and add the Search control box +var map = L.map('sp-location-picker').setView([latitude, longitude], 15), + geocoder = L.Control.Geocoder.nominatim(), + control = L.Control.geocoder({ + geocoder: geocoder, + collapsed: false, + defaultMarkGeocode: false + }).addTo(map), + //Add a marker to use from the begining + marker = L.marker([latitude, longitude],{draggable: true, autoPan: true}).addTo(map); + +L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', { + attribution: '© OpenStreetMap contributors' +}).addTo(map); + +//Pass the values to the fields after dragging +marker.on('dragend', function (e) { + document.getElementById('sp_latitude').value = marker.getLatLng().lat; + document.getElementById('sp_longitude').value = marker.getLatLng().lng; + geocoder.reverse(marker.getLatLng(), map.options.crs.scale(map.getZoom()), function(results) { + var r = results[0]; + if (r) { + document.getElementById('sp_address').value = r.name; + } + }) +}); + +//After searching +control.on('markgeocode', function(e) { + var center = e.geocode.center; + var address = e.geocode.name; + map.setView([center.lat, center.lng], 15); //Center map to the new place + map.removeLayer(marker); //Remove previous marker + marker = L.marker([center.lat, center.lng],{draggable: true, autoPan: true}).addTo(map); //Add new marker to use + //Pass the values to the fields after searching + document.getElementById('sp_latitude').value = center.lat; + document.getElementById('sp_longitude').value = center.lng; + document.getElementById('sp_address').value = address; + //Pass the values to the fields after dragging + marker.on('dragend', function (e) { + document.getElementById('sp_latitude').value = marker.getLatLng().lat; + document.getElementById('sp_longitude').value = marker.getLatLng().lng; + geocoder.reverse(marker.getLatLng(), map.options.crs.scale(map.getZoom()), function(results) { + var r = results[0]; + if (r) { + document.getElementById('sp_address').value = r.name; + } + }) + }); +}).addTo(map); \ No newline at end of file diff --git a/includes/admin/class-sp-admin-assets.php b/includes/admin/class-sp-admin-assets.php index 88a8696f..1b18514e 100755 --- a/includes/admin/class-sp-admin-assets.php +++ b/includes/admin/class-sp-admin-assets.php @@ -90,6 +90,8 @@ class SP_Admin_Assets { //OpenStreetMaps wp_register_script( 'leaflet_js', SP()->plugin_url() . '/assets/js/leaflet.js', array(), '1.4.0' ); wp_register_script( 'control-geocoder', SP()->plugin_url() . '/assets/js/Control.Geocoder.js', array( 'leaflet_js' ) ); + wp_register_script( 'sportspress-admin-geocoder', SP()->plugin_url() . '/assets/js/admin/sp-geocoder.js', array( 'leaflet_js', 'control-geocoder' ), SP_VERSION, true ); + wp_register_script( 'sportspress-admin-setup-geocoder', SP()->plugin_url() . '/assets/js/admin/sp-setup-geocoder.js', array( 'leaflet_js', 'control-geocoder' ), SP_VERSION, true ); wp_register_script( 'sportspress-admin-equationbuilder', SP()->plugin_url() . '/assets/js/admin/equationbuilder.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-draggable', 'jquery-ui-droppable' ), SP_VERSION, true ); @@ -135,8 +137,6 @@ class SP_Admin_Assets { if ( in_array( $screen->id, array( 'edit-sp_venue' ) ) ) { wp_enqueue_script( 'leaflet_js' ); wp_enqueue_script( 'control-geocoder' ); - wp_enqueue_script( 'jquery-locationpicker' ); - wp_enqueue_script( 'sportspress-admin-locationpicker' ); } // Edit color diff --git a/includes/admin/class-sp-admin-setup-wizard.php b/includes/admin/class-sp-admin-setup-wizard.php index cb642f80..328de34b 100644 --- a/includes/admin/class-sp-admin-setup-wizard.php +++ b/includes/admin/class-sp-admin-setup-wizard.php @@ -104,8 +104,12 @@ class SP_Admin_Setup_Wizard { wp_register_script( 'google-maps', '//tboy.co/maps_js' ); wp_register_script( 'sportspress-setup', SP()->plugin_url() . '/assets/js/admin/sportspress-setup.js', array( 'jquery', 'chosen', 'jquery-tiptip' ), SP_VERSION, true ); - wp_register_script( 'jquery-locationpicker', SP()->plugin_url() . '/assets/js/locationpicker.jquery.js', array( 'jquery', 'google-maps' ), '0.1.6', true ); - wp_register_script( 'sportspress-admin-locationpicker', SP()->plugin_url() . '/assets/js/admin/locationpicker.js', array( 'jquery', 'jquery-locationpicker' ), SP_VERSION, true ); + if ( class_exists( 'SportsPress_GoogleMaps' ) ) { + wp_register_script( 'jquery-locationpicker', SP()->plugin_url() . '/assets/js/locationpicker.jquery.js', array( 'jquery', 'google-maps' ), '0.1.6', true ); + wp_register_script( 'sportspress-admin-locationpicker', SP()->plugin_url() . '/assets/js/admin/locationpicker.js', array( 'jquery', 'jquery-locationpicker' ), SP_VERSION, true ); + } else { + + } $strings = apply_filters( 'sportspress_localized_strings', array( 'none' => __( 'None', 'sportspress' ), @@ -115,7 +119,11 @@ class SP_Admin_Setup_Wizard { // Localize scripts wp_localize_script( 'sportspress-setup', 'localized_strings', $strings ); - wp_enqueue_script( 'google-maps' ); + if ( class_exists( 'SportsPress_GoogleMaps' ) ) { + wp_enqueue_script( 'google-maps' ); + }else{ + + } if ( ! empty( $_POST['save_step'] ) && isset( $this->steps[ $this->step ]['handler'] ) ) { call_user_func( $this->steps[ $this->step ]['handler'] ); @@ -531,11 +539,11 @@ class SP_Admin_Setup_Wizard { - -
+ +

- - + + @@ -546,7 +554,12 @@ class SP_Admin_Setup_Wizard {

- + +
+

+

+
@@ -131,6 +130,11 @@ class SP_Admin_Taxonomies {
+ + +

+

+ + @@ -171,6 +181,11 @@ class SP_Admin_Taxonomies { define_constants(); // Actions - add_action( 'sp_venue_add_openstreetmap', array( $this, 'add_venue_openstreetmap' ), 10, 3 ); - add_action( 'sp_venue_edit_openstreetmap', array( $this, 'edit_venue_openstreetmap' ), 10, 3 ); + //add_action( 'sp_venue_add_openstreetmap', array( $this, 'add_venue_openstreetmap' ), 10, 3 ); + //add_action( 'sp_venue_edit_openstreetmap', array( $this, 'edit_venue_openstreetmap' ), 10, 3 ); add_action( 'sp_venue_show_openstreetmap', array( $this, 'show_venue_openstreetmap' ), 10, 4 ); // Filters //add_filter( 'sportspress_openstreetmap', array( $this, 'add_options' ) ); - //add_filter( 'sportspress_equation_alter', array( $this, 'alter_equation' ), 10, 2 ); } @@ -59,142 +58,16 @@ if ( ! class_exists( 'SportsPress_OpenStreetMap' ) ) : * * @return mix */ - public function add_venue_openstreetmap( $latitude, $longitude, $address ) { - ?> -
-

-

-
- - - - -

-

- - - - -