Skip to content

Instantly share code, notes, and snippets.

@codiller
Last active August 3, 2022 12:18
Show Gist options
  • Save codiller/7996d6366ea58d676e156a1cf7ae5493 to your computer and use it in GitHub Desktop.
Save codiller/7996d6366ea58d676e156a1cf7ae5493 to your computer and use it in GitHub Desktop.
<?php
/*
*
* Creates a Gravity Forms custom field type for UTM parameters and URL info
*
*/
if ( ! class_exists( 'GFForms' ) ) {
return;
}
class UTM_Parameters_Field extends GF_Field_Hidden {
public $type = 'utm_parameters';
// # FORM EDITOR & FIELD MARKUP -------------------------------------------------------------------------------------
/**
* Return the field title, for use in the form editor.
*
* @return string
*/
public function get_form_editor_field_title() {
return esc_attr__( 'UTM Parameters', 'utmparameters' );
}
/**
* Assign the UTM Parameters button to the Advanced Fields group.
*
* @return array
*/
public function get_form_editor_button() {
return array(
'group' => 'advanced_fields',
'text' => $this->get_form_editor_field_title()
);
}
/**
* Return the settings which should be available on the field in the form editor.
*
* @return array
*/
function get_form_editor_field_settings() {
return array(
'prepopulate_field_setting',
'label_setting',
'default_input_values_setting',
);
}
/**
* Returns the scripts to be included for this field type in the form editor.
*
* @return string
*/
public function get_form_editor_inline_script_on_page_render() {
// set the default field label
$script = 'function SetDefaultValues_utm_parameters(field) {' .
"field.label = '{$this->get_form_editor_field_title()}';" .
"field.inputs = [new Input(field.id + 0.1, 'initial_landing_page'),new Input(field.id + 0.2, 'referrer'),new Input(field.id + 0.3, 'last_referrer'),new Input(field.id + 0.4, 'utm_campaign'),new Input(field.id + 0.5, 'utm_content'),new Input(field.id + 0.6, 'utm_term'),new Input(field.id + 0.7, 'utm_source'),new Input(field.id + 0.8, 'utm_medium'),new Input(field.id + 0.9, 'visits')];" .
'}';
return $script;
}
/**
* Returns the field inner markup.
*
* @param array $form The Form Object currently being processed.
* @param string|array $value The field value. From default/dynamic population, $_POST, or a resumed incomplete submission.
* @param null|array $entry Null or the Entry Object currently being edited.
*
* @return string
*/
public function get_field_input( $form, $value = '', $entry = null ) {
$is_entry_detail = $this->is_entry_detail();
$is_form_editor = $this->is_form_editor();
$is_admin = $is_entry_detail || $is_form_editor;
$form_id = absint( $form['id'] );
$id = $this->id;
$field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id";
$class_suffix = $is_entry_detail ? '_admin' : '';
$disabled_text = $is_form_editor ? 'disabled="disabled"' : '';
$field_type = $is_admin ? 'text' : 'hidden';
$i = 1;
$markup = '';
foreach ( $this->inputs as $input ) {
$input_markup = sprintf( '<input type="%1$s" name="input_%2$s.%3$s" class="%4$s gform_hidden" id="%5$s_%3$s" value="%6$s" %7$s/>',
$field_type,
$id,
$i,
esc_attr( $input['label'] ),
$field_id,
esc_attr( rgar( $value, strval( $input['id'] ) ) ),
$disabled_text
);
$i ++;
$markup .= $is_admin ? "<span class='ginput_full{$class_suffix}'>{$input_markup}</span>" : $input_markup;
}
return sprintf( "<div class='ginput_complex ginput_container'>%s</div>", $markup );
}
/**
* Format the entry value for display on the entry detail page and for the {all_fields} merge tag.
*
* @param string|array $value The field value.
* @param string $currency The entry currency code.
* @param bool|false $use_text When processing choice based fields should the choice text be returned instead of the value.
* @param string $format The format requested for the location the merge is being used. Possible values: html, text or url.
* @param string $media The location where the value will be displayed. Possible values: screen or email.
*
* @return string
*/
public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
$markup = '<ul>';
foreach ( $this->inputs as $input ) {
$markup .= sprintf( '<li>%s: %s</li>', $input['label'], rgar( $value, $input['id'] ) );
}
$markup .= '</ul>';
return $markup;
}
}
GF_Fields::register( new UTM_Parameters_Field() );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment