Skip to content

Commit

Permalink
Fix geo data handling of manual created events (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
albig authored Nov 4, 2024
1 parent e5e3221 commit de42576
Show file tree
Hide file tree
Showing 10 changed files with 358 additions and 154 deletions.
43 changes: 43 additions & 0 deletions assets/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,46 @@ p a img {
border-left-color: #005437;
box-shadow: 0 1px 1px rgba(0,0,0,.04);
}

.sunflower-field {
margin-top: 5px;
}
.sunflower-field label {
vertical-align: bottom;
}
.sunflower-field input {
margin-top: 5px;
}
.sunflower-field.checkbox {
padding: 10px 0;
}
.sunflower-field.checkbox label {
margin-left: 4px;

}
#sunflower_event_meta_box div.inside {
display: flex;
flex-wrap: nowrap;
flex-direction: column;
min-width: 0;
flex-basis: auto;
}

#sunflower_event_meta_box .components-flex.components-h-stack {
display: flex;
justify-content: flex-start;
flex-wrap: wrap;
}

#sunflower_event_meta_box .components-flex.components-h-stack button {
margin-right: 5px;
}

#leaflet {
border: 1px solid #005437;
margin: 2px 0;
}

#sunflowerHideMap {
display: none;
}
117 changes: 83 additions & 34 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ jQuery( function ( $ ) {

// Methods for edition the location of manual added events.
jQuery( function () {
jQuery( '#sunflowerDeleteMap' ).on( 'click', function () {
sunflowerDeleteLeaflet();
jQuery( '#_sunflower_event_show_map' ).on( 'click', function () {
if ( jQuery( '#_sunflower_event_show_map' ).is( ':checked' ) ) {
jQuery( '#sunflower-map-settings' ).show();
} else {
jQuery( '#sunflower-map-settings' ).hide();
}
} );
jQuery( '#sunflower-fix-location-delete' ).on( 'click', function () {
sunflowerFixLocationDelete();
Expand All @@ -33,21 +37,45 @@ jQuery( function () {
} );
} );

function sunflowerDeleteLeaflet() {
jQuery( '#_sunflower_event_lat' ).val( '' );
jQuery( '#_sunflower_event_lon' ).val( '' );
jQuery( '#_sunflower_event_zoom' ).val( '' );
jQuery( '#leaflet' ).hide();
jQuery( '#sunflowerShowMap' ).show();
}

let marker;
let leaflet = null;
/* eslint-disable-next-line no-unused-vars */
function sunflowerShowLeaflet( lat, lon, zoom, showMarker ) {
if ( lat === -1 && lon === -1 && zoom === -1 ) {
const address = [];

const street = jQuery(
'input[name="_sunflower_event_location_street"]'
).val();
const city = jQuery(
'input[name="_sunflower_event_location_city"]'
).val();

if ( street ) {
address.push( street.trim() );
}

if ( city ) {
address.push( city.trim() );
}
jQuery.ajax( {
url: 'https://nominatim.openstreetmap.org/search?format=json',
method: 'GET',
data: {
q: address.join( ', ' ),
},
async: false,
success( response ) {
lat = response[ 0 ].lat;
lon = response[ 0 ].lon;
zoom = 12;
},
} );
}

if ( leaflet ) {
jQuery( '#leaflet' ).show();
// do not initalize again
jQuery( '#leaflet div' ).show();
// do not initialize again
return;
}
leaflet = L.map( 'leaflet' ).setView( [ lat, lon ], zoom );
Expand All @@ -60,7 +88,7 @@ function sunflowerShowLeaflet( lat, lon, zoom, showMarker ) {
marker = L.marker( [ lat, lon ] ).addTo( leaflet );

if ( showMarker ) {
sunflowerSetMarker( lat, lon, leaflet.getZoom(), marker );
sunflowerSetMarker( lat, lon, leaflet.getZoom(), marker, false );
} else {
jQuery( '#sunflower-location-row' ).show();
}
Expand All @@ -70,36 +98,51 @@ function sunflowerShowLeaflet( lat, lon, zoom, showMarker ) {
ev.latlng.lat,
ev.latlng.lng,
leaflet.getZoom(),
marker
marker,
true
);
} );

jQuery( '#sunflowerShowMap' ).hide();
}

function sunflowerSetMarker( lat, lon, zoom, setmarker ) {
function sunflowerSetMarker( lat, lon, zoom, setmarker, update ) {
jQuery( '#_sunflower_event_lat' ).val( lat );
jQuery( '#_sunflower_event_lon' ).val( lon );
jQuery( '#_sunflower_event_zoom' ).val( zoom );

setmarker.setLatLng( new L.LatLng( lat, lon ) );

// do this only for location fixing
if ( jQuery( '#sunflower-location' ).length === 0 ) {
return;
}
if ( update === true ) {
const address = [];

jQuery.ajax( {
url: ajaxurl,
method: 'POST',
data: {
action: 'sunflower_fix_event_location',
_wpnonce: jQuery( '#_wpnonce-locationfix' ).val(),
lat,
lon,
transient: jQuery( '#sunflower-location option:selected' ).text(),
},
} );
const street = jQuery(
'input[name="_sunflower_event_location_street"]'
).val();
const city = jQuery(
'input[name="_sunflower_event_location_city"]'
).val();

if ( street ) {
address.push( street.trim() );
}

if ( city ) {
address.push( city.trim() );
}

jQuery.ajax( {
url: ajaxurl,
method: 'POST',
data: {
action: 'sunflower_fix_event_location',
_wpnonce: jQuery( '#_wpnonce-locationfix' ).val(),
lat,
lon,
do: 'update',
transient: address.join( ', ' ),
},
} );
}
}

function sunflowerFixLocation() {
Expand All @@ -108,15 +151,21 @@ function sunflowerFixLocation() {
}

function sunflowerFixLocationDelete() {
const transient = jQuery( '#sunflower-location option:selected' ).text();
const poiSelected = jQuery( '#sunflower-location option:selected' ).val();

// remove von von current select list
jQuery( '#sunflower-location' )
.children( 'option[value="' + poiSelected + '"]' )
.remove();
jQuery.ajax( {
url: ajaxurl,
method: 'POST',
data: {
action: 'sunflower_fix_event_location',
_wpnonce: jQuery( '#_wpnonce-locationfix' ).val(),
lat: 0,
lon: 0,
transient: jQuery( '#sunflower-location option:selected' ).text(),
transient,
do: 'delete',
},
} );
}
Expand Down
28 changes: 25 additions & 3 deletions functions/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ function sunflower_notice_php() {
<p class="h3"><?php esc_attr_e( 'PHP Version End of Life', 'sunflower' ); ?></p>
<p>
<?php
$phpversion = 'Current PHP version: ' . phpversion();
$linkverdigado = "<a href='https://www.verdigado.com/' target='_blank' title='verdigado eG'>
$phpversion = 'Current PHP version: ' . phpversion();
$linkverdigado = "<a href='https://www.verdigado.com/' target='_blank' title='verdigado eG'>
<img src='" . get_template_directory_uri() . "/assets/img/verdigado-logo.png' alt='Logo of verdigado eG' /></a>";

printf(
Expand Down Expand Up @@ -193,7 +193,7 @@ function sunflower_admin_style() {
'sunflower-admin-styles',
get_template_directory_uri() . '/assets/css/admin.css',
null,
'2.1.0'
SUNFLOWER_VERSION
);
}

Expand Down Expand Up @@ -276,13 +276,35 @@ function sunflower_login_logo() {
* Fix event location.
*/
function sunflower_fix_event_location() {
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'sunflower_location' ) ) {
return;
}
$transient = sprintf( 'sunflower_geocache_%s', $_POST['transient'] );
if ( 'delete' === $_POST['do'] ) {
delete_transient( $transient );
} else {
$lon = $_POST['lon'];
$lat = $_POST['lat'];
set_transient( $transient, array( $lon, $lat ) );
}
}

add_action( 'wp_ajax_sunflower_fix_event_location', 'sunflower_fix_event_location' );

/**
* Set event location.
*/
function sunflower_set_event_location() {
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'sunflower_location' ) ) {
return;
}
$transient = sprintf( 'sunflower_geocache_%s', $_POST['transient'] );
$lon = $_POST['lon'];
$lat = $_POST['lat'];
set_transient( $transient, array( $lon, $lat ) );

update_post_meta( $id, '_sunflower_event_lat', $lat );
update_post_meta( $id, '_sunflower_event_lon', $lon );
}

add_action( 'wp_ajax_sunflower_fix_event_location', 'sunflower_fix_event_location' );
Loading

0 comments on commit de42576

Please sign in to comment.