This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnanigans-for-wp.php
217 lines (170 loc) · 7.4 KB
/
nanigans-for-wp.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/*
Plugin Name: Nanigans Tracker for WP
Plugin URI: http://acumenholdings.com/
Description: Ads Nanigans event tracking to a WordPress site
Version: 0.0b
Author: Brian Sage
Author URI: http://twitter.com/briansage
*/
if (!class_exists("NTWP")) {
class NTWP {
function NTWP() { // constructor
}
function plugin_loaded_action() {
}
function wp_head_action(){
$output = <<<HTML
HTML;
echo $output;
}
function wp_footer_action(){
$output = "";
$nanigans_app_id = get_option('NTWP_nanigans_app_id');
$output .= <<<HTML
<script type="text/javascript">
// Makes handling cookie values a little easier
if(typeof Cookiemonger == 'undefined') {
Cookiemonger = {
// Reads the whole document.cookie, and returns result as type Array of keyed Arrays
load : function(){
var Cookie = {};
var cookie_arr = document.cookie.split('; ');
if (cookie_arr.length){
for(var i=0, len = cookie_arr.length; i<len; i++){
var pair = Cookie[i].split('=');
Cookie[unescape(pair[0])] = unescape(pair[1]);
}
}
return Cookie;
},
// Write a cookie value.
// expires may be set to null for expiration with session
save : function(cookie_obj,expires,path) {
var params = {
exp: (typeof expires != 'undefined')? expires : new Date(Date.now() + 94608000000).toUTCString(), // About 3 years worth of msecs
p: (typeof path != 'undefined')? path : '/'
}
var cookie_key = '';
var cookie_val = '';
for(var key in cookie_obj) {
cookie_key = escape(key.toString());
cookie_val = escape(cookie_obj[key].toString());
}
var cookie_str = cookie_key +'='+ cookie_val + ';path=' + params.p + ((params.exp == null)? '' : ';expires=' + params.exp);
document.cookie = cookie_str;
},
// Finds the cookie key given, and returns the result as type String
find : function(key){
var Cookie = document.cookie.split('; ');
if (Cookie.length){
for(var i=0, len = Cookie.length; i<len; i++){
var pair = Cookie[i].split('=');
if(unescape(pair[0]) == key) {
return unescape(pair[1]);
}
}
}
return undefined;
},
remove : function(key) {
if (key){
var cookiemonger_delete = {};
cookiemonger_delete[key] = '';
var exp_date = new Date();
exp_date.setDate(exp_date.getDate() - 1);
this.save(cookiemonger_delete,exp_date);
}
}
}
}
// Nanigans Trackers
if (!Cookiemonger.find('nanigans_session')) {
// Find/Assign a user id (persists 3 years)
if (Cookiemonger.find('nanigans_user_id')){
window.nanigans_user_id = Cookiemonger.find('nanigans_user_id');
// If nanigans_user_id could not be found, reuse UTMA id
} else if(typeof Cookiemonger.find('__utma') != 'undefined' && Cookiemonger.find('__utma').split('.').length){
window.nanigans_user_id = Cookiemonger.find('__utma').split('.')[1];
// If all else fails, it's a big, long random for you, Mr. User.
} else {
window.nanigans_user_id = Math.ceil(Math.random() * 999999999999999);
}
Cookiemonger.save({'nanigans_session':'true'}, null ); // Expires with session.
Cookiemonger.save({'nanigans_user_id':window.nanigans_user_id});
window.nanigans_tracker = new Image();
nanigans_tracker.src = "//api.nanigans.com/event.php?app_id={$nanigans_app_id}&type=visit&name=landing&user_id=" + window.nanigans_user_id;
}
// Bind to any email submit forms
$(function(){
$('form').has('input[type="email"],input[name~="email"]').submit('submit',function(){
var nanigans_user_id = Cookiemonger.find('nanigans_user_id');
window.nanigans_tracker = new Image();
nanigans_tracker.src = "//api.nanigans.com/event.php?app_id={$nanigans_app_id}&type=visit&name=email&user_id=" + nanigans_user_id;
});
});
</script>
HTML;
echo $output;
}
function NTWP_admin_menu () {
global $NTWP;
if ( count($_POST) > 0 && isset($_POST['NTWP_settings'])):
// Setup Nanigans Settings Form
$options = array(
'nanigans_app_id'
);
foreach ( $options as $opt ){
delete_option ( 'NTWP_'.$opt, $_POST[$opt] );
add_option ( 'NTWP_'.$opt, $_POST[$opt] );
}
endif;
add_menu_page('Nanigans Settings', 'Nanigans', 'manage_options', 'ntwp_settings', null, plugins_url('nanigans-for-wp-icon.png', __FILE__));
add_submenu_page('ntwp_settings', 'Nanigans Settings', 'Nanigans Settings', 'manage_options', 'ntwp_settings', array(&$NTWP,'NTWP_admin_settings'));
}
function NTWP_admin_settings() {
?>
<div class="wrap">
<h2>Nanigans Settings</h2>
<form method="post" action="">
<div id="col-container">
<div id="col-left">
<div class="col-wrap">
<div class="form-wrap">
<h3>Nanigans <i>Signup</i> Campaign</h3>
<p>These are the list IDs needed for the <strong>primary squeeze page signup action</strong>.</p>
<div class="form-field">
<label for="nanigans_app_id">App ID</label>
<input name="nanigans_app_id" type="text" id="nanigans_app_id" value="<?php echo get_option('NTWP_nanigans_app_id'); ?>" />
<p>This site's Nanigans <b>App ID</b>.</p>
</div>
<p class="submit">
<input type="submit" name="Submit" class="button-primary" value="Save Changes" />
<input type="hidden" name="NTWP_settings" value="save" style="display:none;" />
</p>
</div>
</div>
</div>
</div>
</form>
</div>
<?php
}
function NTWP_warning() {
echo "<div id='NTWP-warning' class='updated fade'><p><strong>Nanigans Settings are almost ready.</strong> ".sprintf('You must <a href="%1$s">enter an Nanigans App ID</a> for it to work.', "admin.php?page=ntwp_settings")."</p></div>";
}
}
}
if (class_exists("NTWP")) {
$NTWP = new NTWP();
}
if (isset($NTWP)) :
add_action( 'plugins_loaded', array(&$NTWP,'plugin_loaded_action') );
add_action( 'wp_head', array(&$NTWP,'wp_head_action') );
add_action( 'wp_footer', array(&$NTWP,'wp_footer_action') );
add_action( 'admin_menu', array(&$NTWP,'NTWP_admin_menu') );
// Warnings
if (!get_option('NTWP_nanigans_app_id') && !get_option('NTWP_nanigans_lid') && !isset($_POST['submit'])):
add_action('admin_notices', array(&$NTWP,'NTWP_warning') );
endif;
endif;