-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathplausible-proxy-speed-module.php
More file actions
117 lines (100 loc) · 2.7 KB
/
plausible-proxy-speed-module.php
File metadata and controls
117 lines (100 loc) · 2.7 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
<?php
/**
* Plugin Name: Plausible Analytics - Proxy Speed Module
* Description: Speeds up Plausible Analytics' proxy for avoiding ad blockers.
* Plugin URI: https://plausible.io
* Author: Plausible HQ
* Version: 1.0.1
* Author URI: https://plausible.io
*
* Text Domain: plausible-analytics
*/
class PlausibleProxySpeed {
/**
* Is the current request a request to our proxy?
*
* @var bool
*/
private $is_proxy_request;
/**
* Current request URI.
*
* @var string
*/
private $request_uri;
/**
* Build properties.
*
* @return void
*/
public function __construct() {
$this->request_uri = $this->get_request_uri();
$this->is_proxy_request = $this->is_proxy_request();
$this->init();
}
/**
* Helper method to retrieve Request URI.
*
* @return string
*/
private function get_request_uri() {
return $_SERVER['REQUEST_URI'] ?? '';
}
/**
* Check if the current request is a proxy request.
*
* The namespace must appear as a path segment under the REST prefix
* (e.g. /wp-json/<namespace>[/...]). Substring matches in query
* strings, fragments, or unrelated path segments are rejected.
*
* @return bool
*/
private function is_proxy_request() {
$namespace = get_option( 'plausible_analytics_proxy_resources' )['namespace'] ?? '';
if ( ! $namespace ) {
return false;
}
$path = parse_url( $this->request_uri, PHP_URL_PATH );
if ( ! is_string( $path ) || $path === '' ) {
return false;
}
$expected = function_exists( 'rest_url' )
? untrailingslashit( (string) wp_parse_url( rest_url( trim( $namespace, '/' ) ), PHP_URL_PATH ) )
: '/wp-json/' . trim( $namespace, '/' );
return $path === $expected
|| str_starts_with( $path, $expected . '/' );
}
/**
* Add filters and actions.
*
* @return void
*/
private function init() {
add_filter( 'option_active_plugins', [ $this, 'filter_active_plugins' ] );
}
/**
* Filter the list of active plugins for custom endpoint requests.
*
* Uses basename() exact-match comparison instead of strpos(), so a
* plugin file path can only match if its filename is exactly in the
* allowlist.
*
* @param array $active_plugins The list of active plugins.
*
* @return array The filtered list of active plugins.
*/
public function filter_active_plugins( $active_plugins ) {
if ( ! $this->is_proxy_request || ! is_array( $active_plugins ) ) {
return $active_plugins;
}
$allowed_plugin_files = [ 'plausible-analytics.php' ];
$filtered_plugins = [];
foreach ( $active_plugins as $plugin ) {
if ( in_array( basename( $plugin ), $allowed_plugin_files, true ) ) {
$filtered_plugins[] = $plugin;
}
}
return $filtered_plugins;
}
}
new PlausibleProxySpeed();