Completed
Push — update/anchor-fm-add-podcast-p... ( 52f496...e64a3b )
by
unknown
251:07 queued 242:18
created

Path_Processor::get_real_path()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
/* HEADER */ // phpcs:ignore
3
4
/**
5
 * This class handles dealing with paths for the autoloader.
6
 */
7
class 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
	 * @return string The tokenized path.
13
	 */
14 View Code Duplication
	public function tokenize_path_constants( $path ) {
15
		$path = wp_normalize_path( $path );
16
17
		$constants = $this->get_normalized_constants();
18
		foreach ( $constants as $constant => $constant_path ) {
19
			$len = strlen( $constant_path );
20
			if ( substr( $path, 0, $len ) !== $constant_path ) {
21
				continue;
22
			}
23
24
			return substr_replace( $path, '{{' . $constant . '}}', 0, $len );
25
		}
26
27
		return $path;
28
	}
29
30
	/**
31
	 * Given a path this will replace any of the path constant tokens with the expanded path.
32
	 *
33
	 * @param string $tokenized_path The path we want to process.
34
	 * @return string The expanded path.
35
	 */
36 View Code Duplication
	public function untokenize_path_constants( $tokenized_path ) {
37
		$tokenized_path = wp_normalize_path( $tokenized_path );
38
39
		$constants = $this->get_normalized_constants();
40
		foreach ( $constants as $constant => $constant_path ) {
41
			$constant = '{{' . $constant . '}}';
42
43
			$len = strlen( $constant );
44
			if ( substr( $tokenized_path, 0, $len ) !== $constant ) {
45
				continue;
46
			}
47
48
			return $this->get_real_path( substr_replace( $tokenized_path, $constant_path, 0, $len ) );
49
		}
50
51
		return $tokenized_path;
52
	}
53
54
	/**
55
	 * Given a file and an array of places it might be, this will find the absolute path and return it.
56
	 *
57
	 * @param string $file The plugin or theme file to resolve.
58
	 * @param array  $directories_to_check The directories we should check for the file if it isn't an absolute path.
59
	 * @return string|false Returns the absolute path to the directory, otherwise false.
60
	 */
61
	public function find_directory_with_autoloader( $file, $directories_to_check ) {
62
		$file = wp_normalize_path( $file );
63
64
		if ( ! $this->is_absolute_path( $file ) ) {
65
			$file = $this->find_absolute_plugin_path( $file, $directories_to_check );
66
			if ( ! isset( $file ) ) {
67
				return false;
68
			}
69
		}
70
71
		// We need the real path for consistency with __DIR__ paths.
72
		$file = $this->get_real_path( $file );
73
74
		// phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
75
		$directory = @is_file( $file ) ? dirname( $file ) : $file;
76
		if ( ! @is_file( $directory . '/vendor/composer/jetpack_autoload_classmap.php' ) ) {
77
			return false;
78
		}
79
		// phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged
80
81
		return $directory;
82
	}
83
84
	/**
85
	 * Fetches an array of normalized paths keyed by the constant they came from.
86
	 *
87
	 * @return string[] The normalized paths keyed by the constant.
88
	 */
89
	private function get_normalized_constants() {
90
		$raw_constants = array(
91
			// Order the constants from most-specific to least-specific.
92
			'WP_PLUGIN_DIR',
93
			'WPMU_PLUGIN_DIR',
94
			'WP_CONTENT_DIR',
95
			'ABSPATH',
96
		);
97
98
		$constants = array();
99
		foreach ( $raw_constants as $raw ) {
100
			if ( ! defined( $raw ) ) {
101
				continue;
102
			}
103
104
			$path = wp_normalize_path( constant( $raw ) );
105
			if ( isset( $path ) ) {
106
				$constants[ $raw ] = $path;
107
			}
108
		}
109
110
		return $constants;
111
	}
112
113
	/**
114
	 * Indicates whether or not a path is absolute.
115
	 *
116
	 * @param string $path The path to check.
117
	 * @return bool True if the path is absolute, otherwise false.
118
	 */
119
	private function is_absolute_path( $path ) {
120
		if ( 0 === strlen( $path ) || '.' === $path[0] ) {
121
			return false;
122
		}
123
124
		// Absolute paths on Windows may begin with a drive letter.
125
		if ( preg_match( '/^[a-zA-Z]:[\/\\\\]/', $path ) ) {
126
			return true;
127
		}
128
129
		// A path starting with / or \ is absolute; anything else is relative.
130
		return ( '/' === $path[0] || '\\' === $path[0] );
131
	}
132
133
	/**
134
	 * Given a file and a list of directories to check, this method will try to figure out
135
	 * the absolute path to the file in question.
136
	 *
137
	 * @param string $normalized_path The normalized path to the plugin or theme file to resolve.
138
	 * @param array  $directories_to_check The directories we should check for the file if it isn't an absolute path.
139
	 *
140
	 * @return string|null The absolute path to the plugin file, otherwise null.
141
	 */
142
	private function find_absolute_plugin_path( $normalized_path, $directories_to_check ) {
143
		// We're only able to find the absolute path for plugin/theme PHP files.
144
		if ( ! is_string( $normalized_path ) || '.php' !== substr( $normalized_path, -4 ) ) {
145
			return null;
146
		}
147
148
		foreach ( $directories_to_check as $directory ) {
149
			$normalized_check = wp_normalize_path( trailingslashit( $directory ) ) . $normalized_path;
150
			// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
151
			if ( @is_file( $normalized_check ) ) {
152
				return $normalized_check;
153
			}
154
		}
155
156
		return null;
157
	}
158
159
	/**
160
	 * Given a path this will figure out the real path that we should be using.
161
	 *
162
	 * @param string $path The path to resolve.
163
	 *
164
	 * @return string The resolved path.
165
	 */
166
	private function get_real_path( $path ) {
167
		// We want to resolve symbolic links for consistency with __DIR__ paths.
168
		// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
169
		$real_path = @realpath( $path );
170
		if ( false === $real_path ) {
171
			// Let the autoloader deal with paths that don't exist.
172
			$real_path = $path;
173
		}
174
175
		// Using realpath will make it platform-specific so we must normalize it after.
176
		if ( $path !== $real_path ) {
177
			$real_path = wp_normalize_path( $real_path );
178
		}
179
180
		return $real_path;
181
	}
182
}
183