forked from aickowicz/mixpanel-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixpanel.php
140 lines (112 loc) · 3.65 KB
/
mixpanel.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
<?php
/*
Plugin Name: MixPanel for WordPress
Plugin URI: http://zippykid.com/
Description: A relatively easy way to integrate MixPanel with your WordPress site
Author: Vid Luther <[email protected]>
Version: 0.3
Author URI: http://zippykid.com/
*/
// if( is_admin() ){
//require_once dirname( __FILE__ ) . '/meta-box.php';
// } else {
// require_once dirname( __FILE__ ) . '/page.php';
// }
namespace zippykid;
class mixPanel {
public function __construct(){
if(is_admin()){
add_action('admin_menu', array($this, 'add_settings_page'));
add_action('admin_init', array($this, 'mixpanel_init'));
require_once dirname( __FILE__ ) . '/meta-box.php';
} else {
require_once dirname( __FILE__ ) . '/page.php';
}
}
public function add_settings_page(){
// This page will be under "Settings"
add_options_page('Mixpanel Options', 'Mixpanel Options', 'manage_options', 'mixpanel-admin', array($this, 'create_settings_page'));
}
public function create_settings_page(){
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Mixpanel Settings</h2>
<?php settings_errors( ) ?>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields('mixpanel_settings_group');
do_settings_sections('mixpanel_options');
?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function print_section_info(){
print 'Enter your Mixpanel settings below:';
}
function my_text_input( $args ) {
$name = esc_attr( $args['name'] );
$value = esc_attr( $args['value'] );
if(strlen($value) > 0) {
$size = strlen($value) + 2;
} else {
$size = 10;
}
echo "<input type='text' name='$name' size='$size' value='$value' />";
}
public function mixpanel_init(){
register_setting('mixpanel_settings_group', 'mixpanel_settings'); # array($this, 'validate'));
$settings = (array) get_option( 'mixpanel_settings' );
add_settings_section(
'mixpanel_settings_section',
'Mixpanel ',
array($this, 'print_section_info'),
'mixpanel_options'
);
add_settings_field(
'token_id',
'Mixpanel Token ID', // human readable part
array($this, 'my_text_input'), // the function that renders the field
'mixpanel_options',
'mixpanel_settings_section', array(
'name' => 'mixpanel_settings[token_id]',
'value' => $settings['token_id'],
)
);
add_settings_field(
'debug_mode',
'Debug Mode? (true/false)', // human readable part
array($this, 'my_text_input'), // the function that renders the field
'mixpanel_options',
'mixpanel_settings_section', array(
'name' => 'mixpanel_settings[debug_mode]',
'value' => $settings['debug_mode'],
)
);
add_settings_field(
'subdomain_cookies',
'Use Subdomain Cookie? (true/false)', // human readable part
array($this, 'my_text_input'), // the function that renders the field
'mixpanel_options',
'mixpanel_settings_section', array(
'name' => 'mixpanel_settings[subdomain_cookie]',
'value' => $settings['subdomain_cookie'],
)
);
}
public function validate( $input ) {
$output = get_option( 'mixpanel_settings' );
if ( ctype_alnum( $input['token_id'] ) ) {
$output['token_id'] = $input['token_id'];
} else {
echo "Adding Error \n"; #die;
add_settings_error( 'mixpanel_options', 'token_id', 'The Mixpanel Token looks invalid (should be alpha numeric)' );
}
return $output;
}
}
$mixPanel = new \zippykid\mixPanel();
?>