This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom.php
159 lines (135 loc) · 4.18 KB
/
custom.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
154
155
156
157
158
159
<?php
/**
* Custom changes like tracking referrer.
*/
class Grow_Custom
{
/**
* Current version of Grow Helper.
*/
public $version = '0.1.0';
/**
* Possible fields.
*/
public $fields = array(
// main
'type',
'url',
'mtke',
// utm
'utm_campaign',
'utm_source',
'utm_medium',
'utm_content',
'utm_id',
'utm_term',
// additional
'session_entry',
'session_start_time',
'session_pages',
'session_count',
);
/**
* Field prefix (for the input field names).
*/
public $fieldPrefix = 'grow_source_';
public function __construct()
{
add_action('wp_enqueue_scripts', array($this, 'scripts_styles'));
// fields
add_action('woocommerce_after_order_notes', array($this, 'source_form_fields'));
add_action('woocommerce_register_form', array($this, 'source_form_fields'));
// update
add_action('woocommerce_checkout_update_order_meta', array($this, 'set_order_source'));
add_action('user_register', array($this, 'set_customer_source'));
}
/**
* Scripts & styles for custom source tracking and cart tracking.
*/
public function scripts_styles()
{
/*
* Enqueue scripts.
*/
wp_enqueue_script('sourcebuster-js', plugins_url('assets/js/sourcebuster.min.js', dirname(__FILE__)), array('jquery'), $this->version, true);
wp_enqueue_script('grow-js', plugins_url('assets/js/grow.js', dirname(__FILE__)), array('jquery'), $this->version, true);
/**
* Pass parameters to Grow JS.
*/
$params = array(
'lifetime' => (int) apply_filters('grow_cookie_lifetime', 6), // 6 months
'session' => (int) apply_filters('grow_session_length', 30), // 30 minutes
'ajaxurl' => admin_url('admin-ajax.php'),
);
wp_localize_script('grow-js', 'grow_params', $params);
}
/**
* Add grow hidden input fields for checkout & customer register froms.
*/
public function source_form_fields()
{
/*
* Hidden field for each possible field.
*/
foreach ($this->fields as $field) {
echo '<input type="hidden" name="'.$this->fieldPrefix.$field.'" value="" />';
}
}
/**
* Set the source data in the order post meta.
*/
public function set_order_source($order_id)
{
$this->set_source_data($order_id, 'order');
}
/**
* Set the source data in the customer user meta.
*/
public function set_customer_source($customer_id)
{
$this->set_source_data($customer_id, 'customer');
}
/**
* Set source data.
*/
public function set_source_data($id, $resource)
{
/**
* Values.
*/
$values = array();
/*
* Get each field if POSTed.
*/
foreach ($this->fields as $field) {
// default empty
$values[$field] = '';
// set if have
if (isset($_POST[$this->fieldPrefix.$field]) && $_POST[$this->fieldPrefix.$field]) {
$values[$field] = sanitize_text_field($_POST[$this->fieldPrefix.$field]);
}
}
/**
* Now parse values to set in meta.
*/
// update function based on order or customer
$update_function = $resource == 'order' ? 'update_post_meta' : 'update_user_meta';
// type
if ($values['type'] && $values['type'] !== '(none)') {
$update_function($id, '_grow_source_type', $values['type']);
}
unset($values['type']);
// referer url
if ($values['url'] && $values['url'] !== '(none)') {
$update_function($id, '_grow_referer', $values['url']);
}
unset($values['url']);
// rest of fields - UTMs & sessions (if not '(none)')
foreach ($values as $key => $value) {
if ($value && $value !== '(none)') {
$update_function($id, '_grow_'.$key, $value);
}
}
}
}
new Grow_Custom();