Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.83% covered (warning)
89.83%
53 / 59
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Path_Processor
89.83% covered (warning)
89.83%
53 / 59
28.57% covered (danger)
28.57%
2 / 7
29.88
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
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
5.03
 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     *
62     * @return string|false Returns the absolute path to the directory, otherwise false.
63     */
64    public function find_directory_with_autoloader( $file, $directories_to_check ) {
65        $file = wp_normalize_path( $file );
66
67        if ( ! $this->is_absolute_path( $file ) ) {
68            $file = $this->find_absolute_plugin_path( $file, $directories_to_check );
69            if ( ! isset( $file ) ) {
70                return false;
71            }
72        }
73
74        // We need the real path for consistency with __DIR__ paths.
75        $file = $this->get_real_path( $file );
76
77        // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
78        $directory = @is_file( $file ) ? dirname( $file ) : $file;
79        if ( ! @is_file( $directory . '/vendor/composer/jetpack_autoload_classmap.php' ) ) {
80            return false;
81        }
82        // phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged
83
84        return $directory;
85    }
86
87    /**
88     * Fetches an array of normalized paths keyed by the constant they came from.
89     *
90     * @return string[] The normalized paths keyed by the constant.
91     */
92    private function get_normalized_constants() {
93        $raw_constants = array(
94            // Order the constants from most-specific to least-specific.
95            'WP_PLUGIN_DIR',
96            'WPMU_PLUGIN_DIR',
97            'WP_CONTENT_DIR',
98            'ABSPATH',
99        );
100
101        $constants = array();
102        foreach ( $raw_constants as $raw ) {
103            if ( ! defined( $raw ) ) {
104                continue;
105            }
106
107            $path = wp_normalize_path( constant( $raw ) );
108            if ( isset( $path ) ) {
109                $constants[ $raw ] = $path;
110            }
111        }
112
113        return $constants;
114    }
115
116    /**
117     * Indicates whether or not a path is absolute.
118     *
119     * @param string $path The path to check.
120     *
121     * @return bool True if the path is absolute, otherwise false.
122     */
123    private function is_absolute_path( $path ) {
124        if ( empty( $path ) || 0 === strlen( $path ) || '.' === $path[0] ) {
125            return false;
126        }
127
128        // Absolute paths on Windows may begin with a drive letter.
129        if ( preg_match( '/^[a-zA-Z]:[\/\\\\]/', $path ) ) {
130            return true;
131        }
132
133        // A path starting with / or \ is absolute; anything else is relative.
134        return ( '/' === $path[0] || '\\' === $path[0] );
135    }
136
137    /**
138     * Given a file and a list of directories to check, this method will try to figure out
139     * the absolute path to the file in question.
140     *
141     * @param string $normalized_path The normalized path to the plugin or theme file to resolve.
142     * @param array  $directories_to_check The directories we should check for the file if it isn't an absolute path.
143     *
144     * @return string|null The absolute path to the plugin file, otherwise null.
145     */
146    private function find_absolute_plugin_path( $normalized_path, $directories_to_check ) {
147        // We're only able to find the absolute path for plugin/theme PHP files.
148        if ( ! is_string( $normalized_path ) || '.php' !== substr( $normalized_path, -4 ) ) {
149            return null;
150        }
151
152        foreach ( $directories_to_check as $directory ) {
153            $normalized_check = wp_normalize_path( trailingslashit( $directory ) ) . $normalized_path;
154            // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
155            if ( @is_file( $normalized_check ) ) {
156                return $normalized_check;
157            }
158        }
159
160        return null;
161    }
162
163    /**
164     * Given a path this will figure out the real path that we should be using.
165     *
166     * @param string $path The path to resolve.
167     *
168     * @return string The resolved path.
169     */
170    private function get_real_path( $path ) {
171        // We want to resolve symbolic links for consistency with __DIR__ paths.
172        // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
173        $real_path = @realpath( $path );
174        if ( false === $real_path ) {
175            // Let the autoloader deal with paths that don't exist.
176            $real_path = $path;
177        }
178
179        // Using realpath will make it platform-specific so we must normalize it after.
180        if ( $path !== $real_path ) {
181            $real_path = wp_normalize_path( $real_path );
182        }
183
184        return $real_path;
185    }
186}