Avoid using duplicate code

This commit is contained in:
savvasha
2019-02-21 13:44:20 +02:00
parent dc15eae7f0
commit 03b67a2173
6 changed files with 157 additions and 148 deletions

View File

@@ -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: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> 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);

View File

@@ -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: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> 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);

View File

@@ -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

View File

@@ -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 {
<tr>
<th scope="row"><?php _e( 'Address', 'sportspress' ); ?></th>
<td>
<input name="address" class="sp-address" type="text">
<div class="sp-location-picker"></div>
<input name="address" id="sp-address" class="sp-address" type="text">
<div id="sp-location-picker"></div>
<p class="description"><?php _e( "Drag the marker to the venue's location.", 'sportspress' ); ?></p>
<input name="latitude" class="sp-latitude" type="hidden" value="40.7324319">
<input name="longitude" class="sp-longitude" type="hidden" value="-73.82480799999996">
<input name="latitude" id="sp-latitude" class="sp-latitude" type="hidden" value="40.7324319">
<input name="longitude" id="sp-longitude" class="sp-longitude" type="hidden" value="-73.82480799999996">
</td>
</tr>
</table>
@@ -546,7 +554,12 @@ class SP_Admin_Setup_Wizard {
<?php wp_nonce_field( 'sp-setup' ); ?>
</p>
</form>
<?php wp_print_scripts( 'sportspress-admin-locationpicker' ); ?>
<?php
if ( class_exists( 'SportsPress_GoogleMaps' ) ) {
wp_print_scripts( 'sportspress-admin-locationpicker' );
} else {
wp_print_scripts( 'sportspress-admin-setup-geocoder' );
}?>
<?php
}

View File

@@ -112,12 +112,11 @@ class SP_Admin_Taxonomies {
$latitude = '40.7324319';
$longitude = '-73.82480799999996';
endif;
if ( class_exists( 'SportsPress_GoogleMaps' ) ) {
do_action ( 'sp_venue_add_googlemaps', $latitude, $longitude, $address );
} else {
do_action ( 'sp_venue_add_openstreetmap', $latitude, $longitude, $address );
}
?>
<div class="form-field">
<p><div id="sp-location-picker" style="width: 95%; height: 320px"></div></p>
<p><?php _e( "Drag the marker to the venue's location.", 'sportspress' ); ?></p>
</div>
<div class="form-field">
<label for="term_meta[sp_address]"><?php _e( 'Address', 'sportspress' ); ?></label>
<input type="text" class="sp-address" name="term_meta[sp_address]" id="term_meta[sp_address]" value="<?php echo esc_attr( $address ); ?>">
@@ -131,6 +130,11 @@ class SP_Admin_Taxonomies {
<input type="text" class="sp-longitude" name="term_meta[sp_longitude]" id="term_meta[sp_longitude]" value="<?php echo esc_attr( $longitude ); ?>">
</div>
<?php
if ( class_exists( 'SportsPress_GoogleMaps' ) ) {
wp_print_scripts( 'sportspress-admin-locationpicker' );
} else {
wp_print_scripts( 'sportspress-admin-geocoder' );
}
}
/**
@@ -152,6 +156,12 @@ class SP_Admin_Taxonomies {
do_action ( 'sp_venue_edit_openstreetmap', $latitude, $longitude, $address );
}
?>
<tr class="form-field">
<td colspan="2">
<p><div id="sp-location-picker" style="width: 95%; height: 320px"></div></p>
<p class="description"><?php _e( "Drag the marker to the venue's location.", 'sportspress' ); ?></p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[sp_address]"><?php _e( 'Address', 'sportspress' ); ?></label></th>
<td>
@@ -171,6 +181,11 @@ class SP_Admin_Taxonomies {
</td>
</tr>
<?php
if ( class_exists( 'SportsPress_GoogleMaps' ) ) {
wp_print_scripts( 'sportspress-admin-locationpicker' );
} else {
wp_print_scripts( 'sportspress-admin-geocoder' );
}
}
/**

View File

@@ -30,13 +30,12 @@ if ( ! class_exists( 'SportsPress_OpenStreetMap' ) ) :
$this->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 ) {
?>
<div class="form-field">
<p><div id="mapDiv" style="width: 95%; height: 320px"></div></p>
<p><?php _e( "Drag the marker to the venue's location.", 'sportspress' ); ?></p>
</div>
<script>
//Initialize the map and add the Search control box
var map = L.map('mapDiv').setView([<?php echo $latitude;?>, <?php echo $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([<?php echo $latitude;?>, <?php echo $longitude;?>],{draggable: true, autoPan: true}).addTo(map);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> 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);
</script>
<?php
}
//public function add_venue_openstreetmap( $latitude, $longitude, $address ) {
//}
/**
* Integrate OpenStreetMap (Edit Venue)
*
* @return mix
*/
public function edit_venue_openstreetmap( $latitude, $longitude, $address ) {
?>
<tr class="form-field">
<td colspan="2">
<p><div id="mapDiv" style="width: 95%; height: 320px"></div></p>
<p class="description"><?php _e( "Drag the marker to the venue's location.", 'sportspress' ); ?></p>
</td>
</tr>
<?php if ( $latitude === '' || $longitude === '' ) {
$latitude = 40.866667;
$longitude = 34.566667;
$zoom = 1;
}else{
$zoom = 15;
}
?>
<script>
//Initialize the map and add the Search control box
var map = L.map('mapDiv').setView([<?php echo $latitude;?>, <?php echo $longitude;?>], <?php echo $zoom; ?>),
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([<?php echo $latitude;?>, <?php echo $longitude;?>],{draggable: true, autoPan: true}).addTo(map);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> 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);
</script>
<?php
}
//public function edit_venue_openstreetmap( $latitude, $longitude, $address ) {
//}
/**
* Integrate OpenStreetMap (Show Venue)