Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
wordpress/src/Proxy.php at develop · plausible/wordpress · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
plausible
wordpress
Repository navigation
Code
Issues
8
(8)
Pull requests
5
(5)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
develop
Breadcrumbs
wordpress
/
src
/
Proxy.php
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
289 lines (247 loc) · 7.06 KB
develop
Breadcrumbs
wordpress
/
src
/
Proxy.php
Copy path
Top
File metadata and controls
Code
Blame
289 lines (247 loc) · 7.06 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
/**
* Plausible Analytics | Proxy.
*
* @since 1.3.0
* @package WordPress
* @subpackage Plausible Analytics
* @copyright This code was copied from CAOS Pro, created by:
* @author Daan van den Bergh
* https://daan.dev/wordpress/caos-pro/
*/
namespace
Plausible
\
Analytics
\
WP
;
use
Exception
;
use
WP_Error
;
use
WP_HTTP_Response
;
use
WP_REST_Request
;
use
WP_REST_Server
;
class
Proxy {
/**
* Proxy IP Headers used to detect the visitors IP prior to sending the data to Plausible's Measurement Protocol.
*
* @see https://support.cloudflare.com/hc/en-us/articles/200170986-How-does-Cloudflare-handle-HTTP-Request-headers-
* @var array
* For CloudFlare compatibility HTTP_CF_CONNECTING_IP has been added.
*/
const
PROXY_IP_HEADERS
= [
'
HTTP_CF_CONNECTING_IP
'
,
'
HTTP_X_FORWARDED_FOR
'
,
'
REMOTE_ADDR
'
,
'
HTTP_CLIENT_IP
'
,
];
/**
* API namespace
*
* @var string
*/
private
$
namespace
=
''
;
/**
* API base
*
* @var string
*/
private
$
base
=
''
;
/**
* Endpoint
*
* @var string
*/
private
$
endpoint
=
''
;
/**
* Build properties.
*
* @return void
* @throws Exception
*/
public
function
__construct
(
$
init
=
true
) {
$
this
->
namespace
= Helpers::
get_proxy_resource
(
'
namespace
'
) .
'
/v1
'
;
$
this
->
base
= Helpers::
get_proxy_resource
(
'
base
'
);
$
this
->
endpoint
= Helpers::
get_proxy_resource
(
'
endpoint
'
);
$
this
->
init
(
$
init
);
}
/**
* Actions
*
* @return void
*/
private
function
init
(
$
init
) {
if
( !
$
init
) {
return
;
}
$
settings
= [];
if
(
array_key_exists
(
'
option_name
'
,
$
_POST
) &&
$
_POST
[
'
option_name
'
] ==
'
proxy_enabled
'
&&
array_key_exists
(
'
option_value
'
,
$
_POST
) &&
$
_POST
[
'
option_value
'
] ==
'
on
'
) {
$
settings
[
'
proxy_enabled
'
] =
'
on
'
;
// @codeCoverageIgnore
}
// No need to continue if Proxy isn't enabled .
if
( Helpers::
proxy_enabled
(
$
settings
) ) {
add_action
(
'
rest_api_init
'
, [
$
this
,
'
register_route
'
] );
}
add_filter
(
'
rest_post_dispatch
'
, [
$
this
,
'
force_http_response_code
'
],
null
,
3
);
}
/**
* A public wrapper to programmatically send hits to the Plausible API.
*
* @see https://plausible.io/docs/events-api
*
* @param string $name Name of the event, defaults to 'pageview', all other names are treated as custom events by the API.
* @param string $domain Domain of the site in Plausible where the event should be registered.
* @param string $url URL of the page where the event was triggered.
* @param array $props Custom properties for the event.
*
* @return array|WP_Error
*/
public
function
do_request
(
$
name
=
'
pageview
'
,
$
domain
=
''
,
$
url
=
''
,
$
props
= [] ) {
$
request
=
new
\
WP_REST_Request
(
'
POST
'
,
"
/
$
this
->
namespace
/v1/
$
this
->
base
/
$
this
->
endpoint
"
);
$
body
= [
'
n
'
=>
$
name
,
'
d
'
=>
$
domain
?: Helpers::
get_domain
(),
'
u
'
=>
$
url
?:
wp_get_referer
(),
];
// URL is required, so if no $url was set and no referer was found, attempt to create it from the REQUEST_URI server variable.
if
(
empty
(
$
body
[
'
u
'
] ) ) {
$
body
[
'
u
'
] =
$
this
->
generate_event_url
();
// @codeCoverageIgnore
}
// Revenue events use a different approach.
if
(
isset
(
$
props
[
'
revenue
'
] ) ) {
$
body
[
'
revenue
'
] =
reset
(
$
props
);
// @codeCoverageIgnore
}
elseif
( !
empty
(
$
props
) ) {
$
body
[
'
p
'
] =
$
props
;
// @codeCoverageIgnore
}
$
request
->
set_body
(
wp_json_encode
(
$
body
) );
return
$
this
->
send_event
(
$
request
);
}
/**
* Attempts to generate the Event URL from available resources.
*
* @return string
*/
public
function
generate_event_url
() {
$
url
=
''
;
$
parts
=
wp_parse_url
(
$
_SERVER
[
'
REQUEST_URI
'
] );
$
home_url_parts
=
wp_parse_url
(
get_home_url
() );
if
(
isset
(
$
home_url_parts
[
'
scheme
'
] ) &&
isset
(
$
home_url_parts
[
'
host
'
] ) &&
isset
(
$
parts
[
'
path
'
] ) ) {
$
url
=
$
home_url_parts
[
'
scheme
'
] .
'
://
'
.
$
home_url_parts
[
'
host
'
] .
$
parts
[
'
path
'
];
}
return
$
url
;
}
/**
* Formats and sends $request to the Plausible API.
*
* @return array|WP_Error
*/
public
function
send_event
(
$
request
) {
$
params
=
$
request
->
get_body
();
$
ip
=
$
this
->
get_user_ip_address
();
$
url
=
'
https://plausible.io/api/event
'
;
$
ua
= !
empty
(
$
_SERVER
[
'
HTTP_USER_AGENT
'
] ) ?
wp_kses
(
$
_SERVER
[
'
HTTP_USER_AGENT
'
],
'
strip
'
) :
''
;
return
wp_remote_post
(
$
url
,
[
'
user-agent
'
=>
$
ua
,
'
headers
'
=>
array_merge
(
$
this
->
get_forwarded_headers
(),
[
'
X-Forwarded-For
'
=>
$
ip
,
'
Content-Type
'
=>
'
application/json
'
,
]
),
'
body
'
=>
wp_kses_no_null
(
$
params
),
]
);
}
/**
* Returns the visitor's Accept-Language and User-Agent Client Hints (Sec-CH-UA*) headers, so Plausible's bot
* detection can use them for proxied requests, too.
*
* @since v2.6.4
*
* @return array
*/
public
function
get_forwarded_headers
() {
$
headers
= [];
foreach
(
$
_SERVER
as
$
key
=>
$
value
) {
if
(
$
key
!==
'
HTTP_ACCEPT_LANGUAGE
'
&&
strpos
(
$
key
,
'
HTTP_SEC_CH_UA
'
) !==
0
) {
continue
;
}
$
value
=
trim
(
wp_kses
(
wp_unslash
( (
string
)
$
value
),
'
strip
'
) );
if
(
$
value
===
''
) {
continue
;
}
// e.g. HTTP_SEC_CH_UA_PLATFORM -> Sec-CH-UA-Platform
$
name
=
str_replace
(
'
'
,
'
-
'
,
ucwords
(
strtolower
(
str_replace
(
'
_
'
,
'
'
,
substr
(
$
key
,
5
) ) ) ) );
$
name
=
str_replace
(
'
Sec-Ch-Ua
'
,
'
Sec-CH-UA
'
,
$
name
);
$
headers
[
$
name
] =
$
value
;
}
return
$
headers
;
}
/**
* @return string
*
* @codeCoverageIgnore
*/
private
function
get_user_ip_address
() {
$
ip
=
''
;
foreach
(
self
::
PROXY_IP_HEADERS
as
$
header
) {
if
(
$
this
->
header_exists
(
$
header
) ) {
$
ip
=
wp_kses
(
$
_SERVER
[
$
header
],
'
strip
'
);
if
(
strpos
(
$
ip
,
'
,
'
) !==
false
) {
$
ip
=
explode
(
'
,
'
,
$
ip
);
return
$
ip
[
0
];
}
return
$
ip
;
}
}
return
$
ip
;
}
/**
* Checks if a HTTP header is set and is not empty.
*
* @param mixed $global
*
* @return bool
*/
private
function
header_exists
(
$
global
) {
return
!
empty
(
$
_SERVER
[
$
global
] );
}
/**
* Register the API route.
*
* @return void
*
* @codeCoverageIgnore Because we have no way of knowing if the API works in integration tests.
*/
public
function
register_route
() {
register_rest_route
(
$
this
->
namespace
,
'
/
'
.
$
this
->
base
.
'
/
'
.
$
this
->
endpoint
,
[
[
'
methods
'
=>
'
POST
'
,
'
callback
'
=> [
$
this
,
'
send_event
'
],
// There's no reason not to allow access to this API.
'
permission_callback
'
=>
'
__return_true
'
,
],
'
schema
'
=>
null
,
]
);
}
/**
* Make sure our response code is returned, instead of the default 200 on success.
*
* @param WP_HTTP_Response $response
* @param WP_REST_Server $server
* @param WP_REST_Request $request
*
* @return WP_HTTP_Response
*
* @codeCoverageIgnore
*/
public
function
force_http_response_code
(
$
response
,
$
server
,
$
request
) {
if
(
strpos
(
$
request
->
get_route
(),
$
this
->
namespace
) ===
false
) {
return
$
response
;
// @codeCoverageIgnore
}
$
response_code
=
wp_remote_retrieve_response_code
(
$
response
->
get_data
() );
$
response
->
set_status
(
$
response_code
);
return
$
response
;
}
}
You can’t perform that action at this time.