Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/wp-rocket/inc/Engine/Preload/Links/Subscriber.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + namespace WP_Rocket\Engine\Preload\Links;
4 +
5 + use WP_Filesystem_Direct;
6 + use WP_Rocket\Admin\Options_Data;
7 + use WP_Rocket\Event_Management\Subscriber_Interface;
8 +
9 + class Subscriber implements Subscriber_Interface {
10 +
11 + /**
12 + * Options Data instance
13 + *
14 + * @var Options_Data
15 + */
16 + private $options;
17 +
18 + /**
19 + * WP_Filesystem_Direct instance.
20 + *
21 + * @var WP_Filesystem_Direct
22 + */
23 + private $filesystem;
24 +
25 + /**
26 + * Script enqueued status.
27 + *
28 + * @var bool
29 + */
30 + private $is_enqueued = false;
31 +
32 + /**
33 + * Instantiate the class.
34 + *
35 + * @param Options_Data $options Options Data instance.
36 + * @param WP_Filesystem_Direct $filesystem The Filesystem object.
37 + */
38 + public function __construct( Options_Data $options, $filesystem ) {
39 + $this->options = $options;
40 + $this->filesystem = $filesystem;
41 + }
42 +
43 + /**
44 + * Return an array of events that this subscriber wants to listen to.
45 + *
46 + * @return array
47 + */
48 + public static function get_subscribed_events() {
49 + return [
50 + 'wp_enqueue_scripts' => 'add_preload_script',
51 + ];
52 + }
53 +
54 + /**
55 + * Adds the inline script to the footer when the option is enabled
56 + *
57 + * @since 3.7
58 + *
59 + * @return void
60 + */
61 + public function add_preload_script() {
62 +
63 + /**
64 + * Bail out if user is logged in
65 + * Don't add preload link script
66 + */
67 + if ( is_user_logged_in() ) {
68 + return;
69 + }
70 +
71 + if ( $this->is_enqueued ) {
72 + return;
73 + }
74 +
75 + if ( ! (bool) $this->options->get( 'preload_links', 0 ) || rocket_bypass() ) {
76 + return;
77 + }
78 +
79 + $js_assets_path = rocket_get_constant( 'WP_ROCKET_PATH' ) . 'assets/js/';
80 +
81 + if ( ! wp_script_is( 'rocket-browser-checker' ) ) {
82 + $checker_filename = rocket_get_constant( 'SCRIPT_DEBUG' ) ? 'browser-checker.js' : 'browser-checker.min.js';
83 +
84 + // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion
85 + wp_register_script(
86 + 'rocket-browser-checker',
87 + '',
88 + [],
89 + rocket_get_constant( WP_ROCKET_VERSION, '' ),
90 + true
91 + );
92 + wp_enqueue_script( 'rocket-browser-checker' );
93 + wp_add_inline_script(
94 + 'rocket-browser-checker',
95 + $this->filesystem->get_contents( "{$js_assets_path}{$checker_filename}" )
96 + );
97 + }
98 +
99 + $preload_filename = rocket_get_constant( 'SCRIPT_DEBUG' ) ? 'preload-links.js' : 'preload-links.min.js';
100 +
101 + // Register handle with no src to add the inline script after.
102 + // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion
103 + wp_register_script(
104 + 'rocket-preload-links',
105 + '',
106 + [
107 + 'rocket-browser-checker',
108 + ],
109 + rocket_get_constant( WP_ROCKET_VERSION, '' ),
110 + true
111 + );
112 + wp_enqueue_script( 'rocket-preload-links' );
113 + wp_add_inline_script(
114 + 'rocket-preload-links',
115 + $this->filesystem->get_contents( "{$js_assets_path}{$preload_filename}" )
116 + );
117 + wp_localize_script(
118 + 'rocket-preload-links',
119 + 'RocketPreloadLinksConfig',
120 + $this->get_preload_links_config()
121 + );
122 +
123 + $this->is_enqueued = true;
124 + }
125 +
126 + /**
127 + * Gets the Preload Links script configuration parameters.
128 + *
129 + * @since 3.7
130 + *
131 + * @return array Preload Links script configuration parameters.
132 + */
133 + private function get_preload_links_config() {
134 + $use_trailing_slash = $this->use_trailing_slash();
135 + $images_ext = 'jpg|jpeg|gif|png|tiff|bmp|webp|avif|pdf|doc|docx|xls|xlsx|php';
136 +
137 + $config = [
138 + 'excludeUris' => $this->get_uris_to_exclude( $use_trailing_slash ),
139 + 'usesTrailingSlash' => $use_trailing_slash,
140 + 'imageExt' => $images_ext,
141 + 'fileExt' => $images_ext . '|html|htm',
142 + 'siteUrl' => home_url(),
143 + 'onHoverDelay' => 100, // milliseconds. -1 disables the "on hover" feature.
144 + 'rateThrottle' => 3, // on hover: limits the number of links preloaded per second.
145 + ];
146 +
147 + /**
148 + * Preload Links script configuration parameters.
149 + *
150 + * This array of parameters are passed as RocketPreloadLinksConfig object and used by the
151 + * `preload-links.min.js` script to configure the behavior of the Preload Links feature.
152 + *
153 + * @since 3.7
154 + *
155 + * @param array $config Preload Links script configuration parameters.
156 + */
157 + $filtered_config = wpm_apply_filters_typed( 'array', 'rocket_preload_links_config', $config );
158 +
159 + return array_merge( $config, $filtered_config );
160 + }
161 +
162 + /**
163 + * Gets the URIs to exclude.
164 + *
165 + * @since 3.7
166 + *
167 + * @param bool $use_trailing_slash When true, uses trailing slash.
168 + *
169 + * @return string
170 + */
171 + private function get_uris_to_exclude( $use_trailing_slash ) {
172 + $site_url = site_url();
173 + $uris = get_rocket_cache_reject_uri( false, false );
174 + $uris = str_replace( [ '/(.*)|', '/(.*)/|' ], '/|', $uris );
175 +
176 + $default = [
177 + '/refer/',
178 + '/go/',
179 + '/recommend/',
180 + '/recommends/',
181 + ];
182 +
183 + $excluded = $default;
184 +
185 + /**
186 + * Filters the patterns excluded from links preload
187 + *
188 + * @since 3.10.8
189 + *
190 + * @param string[] $excluded Array of excluded patterns.
191 + * @param string[] $default Array of default excluded patterns.
192 + */
193 + $excluded = wpm_apply_filters_typed( 'array', 'rocket_preload_links_exclusions', $excluded, $default );
194 +
195 + $excluded = array_filter( $excluded );
196 +
197 + $login_url = wp_login_url(); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
198 + $login_uri = str_replace( home_url(), '', $login_url );
199 +
200 + $excluded = array_filter(
201 + $excluded,
202 + function ( $uri ) use ( $login_uri ) {
203 + return ! str_contains( $login_uri, $uri );
204 + }
205 + );
206 +
207 + $excluded_patterns = '';
208 +
209 + if ( ! empty( $excluded ) ) {
210 + $excluded_patterns = '|' . implode( '|', $excluded );
211 + }
212 +
213 + return $uris . $excluded_patterns;
214 + }
215 +
216 + /**
217 + * Checks if the given URL has a trailing slash.
218 + *
219 + * @since 3.7
220 + *
221 + * @param string $url URL to check.
222 + *
223 + * @return bool
224 + */
225 + private function has_trailing_slash( $url ) {
226 + return substr( $url, -1 ) === '/';
227 + }
228 +
229 + /**
230 + * Indicates if the site uses a trailing slash in the permalink structure.
231 + *
232 + * @since 3.7
233 + *
234 + * @return bool when true, uses `/`; else, no.
235 + */
236 + private function use_trailing_slash() {
237 + return $this->has_trailing_slash( get_permalink() );
238 + }
239 + }
240 +