/
class-plugin.php
227 lines (200 loc) · 5.54 KB
/
class-plugin.php
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
<?php
/**
* Main plugin file.
*
* @package XWP\Site_Performance_Tracker
* @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0)
*/
namespace XWP\Site_Performance_Tracker;
/**
* Class Plugin
*/
class Plugin {
/**
* Asset handle for the JS script.
*
* @var string
*/
const JS_HANDLE_ANALYTICS = 'web-vitals-analytics';
/**
* PerformanceObserver default chance of sending performance metrics to analytics.
*
* Set to 100% which sends analytics on every request.
*
* @var float|int
*/
const TRACKING_DEFAULT_CHANCE = 1;
/**
* Plugin directory path.
*
* @var string
*/
protected $dir_path;
/**
* Plugin directory URL.
*
* @var string
*/
protected $dir_url;
/**
* Setup the plugin
*
* @param string $dir_path Absolute path to the plugin directory root.
*/
public function __construct( $dir_path ) {
$this->dir_path = rtrim( $dir_path, '\\/' );
$this->dir_url = content_url( str_replace( WP_CONTENT_DIR, '', $this->dir_path ) );
}
/**
* Initialize the plugin.
*/
public function init() {
/**
* Check if the performance tracking should be disabled globally.
*
* @param boolean $is_disabled Is disabled flag.
*/
$is_disabled = apply_filters( 'site_performance_tracker_disabled', false );
if ( ! $is_disabled ) {
$this->register_hooks();
}
}
/**
* Register hooks.
*/
protected function register_hooks() {
/**
* Load web vitals analytics.
*/
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
/**
* Load styles for settings UI in Admin.
*/
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
/**
* Load only for modern browsers
*/
add_filter( 'script_loader_tag', array( $this, 'optimize_scripts' ), 10, 2 );
}
/**
* Get absolute path to a file relative to the plugin directory.
*
* @param string $path_relative Path relative to the plugin directory.
*
* @return string
*/
protected function path_to( $path_relative ) {
return sprintf(
'%s/%s',
$this->dir_path,
ltrim( $path_relative, '\/' )
);
}
/**
* Get URL of a file relative to the plugin directory.
*
* @param string $path_relative Path relative to the plugin directory.
*
* @return string
*/
protected function uri_to( $path_relative ) {
return sprintf(
'%s/%s',
$this->dir_url,
ltrim( $path_relative, '\\/' )
);
}
/**
* Enqueue javascript to trigger web vitals tracking.
*/
public function enqueue_scripts() {
$vitals_theme_support = get_theme_support( 'site_performance_tracker_vitals' );
$site_config = $vitals_theme_support ? $vitals_theme_support : get_option( 'spt_settings' );
$asset_meta_file = $this->path_to( 'js/dist/module/web-vitals-analytics.asset.php' );
if ( $site_config && file_exists( $asset_meta_file ) ) {
$asset_meta = require $asset_meta_file;
// Add to footer.
wp_enqueue_script(
self::JS_HANDLE_ANALYTICS,
$this->uri_to( '/js/dist/module/web-vitals-analytics.js' ),
array(),
$asset_meta['version'],
true
);
wp_localize_script(
self::JS_HANDLE_ANALYTICS,
'webVitalsAnalyticsData',
$this->get_tracker_config()
);
/**
* Load the tracker JS file only when needed per chance setting.
*/
$web_vitals_init = "( function () {
if ( 'requestIdleCallback' in window ) {
var randNumber = Math.random();
if ( randNumber <= parseFloat( window.webVitalsAnalyticsData.chance ) ) {
requestIdleCallback( function() {
webVitalsAnalyticsScript = document.querySelector( 'script[data-src*=\"web-vitals-analytics.js\"]' );
webVitalsAnalyticsScript.src = webVitalsAnalyticsScript.dataset.src;
delete webVitalsAnalyticsScript.dataset.src;
} );
}
}
} )();";
wp_add_inline_script( self::JS_HANDLE_ANALYTICS, $web_vitals_init );
}//end if
}
/**
* Enqueue styles for the UI.
*/
public function enqueue_styles() {
$asset_meta_file = $this->path_to( 'css/styles.css' );
if ( file_exists( $asset_meta_file ) ) {
wp_enqueue_style(
'site-performance-tracker-styles',
$this->uri_to( '/css/styles.css' ),
array(),
time()
);
}
}
/**
* Get tracker config to pass to JS.
*
* @return array
*/
public function get_tracker_config() {
$options = get_option( 'spt_settings' ) ? get_option( 'spt_settings' ) : array();
$site_config = isset( get_theme_support( 'site_performance_tracker_vitals' )[0] ) ? get_theme_support( 'site_performance_tracker_vitals' )[0] : array();
$vitals_config = array( array_merge( $options, $site_config ) );
if ( has_filter( 'site_performance_tracker_chance' ) ) {
$chance = apply_filters( 'site_performance_tracker_chance', self::TRACKING_DEFAULT_CHANCE );
$vitals_config['chance'] = floatval( $chance );
} else {
$vitals_config['chance'] = isset( $options['web_vitals_tracking_ratio'] ) ? floatval( $options['web_vitals_tracking_ratio'] ) : floatval( self::TRACKING_DEFAULT_CHANCE );
}
return $vitals_config;
}
/**
* Let the inline JS load our core logic when needed.
*
* @param string $tag Script tag mark-up.
* @param string $handle Script ID.
*
* @return $tag
*/
public function optimize_scripts( $tag, $handle ) {
// Replaces only the first occurrence of src in the tag. Avoids replacing inside inline scripts.
if ( self::JS_HANDLE_ANALYTICS === $handle && false !== strpos( $tag, ' src' ) ) {
return substr_replace(
$tag,
' type="module" defer data-src',
strpos( $tag, ' src' ),
// Offset.
strlen( ' src' )
// Length.
);
}
return $tag;
}
}