AutoloadFileWriter.php
11 months ago
AutoloadGenerator.php
11 months ago
AutoloadProcessor.php
11 months ago
CustomAutoloaderPlugin.php
11 months ago
ManifestGenerator.php
11 months ago
autoload.php
11 months ago
class-autoloader-handler.php
11 months ago
class-autoloader-locator.php
11 months ago
class-autoloader.php
11 months ago
class-container.php
11 months ago
class-hook-manager.php
11 months ago
class-latest-autoloader-guard.php
11 months ago
class-manifest-reader.php
11 months ago
class-path-processor.php
11 months ago
class-php-autoloader.php
11 months ago
class-plugin-locator.php
11 months ago
class-plugins-handler.php
11 months ago
class-shutdown-handler.php
11 months ago
class-version-loader.php
11 months ago
class-version-selector.php
11 months ago
index.php
11 months ago
class-path-processor.php
108 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | // phpcs:ignore |
| 4 | class Path_Processor { |
| 5 | public function tokenize_path_constants( $path ) { |
| 6 | $path = wp_normalize_path( $path ); |
| 7 | $constants = $this->get_normalized_constants(); |
| 8 | foreach ( $constants as $constant => $constant_path ) { |
| 9 | $len = strlen( $constant_path ); |
| 10 | if ( substr( $path, 0, $len ) !== $constant_path ) { |
| 11 | continue; |
| 12 | } |
| 13 | return substr_replace( $path, '{{' . $constant . '}}', 0, $len ); |
| 14 | } |
| 15 | return $path; |
| 16 | } |
| 17 | public function untokenize_path_constants( $tokenized_path ) { |
| 18 | $tokenized_path = wp_normalize_path( $tokenized_path ); |
| 19 | $constants = $this->get_normalized_constants(); |
| 20 | foreach ( $constants as $constant => $constant_path ) { |
| 21 | $constant = '{{' . $constant . '}}'; |
| 22 | $len = strlen( $constant ); |
| 23 | if ( substr( $tokenized_path, 0, $len ) !== $constant ) { |
| 24 | continue; |
| 25 | } |
| 26 | return $this->get_real_path( substr_replace( $tokenized_path, $constant_path, 0, $len ) ); |
| 27 | } |
| 28 | return $tokenized_path; |
| 29 | } |
| 30 | public function find_directory_with_autoloader( $file, $directories_to_check ) { |
| 31 | $file = wp_normalize_path( $file ); |
| 32 | if ( ! $this->is_absolute_path( $file ) ) { |
| 33 | $file = $this->find_absolute_plugin_path( $file, $directories_to_check ); |
| 34 | if ( ! isset( $file ) ) { |
| 35 | return false; |
| 36 | } |
| 37 | } |
| 38 | // We need the real path for consistency with __DIR__ paths. |
| 39 | $file = $this->get_real_path( $file ); |
| 40 | // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged |
| 41 | $directory = @is_file( $file ) ? dirname( $file ) : $file; |
| 42 | if ( ! @is_file( $directory . '/vendor/composer/jetpack_autoload_classmap.php' ) ) { |
| 43 | return false; |
| 44 | } |
| 45 | // phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged |
| 46 | return $directory; |
| 47 | } |
| 48 | private function get_normalized_constants() { |
| 49 | $raw_constants = array( |
| 50 | // Order the constants from most-specific to least-specific. |
| 51 | 'WP_PLUGIN_DIR', |
| 52 | 'WPMU_PLUGIN_DIR', |
| 53 | 'WP_CONTENT_DIR', |
| 54 | 'ABSPATH', |
| 55 | ); |
| 56 | $constants = array(); |
| 57 | foreach ( $raw_constants as $raw ) { |
| 58 | if ( ! defined( $raw ) ) { |
| 59 | continue; |
| 60 | } |
| 61 | $path = wp_normalize_path( constant( $raw ) ); |
| 62 | if ( isset( $path ) ) { |
| 63 | $constants[ $raw ] = $path; |
| 64 | } |
| 65 | } |
| 66 | return $constants; |
| 67 | } |
| 68 | private function is_absolute_path( $path ) { |
| 69 | if ( empty( $path ) || 0 === strlen( $path ) || '.' === $path[0] ) { |
| 70 | return false; |
| 71 | } |
| 72 | // Absolute paths on Windows may begin with a drive letter. |
| 73 | if ( preg_match( '/^[a-zA-Z]:[\/\\\\]/', $path ) ) { |
| 74 | return true; |
| 75 | } |
| 76 | // A path starting with / or \ is absolute; anything else is relative. |
| 77 | return ( '/' === $path[0] || '\\' === $path[0] ); |
| 78 | } |
| 79 | private function find_absolute_plugin_path( $normalized_path, $directories_to_check ) { |
| 80 | // We're only able to find the absolute path for plugin/theme PHP files. |
| 81 | if ( ! is_string( $normalized_path ) || '.php' !== substr( $normalized_path, -4 ) ) { |
| 82 | return null; |
| 83 | } |
| 84 | foreach ( $directories_to_check as $directory ) { |
| 85 | $normalized_check = wp_normalize_path( trailingslashit( $directory ) ) . $normalized_path; |
| 86 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 87 | if ( @is_file( $normalized_check ) ) { |
| 88 | return $normalized_check; |
| 89 | } |
| 90 | } |
| 91 | return null; |
| 92 | } |
| 93 | private function get_real_path( $path ) { |
| 94 | // We want to resolve symbolic links for consistency with __DIR__ paths. |
| 95 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 96 | $real_path = @realpath( $path ); |
| 97 | if ( false === $real_path ) { |
| 98 | // Let the autoloader deal with paths that don't exist. |
| 99 | $real_path = $path; |
| 100 | } |
| 101 | // Using realpath will make it platform-specific so we must normalize it after. |
| 102 | if ( $path !== $real_path ) { |
| 103 | $real_path = wp_normalize_path( $real_path ); |
| 104 | } |
| 105 | return $real_path; |
| 106 | } |
| 107 | } |
| 108 |