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