I created a ACF block. It calculates the distance from the displayed ski resort to the 3 closest ski resorts and displays a summary and is meant to use in a side bar or something.
I have a function called distance() that takes two latitude and longitude points and calculates the difference. I am getting a PHP Fatal Error when I edit the custom post type fs_ski_resorts, although in the front end everything displays correctly. Also, I have added this block to 2 custom post types. They both display perfectly, however, 1 of the 2 produces the fatal error in the backend with editing it. There is only 1 block on the page, too. Strange.
[25-Aug-2024 12:09:31 UTC] PHP Fatal error: Cannot redeclare distance() (previously declared in /home/example/htdocs/www.example.com/wp-content/plugins/fs-nearby-resorts-block/template-parts/fs-nearby-resorts-block-template.php:101) in /home/example/htdocs/www.example.com/wp-content/plugins/fs-nearby-resorts-block/template-parts/fs-nearby-resorts-block-template.php on line 101
I have only declared distance one, however, it seems somehow it is getting declared twice. I cannot figure out how to debug this anymore... any ideas on how to debug or what the problem may be?
Here is the code:
<?php
$id = 'fs-nearby-resorts-block-'.$block['id'];
if(!empty($block['anchor'])){
$id = $block['anchor'];
}
if(!empty($block['className'])){
$className .= ' '.$block['className'];
}
//if(!empty($block['align'])){
// $className .= ' align'.$block['align'];
//}
$className = "fs-nearby-resorts-block";
global $post;
$post_slug = $post->post_name;
$distanceFromSkiResort = 100; // in kilometers.
$latitude_from = do_shortcode('[fs_ski_resort_field key="aa_y" id="'.$post_id.'"]');
$longitude_from = do_shortcode('[fs_ski_resort_field key="aa_x" id="'.$post_id.'"]');
// args
$args = array(
'posts_per_page' =>-1,
'post_type' => 'fs_ski_resorts',
);
// query
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ): ?>
<ul>
<?php
$resortArray = array();
while( $the_query->have_posts() ) : $the_query->the_post();
$to_ski_resort_id = get_the_ID();
$latitude_to = do_shortcode('[fs_ski_resort_field key="aa_y" id="'.$to_ski_resort_id.'"]');
$longitude_to = do_shortcode('[fs_ski_resort_field key="aa_x" id="'.$to_ski_resort_id.'"]');
$distance = (float) round(distance($latitude_from, $longitude_from, $latitude_to, $longitude_to, 'K'),1);
if ($distance <= $distanceFromSkiResort) {
if ($post_id <> $to_ski_resort_id) { // remove the current resort from the list of ski resorts within X km.
$resortData = array (
'get_permalink' => get_permalink(),
'get_the_title' => get_the_title(),
'distanceToResort' => $distance,
'aa_pistes_length_km' => do_shortcode('[fs_ski_resort_field key="aa_pistes_length_km" id="'.$post_id.'"]'),
'aa_lifts_length_km' => do_shortcode('[fs_ski_resort_field key="aa_lifts_length_km" id="'.$post_id.'"]'),
'aa_pistes_count' => do_shortcode('[fs_ski_resort_field key="aa_pistes_count" id="'.$post_id.'"]'),
'aa_lifts_count' => do_shortcode('[fs_ski_resort_field key="aa_lifts_count" id="'.$post_id.'"]'),
'aa_min' => do_shortcode('[fs_ski_resort_field key="aa_min" id="'.$post_id.'"]'),
'aa_max' => do_shortcode('[fs_ski_resort_field key="aa_max" id="'.$post_id.'"]'),
'aa_ttl_vertical' => do_shortcode('[fs_ski_resort_field key="aa_ttl_vertical" id="'.$post_id.'"]'),
'aa_novice_easy_length_km' => do_shortcode('[fs_ski_resort_field key="aa_novice_easy_length_km" id="'.$post_id.'"]'),
'aa_novice_easy_count' => do_shortcode('[fs_ski_resort_field key="aa_novice_easy_count" id="'.$post_id.'"]'),
'aa_intermediate_count' => do_shortcode('[fs_ski_resort_field key="aa_intermediate_count" id="'.$post_id.'"]'),
'aa_intermediate_length_km' => do_shortcode('[fs_ski_resort_field key="aa_intermediate_length_km" id="'.$post_id.'"]'),
'aa_advanced_expert_freeridee_extreme_count' => do_shortcode('[fs_ski_resort_field key="aa_advanced_expert_freeridee_extreme_count" id="'.$post_id.'"]'),
'aa_advanced_expert_freeridee_extreme_length_km' => do_shortcode('[fs_ski_resort_field key="aa_advanced_expert_freeridee_extreme_length_km" id="'.$post_id.'"]'),
);
$resortArray[get_the_ID()] = $resortData;
}
}
endwhile;
array_multisort(array_column($resortArray, 'distanceToResort'), SORT_ASC, $resortArray);
$i = 1;
echo '<h2>3 Ski Resorts Nearby: </h2>';
foreach ($resortArray as $resort) {
echo '<a href="'.$resort['get_permalink'].'">'. $resort['get_the_title'] .'</a> is '. $resort['distanceToResort'] . "km away!</br>";
echo '<ul>';
echo '<li style="color:#339933"> Beginner: '.$resort['aa_novice_easy_count'].' runs over '.$resort['aa_novice_easy_length_km'].' km </li></br>';
echo '<li style="color:#ed151f"> Intermediate: '.$resort['aa_intermediate_count'].' runs over '.$resort['aa_intermediate_length_km'].' km </li></br>';
echo '<li>Expert: '.$resort['aa_advanced_expert_freeridee_extreme_count'].' runs over '.$resort['aa_advanced_expert_freeridee_extreme_length_km'].' km </li></br>';
echo '</ul>';
echo '<hr></br>';
if ($i++ == 3) break;
}
?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post()
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
distance()
in a template file. Try moving that code to a separate file that is then loaded by the template file (using, eg,require
or something similar).