-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgravity-forms-utm-tracking.php
153 lines (129 loc) · 5.44 KB
/
gravity-forms-utm-tracking.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Plugin Name: Gravity Forms UTM Tracking
* Plugin URI: https://tomjacobs.co.uk
* Description: Automatically captures and stores UTM parameters in Gravity Forms submissions.
* Version: 1.0.1
* Author: TomJacobsUK
* Author URI: https://github.com/TomJacobsUK
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
if (!defined('ABSPATH')) exit;
class GF_UTM_Tracking {
public function __construct() {
add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']);
add_action('gform_after_save_form', [$this, 'ensure_utm_fields']);
add_filter('gform_field_value', [$this, 'populate_utm_fields'], 10, 3);
add_action('wpmu_new_blog', [$this, 'activate_for_new_site']);
add_filter('pre_set_site_transient_update_plugins', [$this, 'check_for_update']);
add_filter('plugins_api', [$this, 'plugins_api_handler'], 10, 3);
}
public function enqueue_scripts() {
wp_enqueue_script('gf-utm-tracking', plugin_dir_url(__FILE__) . '/js/utm-tracking.js', [], '1.0.0', true);
}
public function ensure_utm_fields($form) {
$utm_fields = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'landing_page'];
$updated = false;
foreach ($utm_fields as $utm) {
$field_exists = false;
foreach ($form['fields'] as $field) {
if (isset($field->inputName) && $field->inputName === $utm) {
$field_exists = true;
break;
}
}
if (!$field_exists) {
$new_field_id = 0;
foreach ($form['fields'] as $field) {
if ($field->id > $new_field_id) {
$new_field_id = $field->id;
}
}
$new_field_id++;
$new_field = GF_Fields::create([
'id' => $new_field_id,
'type' => 'hidden',
'inputName' => $utm,
'label' => ucfirst(str_replace('_', ' ', $utm)),
'allowsPrepopulate' => true,
]);
$form['fields'][] = $new_field;
$updated = true;
}
}
if ($updated) {
GFAPI::update_form($form);
}
}
public function populate_utm_fields($value, $field, $name) {
if (in_array($name, ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'landing_page'])) {
return isset($_COOKIE[$name]) ? sanitize_text_field($_COOKIE[$name]) : '';
}
return $value;
}
public function activate_for_new_site($blog_id) {
if (is_plugin_active_for_network(plugin_basename(__FILE__))) {
switch_to_blog($blog_id);
$this->ensure_utm_fields();
restore_current_blog();
}
}
public function check_for_update($transient) {
if (empty($transient->checked)) {
return $transient;
}
$remote_version = $this->get_remote_version();
$plugin_data = get_plugin_data(__FILE__);
$plugin_version = $plugin_data['Version'];
if (version_compare($plugin_version, $remote_version, '<')) {
$plugin_slug = plugin_basename(__FILE__);
$transient->response[$plugin_slug] = (object) [
'slug' => $plugin_slug,
'new_version' => $remote_version,
'url' => 'https://github.com/TomJacobsUK/gravity-forms-utm-tracking',
'package' => 'https://github.com/TomJacobsUK/gravity-forms-utm-tracking/archive/refs/heads/main.zip',
];
}
return $transient;
}
public function plugins_api_handler($result, $action, $args) {
if ($action !== 'plugin_information') {
return $result;
}
if ($args->slug !== plugin_basename(__FILE__)) {
return $result;
}
$remote_info = $this->get_remote_info();
return (object) [
'name' => 'Gravity Forms UTM Tracking',
'slug' => plugin_basename(__FILE__),
'version' => $remote_info->version,
'author' => 'Tom Jacobs',
'author_profile' => 'https://tomjacobs.co.uk',
'homepage' => 'https://github.com/TomJacobsUK/gravity-forms-utm-tracking',
'short_description' => 'Automatically captures and stores UTM parameters in Gravity Forms submissions.',
'sections' => [
'description' => $remote_info->description,
'changelog' => $remote_info->changelog,
],
'download_link' => 'https://github.com/TomJacobsUK/gravity-forms-utm-tracking/archive/refs/heads/main.zip',
];
}
private function get_remote_version() {
$remote_info = $this->get_remote_info();
return $remote_info->version;
}
private function get_remote_info() {
$response = wp_remote_get('https://raw.githubusercontent.com/TomJacobsUK/gravity-forms-utm-tracking/main/info.json');
if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) {
return (object) [
'version' => '1.0.0',
'description' => '',
'changelog' => '',
];
}
return json_decode(wp_remote_retrieve_body($response));
}
}
new GF_UTM_Tracking();