Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Static_Files
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
10
100.00% covered (success)
100.00%
1 / 1
 exists
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
10
1<?php
2/**
3 * Detect static files that shadow the SEO package's generated output.
4 *
5 * @package automattic/jetpack-seo
6 */
7
8namespace Automattic\Jetpack\SEO;
9
10use Automattic\Jetpack\Constants;
11use Automattic\Jetpack\Status\Host;
12
13/**
14 * Resolve the public root on hosts that keep WordPress core in a separate directory.
15 */
16class Static_Files {
17
18    /**
19     * Check whether a static file takes precedence over WordPress-generated output.
20     *
21     * @since 0.10.0
22     * @param string $filename Static filename relative to the site's root.
23     * @return bool Whether the file exists.
24     */
25    public static function exists( $filename ) {
26        $root = rtrim( wp_normalize_path( Constants::get_constant( 'ABSPATH' ) ), '/' );
27
28        // Atomic serves the parent directory while keeping managed WordPress core in __wp__.
29        if ( '__wp__' === basename( $root ) && ( new Host() )->is_atomic_platform() ) {
30            $root = dirname( $root );
31        }
32
33        $home = wp_parse_url( home_url( '/' ) );
34        $site = wp_parse_url( site_url( '/' ) );
35        if ( isset( $home['host'] ) && isset( $site['host'] ) && 0 === strcasecmp( $home['host'], $site['host'] ) && ( $home['port'] ?? null ) === ( $site['port'] ?? null ) ) {
36            $home_path = $home['path'] ?? '/';
37            $site_path = $site['path'] ?? '/';
38            if ( 0 === strpos( $site_path, $home_path ) && $site_path !== $home_path ) {
39                $relative = '/' . trim( rawurldecode( substr( $site_path, strlen( $home_path ) ) ), '/' );
40                // URL paths alone cannot establish the filesystem layout on mapped hosts.
41                if ( substr( $root, -strlen( $relative ) ) === $relative ) {
42                    $root = substr( $root, 0, -strlen( $relative ) );
43                }
44            }
45        }
46
47        return file_exists( $root . '/' . $filename );
48    }
49}