Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Created May 23, 2024 20:42
Show Gist options
  • Save Crocoblock/a05b8d7567c4bd691952ca4cb054212c to your computer and use it in GitHub Desktop.
Save Crocoblock/a05b8d7567c4bd691952ca4cb054212c to your computer and use it in GitHub Desktop.
JetFormBuilder JetEngine Get coordinates from map autocomplete field and store to user
<?php
add_action( 'jet-form-builder/custom-action/update-user-address', function( $request ) {
if ( ! function_exists( 'jet_engine' ) ) {
return;
}
if ( empty( $request['user_id'] ) ) {
return;
}
$address_field = 'address';
if ( empty( $request[ $address_field ] ) || ! is_string( $request[ $address_field ] ) ) {
return;
}
$module = \Jet_Engine\Modules\Maps_Listings\Module::instance();
$provider_id = $module->settings->get( 'geocode_provider' );
$geocode_provider = $module->providers->get_providers( 'geocode', $provider_id );
if ( ! $geocode_provider ) {
return;
}
$location = $geocode_provider->get_location_data( $request['address'] );
$lat = $location[ 'lat' ] ?? false;
$lng = $location[ 'lng' ] ?? false;
if ( $lat === false || $lng === false ) {
return;
}
$prefix = md5( $address_field );
$service_values[ $prefix . '_hash' ] = md5( sprintf( '%s,%s', $lat, $lng ) );
$service_values[ $prefix . '_lat' ] = $lat;
$service_values[ $prefix . '_lng' ] = $lng;
foreach ( $service_values as $field => $value ) {
update_user_meta( $request['user_id'], $field, $value );
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment