Hi Minesh
I thought my previous update answered your question. I'll try to clarify:
We were provided with code to limit the address entry to a particular latitude and longitude range. This range basically places a kind of "coordinate rectangle" over Germany, so if the address entered falls outside of that rectangle specified by the coordinates then the message should be displayed.
In answer to your previous update, we need Toolset's expertise to determine why this code is only working SOMETIMES and not every time.
For example, maybe 1 time out of 3 or 4 times, it is possible to follow exactly the same steps and get a different result. We cannot ascertain why the code is not working 100% of the time, because we are simply following the same steps each time (create an Ad or edit an Ad).
Kind regards
Simon
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Sorry for late reply but support is busy here due to holiday season.
I see you already restricted the map address field to display only Germany address when you type the address. Did you added that recently or it was already there?
Do you have any specific string that I can use that should help me to see the issue?
HI Minesh
There was existing JavaScript to limit the autocomplete suggestions from Google Maps to addresses in Germany, but if someone doesn't use that autocomplete suggestion, they might still type something like for example "Taranto, Italy", "Taranto", "Paris, France", "Paris" etc, in which case the code which Luo provided should react when the user tries to save the record.
If you read through the thread, you can see we were using the "Taranto" location in Italy. But you could use any city that is obviously not in Germany, eg, "Jaipur" or "Sydney".
You will find when you try to create a few ads, that the code provided by Luo works sometimes, but not all the time, as would be expected.
Kind regards
Simon
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
With the code snippet "function-validate-nanny-ad-dev-approximate-location" that is added to "Custom Code" section I've added the following function. This function will help us to know the country of the address string which is input type user.
=> hidden link
function getGeoCounty($geoAddress) {
$map_api_key = 'AIzaSyB75kUxqo7733GffPK-aV7npbVoL5HBmis';
$cleanAddress = str_replace (" ", "+", $geoAddress);
$map_api_url = "<em><u>hidden link</u></em>".$cleanAddress."&sensor=false&key=".$map_api_key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $map_api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$jsondata = @json_decode(curl_exec($ch), true);
if(isset($jsondata["results"])) {
foreach ($jsondata["results"] as $result) {
foreach ($result["address_components"] as $address) {
if (in_array("country", $address["types"])) {
return $address["long_name"];
}
}
}
}
return 0;
}
if the country is found it will return the country otherwise it will return 0.
Now with the "cred_form_validate" hook I've adjusted the code as given under:
add_filter('cred_form_validate', 'validate_nanny_ad_approximate_location', 10, 2);
function validate_nanny_ad_approximate_location ( $error_fields, $form_data ) {
$slug = 'wpcf-nanny-approximate-location';
list($fields,$errors)=$error_fields;
$country = getGeoCounty($_POST[$slug]);
$country = strtolower(trim($country));
// Post Form - New Nanny Ad (921)
// Post Form - Edit Nanny Ad (923)
// [dev] Post Form - New Nanny Ad - Unverified Nanny (23612)
if (in_array($form_data['id'], array(921, 923, 23612))){
if($country==0 or $country!='germany'){
$errors[$slug] = __('Please enter a valid address in Germany');
}
}
return array($fields,$errors);
}
Could you please check now if it works as expected.
HI Minesh
Thanks for this, this looks like an even better solution! Much more exact! I will need to test this with my colleague and get back to you. But on my preliminary tests, it looked good.
Can you please set this to Waiting on User Feedback and make sure it doesn't close? It might take a few days to get back to you due to all the xmas nonsense. 🙂
Kind regards
Simon
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Really glad that you like the solution I shared, always eager to know the feedback of clients. If everything works as expected then I suggest you to mark resolve the ticket.