Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
66 / 72
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Path_Processor
91.67% covered (success)
91.67%
66 / 72
28.57% covered (danger)
28.57%
2 / 7
35.71
0.00% covered (danger)
0.00%
0 / 1
 tokenize_path_constants
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
 untokenize_path_constants
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
3.10
 find_directory_with_autoloader
95.65% covered (success)
95.65%
22 / 23
0.00% covered (danger)
0.00%
0 / 1
11
 get_normalized_constants
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
4
 is_absolute_path
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
6.29
 find_absolute_plugin_path
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 get_real_path
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
1<?php
2/* HEADER */ // phpcs:ignore
3
4/**
5 * This class handles dealing with paths for the autoloader.
6 */
7class Path_Processor {
8    /**
9     * Given a path this will replace any of the path constants with a token to represent it.
10     *
11     * @param string $path The path we want to process.
12     *
13     * @return string The tokenized path.
14     */
15    public function tokenize_path_constants( $path ) {
16        $path = wp_normalize_path( $path );
17
18        $constants = $this->get_normalized_constants();
19        foreach ( $constants as $constant => $constant_path ) {
20            $len = strlen( $constant_path );
21            if ( substr( $path, 0, $len ) !== $constant_path ) {
22                continue;
23            }
24
25            return substr_replace( $path, '{{' . $constant . '}}', 0, $len );
26        }
27
28        return $path;
29    }
30
31    /**
32     * Given a path this will replace any of the path constant tokens with the expanded path.
33     *
34     * @param string $tokenized_path The path we want to process.
35     *
36     * @return string The expanded path.
37     */
38    public function untokenize_path_constants( $tokenized_path ) {
39        $tokenized_path = wp_normalize_path( $tokenized_path );
40
41        $constants = $this->get_normalized_constants();
42        foreach ( $constants as $constant => $constant_path ) {
43            $constant = '{{' . $constant . '}}';
44
45            $len = strlen( $constant );
46            if ( substr( $tokenized_path, 0, $len ) !== $constant ) {
47                continue;
48            }
49
50            return $this->get_real_path( substr_replace( $tokenized_path, $constant_path, 0, $len ) );
51        }
52
53        return $tokenized_path;
54    }
55
56    /**
57     * Given a file and an array of places it might be, this will find the absolute path and return it.
58     *
59     * @param string $file The plugin or theme file to resolve.
60     * @param array  $directories_to_check The directories we should check for the file if it isn't an absolute path.
61     * @param bool   $safe_mode Reject `$file` if it appears unsafe.
62     *
63     * @return string|false Returns the absolute path to the directory, otherwise false.
64     */
65    public function find_directory_with_autoloader( $file, $directories_to_check, $safe_mode = false ) {
66        $file = wp_normalize_path( $file );
67
68        if ( ! $this->is_absolute_path( $file ) ) {
69            $file = $this->find_absolute_plugin_path( $file, $directories_to_check );
70            if ( ! isset( $file ) ) {
71                return false;
72            }
73        }
74
75        if ( $safe_mode ) {
76            // Reject if the path (before resolving symlinks) isn't in a checked directory, or possibly tries to traverse out of it.
77            $ok = false;
78            foreach ( $directories_to_check as $directory ) {
79                $directory = wp_normalize_path( trailingslashit( $directory ) );
80                // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions -- If we have wp_normalize_path, we should have WP's polyfills.
81                if ( ! str_starts_with( $file, $directory ) ) {
82                    continue;
83                }
84                $slug_part = substr( $file, strlen( $directory ) - 1 );
85                // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions -- If we have wp_normalize_path, we should have WP's polyfills.
86                if ( str_contains( $slug_part, '/../' ) || str_ends_with( $slug_part, '/..' ) ) {
87                    continue;
88                }
89                $ok = true;
90                break;
91            }
92            if ( ! $ok ) {
93                return false;
94            }
95        }
96
97        // We need the real path for consistency with __DIR__ paths.
98        $file = $this->get_real_path( $file );
99
100        // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
101        $directory = @is_file( $file ) ? dirname( $file ) : $file;
102        if ( ! @is_file( $directory . '/vendor/composer/jetpack_autoload_classmap.php' ) ) {
103            return false;
104        }
105        // phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged
106
107        return $directory;
108    }
109
110    /**
111     * Fetches an array of normalized paths keyed by the constant they came from.
112     *
113     * @return string[] The normalized paths keyed by the constant.
114     */
115    private function get_normalized_constants() {
116        $raw_constants = array(
117            // Order the constants from most-specific to least-specific.
118            'WP_PLUGIN_DIR',
119            'WPMU_PLUGIN_DIR',
120            'WP_CONTENT_DIR',
121            'ABSPATH',
122        );
123
124        $constants = array();
125        foreach ( $raw_constants as $raw ) {
126            if ( ! defined( $raw ) ) {
127                continue;
128            }
129
130            $path = wp_normalize_path( constant( $raw ) );
131            if ( isset( $path ) ) {
132                $constants[ $raw ] = $path;
133            }
134        }
135
136        return $constants;
137    }
138
139    /**
140     * Indicates whether or not a path is absolute.
141     *
142     * @param string $path The path to check.
143     *
144     * @return bool True if the path is absolute, otherwise false.
145     */
146    private function is_absolute_path( $path ) {
147        if ( empty( $path ) || 0 === strlen( $path ) || '.' === $path[0] ) {
148            return false;
149        }
150
151        // Absolute paths on Windows may begin with a drive letter.
152        if ( preg_match( '/^[a-zA-Z]:[\/\\\\]/', $path ) ) {
153            return true;
154        }
155
156        // A path starting with / or \ is absolute; anything else is relative.
157        return ( '/' === $path[0] || '\\' === $path[0] );
158    }
159
160    /**
161     * Given a file and a list of directories to check, this method will try to figure out
162     * the absolute path to the file in question.
163     *
164     * @param string $normalized_path The normalized path to the plugin or theme file to resolve.
165     * @param array  $directories_to_check The directories we should check for the file if it isn't an absolute path.
166     *
167     * @return string|null The absolute path to the plugin file, otherwise null.
168     */
169    private function find_absolute_plugin_path( $normalized_path, $directories_to_check ) {
170        // We're only able to find the absolute path for plugin/theme PHP files.
171        if ( ! is_string( $normalized_path ) || '.php' !== substr( $normalized_path, -4 ) ) {
172            return null;
173        }
174
175        foreach ( $directories_to_check as $directory ) {
176            $normalized_check = wp_normalize_path( trailingslashit( $directory ) ) . $normalized_path;
177            // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
178            if ( @is_file( $normalized_check ) ) {
179                return $normalized_check;
180            }
181        }
182
183        return null;
184    }
185
186    /**
187     * Given a path this will figure out the real path that we should be using.
188     *
189     * @param string $path The path to resolve.
190     *
191     * @return string The resolved path.
192     */
193    private function get_real_path( $path ) {
194        // We want to resolve symbolic links for consistency with __DIR__ paths.
195        // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
196        $real_path = @realpath( $path );
197        if ( false === $real_path ) {
198            // Let the autoloader deal with paths that don't exist.
199            $real_path = $path;
200        }
201
202        // Using realpath will make it platform-specific so we must normalize it after.
203        if ( $path !== $real_path ) {
204            $real_path = wp_normalize_path( $real_path );
205        }
206
207        return $real_path;
208    }
209}