-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpirsch-wordpress.php
232 lines (199 loc) · 7.29 KB
/
pirsch-wordpress.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* Plugin Name: Pirsch Analytics
* Plugin URI: https://pirsch.io/
* Description: Connect your Wordpress website to Pirsch Analytics.
* Version: 1.7.0
* Requires at least: 5.2
* Requires PHP: 8.1
* Author: Emvi Software GmbH
* License: MIT
* License URI: https://github.com/pirsch-analytics/pirsch-wordpress/blob/master/LICENSE
*/
if(!defined('WPINC')) {
die;
}
require __DIR__.'/vendor/autoload.php';
function pirsch_analytics_activate() {}
function pirsch_analytics_deactivate() {
delete_option('pirsch_analytics_client_id');
delete_option('pirsch_analytics_client_secret');
delete_option('pirsch_analytics_header');
delete_option('pirsch_analytics_path_filter');
}
function pirsch_analytics_settings_page_init() {
register_setting('pirsch_analytics_page', 'pirsch_analytics_client_id');
register_setting('pirsch_analytics_page', 'pirsch_analytics_client_secret');
register_setting('pirsch_analytics_page', 'pirsch_analytics_header');
register_setting('pirsch_analytics_page', 'pirsch_analytics_path_filter');
add_settings_section(
'pirsch_analytics_page',
__('Settings', 'pirsch_analytics'),
'pirsch_analytics_settings_callback',
'pirsch_analytics_page'
);
add_settings_field(
'pirsch_analytics_client_id',
__('Client ID', 'pirsch_analytics'),
'pirsch_analytics_client_id_callback',
'pirsch_analytics_page',
'pirsch_analytics_page'
);
add_settings_field(
'pirsch_analytics_client_secret',
__('Client Secret', 'pirsch_analytics'),
'pirsch_analytics_client_secret_callback',
'pirsch_analytics_page',
'pirsch_analytics_page'
);
add_settings_field(
'pirsch_analytics_header',
__('Header', 'pirsch_analytics'),
'pirsch_analytics_header_callback',
'pirsch_analytics_page',
'pirsch_analytics_page'
);
add_settings_field(
'pirsch_analytics_path_filter',
__('Path Filter', 'pirsch_analytics'),
'pirsch_analytics_path_filter_callback',
'pirsch_analytics_page',
'pirsch_analytics_page'
);
}
function pirsch_analytics_settings_callback() {
echo '<p>To gain a client ID and secret, navigate to the Pirsch dashboard, click on settings, and create a new client.
<strong>You can also use a single access token and skip the client ID.</strong> Read our <a href="https://docs.pirsch.io/get-started/backend-integration/" target="_blank">backend integration</a> for details.</p>';
echo '<p>The header is optional and should only be set when WordPress is running behind a proxy or load balancer.
Pirsch requires the real visitor IP address, so you must provide the right header.<br />
Options are: CF-Connecting-IP, True-Client-IP, X-Forwarded-For, Forwarded, X-Real-IP.</p>';
echo '<p>The path filter can be used to exclude pages and files. Enter a comma separated list of <a href="https://www.php.net/manual/en/reference.pcre.pattern.syntax.php" target="_blank">regular expressions</a>, to filter unwanted page views.</p>';
echo '<p><a href="https://dashboard.pirsch.io/settings" target="_blank">Go to the Pirsch settings page</a></p>';
}
function pirsch_analytics_client_id_callback() {
$value = get_option('pirsch_analytics_client_id', '');
echo '<input type="text" name="pirsch_analytics_client_id" value="'.esc_attr($value).'" />';
}
function pirsch_analytics_header_callback() {
$value = get_option('pirsch_analytics_header', '');
echo '<input type="text" name="pirsch_analytics_header" value="'.esc_attr($value).'" />';
}
function pirsch_analytics_client_secret_callback() {
$value = get_option('pirsch_analytics_client_secret', '');
echo '<input type="password" name="pirsch_analytics_client_secret" value="'.esc_attr($value).'" />';
}
function pirsch_analytics_path_filter_callback() {
$value = get_option('pirsch_analytics_path_filter', '');
echo '<input type="text" name="pirsch_analytics_path_filter" value="'.esc_attr($value).'" />';
}
function pirsch_analytics_settings_page_html() {
if(!current_user_can('manage_options')) {
return;
}
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form action="options.php" method="post">
<?php
settings_fields('pirsch_analytics_page');
do_settings_sections('pirsch_analytics_page');
submit_button(__( 'Save Settings', 'textdomain'));
?>
</form>
</div>
<?php
}
function pirsch_analytics_add_settings_page() {
add_submenu_page(
'tools.php',
'Pirsch Analytics',
'Pirsch Analytics',
'manage_options',
'pirsch_analytics_page',
'pirsch_analytics_settings_page_html'
);
}
function pirsch_analytics_remove_settings_page() {
remove_menu_page('pirsch_analytics_page');
}
function pirsch_analytics_middleware() {
try {
if (!is_admin() && !pirsch_analytics_is_wp_site() && !pirsch_analytics_is_excluded()) {
$clientID = get_option('pirsch_analytics_client_id');
$clientSecret = get_option('pirsch_analytics_client_secret');
$header = get_option('pirsch_analytics_header');
if (!empty($clientSecret)) {
$client = new Pirsch\Client($clientID, $clientSecret, Pirsch\Client::DEFAULT_TIMEOUT);
$options = new Pirsch\HitOptions();
if (!empty($header)) {
switch (strtolower($header)) {
case 'cf-connecting-ip':
$options->ip = pirsch_analytics_parse_x_forwarded_for($_SERVER['HTTP_CF_CONNECTING_IP']);
break;
case 'true-client-ip':
$options->ip = pirsch_analytics_parse_x_forwarded_for($_SERVER['HTTP_TRUE_CLIENT_IP']);
break;
case 'x-forwarded-for':
$options->ip = pirsch_analytics_parse_x_forwarded_for($_SERVER['HTTP_X_FORWARDED_FOR']);
break;
case 'forwarded':
$options->ip = pirsch_analytics_parse_forwarded_header($_SERVER['HTTP_FORWARDED']);
break;
case 'x-real-ip':
$options->ip = $_SERVER['HTTP_X_REAL_IP'];
break;
}
}
$client->pageview($options);
}
}
} catch(Exception $e) {
error_log($e->getMessage());
}
}
function pirsch_analytics_is_wp_site() {
$pattern = "/\/*wp-.*/i";
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
return preg_match($pattern, $path) === 1;
}
function pirsch_analytics_is_excluded() {
$filter = trim(get_option('pirsch_analytics_path_filter'));
if (!empty($filter)) {
$patterns = explode(',', $filter);
foreach ($patterns as $pattern) {
if (preg_match('/'.$pattern.'/U', $_SERVER['REQUEST_URI'])) {
return true;
}
}
}
return false;
}
function pirsch_analytics_parse_x_forwarded_for($header) {
$parts = explode(',', $header);
$n = count($parts);
if ($n > 0) {
return $parts[$n-1];
}
return '';
}
function pirsch_analytics_parse_forwarded_header($header) {
$parts = explode(',', $header);
$n = count($parts);
if ($n > 0) {
$parts = explode(';', $parts[$n-1]);
foreach ($parts as $part) {
$kv = explode('=', $part, 1);
if (count($kv) == 2 && $kv[0] == 'for') {
return $kv[1];
}
}
unset($part);
}
return '';
}
register_activation_hook(__FILE__, 'pirsch_analytics_activate');
register_deactivation_hook(__FILE__, 'pirsch_analytics_deactivate');
add_action('admin_init', 'pirsch_analytics_settings_page_init');
add_action('admin_menu', 'pirsch_analytics_add_settings_page');
add_action('admin_menu', 'pirsch_analytics_remove_settings_page', 99);
add_action('init', 'pirsch_analytics_middleware');