Last active
January 20, 2020 20:55
-
-
Save njeffers/bccdac0c4864f3043afd7e286b4d64b9 to your computer and use it in GitHub Desktop.
Gravity Forms Splices
This file contains 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
/** | |
* This is a helper function to grab existing forms and splice them into another form - kinda like form templates | |
* | |
* @param int $template_form_id - the ID of the form you want to copy over | |
* @param int $destination_form_id - ID of the form you want to copy it to | |
* @param int $num_fields_to_skip - how many fields to skip before splicing the form in. Note: this is a count, | |
* NOT 0 index. 2 = 2 fields. | |
* | |
* @author Nick Jeffers | |
* @url nickjeffers.com | |
*/ | |
function nj_gf_template_copier( $template_form_id = 0, $destination_form_id = 0, $num_fields_to_skip = 0 ) | |
{ | |
// make sure the current user is admin | |
if ( ! current_user_can( 'manage_options' ) ) { | |
return; | |
} | |
// did we pass form ids? | |
if ( ! $template_form_id || ! $destination_form_id ) { | |
echo 'Missing form ID(s) to copy to/from'; | |
return; | |
} | |
// attempt to gather the form objects | |
$template_form = GFAPI::get_form( $template_form_id ); | |
$destination_form = GFAPI::get_form( $destination_form_id ); | |
if ( ! $template_form || ! $destination_form ) { | |
echo 'One of your forms doesn\'t exist'; | |
return; | |
} | |
// Figure out what the next available FIELD ID is in your form | |
$new_field_id = 0; | |
foreach ( $destination_form[ 'fields' ] as $field ) { | |
if ( $field->id > $new_field_id ) { | |
$new_field_id = $field->id; | |
} | |
} | |
$new_field_id++; | |
// Gather the fields from the Template Form | |
$fields_to_copy = $template_form[ 'fields' ]; | |
if ( ! $fields_to_copy ) { | |
echo 'Your template form is blank'; | |
return; | |
} | |
// go through each field and reassign the field ID to the next available | |
foreach ( $fields_to_copy as $field_to_copy ) { | |
$old_field_id = $field_to_copy->id; | |
$field_to_copy->id = $new_field_id; | |
$new_field_id++; | |
/** Checks if the current field has nested inputs, | |
* such as the "name" field with 1.2, 1.4, etc. | |
* If so, gets the new field ID and replaces it on the "subfields" inside of this one. | |
* Ex: 1.2 becomes 9.2 | |
**/ | |
if ( isset( $field_to_copy->inputs ) ) { | |
// by reference because Gravity Forms goes haywire otherwise | |
foreach ( $field_to_copy->inputs as &$field_input ) { | |
$field_input[ 'id' ] = str_replace( $old_field_id . '.', $field_to_copy->id . '.', $field_input[ 'id' ] ); | |
} | |
} | |
} | |
// Make a copy of the existing form's fields | |
$temp_form_fields = $destination_form[ 'fields' ]; | |
// Loops through the beginning of the form to grab the first portion of the form (right before the splice area) | |
$before = array_splice( $temp_form_fields, 0, $num_fields_to_skip ); | |
// grabs the remaining fields | |
$after = array_splice( $temp_form_fields, 0 ); | |
// empty out the fields on the destination form before rebuilding it | |
unset( $destination_form[ 'fields' ] ); | |
// Splice them all back together | |
$destination_form[ 'fields' ] = array_merge( $before, $fields_to_copy, $after ); | |
// Finally, update the form and cross your fingers | |
GFAPI::update_form( $destination_form ); | |
} | |
// Example Call: | |
// nj_gf_template_copier( 3, 4, 0 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment