-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathHelpers.php
More file actions
299 lines (254 loc) · 7.33 KB
/
Helpers.php
File metadata and controls
299 lines (254 loc) · 7.33 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* Plausible Analytics | Helpers
*
* @since 1.0.0
* @package WordPress
* @subpackage Plausible Analytics
*/
namespace Plausible\Analytics\WP;
use Exception;
/**
* We use 'static' (late static binding) instead of 'self' (early binding), so we can mock (where needed) in our tests.
*/
class Helpers {
/**
* Get Analytics URL.
*
* @return string
* @throws Exception
* @since 1.0.0
*
*/
public static function get_js_url( $local = false ) {
$file_name = static::get_filename();
/**
* If the Avoid Ad Blockers option is enabled, return URL pointing to the local file.
*/
if ( $local && static::proxy_enabled() ) {
return esc_url( static::get_proxy_resource( 'cache_url' ) . $file_name . '.js' );
}
return esc_url( static::get_hosted_domain_url() . "/js/$file_name.js" );
}
/**
* Get filename (without file extension)
*
* @return string
* @throws Exception
*
* @codeCoverageIgnore
* @since 1.3.0
*/
public static function get_filename() {
$client = static::get_client();
if ( $client instanceof Client ) {
return $client->get_tracker_id();
}
return '';
}
/**
* Build the API client.
*
* @return false|Client
*
* @codeCoverageIgnore This seam's only function is to keep our code testable.
*/
protected static function get_client() {
$client = new ClientFactory();
return $client->build();
}
/**
* Is the proxy enabled?
*
* @param array $settings Allows passing a current settings object.
*
* @return bool
*/
public static function proxy_enabled( $settings = [] ) {
if ( empty( $settings ) ) {
$settings = static::get_settings();
}
return ! empty( $settings['proxy_enabled'] ) || isset( $_GET['plausible_proxy'] );
}
/**
* Get Settings.
*
* @return array
* @since 1.0.0
* @access public
*/
public static function get_settings() {
$defaults = [
'domain_name' => '',
'api_token' => '',
'enhanced_measurements' => [
EnhancedMeasurements::FOUR_O_FOUR,
EnhancedMeasurements::FILE_DOWNLOADS,
EnhancedMeasurements::OUTBOUND_LINKS,
EnhancedMeasurements::FORM_COMPLETIONS,
EnhancedMeasurements::SEARCH_QUERIES,
],
'affiliate_links' => [],
'query_params' => [],
'proxy_enabled' => '',
'enable_analytics_dashboard' => '',
'shared_link' => '',
'excluded_pages' => '',
'tracked_user_roles' => [],
'expand_dashboard_access' => [],
'disable_toolbar_menu' => '',
'self_hosted_domain' => '',
'self_hosted_shared_link' => '',
];
$settings = get_option( 'plausible_analytics_settings', [] );
return apply_filters( 'plausible_analytics_settings', wp_parse_args( $settings, $defaults ) );
}
/**
* Get a proxy resource by name.
*
* @param string $resource_name
*
* @return string Value of resource from DB or empty string if Bypass ad blockers option is disabled.
* @throws Exception
*/
public static function get_proxy_resource( $resource_name = '' ) {
$resources = static::get_proxy_resources();
/**
* Create the cache directory if it doesn't exist.
*/
if ( ( $resource_name === 'cache_dir' || $resource_name === 'cache_url' ) && ! is_dir( $resources['cache_dir'] ) ) {
wp_mkdir_p( $resources[ $resource_name ] );
}
return $resources[ $resource_name ] ?? '';
}
/**
* Get (and generate/store if non-existent) proxy resources.
*
* @return array
* @throws Exception
*
* @codeCoverageIgnore
*/
public static function get_proxy_resources() {
static $resources;
if ( $resources === null ) {
$resources = get_option( 'plausible_analytics_proxy_resources', [] );
}
/**
* Force a refresh of our resources if the user recently switched to SSL and we still have non-SSL resources stored.
*/
if ( ! empty( $resources ) && is_ssl() && isset( $resources['cache_url'] ) && ( strpos( $resources['cache_url'], 'http:' ) !== false ) ) {
$resources = [];
}
if ( empty( $resources ) ) {
$cache_dir = bin2hex( random_bytes( 5 ) );
$upload_dir = wp_get_upload_dir();
$resources = [
'namespace' => bin2hex( random_bytes( 3 ) ),
'base' => bin2hex( random_bytes( 2 ) ),
'endpoint' => bin2hex( random_bytes( 4 ) ),
'cache_dir' => trailingslashit( $upload_dir['basedir'] ) . trailingslashit( $cache_dir ),
'cache_url' => trailingslashit( $upload_dir['baseurl'] ) . trailingslashit( $cache_dir ),
];
update_option( 'plausible_analytics_proxy_resources', $resources );
}
return $resources;
}
/**
* Returns the URL of the domain where Plausible Analytics is hosted: self-hosted or cloud.
*
* @return string
*/
public static function get_hosted_domain_url() {
$settings = static::get_settings();
if ( defined( 'PLAUSIBLE_SELF_HOSTED_DOMAIN' ) ) {
return esc_url( 'https://' . PLAUSIBLE_SELF_HOSTED_DOMAIN ); // @codeCoverageIgnore
}
if ( ! empty( $settings['self_hosted_domain'] ) ) {
/**
* Until proven otherwise, let's just assume people are all on SSL.
*/
return esc_url( 'https://' . $settings['self_hosted_domain'] );
}
return esc_url( 'https://plausible.io' );
}
/**
* @param $option_name
* @param $option_value
*
* @return void
*/
public static function update_setting( $option_name, $option_value ) {
$settings = static::get_settings();
$settings[ $option_name ] = $option_value;
update_option( 'plausible_analytics_settings', $settings );
}
/**
* A convenient way to retrieve the absolute path to the local JS file. Proxy should be enabled when this method is called!
*
* @return string
* @throws Exception
*/
public static function get_js_path() {
return static::get_proxy_resource( 'cache_dir' ) . static::get_filename() . '.js';
}
/**
* Get entered Domain Name or provide alternative if not entered.
*
* @return string
* @since 1.0.0
* @access public
*/
public static function get_domain() {
$settings = static::get_settings();
if ( ! empty( $settings['domain_name'] ) ) {
return $settings['domain_name'];
}
$url = home_url();
return preg_replace( '/^http(s?):\/\/(www\.)?/i', '', $url );
}
/**
* Get Data API URL.
*
* @return string
* @throws Exception
* @since 1.2.2
* @access public
*/
public static function get_endpoint_url() {
if ( static::proxy_enabled() ) {
// This will make sure the API endpoint is properly registered when we're testing.
$append = isset( $_GET['plausible_proxy'] ) ? '?plausible_proxy=1' : '';
return static::get_rest_endpoint() . $append;
}
return esc_url( static::get_hosted_domain_url() . '/api/event' );
}
/**
* Returns the Proxy's REST endpoint.
*
* @return string
* @throws Exception
*/
public static function get_rest_endpoint( $abs_url = true ) {
$namespace = static::get_proxy_resource( 'namespace' );
$base = static::get_proxy_resource( 'base' );
$endpoint = static::get_proxy_resource( 'endpoint' );
$uri = "$namespace/v1/$base/$endpoint";
if ( $abs_url ) {
return get_rest_url( null, $uri );
}
return '/' . rest_get_url_prefix() . '/' . $uri;
}
/**
* Get user role for the logged-in user.
*
* @return string
* @since 1.3.0
* @access public
*/
public static function get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
return array_shift( $user_roles );
}
}