GoogleAnalytics.php
5.63 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
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
<?php
namespace WP_Rocket\Addon\GoogleTracking;
use WP_Rocket\deprecated\DeprecatedClassTrait;
use WP_Rocket\Addon\Busting\FileBustingTrait;
use WP_Rocket\Busting\Abstract_Busting;
use WP_Rocket\Logger\Logger;
/**
* Manages the cache busting of the Google Analytics file.
*
* @since 3.9 deprecated
* @since 3.1
*/
class GoogleAnalytics extends Abstract_Busting {
use FileBustingTrait;
use DeprecatedClassTrait;
/**
* Context used for the logger.
*
* @var string
* @since 3.2.4
* @author Grégory Viguier
*/
const LOGGER_CONTEXT = 'gg analytics';
/**
* Google Analytics URL.
*
* @var string
* @since 3.1
* @access protected
* @author Remy Perona
*/
protected $url = 'https://www.google-analytics.com/analytics.js';
/**
* File name (local).
* %s is a "version": a md5 hash of the file contents.
*
* @var string
* @since 3.2.4
* @access protected
* @author Grégory Viguier
*/
protected $filename_pattern = 'ga-%s.js';
/**
* Current file version (local): a md5 hash of the file contents.
*
* @var string
* @since 3.2.4
* @access protected
* @author Grégory Viguier
*/
protected $file_version;
/**
* Flag to track the replacement.
*
* @var bool
* @since 3.1
* @access protected
* @author Remy Perona
*/
protected $is_replaced = false;
/**
* Filesystem object.
*
* @var object
* @since 3.2.4
* @access protected
* @author Grégory Viguier
*/
protected $filesystem = false;
/**
* Constructor.
*
* @since 3.1
* @access public
* @author Remy Perona
*
* @param string $busting_path Path to the busting directory.
* @param string $busting_url URL of the busting directory.
*/
public function __construct( $busting_path, $busting_url ) {
self::deprecated_class( '3.9' );
$this->busting_path = $busting_path . 'google-tracking/';
$this->busting_url = $busting_url . 'google-tracking/';
$this->filesystem = rocket_direct_filesystem();
}
/** ----------------------------------------------------------------------------------------- */
/** PUBLIC METHODS ========================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Performs the replacement process.
*
* @since 3.1
* @access public
* @author Remy Perona
*
* @param string $html HTML content.
* @return string
*/
public function replace_url( $html ) {
$this->is_replaced = false;
$tag = $this->find( '<script\s*(?<attr>[^>]*)?>(?<content>.*)?<\/script>', $html );
if ( ! $tag ) {
return $html;
}
Logger::info(
'GOOGLE ANALYTICS CACHING PROCESS STARTED.',
[
self::LOGGER_CONTEXT,
'tag' => $tag,
]
);
if ( ! $this->save( $this->url ) ) {
return $html;
}
$replace_tag = preg_replace( '/(?:https?:)?\/\/www\.google-analytics\.com\/analytics\.js/i', $this->get_busting_url(), $tag );
$html = str_replace( $tag, $replace_tag, $html );
$this->is_replaced = true;
Logger::info(
'Google Analytics caching process succeeded.',
[
self::LOGGER_CONTEXT,
'file' => $this->get_busting_path(),
]
);
return $html;
}
/**
* Tell if the replacement was sucessful or not.
*
* @since 3.1
* @access public
* @author Remy Perona
*
* @return bool
*/
public function is_replaced() {
return $this->is_replaced;
}
/**
* Saves the content of the URL to cache to the busting file.
*
* @since 3.2.4
* @access public
* @author Grégory Viguier
*
* @param string $url URL to get the content from.
* @return bool
*/
public function refresh_save( $url ) {
// Before doing anything, make sure the busting file can be created.
if ( ! $this->is_busting_dir_writable() ) {
return false;
}
// Get remote content.
$content = $this->get_remote_contents( $url );
if ( ! $content ) {
// Could not get the remote contents.
return false;
}
$version = md5( $content );
$path = $this->get_busting_file_path( $version );
return $this->update_file_contents( $path, $content );
}
/** ----------------------------------------------------------------------------------------- */
/** REMOTE FILE ============================================================================= */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the Google Analytics URL.
*
* @since 3.1
* @access public
* @author Remy Perona
*
* @return string
*/
public function get_url() {
return $this->url;
}
/** ----------------------------------------------------------------------------------------- */
/** VARIOUS INTERNAL TOOLS ================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Searches for element(s) in the DOM.
*
* @since 3.1
* @access public
* @author Remy Perona
*
* @param string $pattern Pattern to match.
* @param string $html HTML content.
* @return string
*/
protected function find( $pattern, $html ) {
preg_match_all( '/' . $pattern . '/si', $html, $all_matches, PREG_SET_ORDER );
$matches = array_map(
function( $match ) {
if (
! preg_match( '/src\s*=\s*[\'"]\s*(?:https?:)?\/\/www\.google-analytics\.com\/analytics\.js\s*[\'"]/i', $match['attr'] . $match['content'] )
&&
false === strpos( $match['content'], 'GoogleAnalyticsObject' )
) {
return;
}
return $match[0];
},
$all_matches
);
$matches = array_values( array_filter( $matches ) );
if ( ! $matches ) {
return false;
}
return $matches[0];
}
}