Categories
March 6, 2024
Locations
ID);
}
// ACF Post Object / Page Link returned as an array.
if (is_array($value) && !empty($value['ID'])) {
return get_permalink((int) $value['ID']);
}
// ACF Page Link / Post Object returned as an ID.
if (is_numeric($value)) {
return get_permalink((int) $value);
}
// ACF URL field.
return $value;
};
$format_phone_url = static function ($phone) {
$digits = preg_replace('/\D+/', '', (string) $phone);
if (strlen($digits) === 10) {
return '+1' . $digits;
}
if (strlen($digits) === 11 && substr($digits, 0, 1) === '1') {
return '+' . $digits;
}
return $digits ? '+' . $digits : '';
};
$locations = get_posts([
'post_type' => $location_post_type,
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
]);
$location_items = [];
foreach ($locations as $location) {
$location_id = $location->ID;
$branch_name = trim((string) get_field('branch_name', $location_id));
$branch_name = $branch_name ?: get_the_title($location_id);
$address_lines = array_values(array_filter([
trim((string) get_field('address_line_1', $location_id)),
trim((string) get_field('address_line_2', $location_id)),
trim((string) get_field('address_line_3', $location_id)),
], static function ($line) {
return $line !== '';
}));
$phone = trim((string) get_field('phone', $location_id));
$details_url = $get_location_url(
get_field('individual_location_page', $location_id)
);
$location_items[] = [
'name' => $branch_name,
'address_lines' => $address_lines,
'phone' => $phone,
'phone_url' => $format_phone_url($phone),
'details_url' => $details_url,
];
}
$map_locations = array_values(array_filter($location_items, static function ($location) {
return !empty($location['address_lines']);
}));
?>