Skip to content

Instantly share code, notes, and snippets.

@kurtmilam
Last active December 21, 2015 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtmilam/6316124 to your computer and use it in GitHub Desktop.
Save kurtmilam/6316124 to your computer and use it in GitHub Desktop.
Create custom webhooks for Gravity Forms submissions
/* Copyright (C) 2012-2014 Kurt Milam - http://xioup.com | Source: https://gist.github.com/6316124
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**/
// Clear, generalized, reusable way to create custom webhooks for Gravity Forms submissions
// Add to custom_functions.php
//Try to use the admin label, first, then the label, then fall back to the id
function sanitize_gf_field_name($field)
{
if (! empty($field['adminLabel']))
{
$key = $field['adminLabel'];
}
elseif (! empty($field['label']))
{
$key = $field['label'];
}
else
{
$key = strval($field['id']);
}
return sanitize_title_with_dashes($key);
}
//returns a flattened version of the $form array (preserves the nested inputs, too)
function flatten_gf_form($form)
{
$flattened_form = array();
foreach ($form['fields'] as $k => $v)
{
$v['sanitized_field_name'] = sanitize_gf_field_name($v);
$flattened_form['f'.$v['id']] = $v;
if (array_key_exists('inputs', $v) && is_array($v['inputs']))
{
foreach ($v['inputs'] as $ik => $iv)
{
$iv['sanitized_container_field_name'] = $v['sanitized_field_name'];
$iv['sanitized_field_name'][$v['sanitized_field_name']] = sanitize_gf_field_name($iv);
$iv['parent_id'] = 'f'.strval($v['id']);
$flattened_form['f'.$iv['id']] = $iv;
}
}
}
return $flattened_form;
}
//pass in the $entry and $flattened_form arrays
//returns an $entry array with keys based on sanitize_gf_field_names rules
function sanitize_gf_entry($entry, $flattened_form)
{
$sanitized_entry = array();
foreach ($entry as $k => $v)
{
//stripslashes if magic_quotes is enabled on the server
if(get_magic_quotes_gpc())
{
$v_clean = stripslashes($v);
} else {
$v_clean = $v;
}
if (array_key_exists('f'.$k, $flattened_form))
{
$field_name = $flattened_form['f'.$k]['sanitized_field_name'];
if (is_array($field_name))
{
$parent_id = $flattened_form['f'.$k]['parent_id'];
$parent_name = $flattened_form[$parent_id]['sanitized_field_name'];
$sanitized_entry[$parent_name] = array_merge_recursive
(
(array)$sanitized_entry[$parent_name],
array($field_name[$parent_name] => $v_clean)
);
}
else
{
$sanitized_entry = array_merge_recursive
(
(array)$sanitized_entry,
array($field_name => $v_clean)
);
}
} else {
$sanitized_entry['gf'][$k] = $v_clean;
}
}
return $sanitized_entry;
}
//Call the utility functions from the gform_post_submission hook
function post_submission($entry, $form) {
$query_string = http_build_query(sanitize_gf_entry($entry, flatten_gf_form($form)));
//do something with your new querystring here
}
add_action('gform_post_submission', 'post_submission', 10, 2);
/*
Example querystring for a form with one field named "Untitled" and one extended complex Name field (incriminating details removed, spaces added to aid with line breaks):
gf[id]=197
&gf[form_id]=3
&gf[date_created]=2011-05-11+12%3A29%3A19
&gf[is_starred]=0
&gf[is_read]=0
&gf[currency]=USD
&gf[user_agent]=Mozilla%2F5.0+%28Windows+NT+6.0%29+AppleWebKit%2F534.24+%28KHTML%2C+like+Gecko%29+Chrome%2F11.0.696.60+Safari%2F534.24
&untitled=Sam+and+Janet+Evening
&name[prefix]=Mr.+
&name[first]=John
&name[last]=Doe
&name[suffix]=Esq.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment