action-filter-loader
3 weeks ago
block-editor
3 weeks ago
container
3 weeks ago
cookie
3 weeks ago
languages
4 years ago
notices
3 weeks ago
privacy
3 weeks ago
templates
3 weeks ago
templating
3 weeks ago
twig-extensions
3 weeks ago
utilities
3 weeks ago
wpml-wp
3 weeks ago
ISitePress.php
4 years ago
class-wpml-file.php
3 weeks ago
class-wpml-file.php
73 lines
| 1 | <?php |
| 2 | |
| 3 | class WPML_File { |
| 4 | private $wp_api; |
| 5 | |
| 6 | private $filesystem; |
| 7 | |
| 8 | public function __construct( ?WPML_WP_API $wp_api = null, ?WP_Filesystem_Direct $filesystem = null ) { |
| 9 | if ( ! $wp_api ) { |
| 10 | $wp_api = new WPML_WP_API(); |
| 11 | } |
| 12 | |
| 13 | $this->wp_api = $wp_api; |
| 14 | |
| 15 | if ( ! $filesystem ) { |
| 16 | $filesystem = new WP_Filesystem_Direct( null ); |
| 17 | } |
| 18 | |
| 19 | $this->filesystem = $filesystem; |
| 20 | } |
| 21 | |
| 22 | public function fix_dir_separator( $path ) { |
| 23 | $directory_separator = $this->wp_api->constant( 'DIRECTORY_SEPARATOR' ); |
| 24 | |
| 25 | return ( '\\' === $directory_separator ) ? str_replace( '/', '\\', $path ) : str_replace( '\\', '/', $path ); |
| 26 | } |
| 27 | |
| 28 | public function get_uri_from_path( $path ) { |
| 29 | $base = null; |
| 30 | |
| 31 | if ( $this->wp_api->defined( 'WP_CONTENT_DIR' ) && $this->wp_api->defined( 'WP_CONTENT_URL' ) ) { |
| 32 | $base_path = $this->fix_dir_separator( $this->wp_api->constant( 'WP_CONTENT_DIR' ) ); |
| 33 | |
| 34 | if ( 0 === strpos( $path, $base_path ) ) { |
| 35 | $base = array( |
| 36 | 'path' => $base_path, |
| 37 | 'uri' => $this->wp_api->constant( 'WP_CONTENT_URL' ), |
| 38 | ); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | if ( ! $base ) { |
| 43 | $base = array( |
| 44 | 'path' => $this->wp_api->constant( 'ABSPATH' ), |
| 45 | 'uri' => site_url(), |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | $base['uri'] = preg_replace( '/(^https?:)/', '', $base['uri'] ); |
| 50 | $relative_path = substr( $path, strlen( $base['path'] ) ); |
| 51 | $relative_path = str_replace( array( '/', '\\' ), '/', $relative_path ); |
| 52 | $relative_path = ltrim( $relative_path, '/' ); |
| 53 | |
| 54 | return trailingslashit( $base['uri'] ) . $relative_path; |
| 55 | } |
| 56 | |
| 57 | public function get_relative_path( $path ) { |
| 58 | return str_replace( $this->fix_dir_separator( ABSPATH ), '', $this->fix_dir_separator( $path ) ); |
| 59 | } |
| 60 | |
| 61 | public function get_full_path( $path ) { |
| 62 | return ABSPATH . $this->get_relative_path( $path ); |
| 63 | } |
| 64 | |
| 65 | public function file_exists( $path ) { |
| 66 | return $this->filesystem->is_readable( $this->get_full_path( $path ) ); |
| 67 | } |
| 68 | |
| 69 | public function get_file_modified_timestamp( $path ) { |
| 70 | return $this->filesystem->mtime( $this->get_full_path( $path ) ); |
| 71 | } |
| 72 | } |
| 73 |