Created
September 23, 2024 01:11
-
-
Save zaczacariah/639238f0550c1b3055d804bb2745ab3c to your computer and use it in GitHub Desktop.
A function for updating a users geo markers in PMPRO when updating their profile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function update_lat_lng_from_physical_address( $user_id ) { | |
$current_hook = current_filter(); | |
error_log(“update_lat_lng_from_physical_address triggered by hook: $current_hook for user_id: $user_id”); | |
// Retrieve physical address fields | |
$physical_address = array( | |
‘street’ => ( !empty( $_REQUEST[‘physical_address1’] ) ) ? $_REQUEST[‘physical_address1’] : get_user_meta( $user_id, ‘physical_address1’, true ), | |
‘city’ => ( !empty( $_REQUEST[‘physical_suburb’] ) ) ? $_REQUEST[‘physical_suburb’] : get_user_meta( $user_id, ‘physical_suburb’, true ), | |
‘state’ => ( !empty( $_REQUEST[‘physical_state’] ) ) ? $_REQUEST[‘physical_state’] : get_user_meta( $user_id, ‘physical_state’, true ), | |
‘zip’ => ( !empty( $_REQUEST[‘physical_post_code’] ) ) ? $_REQUEST[‘physical_post_code’] : get_user_meta( $user_id, ‘physical_post_code’, true ), | |
‘country’ => ‘Australia’, | |
); | |
error_log(“Physical address data: ” . print_r($physical_address, true)); | |
// If any of the physical address fields are empty, bail. | |
if ( empty( $physical_address[‘street’] ) || empty( $physical_address[‘city’] ) || empty( $physical_address[‘zip’] ) || empty( $physical_address[‘country’] ) ) { | |
error_log(“Incomplete physical address data. Exiting function.”); | |
return; | |
} | |
// Log existing coordinates before updating | |
$existing_lat = get_user_meta( $user_id, ‘pmpro_lat’, true ); | |
$existing_lng = get_user_meta( $user_id, ‘pmpro_lng’, true ); | |
error_log(“Existing coordinates – Lat: $existing_lat, Lng: $existing_lng”); | |
// Geocode the physical address | |
error_log(“Attempting to geocode address”); | |
$coordinates = pmpromm_geocode_address( $physical_address ); | |
error_log(“Geocoding result: ” . print_r($coordinates, true)); | |
// Update user meta with latitude and longitude | |
if ( is_array( $coordinates ) && !empty( $coordinates[‘lat’] ) && !empty( $coordinates[‘lng’] ) ) { | |
update_user_meta( $user_id, ‘pmpro_lat’, $coordinates[‘lat’] ); | |
update_user_meta( $user_id, ‘pmpro_lng’, $coordinates[‘lng’] ); | |
// Log new coordinates after updating | |
$new_lat = $coordinates[‘lat’]; | |
$new_lng = $coordinates[‘lng’]; | |
error_log(“Updated coordinates – Lat: $new_lat, Lng: $new_lng”); | |
} else { | |
error_log(“Failed to update coordinates. Invalid or empty geocoding result.”); | |
} | |
error_log(“Finished update_lat_lng_from_physical_address for user_id: $user_id”); | |
} | |
add_action( ‘pmpro_personal_options_update’, ‘update_lat_lng_from_physical_address’ ); | |
add_action( ‘personal_options_update’, ‘update_lat_lng_from_physical_address’ ); | |
add_action( ‘edit_user_profile_update’, ‘update_lat_lng_from_physical_address’ ); | |
add_action( ‘pmpro_after_checkout’, ‘update_lat_lng_from_physical_address’ ); | |
add_action( ‘pmproiucsv_post_user_import’, ‘update_lat_lng_from_physical_address’ ); | |
Additional Informatio |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment