Skip to content

Instantly share code, notes, and snippets.

@EddyRespondek
Created November 17, 2014 22:55
Show Gist options
  • Save EddyRespondek/a350a14763fc2247ae9e to your computer and use it in GitHub Desktop.
Save EddyRespondek/a350a14763fc2247ae9e to your computer and use it in GitHub Desktop.
<?php
add_action("gform_pre_submission_filter", "after_submission", 10, 2);
function after_submission($form) {
//only process if this is the profile details form
if($form["id"] == 12) {
//save data and notify admin that data has been updated.
global $current_user;
global $wpdb;
get_currentuserinfo();
$user_id = $current_user->id;
$i = 0;
$oldValues = array();
$changesMade = array();
$emailAndPassword = 0;
$sendNotification = true;
foreach($form["fields"] as $field){
//prep label names
$wmlTitle = strtolower($field["label"]);
$wmlTitle = str_replace(" ","_",$wmlTitle);
$type = false;
$key = false;
$newValue = $_POST["input_".$field["id"]];
//we do not update username
if($field["id"] == 1) {continue;}
//create field values array
//translate into wlm fields from gravity forms
if($field["id"] == 7) { $oldValue = get_user_meta( $user_id, 'dob', true ); $wmlTitle = "dob"; $type = "wlmeta";}
if($field["id"] == 6) { $oldValue = get_user_meta( $user_id, 'mobile_number', true ); $wmlTitle = "mobile_number"; $type = "wlmeta";}
if($field["id"] == 4) { $oldValue = get_user_meta( $user_id, 'male_first_name', true ); $wmlTitle = "male_first_name"; $type = "wlmeta";}
if($field["id"] == 5) { $oldValue = get_user_meta( $user_id, 'male_surname', true ); $wmlTitle = "male_surname"; $type = "wlmeta"; }
$oldValue = wlm_custom($wmlTitle);
//default wp fields
if($field["id"] == 11) { $oldValue = patient_email(); $type = "wpmeta"; $key = "user_email"; }
if($field["id"] == 2) { $oldValue = patient_first_name(); $type = "wpmeta"; $key = "first_name"; }
if($field["id"] == 3) { $oldValue = patient_last_name(); $type = "wpmeta"; $key = "last_name"; }
if ( $field["id"] == 12 ) {
if ( $newValue !== "" ) {
$type = "wpmeta"; $key = "user_pass";
}
}
if ($type == "wlmeta") {
$wmlTitleCustom = "custom_".$wmlTitle;
if ( is_null(wlm_custom($wmlTitle)) ) {
$wpdb->insert(
'wp_wlm_user_options',
array(
'user_id' => $user_id,
'option_name' => $wmlTitleCustom,
'option_value' => $newValue,
),
array(
'%d',
'%s',
'%s'
)
);
} else {
$wpdb->update(
'wp_wlm_user_options',
array(
'option_value' => $newValue,
),
array(
'user_id' => $user_id,
'option_name' => $wmlTitleCustom
),
array(
'%s'
),
array(
'%d',
'%s'
)
);
}
}
if ($type == "wpmeta" && $key !== false) {
$user_id = wp_update_user( array( 'ID' => $user_id, $key => $newValue ) );
}
//$result = mysql_query("UPDATE wp_wlm_user_options SET option_value ='".$newValue."' WHERE user_id='$user_id' AND option_name ='".$wmlTitleCustom."'");
//if (!$result) {
// die('Invalid query: ' . mysql_error());
//}
//$oldValue = stripslashes($oldValue);
//compare to old values.
if( $newValue != $oldValue ) {
//store in an array
if($field["id"] == 12 || $field["id"] == 9 || $field["id"] == 11) {
//don't add to changes
} else {
$oldValues[$wmlTitle] = $oldValue;
$changesMade[$i] = $wmlTitle ." changed TO " .$newValue . " FROM " . $oldValue.".";
}
}
$i++;
}
//update basic wp info
wp_update_user( array ( 'ID' => $user_id,
'first_name' => $_POST['input_2'],
'last_name' => $_POST['input_3'],
'user_email' => $_POST['input_11']
)
);
$password = $_POST['input_12'];
$count = count($oldValues);
//dont' send a notificaiton to admin if not necessary.
//rules
//a - do not send if only password and or email has been changed
//b - do not send if no changes have been made at all;
//rule a
if($count == 1 && array_key_exists("email", $oldValues)) {
$sendNotification = false;
}
//rule b
if($count==0) {
$sendNotification = false;
}
//build custom message;
foreach($changesMade as $change){
$newMessage .= $change . "<br />";
}
//display new message;
$dateStamp = getDateStamp();
foreach ($form["notifications"] as $notification) {
if ($notification['name'] == 'Admin Notification') {
if(!$sendNotification) {
$notification["to"] = "junk@mydomain.com.au";
}
$notification["subject"] = $_POST["input_1"] . " " .$dateStamp ." ". $notification["subject"];
$newsubject = $_POST["input_1"] . " " .$dateStamp ." ". $notification["subject"];
$oldMessage = $notification['message'];
$newMessage = nl2br(str_replace('$changes$', $newMessage, $oldMessage));
//set new notification message
$notification['message'] = $newMessage;
}
}
$form['notification']['subject'] = $newSubject;
$form['notification']['to'] = 'user@mydomain.com.au';
$form['notification']['message'] = $newMessage;
//update password;
if(!$password=="") {
wp_set_password( $password, $user_id );
}
//update_user_option( get_current_user_id(), 'default_password_nag', false, true );
update_user_option( get_current_user_id(), 'default_password_nag', false );
}
return $form;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment