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/InitOptions.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
/
InitOptions.php
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
198 lines (161 loc) · 4.86 KB
develop
Breadcrumbs
wordpress
/
src
/
InitOptions.php
Copy path
Top
File metadata and controls
Code
Blame
198 lines (161 loc) · 4.86 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
<?php
/**
* Plausible Analytics | Filters.
*
* @since 1.0.0
* @package WordPress
* @subpackage Plausible Analytics
*/
namespace
Plausible
\
Analytics
\
WP
;
use
WP_Post
;
use
WP_Term
;
class
InitOptions {
/**
* Constructor.
*
* @since 1.0.0
* @access public
* @return void
*/
public
function
__construct
() {
add_filter
(
'
plausible_analytics_init_options
'
, [
$
this
,
'
maybe_add_pageview_props
'
] );
add_filter
(
'
plausible_analytics_init_options
'
, [
$
this
,
'
maybe_add_proxy_options
'
] );
add_filter
(
'
plausible_analytics_init_options
'
, [
$
this
,
'
maybe_exclude_pageview
'
] );
add_filter
(
'
plausible_analytics_init_options
'
, [
$
this
,
'
maybe_track_logged_in_users
'
] );
}
/**
* Adds custom parameters Author and Category if Custom Pageview Properties is enabled.
*
* @param $options array
*
* @return array
*/
public
function
maybe_add_pageview_props
(
$
options
= [] ) {
$
settings
= Helpers::
get_settings
();
if
( !
is_array
(
$
settings
[
'
enhanced_measurements
'
] ) || !
in_array
( EnhancedMeasurements::
PAGEVIEW_PROPS
,
$
settings
[
'
enhanced_measurements
'
] ) ) {
return
$
options
;
// @codeCoverageIgnore
}
global
$
post
;
if
( !
$
post
instanceof
WP_Post || !
is_single
() ) {
return
$
options
;
// @codeCoverageIgnore
}
$
author
=
$
post
->
post_author
;
if
(
$
author
) {
$
author_name
=
get_the_author_meta
(
'
display_name
'
,
$
author
);
$
options
[
'
customProperties
'
][
'
author
'
] =
$
author_name
;
}
// Add support for the post-category and tags along with custom taxonomies.
$
taxonomies
=
get_object_taxonomies
(
$
post
->
post_type
);
// Loop through existing taxonomies.
foreach
(
$
taxonomies
as
$
taxonomy
) {
$
terms
=
get_the_terms
(
$
post
->
ID
,
$
taxonomy
);
// Skip the iteration if `$terms` is not an array.
if
( !
is_array
(
$
terms
) ) {
continue
;
// @codeCoverageIgnore;
}
// Loop through the terms.
foreach
(
$
terms
as
$
term
) {
if
(
$
term
instanceof
WP_Term ) {
$
options
[
'
customProperties
'
][
$
taxonomy
] =
$
term
->
name
;
}
}
}
return
$
options
;
}
/**
* Modify the endpoint option if Proxy is enabled.
*
* @param array $options
*
* @return array
*
* @throws \Exception
*/
public
function
maybe_add_proxy_options
(
$
options
= [] ) {
if
( ! Helpers::
proxy_enabled
() ) {
return
$
options
;
// @codeCoverageIgnore
}
$
options
[
'
endpoint
'
] = Helpers::
get_endpoint_url
();
return
$
options
;
}
/**
* Exclude this Pageview from tracking if it matches any of the defined patterns.
*
* @param $options
*
* @return array|mixed
*/
public
function
maybe_exclude_pageview
(
$
options
= [] ) {
$
settings
= Helpers::
get_settings
();
// Triggered when exclude pages is enabled.
if
(
empty
(
$
settings
[
'
excluded_pages
'
] ) ) {
return
$
options
;
// @codeCoverageIgnore
}
$
excluded_pages
=
$
settings
[
'
excluded_pages
'
];
$
current_request
=
$
this
->
get_current_request
();
if
(
$
this
->
url_matches_patterns
(
$
current_request
,
$
excluded_pages
) ) {
$
options
[
'
transformRequest
'
] =
'
() => { return null; }
'
;
}
return
$
options
;
}
/**
* This a seam for @see add_query_arg() to be mocked in unit tests.
*
* @since 2.6.2 Uses add_query_arg( [] ) instead of add_query_arg( null, null ): both return the current request URI,
* but the latter uses null as an array offset, which is deprecated since PHP 8.5.
*
* @codeCoverageIgnore
*/
protected
function
get_current_request
() {
return
add_query_arg
( [] );
}
/**
* Does $url match any of the $patterns?
*
* @param $url
* @param $patterns
*
* @return bool
*/
private
function
url_matches_patterns
(
$
url
,
$
patterns
) {
// Split string by new lines (\n) and comma (,)
$
patterns
=
preg_split
(
"
/[
\n
,]+/
"
,
$
patterns
, -
1
,
PREG_SPLIT_NO_EMPTY
);
foreach
(
$
patterns
as
$
pattern
) {
// Escape regex-symbols (can't use preg_quote() here, because it escapes dashes).
$
regex
=
preg_replace
(
'
/([.^$+?{}()[\]|])/
'
,
'\\\\
$1
'
,
$
pattern
);
// Convert * to regex syntax.
$
regex
=
str_replace
(
'
*
'
,
'
.*
'
,
$
regex
);
// Full match.
$
regex
=
"
#
$
regex
#i
"
;
if
(
preg_match
(
$
regex
,
$
url
) ) {
return
true
;
}
}
return
false
;
}
/**
* Adds a custom parameter User Logged In if Custom Properties is enabled.
*
* @since v2.4.0
*
* @param $options
*
* @return array
*/
public
function
maybe_track_logged_in_users
(
$
options
= [] ) {
$
settings
= Helpers::
get_settings
();
if
( !
is_array
(
$
settings
[
'
enhanced_measurements
'
] ) || !
in_array
( EnhancedMeasurements::
PAGEVIEW_PROPS
,
$
settings
[
'
enhanced_measurements
'
] ) ) {
return
$
options
;
// @codeCoverageIgnore
}
$
logged_in
=
_x
(
'
no
'
,
'
Value when user is not logged in.
'
,
'
plausible-analytics
'
);
if
(
is_user_logged_in
() ) {
$
user
=
wp_get_current_user
();
$
roles
=
$
user
->
roles
;
if
( !
empty
(
$
roles
) ) {
$
logged_in
=
$
roles
[
0
];
}
}
$
options
[
'
customProperties
'
][
'
user_logged_in
'
] =
$
logged_in
;
return
$
options
;
}
}
You can’t perform that action at this time.