Subscriber.php
5.85 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
<?php
namespace WP_Rocket\Engine\Preload\Links;
use WP_Filesystem_Direct;
use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Event_Management\Subscriber_Interface;
class Subscriber implements Subscriber_Interface {
/**
* Options Data instance
*
* @var Options_Data
*/
private $options;
/**
* WP_Filesystem_Direct instance.
*
* @var WP_Filesystem_Direct
*/
private $filesystem;
/**
* Script enqueued status.
*
* @var bool
*/
private $is_enqueued = false;
/**
* Instantiate the class.
*
* @param Options_Data $options Options Data instance.
* @param WP_Filesystem_Direct $filesystem The Filesystem object.
*/
public function __construct( Options_Data $options, $filesystem ) {
$this->options = $options;
$this->filesystem = $filesystem;
}
/**
* Return an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events() {
return [
'wp_enqueue_scripts' => 'add_preload_script',
];
}
/**
* Adds the inline script to the footer when the option is enabled
*
* @since 3.7
*
* @return void
*/
public function add_preload_script() {
/**
* Bail out if user is logged in
* Don't add preload link script
*/
if ( is_user_logged_in() ) {
return;
}
if ( $this->is_enqueued ) {
return;
}
if ( ! (bool) $this->options->get( 'preload_links', 0 ) || rocket_bypass() ) {
return;
}
$js_assets_path = rocket_get_constant( 'WP_ROCKET_PATH' ) . 'assets/js/';
if ( ! wp_script_is( 'rocket-browser-checker' ) ) {
$checker_filename = rocket_get_constant( 'SCRIPT_DEBUG' ) ? 'browser-checker.js' : 'browser-checker.min.js';
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion
wp_register_script(
'rocket-browser-checker',
'',
[],
'',
true
);
wp_enqueue_script( 'rocket-browser-checker' );
wp_add_inline_script(
'rocket-browser-checker',
$this->filesystem->get_contents( "{$js_assets_path}{$checker_filename}" )
);
}
$preload_filename = rocket_get_constant( 'SCRIPT_DEBUG' ) ? 'preload-links.js' : 'preload-links.min.js';
// Register handle with no src to add the inline script after.
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion
wp_register_script(
'rocket-preload-links',
'',
[
'rocket-browser-checker',
],
'',
true
);
wp_enqueue_script( 'rocket-preload-links' );
wp_add_inline_script(
'rocket-preload-links',
$this->filesystem->get_contents( "{$js_assets_path}{$preload_filename}" )
);
wp_localize_script(
'rocket-preload-links',
'RocketPreloadLinksConfig',
$this->get_preload_links_config()
);
$this->is_enqueued = true;
}
/**
* Gets the Preload Links script configuration parameters.
*
* @since 3.7
*
* @return string[] Preload Links script configuration parameters.
*/
private function get_preload_links_config() {
$use_trailing_slash = $this->use_trailing_slash();
$images_ext = 'jpg|jpeg|gif|png|tiff|bmp|webp|avif|pdf|doc|docx|xls|xlsx|php';
$config = [
'excludeUris' => $this->get_uris_to_exclude( $use_trailing_slash ),
'usesTrailingSlash' => $use_trailing_slash,
'imageExt' => $images_ext,
'fileExt' => $images_ext . '|html|htm',
'siteUrl' => home_url(),
'onHoverDelay' => 100, // milliseconds. -1 disables the "on hover" feature.
'rateThrottle' => 3, // on hover: limits the number of links preloaded per second.
];
/**
* Preload Links script configuration parameters.
*
* This array of parameters are passed as RocketPreloadLinksConfig object and used by the
* `preload-links.min.js` script to configure the behavior of the Preload Links feature.
*
* @since 3.7
*
* @param string[] $config Preload Links script configuration parameters.
*/
$filtered_config = apply_filters( 'rocket_preload_links_config', $config );
if ( ! is_array( $filtered_config ) ) {
return $config;
}
return array_merge( $config, $filtered_config );
}
/**
* Gets the URIs to exclude.
*
* @since 3.7
*
* @param bool $use_trailing_slash When true, uses trailing slash.
*
* @return string
*/
private function get_uris_to_exclude( $use_trailing_slash ) {
$site_url = site_url();
$uris = get_rocket_cache_reject_uri( false, false );
$uris = str_replace( [ '/(.*)|', '/(.*)/|' ], '/|', $uris );
$default = [
'/refer/',
'/go/',
'/recommend/',
'/recommends/',
];
$excluded = $default;
/**
* Filters the patterns excluded from links preload
*
* @since 3.10.8
*
* @param string[] $excluded Array of excluded patterns.
* @param string[] $default Array of default excluded patterns.
*/
$excluded = apply_filters( 'rocket_preload_links_exclusions', $excluded, $default );
if ( ! is_array( $excluded ) ) {
$excluded = (array) $excluded;
}
$excluded = array_filter( $excluded );
$login_url = wp_login_url(); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
$login_uri = str_replace( home_url(), '', $login_url );
$excluded = array_filter(
$excluded,
function ( $uri ) use ( $login_uri ) {
return ! str_contains( $login_uri, $uri );
}
);
$excluded_patterns = '';
if ( ! empty( $excluded ) ) {
$excluded_patterns = '|' . implode( '|', $excluded );
}
return $uris . $excluded_patterns;
}
/**
* Checks if the given URL has a trailing slash.
*
* @since 3.7
*
* @param string $url URL to check.
*
* @return bool
*/
private function has_trailing_slash( $url ) {
return substr( $url, -1 ) === '/';
}
/**
* Indicates if the site uses a trailing slash in the permalink structure.
*
* @since 3.7
*
* @return bool when true, uses `/`; else, no.
*/
private function use_trailing_slash() {
return $this->has_trailing_slash( get_permalink() );
}
}