-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathActions.php
More file actions
200 lines (172 loc) · 5.62 KB
/
Actions.php
File metadata and controls
200 lines (172 loc) · 5.62 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
<?php
/**
* Plausible Analytics | Actions.
* @since 1.0.0
* @package WordPress
* @subpackage Plausible Analytics
*/
namespace Plausible\Analytics\WP;
class Actions {
/**
* Constructor.
* @since 1.0.0
* @access public
* @return void
*/
public function __construct() {
add_action( 'wp_head', [ $this, 'insert_version_meta_tag' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'maybe_register_assets' ] );
add_action( 'admin_bar_menu', [ $this, 'admin_bar_node' ], 100 );
}
/**
* This <meta> tag "tells" the Plausible API which version of the plugin is used, to allow tailored error messages,
* specific to the plugin version.
* @return void
*/
public function insert_version_meta_tag() {
$version = PLAUSIBLE_ANALYTICS_VERSION;
echo "<meta name='plausible-analytics-version' content='$version' />\n";
}
/**
* Register Assets.
* @since 1.0.0
* @access public
* @return void
*/
public function maybe_register_assets() {
$settings = Helpers::get_settings();
$user_role = Helpers::get_user_role();
/**
* Bail if tracked_user_roles is empty (which means no roles should be tracked) or,
* if current role should not be tracked.
*/
if ( ( ! empty( $user_role ) && ! isset( $settings[ 'tracked_user_roles' ] ) ) || ( ! empty( $user_role ) && ! in_array( $user_role, $settings[ 'tracked_user_roles' ], true ) ) ) {
return; // @codeCoverageIgnore
}
$version = Helpers::proxy_enabled() && file_exists( Helpers::get_js_path() ) ? filemtime( Helpers::get_js_path() ) : PLAUSIBLE_ANALYTICS_VERSION;
wp_enqueue_script(
'plausible-analytics',
Helpers::get_js_url( true ),
'',
$version,
[
'in_footer' => apply_filters( 'plausible_load_js_in_footer', false ),
'strategy' => 'defer',
]
);
// Goal tracking inline script (Don't disable this as it is required by 404).
wp_add_inline_script(
'plausible-analytics',
'window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }'
);
// Track 404 pages (if enabled)
if ( Helpers::is_enhanced_measurement_enabled( '404' ) && is_404() ) {
$data = wp_json_encode(
[
'props' => [
'path' => 'document.location.pathname',
],
]
);
/**
* document.location.pathname is a variable. @see wp_json_encode() doesn't allow passing variable, only strings. This fixes that.
*/
$data = str_replace( '"document.location.pathname"', 'document.location.pathname', $data );
wp_add_inline_script(
'plausible-analytics',
"document.addEventListener('DOMContentLoaded', function () { plausible( '404', $data ); });"
);
}
// Track search results. Tracks a search event with the search term and the number of results, and a pageview with the site's search URL.
if ( Helpers::is_enhanced_measurement_enabled( 'search' ) && is_search() ) {
global $wp_query;
$data = wp_json_encode(
[
'props' => [
// convert queries to lowercase and remove trailing whitespace to ensure same terms are grouped together
'search_query' => strtolower( trim( get_search_query() ) ),
'result_count' => $wp_query->found_posts,
],
]
);
$script = "plausible('WP Search Queries', $data );";
wp_add_inline_script(
'plausible-analytics',
"document.addEventListener('DOMContentLoaded', function() {\n$script\n});"
);
}
// This action allows you to add your own custom scripts!
do_action( 'plausible_analytics_after_register_assets', $settings );
}
/**
* Create admin bar nodes.
* @since 1.3.0
* @access public
*
* @param \WP_Admin_Bar $admin_bar Admin bar object.
*
* @return void
*/
public function admin_bar_node( $admin_bar ) {
$disable = ! empty( Helpers::get_settings()[ 'disable_toolbar_menu' ] );
if ( $disable ) {
return; // @codeCoverageIgnore
}
$settings = Helpers::get_settings();
$current_user = wp_get_current_user();
$has_access = false;
$user_roles_have_access = array_merge(
[ 'administrator' ],
$settings[ 'expand_dashboard_access' ] ?? []
);
foreach ( $current_user->roles as $role ) {
if ( in_array( $role, $user_roles_have_access, true ) ) {
$has_access = true;
break;
}
}
if ( ! $has_access ) {
return;
}
// Add main admin bar node.
$args[] = [
'id' => 'plausible-analytics',
'title' => 'Plausible Analytics',
];
if ( ! empty( $settings[ 'enable_analytics_dashboard' ] ) || ( ! empty( $settings[ 'self_hosted_domain' ] ) && ! empty( $settings[ 'self_hosted_shared_link' ] ) ) ) {
$args[] = [
'id' => 'view-analytics',
'title' => esc_html__( 'View Analytics', 'plausible-analytics' ),
'href' => admin_url( 'index.php?page=plausible_analytics_statistics' ),
'parent' => 'plausible-analytics',
];
// Add link to individual page stats.
if ( is_singular() ) {
global $post;
$uri = wp_make_link_relative( get_permalink( $post->ID ) );
$args[] = [
'id' => 'view-page-analytics',
'title' => esc_html__( 'View Page Analytics', 'plausible-analytics' ),
'href' => add_query_arg(
'page-url',
is_home() ? '' : $uri,
admin_url( 'index.php?page=plausible_analytics_statistics' )
),
'parent' => 'plausible-analytics',
];
}
}
// Add link to Plausible Settings page.
if ( current_user_can( 'manage_options' ) ) {
$args[] = [
'id' => 'settings',
'title' => esc_html__( 'Settings', 'plausible-analytics' ),
'href' => admin_url( 'options-general.php?page=plausible_analytics' ),
'parent' => 'plausible-analytics',
];
}
foreach ( $args as $arg ) {
$admin_bar->add_node( $arg );
}
}
}