| | |
| | | } |
| | | } |
| | | |
| | | |
| | | $allFields = $meta->getAll($fields); |
| | | |
| | | foreach ($allFields as $name => $value) { |
| | |
| | | |
| | | protected static function renderLocation(string $name, mixed $value, array $config): string |
| | | { |
| | | if (!Site::hasIntegration('maps')) { |
| | | return ''; |
| | | } |
| | | $googleMaps = GoogleMaps::getInstance(); |
| | | if (!$googleMaps->isSetUp()) { |
| | | return '<div class="notice notice-warning"><p>Google Maps not configured. Please configure in Integrations settings.</p></div>'; |
| | | } |
| | | $location = is_array($value) ? $value : []; |
| | | $fields = ['address', 'street', 'city', 'province', 'postal_code', 'country', 'lat', 'lng']; |
| | | |
| | | $field_id = esc_attr($name); |
| | | $map_id = sprintf('%s_map', $field_id); |
| | | $components = ['address', 'lat', 'lng', 'street', 'city', 'province', 'postal_code', 'country']; |
| | | if (!empty($value)) { |
| | | $lat = (float)$value['lat']??''; |
| | | $lng = (float)$value['lng']??''; |
| | | |
| | | $coords = [ |
| | | 'lat' => $lat, |
| | | 'ng' => $lng |
| | | ]; |
| | | } else { |
| | | $coords = null; |
| | | } |
| | | if (!array_key_exists('data', $config)) { |
| | | $config['data'] =[]; |
| | | } |
| | | $js_config = [ |
| | | 'fieldId' => $field_id, |
| | | 'initialCoords' => $coords |
| | | ]; |
| | | |
| | | $json_config = htmlspecialchars(json_encode($js_config), ENT_QUOTES, 'UTF-8'); |
| | | $config['data']['location-field-init'] = $json_config; |
| | | |
| | | $input = ''; |
| | | if (!empty($value) && array_key_exists('address', $value)) { |
| | | $input = sprintf( |
| | | '<p><b>Current location:</b> %s</p><p class="hint">Search below to change:</p>', |
| | | esc_html($value['address']) |
| | | ); |
| | | } |
| | | $links = (!empty($value)) ? jvbLocationLinks($value) : ''; |
| | | $input .= sprintf( |
| | | '<div class="location-search-wrapper"> |
| | | <div class="autocomplete-wrapper"></div> |
| | | <div class="location-preview"> |
| | | <div id="%s" class="location-map"></div> |
| | | %s |
| | | </div>', |
| | | esc_attr($map_id), |
| | | $links |
| | | ); |
| | | |
| | | if (!empty($value)) { |
| | | foreach($components as $el) { |
| | | $input .= sprintf( |
| | | '<input type="hidden" |
| | | name="%s[%s]" |
| | | value="%s" |
| | | data-location-field="%s">', |
| | | '<h3>%s</h3><input type="search" id="%s-search" placeholder="%s" autocomplete="off" data-ignore data-location-search /> |
| | | <p class="message" hidden aria-live="polite">{ <span>Searching locations</span> }</p> |
| | | <ul class="search-results"></ul><p><small><i>{ All Results © OpenStreetMap contributors, ODbL 1.0 }</i></small></p>', |
| | | $config['label'], |
| | | esc_attr($name), |
| | | $el, |
| | | $value[$el]??'', |
| | | $el |
| | | esc_attr($config['placeholder'] ?? 'Search for an address…'), |
| | | ); |
| | | |
| | | $input .= '<fieldset class="location"><legend>Current Location</legend>'; |
| | | foreach ($fields as $field) { |
| | | $title = match($field) { |
| | | 'address' => 'House Number', |
| | | 'lat' => 'Latitude', |
| | | 'lng' => 'Longitude', |
| | | 'postal_code' => 'Postal Code', |
| | | default => ucfirst($field) |
| | | }; |
| | | $open = match($field) { |
| | | 'address','postal_code','lat' => '<div class="row">', |
| | | default => '', |
| | | }; |
| | | $close = match ($field) { |
| | | 'street','country','lng' => '</div>', |
| | | default => '', |
| | | }; |
| | | $input .= sprintf( |
| | | '%s<div><label for="%s|%s">%s</label> |
| | | <input type="text" name="%s" data-field-name="%s" id="%s|%s" value="%s" /></div>%s', |
| | | $open, |
| | | esc_attr($name), |
| | | esc_attr($field), |
| | | $title, |
| | | esc_attr("{$name}|{$field}"), |
| | | esc_attr($field), |
| | | esc_attr($name), |
| | | esc_attr($field), |
| | | esc_attr($location[$field] ?? ''), |
| | | $close |
| | | ); |
| | | } |
| | | } |
| | | $input .= '</fieldset>'; |
| | | |
| | | $input .= '</div>'; |
| | | // if (!empty($location['lat']) && !empty($location['lng'])) { |
| | | // $input .= sprintf( |
| | | // '<div class="location-preview" data-lat="%s" data-lng="%s"></div>', |
| | | // esc_attr($location['lat']), |
| | | // esc_attr($location['lng']) |
| | | // ); |
| | | // } |
| | | |
| | | $config['skipInput'] = true; |
| | | $config['label'] = false; |
| | | return static::fieldWrap($name, $input, $config); |
| | | } |
| | | |