PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / trunk
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score vtrunk
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
← All changes | includes/classes/Optimizer/Helper.php +177 -8 2.0.2trunk View file →
@@ -52,9 +52,9 @@
52 52 public static function realpath( $url, $site_url ) {
53 53 $url_path = wp_parse_url( $url, PHP_URL_PATH );
54 54 $site_url_path = wp_parse_url( $site_url, PHP_URL_PATH );
55 55 // To avoid partial matches; subdir install at `/wp` would match `/wp-includes`
56 - $site_url_path = trailingslashit( $site_url_path );
56 + $site_url_path = is_null( $site_url_path ) ? '/' : trailingslashit( $site_url_path );
57 57
58 58 // If this is a subdirectory site, we need to strip off the subdir from the URL.
59 59 // In a multisite install, the subdir is virtual and therefore not needed in the path.
60 60 // In a single-site subdir install, the subdir is included in the ABSPATH and therefore ends up duplicated.
@@ -61,12 +61,12 @@
61 61 if ( $site_url_path && '/' !== $site_url_path
62 62 && 0 === strpos( $url_path, $site_url_path ) ) {
63 63 $url_path_without_subdir = preg_replace( '#^' . $site_url_path . '#', '', $url_path, 1 );
64 64
65 - return realpath( ABSPATH . $url_path_without_subdir );
65 + return wp_normalize_path( realpath( ABSPATH . $url_path_without_subdir ) );
66 66 }
67 67
68 - return realpath( ABSPATH . $url_path );
68 + return wp_normalize_path( realpath( ABSPATH . $url_path ) );
69 69 }
70 70
71 71 /**
72 72 * Replace path in the buffer
@@ -95,11 +95,16 @@
95 95 *
96 96 * @return string
97 97 */
98 98 public static function get_optimized_url( $path, $minify ) {
99 + $settings = \PoweredCache\Utils\get_settings();
99 100 $optimizer_url = POWERED_CACHE_URL . 'includes/file-optimizer.php??';
100 - $optimized_url = $optimizer_url . $path . '?minify=' . absint( $minify );
101 + if ( $settings['rewrite_file_optimizer'] ) {
102 + $optimizer_url = site_url() . '/_static/??';
103 + }
101 104
105 + $optimized_url = $optimizer_url . $path . '&minify=' . absint( $minify );
106 +
102 107 $optimized_url = esc_url_raw( apply_filters( 'powered_cache_fo_optimized_url', $optimized_url, $path, $minify ) );
103 108
104 109 return $optimized_url;
105 110 }
@@ -104,8 +109,34 @@
104 109 return $optimized_url;
105 110 }
106 111
107 112 /**
113 + * Get the optimizer relative path
114 + * This is used for rewrite_file_optimizer option
115 + *
116 + * @return mixed|null
117 + * @since 3.3.2
118 + */
119 + public static function get_file_optimizer_relative_path() {
120 + // Calculate the relative path from the WordPress root to file-optimizer.php
121 + $relative_path = str_replace( ABSPATH, '', POWERED_CACHE_PATH . 'includes/file-optimizer.php' );
122 + $relative_path = ltrim( $relative_path, '/' ); // Remove leading slash
123 + /**
124 + * Filters relative path for file optimizer
125 + *
126 + * @hook powered_cache_fo_relative_path
127 + *
128 + * @param {string} relative path of file-optimizer.php
129 + *
130 + * @return {string} New value.
131 + * @since 3.3.2
132 + */
133 + $relative_path = apply_filters( 'powered_cache_fo_relative_path', $relative_path );
134 +
135 + return $relative_path;
136 + }
137 +
138 + /**
108 139 * Check if the given SRC excluded
109 140 *
110 141 * @param string $src URL
111 142 *
@@ -111,10 +142,9 @@
111 142 *
112 143 * @return bool
113 144 */
114 145 public static function is_excluded_js( $src ) {
115 - $settings = \PoweredCache\Utils\get_settings();
116 - $excluded_files = preg_split( '#(\r\n|\n|\r)#', $settings['excluded_js_files'], - 1, PREG_SPLIT_NO_EMPTY );
146 + $excluded_files = self::get_excluded_js();
117 147 $excluded_files = implode( '|', $excluded_files );
118 148
119 149 if ( ! empty( $excluded_files ) && preg_match( '#(' . $excluded_files . ')#', $src ) ) {
120 150 return true;
@@ -130,10 +160,9 @@
130 160 *
131 161 * @return bool
132 162 */
133 163 public static function is_excluded_css( $src ) {
134 - $settings = \PoweredCache\Utils\get_settings();
135 - $excluded_files = preg_split( '#(\r\n|\n|\r)#', $settings['excluded_css_files'], - 1, PREG_SPLIT_NO_EMPTY );
164 + $excluded_files = self::get_excluded_css();
136 165 $excluded_files = implode( '|', $excluded_files );
137 166
138 167 if ( ! empty( $excluded_files ) && preg_match( '#(' . $excluded_files . ')#', $src ) ) {
139 168 return true;
@@ -140,6 +169,146 @@
140 169 }
141 170
142 171 return false;
143 172 }
173 +
174 + /**
175 + * Check if excluded or not from defer
176 + *
177 + * @param string $tag Script tag
178 + *
179 + * @return bool
180 + */
181 + public static function is_defer_excluded( $tag ) {
182 + $excluded_files = self::get_defer_exclusions();
183 + $excluded_files = implode( '|', $excluded_files );
184 +
185 + if ( false !== stripos( $tag, 'data-no-defer' ) ) {
186 + return true;
187 + }
188 +
189 + if ( ! empty( $excluded_files ) && preg_match( '#(' . $excluded_files . ')#', $tag ) ) {
190 + return true;
191 + }
192 +
193 + return false;
194 + }
195 +
196 + /**
197 + * Check if excluded or not from delay
198 + *
199 + * @param string $tag Script tag
200 + *
201 + * @return bool
202 + */
203 + public static function is_delay_excluded( $tag ) {
204 + $excluded_files = self::get_delay_exclusions();
205 + $excluded_files = implode( '|', $excluded_files );
206 +
207 + if ( false !== stripos( $tag, 'data-no-delay' ) ) {
208 + return true;
209 + }
210 +
211 + if ( ! empty( $excluded_files ) && preg_match( '#(' . $excluded_files . ')#', $tag ) ) {
212 + return true;
213 + }
214 +
215 + return false;
216 + }
217 +
218 + /**
219 + * Get defer exclusion list
220 + *
221 + * @return array
222 + * @since 3.2
223 + */
224 + public static function get_defer_exclusions() {
225 + $settings = \PoweredCache\Utils\get_settings();
226 + $excluded_files = preg_split( '#(\r\n|\n|\r)#', $settings['js_defer_exclusions'], - 1, PREG_SPLIT_NO_EMPTY );
227 +
228 + /**
229 + * Filter the defer exclusions
230 + *
231 + * @hook powered_cache_defer_exclusions
232 + *
233 + * @param {array} $settings Excluded files
234 + *
235 + * @return {array} New value
236 + * @since 3.2
237 + */
238 + return (array) apply_filters( 'powered_cache_defer_exclusions', $excluded_files );
239 + }
240 +
241 + /**
242 + * Get delay exclusion list
243 + *
244 + * @return array
245 + * @since 3.2
246 + */
247 + public static function get_delay_exclusions() {
248 + $settings = \PoweredCache\Utils\get_settings();
249 + $excluded_files = preg_split( '#(\r\n|\n|\r)#', $settings['js_delay_exclusions'], - 1, PREG_SPLIT_NO_EMPTY );
250 +
251 + $excluded_files[] = 'wp-includes/js/dist/interactivity.min.js';
252 + $excluded_files[] = 'wp-includes/blocks/image/view.min.js'; // lightbox doesnt work in delayed mode
253 +
254 + /**
255 + * Filter the delay exclusions
256 + *
257 + * @hook powered_cache_delay_exclusions
258 + *
259 + * @param {array} $settings Excluded files
260 + *
261 + * @return {array} New value
262 + * @since 3.2
263 + */
264 + return (array) apply_filters( 'powered_cache_delay_exclusions', $excluded_files );
265 + }
266 +
267 + /**
268 + * Get JS exclusion list
269 + *
270 + * @return array
271 + * @since 3.2
272 + */
273 + public static function get_excluded_js() {
274 + $settings = \PoweredCache\Utils\get_settings();
275 + $excluded_files = preg_split( '#(\r\n|\n|\r)#', $settings['excluded_js_files'], - 1, PREG_SPLIT_NO_EMPTY );
276 +
277 + /**
278 + * Filter the excluded JS files
279 + *
280 + * @hook powered_cache_fo_excluded_js_files
281 + *
282 + * @param {array} $settings Excluded files
283 + *
284 + * @return {array} New value
285 + * @since 3.2
286 + */
287 + return (array) apply_filters( 'powered_cache_fo_excluded_js_files', $excluded_files );
288 + }
289 +
290 + /**
291 + * Get CSS exclusion list
292 + *
293 + * @return array
294 + * @since 3.2
295 + */
296 + public static function get_excluded_css() {
297 + $settings = \PoweredCache\Utils\get_settings();
298 + $excluded_files = preg_split( '#(\r\n|\n|\r)#', $settings['excluded_css_files'], - 1, PREG_SPLIT_NO_EMPTY );
299 +
300 + /**
301 + * Filter the excluded css files
302 + *
303 + * @hook powered_cache_fo_excluded_css_files
304 + *
305 + * @param {array} $settings Excluded files
306 + *
307 + * @return {array} New value
308 + * @since 3.2
309 + */
310 + return (array) apply_filters( 'powered_cache_fo_excluded_css_files', $excluded_files );
311 + }
312 +
144 313
145 314 }