Last active
March 22, 2022 20:57
-
-
Save w-jerome/cbaeb172f652dfb70de1c974e3db70b4 to your computer and use it in GitHub Desktop.
Gravity Forms - UTM fields
This file contains hidden or 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
<?php | |
// Doc : https://stackoverflow.com/questions/43009398/custom-gravity-forms-field-with-multiple-inputs | |
class GF_Field_UTM extends GF_Field { | |
public $type = 'utm'; | |
private $fields_name = array( | |
'referrer', | |
'last_referrer', | |
'utm_campaign', | |
'utm_content', | |
'utm_term', | |
'utm_source', | |
'utm_medium', | |
); | |
public function get_form_editor_field_title() { | |
return esc_attr__('UTM', 'gravityforms'); | |
} | |
public function get_form_editor_button() { | |
return array( | |
'group' => 'advanced_fields', | |
'text' => $this->get_form_editor_field_title(), | |
'onclick' => "StartAddField('".$this->type."');", | |
); | |
} | |
public function get_form_editor_field_settings() { | |
return array( | |
'conditional_logic_field_setting', | |
'prepopulate_field_setting', | |
'error_message_setting', | |
'label_setting', | |
'admin_label_setting', | |
'rules_setting', | |
'visibility_setting', | |
'duplicate_setting', | |
'description_setting', | |
'css_class_setting', | |
); | |
} | |
public function is_conditional_logic_supported() { | |
return true; | |
} | |
public function get_field_input($form, $value = '', $entry = null) { | |
$is_entry_detail = $this->is_entry_detail(); | |
$is_form_editor = $this->is_form_editor(); | |
$form_id = $form['id']; | |
$field_id = intval($this->id); | |
$disabled_text = $is_form_editor ? "disabled='disabled'" : ''; | |
$class_suffix = $is_entry_detail ? '_admin' : ''; | |
$required_attribute = $this->isRequired ? 'aria-required="true"' : ''; | |
$invalid_attribute = $this->failed_validation ? 'aria-invalid="true"' : 'aria-invalid="false"'; | |
$css_class = $this->get_css_class(); | |
// Init input variable | |
$values = array(); | |
foreach ($this->fields_name as $key => $custom_field) { | |
$values[] = array( | |
'name' => $custom_field, | |
'value' => (!empty($_GET[$custom_field])) ? filter_var($_GET[$custom_field], FILTER_SANITIZE_STRING) : '', | |
'tabindex' => GFCommon::get_tabindex(), | |
); | |
} | |
foreach ($this->fields_name as $key => $custom_field) { | |
$custom_field_value = $values[$key]['value']; | |
$custom_field_tabindex = $values[$key]['tabindex']; | |
$values[$key]['markup'] = '<input type="hidden" name="input_'.$field_id.'.'.(($key) + 1).'" id="input_'.$field_id.'_'.$form_id.'_'.(($key) + 1).'" value="'.$custom_field_value.'" aria-label="'.$custom_field.'" '.$custom_field_tabindex.' '.$disabled_text.' '.$required_attribute.' '.$invalid_attribute.'>'; | |
} | |
$render_markup = '<div class="ginput_complex'.$class_suffix.' ginput_container '.$css_class.' gfield_trigger_change" id="'.$field_id.'">'; | |
foreach ($this->fields_name as $key => $custom_field) { | |
$render_markup .= $values[$key]['markup']; | |
} | |
$render_markup .= '<div class="gf_clear gf_clear_complex"></div></div>'; | |
return $render_markup; | |
} | |
public function get_css_class() { | |
$css_class = ''; | |
$visible_input_count = 0; | |
foreach ($this->fields_name as $key => $custom_field) { | |
$get_input_by_key = GFFormsModel::get_input($this, $this->id . '.'.(($key) + 1)); | |
if ($get_input_by_key && ! rgar($get_input_by_key, 'isHidden')) { | |
$visible_input_count++; | |
$css_class .= 'has_'.$custom_field.' '; | |
} else { | |
$css_class .= 'no_'.$custom_field.' '; | |
} | |
} | |
$css_class .= 'gf_utm_has_'.$visible_input_count.' ginput_container_utm'; | |
return trim($css_class); | |
} | |
public function get_form_editor_inline_script_on_page_render() { | |
$render_markup = 'function SetDefaultValues_'.$this->type.'(field) {'; | |
$render_markup .= 'field.label = \''.$this->get_form_editor_field_title().'\';'; | |
$render_markup .= 'field.inputs = ['; | |
foreach ($this->fields_name as $key => $custom_field) { | |
$render_markup .= 'new Input(field.id + \'.'.(($key) + 1).'\', \''.$custom_field.'\')'; | |
if (($key + 1) < count($this->fields_name)) { | |
$render_markup .= ','; | |
} | |
} | |
$render_markup .= '];'; | |
$render_markup .= '}'; | |
return $render_markup; | |
} | |
public function get_value_entry_detail($value, $currency = '', $use_text = false, $format = 'html', $media = 'screen') { | |
$return = ''; | |
if (is_array($value)) { | |
$values = array(); | |
foreach ($this->fields_name as $key => $custom_field) { | |
$input_value = trim(rgget($this->id.'.'.(($key) + 1), $value)); | |
$values[$custom_field] = (!empty($input_value)) ? $input_value : ''; | |
} | |
$return = json_encode($values, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT, 4); | |
$return = nl2br($return); | |
} | |
return $return; | |
} | |
} | |
GF_Fields::register(new GF_Field_UTM()); | |
?> |
This file contains hidden or 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
<?php | |
/* | |
* Les prochaines fonctions ajoute dynamiquement les UTM dans les champs | |
*/ | |
// UTM Source | |
function gform_field_value_utm_source_function($value) { | |
return (!empty($_GET['utm_source'])) ? filter_var($_GET['utm_source'], FILTER_SANITIZE_STRING) : ''; | |
} | |
add_filter('gform_field_value_utm_source', 'gform_field_value_utm_source_function'); | |
// UTM Medium | |
function gform_field_value_utm_medium_function($value) { | |
return (!empty($_GET['utm_medium'])) ? filter_var($_GET['utm_medium'], FILTER_SANITIZE_STRING) : ''; | |
} | |
add_filter('gform_field_value_utm_medium', 'gform_field_value_utm_medium_function'); | |
// UTM Campaign | |
function gform_field_value_utm_campaign_function($value) { | |
return (!empty($_GET['utm_campaign'])) ? filter_var($_GET['utm_campaign'], FILTER_SANITIZE_STRING) : ''; | |
} | |
add_filter('gform_field_value_utm_campaign', 'gform_field_value_utm_campaign_function'); | |
// UTM Term | |
function gform_field_value_utm_term_function($value) { | |
return (!empty($_GET['utm_term'])) ? filter_var($_GET['utm_term'], FILTER_SANITIZE_STRING) : ''; | |
} | |
add_filter('gform_field_value_utm_term', 'gform_field_value_utm_term_function'); | |
// UTM Content | |
function gform_field_value_utm_content_function($value) { | |
return (!empty($_GET['utm_content'])) ? filter_var($_GET['utm_content'], FILTER_SANITIZE_STRING) : ''; | |
} | |
add_filter('gform_field_value_utm_content', 'gform_field_value_utm_content_function'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment