Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
10.62% covered (danger)
10.62%
17 / 160
11.11% covered (danger)
11.11%
1 / 9
CRAP
n/a
0 / 0
jetpack_boost_handle_minify_request
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
72
jetpack_boost_check_404_handler
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
jetpack_boost_404_tester_cron
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
jetpack_boost_404_tester
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
jetpack_boost_404_setup
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
jetpack_boost_page_optimize_cleanup_cache
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
jetpack_boost_minify_remove_stale_static_files
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
90
jetpack_boost_build_minify_output
0.00% covered (danger)
0.00%
0 / 79
0.00% covered (danger)
0.00%
0 / 1
552
jetpack_boost_minify_get_file_parts
44.44% covered (danger)
44.44%
4 / 9
0.00% covered (danger)
0.00%
0 / 1
9.29
1<?php
2
3use Automattic\Jetpack_Boost\Admin\Config as Boost_Admin_Config;
4use Automattic\Jetpack_Boost\Lib\Minify;
5use Automattic\Jetpack_Boost\Lib\Minify\Config;
6use Automattic\Jetpack_Boost\Lib\Minify\File_Paths;
7use Automattic\Jetpack_Boost\Lib\Minify\Utils;
8
9if ( ! defined( 'JETPACK_BOOST_STATIC_CACHE_404_TESTER_PATH' ) ) {
10    define( 'JETPACK_BOOST_STATIC_CACHE_404_TESTER_PATH', '/wp-content/boost-cache/static/testing_404.js' );
11}
12
13function jetpack_boost_handle_minify_request( $request_uri ) {
14    // We handle the cache here, tell other caches not to.
15    if ( ! defined( 'DONOTCACHEPAGE' ) ) {
16        define( 'DONOTCACHEPAGE', true );
17    }
18
19    $output  = jetpack_boost_build_minify_output( $request_uri );
20    $content = $output['content'];
21    $headers = $output['headers'];
22
23    foreach ( $headers as $header ) {
24        header( $header );
25    }
26
27    // Check if we're on Atomic and take advantage of the Atomic Edge Cache.
28    if ( defined( 'ATOMIC_CLIENT_ID' ) ) {
29        header( 'A8c-Edge-Cache: cache' );
30    }
31
32    header( 'X-Page-Optimize: uncached' );
33    header( 'Cache-Control: max-age=' . 31536000 );
34    header( 'ETag: "' . md5( $content ) . '"' );
35
36    echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- We need to trust this unfortunately.
37
38    // Cache the generated data, if possible.
39    $use_cache = Config::can_use_static_cache();
40    if ( $use_cache ) {
41        $file_parts = jetpack_boost_minify_get_file_parts( $request_uri );
42        if ( is_array( $file_parts ) && isset( $file_parts['file_name'] ) && isset( $file_parts['file_extension'] ) ) {
43            $cache_dir       = Config::get_static_cache_dir_path();
44            $cache_file_path = $cache_dir . '/' . $file_parts['file_name'] . '.min.' . $file_parts['file_extension'];
45
46            // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
47            file_put_contents( $cache_file_path, $content );
48        }
49    }
50}
51
52/**
53 * Using a crafted request, we can check if is_404() is working in wp-content/
54 * The constant JETPACK_BOOST_STATIC_CACHE_404_TESTER_PATH is the path to the file that will be requested.
55 */
56function jetpack_boost_check_404_handler( $request_uri ) {
57    if ( ! str_contains( strtolower( $request_uri ), JETPACK_BOOST_STATIC_CACHE_404_TESTER_PATH ) ) {
58        return;
59    }
60
61    if ( is_404() ) {
62        if ( ! is_dir( Config::get_static_cache_dir_path() ) ) {
63            mkdir( Config::get_static_cache_dir_path(), 0775, true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir
64        }
65        file_put_contents( Config::get_static_cache_dir_path() . '/404', '1' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
66        return true;
67    } else {
68        wp_delete_file( Config::get_static_cache_dir_path() . '/404' );
69        return false;
70    }
71}
72
73/**
74 * This ensures that the 404 tester is only run once per day, espicially for multisite.
75 */
76function jetpack_boost_404_tester_cron() {
77    // If we see it's been executed within 24 hours, don't run
78    if ( ! jetpack_boost_should_run_daily_network_cron_job( '404_tester' ) ) {
79        return;
80    }
81
82    jetpack_boost_404_tester();
83}
84
85/**
86 * This function is used to test if is_404() is working in wp-content/
87 * It sends a request to a non-existent URL, that will execute the 404 handler
88 * in jetpack_boost_check_404_handler().
89 * Define the constant JETPACK_BOOST_DISABLE_404_TESTER to disable this.
90 * The constant JETPACK_BOOST_STATIC_CACHE_404_TESTER_PATH is the path to the file that will be requested.
91 *
92 * This function is called when the Minify_CSS or Minify_JS module is activated, and once per day.
93 */
94function jetpack_boost_404_tester() {
95    if ( defined( 'JETPACK_BOOST_DISABLE_404_TESTER' ) && JETPACK_BOOST_DISABLE_404_TESTER ) {
96        return;
97    }
98
99    $minification_enabled = '';
100    wp_remote_get( home_url( JETPACK_BOOST_STATIC_CACHE_404_TESTER_PATH ) );
101    if ( file_exists( Config::get_static_cache_dir_path() . '/404' ) ) {
102        wp_delete_file( Config::get_static_cache_dir_path() . '/404' );
103        $minification_enabled = 1;
104    } else {
105        $minification_enabled = 0;
106    }
107    update_site_option( 'jetpack_boost_static_minification', $minification_enabled );
108
109    return $minification_enabled;
110}
111
112add_action( 'jetpack_boost_404_tester_cron', 'jetpack_boost_404_tester_cron' );
113
114/**
115 * Setup the 404 tester.
116 *
117 * Schedule the 404 tester if the concatenation modules
118 * haven't been toggled since this feature was released.
119 * Only run this in wp-admin to avoid excessive updates to the option.
120 */
121function jetpack_boost_404_setup() {
122    // If we're on Atomic or Woa, don't setup the 404 tester.
123    if ( in_array( Boost_Admin_Config::get_hosting_provider(), array( 'atomic', 'woa' ), true ) ) {
124        return;
125    }
126
127    if ( is_admin() && get_site_option( 'jetpack_boost_static_minification', 'na' ) === 'na' ) {
128        update_site_option( 'jetpack_boost_static_minification', 0 ); // Add a default value if not set to avoid an extra SQL query.
129    }
130
131    jetpack_boost_page_optimize_schedule_404_tester();
132}
133
134/**
135 * This function is used to clean up the static cache folder.
136 * It removes files with the file extension passed in the $file_extension parameter.
137 *
138 * @param string $file_extension The file extension to clean up.
139 */
140function jetpack_boost_page_optimize_cleanup_cache( $file_extension ) {
141    $files = glob( Config::get_static_cache_dir_path() . "/*.min.{$file_extension}" );
142    foreach ( $files as $file ) {
143        wp_delete_file( $file );
144    }
145}
146
147/**
148 * This function is used to clean up the static cache folder.
149 * It removes files that are stale and no longer needed.
150 * A file is considered stale if it's older than the files it depends on.
151 */
152function jetpack_boost_minify_remove_stale_static_files() {
153    $concat_files = glob( Config::get_static_cache_dir_path() . '/*.min.*' );
154    foreach ( $concat_files as $concat_file ) {
155        if ( ! file_exists( $concat_file ) ) {
156            continue;
157        }
158
159        $file_mtime = filemtime( $concat_file );
160        $file_parts = pathinfo( $concat_file );
161        $hash       = substr( $file_parts['basename'], 0, strpos( $file_parts['basename'], '.' ) );
162        $paths      = File_Paths::get( $hash );
163        if ( $paths ) {
164            $args = $paths->get_paths();
165            if ( ! is_array( $args ) ) {
166                continue;
167            }
168
169            // Get the site path relative to the webroot.
170            $site_url_path = wp_parse_url( site_url(), PHP_URL_PATH );
171            if ( ! $site_url_path ) {
172                $site_url_path = '';
173            }
174
175            // Get the webroot path by removing the site path from the ABSPATH. In case it's a subdirectory install, webroot is different from ABSPATH.
176            $webroot = substr( ABSPATH, 0, - strlen( $site_url_path ) - 1 );
177
178            foreach ( $args as $dependency_filename ) {
179                if ( ! file_exists( $webroot . $dependency_filename ) || filemtime( $webroot . $dependency_filename ) > $file_mtime ) {
180                    wp_delete_file( $concat_file ); // remove the file from the cache because it's stale.
181                }
182            }
183        }
184    }
185}
186
187function jetpack_boost_build_minify_output( $request_uri ) {
188    $utils                             = new Utils();
189    $jetpack_boost_page_optimize_types = jetpack_boost_page_optimize_types();
190
191    // Config
192    $concat_max_files = jetpack_boost_minify_concat_max_files();
193    $concat_unique    = true;
194
195    $file_parts = jetpack_boost_minify_get_file_parts( $request_uri );
196    if ( ! $file_parts ) {
197        jetpack_boost_page_optimize_status_exit( 404 );
198    }
199
200    $file_paths = jetpack_boost_page_optimize_get_file_paths( $file_parts['file_name'] );
201
202    // file_paths contain something like array( '/foo/bar.css', '/foo1/bar/baz.css' )
203    if ( count( $file_paths ) > $concat_max_files ) {
204        jetpack_boost_page_optimize_status_exit( 400 );
205    }
206
207    // If we're in a subdirectory context, use that as the root.
208    // We can't assume that the root serves the same content as the subdir.
209    $subdir_path_prefix = '';
210    $request_path       = $utils->parse_url( $request_uri, PHP_URL_PATH );
211    $_static_index      = strpos( $request_path, jetpack_boost_get_static_prefix() );
212    if ( $_static_index > 0 ) {
213        $subdir_path_prefix = substr( $request_path, 0, $_static_index );
214    }
215    unset( $request_path, $_static_index );
216
217    $last_modified = 0;
218    $pre_output    = '';
219    $output        = '';
220
221    $mime_type = '';
222
223    foreach ( $file_paths as $uri ) {
224        $fullpath = jetpack_boost_page_optimize_get_path( $uri );
225
226        if ( ! file_exists( $fullpath ) ) {
227            jetpack_boost_page_optimize_status_exit( 404 );
228        }
229
230        $mime_type = jetpack_boost_page_optimize_get_mime_type( $fullpath );
231        if ( ! in_array( $mime_type, $jetpack_boost_page_optimize_types, true ) ) {
232            jetpack_boost_page_optimize_status_exit( 400 );
233        }
234
235        if ( $concat_unique ) {
236            if ( ! isset( $last_mime_type ) ) {
237                $last_mime_type = $mime_type;
238            }
239
240            if ( $last_mime_type !== $mime_type ) {
241                jetpack_boost_page_optimize_status_exit( 400 );
242            }
243        }
244
245        $stat = stat( $fullpath );
246        if ( false === $stat ) {
247            jetpack_boost_page_optimize_status_exit( 500 );
248        }
249
250        if ( $stat['mtime'] > $last_modified ) {
251            $last_modified = $stat['mtime'];
252        }
253
254        // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
255        $buf = file_get_contents( $fullpath );
256        if ( false === $buf ) {
257            jetpack_boost_page_optimize_status_exit( 500 );
258        }
259
260        if ( 'text/css' === $mime_type ) {
261            $dirpath = jetpack_boost_strip_parent_path( $subdir_path_prefix, dirname( $uri ) );
262
263            // url(relative/path/to/file) -> url(/absolute/and/not/relative/path/to/file)
264            $buf = jetpack_boost_page_optimize_relative_path_replace( $buf, $dirpath );
265
266            // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
267            // This regex changes things like AlphaImageLoader(...src='relative/path/to/file'...) to AlphaImageLoader(...src='/absolute/path/to/file'...)
268            $buf = preg_replace(
269                '/(Microsoft.AlphaImageLoader\s*\([^\)]*src=(?:\'|")?)([^\/\'"\s\)](?:(?<!http:|https:).)*)\)/isU',
270                '$1' . ( $dirpath === '/' ? '/' : $dirpath . '/' ) . '$2)',
271                $buf
272            );
273
274            // The @charset rules must be on top of the output
275            if ( str_starts_with( $buf, '@charset' ) ) {
276                $buf = preg_replace_callback(
277                    '/(?P<charset_rule>@charset\s+[\'"][^\'"]+[\'"];)/i',
278                    function ( $match ) use ( &$pre_output ) {
279                        if ( str_starts_with( $pre_output, '@charset' ) ) {
280                            return '';
281                        }
282
283                        $pre_output = $match[0] . "\n" . $pre_output;
284
285                        return '';
286                    },
287                    $buf
288                );
289            }
290
291            // Move the @import rules on top of the concatenated output.
292            // Only @charset rule are allowed before them.
293            if ( str_contains( $buf, '@import' ) ) {
294                $buf = preg_replace_callback(
295                    '/(?P<pre_path>@import\s+(?:url\s*\()?[\'"\s]*)(?P<path>[^\'"\s](?:https?:\/\/.+\/?)?.+?)(?P<post_path>[\'"\s\)]*;)/i',
296                    function ( $match ) use ( $dirpath, &$pre_output ) {
297                        if ( ! str_starts_with( $match['path'], 'http' ) && '/' !== $match['path'][0] ) {
298                            $pre_output .= $match['pre_path'] . ( $dirpath === '/' ? '/' : $dirpath . '/' ) .
299                                            $match['path'] . $match['post_path'] . "\n";
300                        } else {
301                            $pre_output .= $match[0] . "\n";
302                        }
303
304                        return '';
305                    },
306                    $buf
307                );
308            }
309
310            // If filename indicates it's already minified, don't minify it again.
311            if ( ! jetpack_boost_page_optimize_is_already_minified( $fullpath ) ) {
312                // Minify CSS.
313                $buf = Minify::css( $buf );
314            }
315            $output .= "$buf";
316        } else {
317            // If filename indicates it's already minified, don't minify it again.
318            if ( ! jetpack_boost_page_optimize_is_already_minified( $fullpath ) ) {
319                // Minify JS
320                $buf = Minify::js( $buf );
321            }
322
323            $output .= "$buf;\n";
324        }
325    }
326
327    // Don't let trailing whitespace ruin everyone's day. Seems to get stripped by batcache
328    // resulting in ns_error_net_partial_transfer errors.
329    $output = rtrim( $output );
330
331    $headers = array(
332        'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $last_modified ) . ' GMT',
333        "Content-Type: $mime_type",
334    );
335
336    return array(
337        'headers' => $headers,
338        'content' => $pre_output . $output,
339    );
340}
341
342/**
343 * Get the file name and extension from the request URI.
344 *
345 * @param string $request_uri The request URI.
346 * @return array|false The file name and extension, or false if the request URI is invalid.
347 */
348function jetpack_boost_minify_get_file_parts( $request_uri ) {
349    $utils       = new Utils();
350    $request_uri = $utils->unslash( $request_uri );
351
352    $file_path = $utils->parse_url( $request_uri, PHP_URL_PATH );
353    if ( $file_path === false ) {
354        return false;
355    }
356
357    $file_info = pathinfo( $file_path );
358
359    $minify_path = $utils->parse_url( jetpack_boost_get_minify_url(), PHP_URL_PATH );
360    if ( trailingslashit( $file_info['dirname'] ) !== $minify_path ) {
361        return false;
362    }
363
364    $allowed_extensions = array_keys( jetpack_boost_page_optimize_types() );
365    if ( ! isset( $file_info['extension'] ) || ! in_array( $file_info['extension'], $allowed_extensions, true ) ) {
366        return false;
367    }
368
369    // The base name (without the extension) might contain ".min".
370    // Example - 777873a36e.min
371    $file_name_parts = explode( '.', $file_info['basename'] );
372    $file_name       = $file_name_parts[0];
373
374    return array(
375        'file_name'      => $file_name,
376        'file_extension' => $file_info['extension'] ?? '',
377    );
378}