Nadat er een import is gedaan met WP All Import worden de coordinaten niet opgeslagen. Dit zorgt voor gebrekkige resultaten in de google map. Om dit te ondervangen moet er met een cronjob worden ingesteld dat een url wordt aangeroepen. Deze url is:
https://<website>.nl/?getcoords
Als dit is aangeroepen zal er een script draaien die checkt of de coordinaten zijn ingevuld. Er wordt elk uur een key ingesteld zodat ze niet oneindig blijven checken.
In functions.php het script aanroepen:
<?php include('locs.php'); ?>
Script in de themes map plaatsen:
<?php
// timezone instellen
date_default_timezone_set('Europe/Amsterdam');
// alleen het huidige uur i.v.m. de scankey
$tijd = date('H',time());
global $ld_recalc;
$ld_recalc = array(
'posts_per_run' => 16,
'post_types' => array( 'praktijken' ),
// scan identifier is het huidige uur, na dit uur worden de velden automatisch opnieuw geupdate.
'scan_key' => 'acf-recalc-scan',
'scan_identifier' => $tijd,
'fields' => array(
'google_map' => 'field_5981d3c2d144f',
'address' => 'field_56031abc0cb98',
'city' => 'field_56031ad50cb9a',
'state' => 'field_56031aec0cb9b',
'zip' => 'field_56031b1f0cb9c',
),
);
function recalc_acf_locations_init() {
if ( !isset($_GET['getcoords']) ) return;
global $ld_recalc;
$args = array(
'post_type' => $ld_recalc['post_types'],
'meta_query' => array(
'relation' => 'OR',
array(
'key' => $ld_recalc['scan_key'],
'compare' => 'NOT EXISTS',
'value' => ' ',
),
array(
'key' => $ld_recalc['scan_key'],
'compare' => '!=',
'value' => $ld_recalc['scan_identifier'],
),
),
'posts_per_page' => $ld_recalc['posts_per_run'],
);
$locations = new WP_Query($args);
if ( $locations->have_posts() ) {
echo '<h2>Scanning '. min($locations->found_posts, $ld_recalc['posts_per_run']).' van '. $locations->found_posts.'...</h2>';
echo '<div style="font-size: 14px; font-family: Arial, sans-serif;">';
foreach( $locations->posts as $i => $p ) {
echo '<div style="width: 50%; float: left; overflow: auto;">';
$url = get_permalink( $p->ID );
$edit = get_edit_post_link( $p->ID );
echo '<p><strong>'.($i+1).') '.ucwords(str_replace(array('-','_'), ' ', $p->post_type)).' #'. $p->ID .'</strong> - <a href="'.esc_attr($url).'" target="_blank">'. esc_html($p->post_title) .'</a></p>';
echo '<pre style="border-bottom: 1px solid #666; padding-bottom: 15px; margin: 15px 0;">';
recalc_acf_location_single( $p->ID );
echo '</pre>';
echo '</div>';
}
echo '</div>';
}else{
wp_die('<p><strong>Eemvitaal praktijken:</strong> All locaties bevatten de latitude/longitude velden!</p>');
}
exit;
}
add_action( 'wp', 'recalc_acf_locations_init' );
function recalc_acf_location_single( $post_id ) {
global $ld_recalc;
$location = get_field( $ld_recalc['fields']['google_map'], $post_id );
$lookup_address = null;
$result = null;
$full_address = null;
if ( isset($location['address']) ) {
$full_address = $location['address'];
}else{
if ( empty($ld_recalc['fields']['city']) || empty($ld_recalc['fields']['state']) || empty($ld_recalc['fields']['zip']) ) {
$full_address = get_field( $ld_recalc['fields']['address'], $post_id );
}else{
$address = get_field( $ld_recalc['fields']['address'], $post_id );
$city = get_field( $ld_recalc['fields']['city'], $post_id );
$state = get_field( $ld_recalc['fields']['state'], $post_id );
$zip = get_field( $ld_recalc['fields']['zip'], $post_id );
$full_address = sprintf('%s, %s, %s %s, Nederland', $address, $city, $state, $zip );
}
}
if ( empty($full_address) ) {
echo '<strong>FOUT:</strong> Locatie heeft geen geldig adres en er zijn geen fallback velden opgegeven.';
exit;
}else{
echo '“' . $full_address . '”<br>';
}
$result = recalc_acf_location_lookup( $post_id, $full_address );
if ( $result ) {
echo 'Locatie is gevonden en opgeslagen!';
return true;
}else{
echo '<h2>FOUT! Google map kan het adres niet vinden.</h2>';
exit;
}
}
function recalc_acf_location_lookup( $post_id, $full_address ) {
global $ld_recalc;
$address_one_line = preg_replace('/ *(\r\n|\r|\n)+ */', " ", $full_address);
$coords = recalc_acf_get_latlng( $address_one_line );
if ( $coords ) {
$location = get_field( $ld_recalc['fields']['google_map'], $post_id );
if ( !is_array($location) ) {
$location = array(
'address' => '',
'lat' => '',
'lng' => ''
);
}
if ( empty($location['address']) ) {
$location['address'] = $address_one_line;
}else{
if ( $address_one_line === $location['address'] && $location['lat'] === $coords['lat'] && $location['lng'] === $coords['lng'] ) {
echo 'Locatie is up to date, geen aanpassingen gedaan. ';
update_post_meta( $post_id, $ld_recalc['scan_key'], $ld_recalc['scan_identifier'] );
return true;
}
}
$location['lat'] = $coords['lat'];
$location['lng'] = $coords['lng'];
$result = update_field( $ld_recalc['fields']['google_map'], $location, $post_id );
if ( $result ) {
// Save lat/long as separate meta keys too.
update_post_meta( $post_id, 'latitude', $location['lat'] );
update_post_meta( $post_id, 'longitude', $location['lng'] );
update_post_meta( $post_id, $ld_recalc['scan_key'], $ld_recalc['scan_identifier'] );
return true;
}else{
echo '<h2>FOUT! Niet gelukt om het google map veld te updaten voor ID #'. $post_id .':</h2>';
exit;
}
}
return false;
}
function recalc_acf_get_latlng( $address ) {
// http://stackoverflow.com/a/8633623/470480
$address = urlencode($address); // Spaces as + signs
$request = wp_remote_get("https://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&key=AIzaSyB2ejtSDkal56Uj81gcf-pQORME1wKTDe0");
$json = wp_remote_retrieve_body( $request );
if ( !$json ) {
echo 'Google Maps gaf een leeg resultaat';
return false;
}
$data = json_decode($json);
if ( !$data ) {
echo '<h2>ERROR! Google Maps gaf een onjuiste reactie, verwachte JSON data:</h2>';
echo esc_html(print_r($json, true));
exit;
}
if ( isset($data->{'error_message'}) ) {
echo '<h2>FOUT! Google Maps API geeft een foutmelding:</h2>';
echo '<strong>'. esc_html($data->{'status'}) .'</strong> ' . esc_html($data->{'error_message'}) .'<br>';
exit;
}
if ( empty($data->{'results'}[0]->{'geometry'}->{'location'}->{'lat'}) || empty($data->{'results'}[0]->{'geometry'}->{'location'}->{'lng'}) ) {
echo '<h2>FOUT! Latitude/Longitude velden kunnen niet worden gevonden:</h2>';
echo esc_html(print_r($data, true));
exit;
}
$lat = $data->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$lng = $data->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
// Value can be negative, so check for specifically 0.
if ( floatval( $lat ) === 0 || floatval( $lng ) === 0 ) {
echo '<h2>FOUT! Latitude/Longitude velden zijn niet juist:</h2>';
var_dump('Latitude:', $lat);
var_dump('Longitude:', $lng);
var_dump('Result:', $data->{'results'}[0]);
exit;
}
return array( 'lat' => $lat, 'lng' => $lng );
}
function recalc_acf_clean_address( $address ) {
$address = preg_replace('/ *(\r\n|\r|\n)+ */', " ", $address); // No linebreaks
return $address;
}
#acf #coordinaten #google maps #locaties #php #WP All import Pro