Skip to content

Instantly share code, notes, and snippets.

@byron-roots
Created February 2, 2022 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byron-roots/c495a30a312120aaa7bedb2e694bae28 to your computer and use it in GitHub Desktop.
Save byron-roots/c495a30a312120aaa7bedb2e694bae28 to your computer and use it in GitHub Desktop.
Group Booking GForms
// For 2 entries
// First Entry
array(18) {
["input_15"]=>
string(5) "24422"
["input_3_2"]=>
string(3) "Mr."
["input_3_3"]=>
string(5) "Byron"
["input_3_6"]=>
string(10) "Fitzgerald"
["input_4"]=>
string(0) ""
["input_5"]=>
string(22) "byron@rootstudio.co.uk"
["input_6"]=>
string(11) "01522528246"
["input_7_1"]=>
string(37) "The Terrace, Grantham Street, Lincoln"
["input_7_2"]=>
string(0) ""
["input_7_3"]=>
string(7) "Lincoln"
["input_7_4"]=>
string(12) "Lincolnshire"
["input_7_5"]=>
string(7) "LN2 1BD"
["input_7_6"]=>
string(14) "United Kingdom"
["input_11"]=>
string(0) ""
["input_12"]=>
string(16) "Byron Fitzgerald"
["input_13"]=>
string(4) "test"
["input_14"]=>
string(11) "01522528246"
["input_16"]=>
string(3) "Yes"
}
// Second Entry
array(18) {
["input_15"]=>
string(5) "24422"
["input_3_2"]=>
string(3) "Mr."
["input_3_3"]=>
string(8) "Winifred"
["input_3_6"]=>
string(7) "Bradley"
["input_4"]=>
string(0) ""
["input_5"]=>
string(22) "byron@rootstudio.co.uk"
["input_6"]=>
string(11) "01522528246"
["input_7_1"]=>
string(37) "The Terrace, Grantham Street, Lincoln"
["input_7_2"]=>
string(0) ""
["input_7_3"]=>
string(7) "Lincoln"
["input_7_4"]=>
string(12) "Lincolnshire"
["input_7_5"]=>
string(7) "LN2 1BD"
["input_7_6"]=>
string(14) "United Kingdom"
["input_11"]=>
string(9) "Something"
["input_12"]=>
string(6) "Second"
["input_13"]=>
string(6) "person"
["input_14"]=>
string(9) "132412421"
["input_16"]=>
NULL
}
// Entry data when accesing ater post creation note ID's are different here
// First Entry
array(40) {
["id"]=>
string(5) "20752"
["status"]=>
string(6) "active"
["form_id"]=>
string(3) "100"
["ip"]=>
string(3) "::1"
["source_url"]=>
string(109) "https://stbarnabashospice.local/wp-admin/admin-post.php?redirect=https://stbarnabashospice.local/colour-dash/"
["currency"]=>
string(3) "GBP"
["post_id"]=>
NULL
["date_created"]=>
string(19) "2022-02-02 10:03:54"
["date_updated"]=>
string(19) "2022-02-02 10:03:54"
["is_starred"]=>
int(0)
["is_read"]=>
int(0)
["user_agent"]=>
string(120) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
["payment_status"]=>
NULL
["payment_date"]=>
NULL
["payment_amount"]=>
NULL
["payment_method"]=>
string(0) ""
["transaction_id"]=>
NULL
["is_fulfilled"]=>
NULL
["created_by"]=>
string(4) "1392"
["transaction_type"]=>
NULL
[15]=>
string(5) "24422"
["3.2"]=>
string(3) "Mr."
["3.3"]=>
string(5) "Byron"
["3.4"]=>
string(0) ""
["3.6"]=>
string(10) "Fitzgerald"
["3.8"]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(22) "byron@rootstudio.co.uk"
[6]=>
string(11) "01522528246"
["7.1"]=>
string(37) "The Terrace, Grantham Street, Lincoln"
["7.2"]=>
string(0) ""
["7.3"]=>
string(7) "Lincoln"
["7.4"]=>
string(12) "Lincolnshire"
["7.5"]=>
string(7) "LN2 1BD"
["7.6"]=>
string(14) "United Kingdom"
[16]=>
string(3) "Yes"
[11]=>
string(0) ""
[12]=>
string(16) "Byron Fitzgerald"
[13]=>
string(4) "test"
[14]=>
string(11) "01522528246"
}
// Second Entry
array(40) {
["id"]=>
string(5) "20753"
["status"]=>
string(6) "active"
["form_id"]=>
string(3) "100"
["ip"]=>
string(3) "::1"
["source_url"]=>
string(109) "https://stbarnabashospice.local/wp-admin/admin-post.php?redirect=https://stbarnabashospice.local/colour-dash/"
["currency"]=>
string(3) "GBP"
["post_id"]=>
NULL
["date_created"]=>
string(19) "2022-02-02 10:03:57"
["date_updated"]=>
string(19) "2022-02-02 10:03:57"
["is_starred"]=>
int(0)
["is_read"]=>
int(0)
["user_agent"]=>
string(120) "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
["payment_status"]=>
NULL
["payment_date"]=>
NULL
["payment_amount"]=>
NULL
["payment_method"]=>
string(0) ""
["transaction_id"]=>
NULL
["is_fulfilled"]=>
NULL
["created_by"]=>
string(4) "1392"
["transaction_type"]=>
NULL
[15]=>
string(5) "24422"
["3.2"]=>
string(3) "Mr."
["3.3"]=>
string(8) "Winifred"
["3.4"]=>
string(0) ""
["3.6"]=>
string(7) "Bradley"
["3.8"]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(22) "byron@rootstudio.co.uk"
[6]=>
string(11) "01522528246"
["7.1"]=>
string(37) "The Terrace, Grantham Street, Lincoln"
["7.2"]=>
string(0) ""
["7.3"]=>
string(7) "Lincoln"
["7.4"]=>
string(12) "Lincolnshire"
["7.5"]=>
string(7) "LN2 1BD"
["7.6"]=>
string(14) "United Kingdom"
[16]=>
string(0) ""
[12]=>
string(6) "Second"
[13]=>
string(6) "person"
[14]=>
string(9) "132412421"
[11]=>
string(0) ""
}
// Results for both entries, not the entry_id is the same
// First Result
array(6) {
["is_valid"]=>
bool(true)
["page_number"]=>
int(0)
["source_page_number"]=>
int(1)
["confirmation_message"]=>
string(260) "
Thanks for contacting us! We will get in touch with you shortly.
"
["confirmation_type"]=>
string(7) "message"
["entry_id"]=>
string(5) "20752"
}
// Second Result
array(6) {
["is_valid"]=>
bool(true)
["page_number"]=>
int(0)
["source_page_number"]=>
int(1)
["confirmation_message"]=>
string(260) "
Thanks for contacting us! We will get in touch with you shortly.
"
["confirmation_type"]=>
string(7) "message"
["entry_id"]=>
string(5) "20752"
}
<?php
add_action('admin_post_nopriv_studio_race_group_form_submit', 'studio_race_group_form_submit');
add_action('admin_post_studio_race_group_form_submit', 'studio_race_group_form_submit');
// Save group booking entries
function studio_race_group_form_submit()
{
$entrant_form_id = $_POST['nested_id'];
$fields = array_filter($_POST, function ($key) {
return substr($key, 0, strlen('input')) === 'input';
}, ARRAY_FILTER_USE_KEY);
$entrantData = array_filter($_POST, function ($key) use ($entrant_form_id) {
return substr($key, 0, strlen('form')) === "form";
}, ARRAY_FILTER_USE_KEY);
$entrantData = $entrantData["form_$entrant_form_id"];
$entrants = [];
$first = array_key_first($entrantData);
$length = count($entrantData[$first]);
for ($i = 0; $i < $length; $i++) {
foreach ($entrantData as $key => $value) {
unset($_POST["form_$entrant_form_id"][$i][$key]);
$entrants[$i][$key] = $value[$i];
}
}
$data = [];
$form_id = $_POST['form_id'];
$form = GFAPI::get_form($_POST['form_id']);
$entrant_form = GFAPI::get_form($entrant_form_id);
foreach ($fields as $key => $value) {
$id = str_replace("{$form_id}_", '', $key);
unset($_POST[$key]);
$data[$id] = $value;
}
$entrant_data = [];
foreach ($entrants as $index => $entrant) {
$entrant_data[$index] = [];
foreach ($entrant as $key => $value) {
$id = str_replace('.', '_', str_replace('{$form_id}_', '', str_replace("form_{$entrant_form_id}_", '', $key)));
$entrant_data[$index][$id] = $value;
}
}
foreach ($entrant_data as $data) {
var_dump($data); // See $data.txt
$result = GFAPI::submit_form($entrant_form_id, $data);
foreach ($data as $key => $value) {
unset($_POST[$key]);
}
var_dump($result); // See $result.txt
}
}
// Update race number for entry
add_action('gform_advancedpostcreation_post_after_creation', 'save_post_callback', 10, 4);
function save_post_callback($post_id, $feed, $entry, $form)
{
if ($feed['meta']['postType'] != 'entrant') {
return;
}
if (get_field('race_number', $post_id)) {
return;
}
var_dump($entry); // See $entry.txt
$event_id = get_field('race', $post_id);
$query = new WP_Query([
'post_type' => 'entrant',
'meta_key' => 'race',
'meta_value' => $event_id,
'posts_per_page' => 1,
'post__not_in' => [$post_id],
'orderby' => 'race_number',
'order' => 'DESC',
]);
$posts = $query->get_posts();
$race_number_offset = get_field('race_number_offset', $event_id);
if (!isset($posts[0])) {
update_field('race_number', $race_number_offset, $post_id);
return;
}
$post = $posts[0];
$current_race_number = get_field('race_number', $post->ID);
if ($current_race_number < $race_number_offset) {
update_field('race_number', $race_number_offset, $post_id);
} else {
update_field('race_number', $current_race_number + 1, $post_id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment