| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\Styles; |
| 4 |
|
| 5 |
class CSS_Files_Manager { |
| 6 |
const DEFAULT_CSS_DIR = 'elementor/css/'; |
| 7 |
const FILE_EXTENSION = '.css'; |
| 8 |
// Read and write permissions for the owner |
| 9 |
const PERMISSIONS = 0644; |
| 10 |
|
| 11 |
public function get( string $handle, string $media, callable $get_css, bool $is_valid_cache ): ?Style_File { |
| 12 |
$filesystem = $this->get_filesystem(); |
| 13 |
$path = $this->get_path( $handle ); |
| 14 |
|
| 15 |
if ( $is_valid_cache ) { |
| 16 |
if ( ! $filesystem->exists( $path ) ) { |
| 17 |
return null; |
| 18 |
} |
| 19 |
|
| 20 |
// Return the existing file |
| 21 |
return Style_File::create( |
| 22 |
$this->sanitize_handle( $handle ), |
| 23 |
$this->get_filesystem_path( $this->get_path( $handle ) ), |
| 24 |
$this->get_url( $handle ), |
| 25 |
$media, |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
$css = $get_css(); |
| 30 |
|
| 31 |
if ( empty( $css ) ) { |
| 32 |
return null; |
| 33 |
} |
| 34 |
|
| 35 |
$filesystem_path = $this->get_filesystem_path( $path ); |
| 36 |
|
| 37 |
$is_created = $filesystem->put_contents( $filesystem_path, $css, self::PERMISSIONS ); |
| 38 |
|
| 39 |
if ( false === $is_created ) { |
| 40 |
return null; |
| 41 |
} |
| 42 |
|
| 43 |
return Style_File::create( |
| 44 |
$this->sanitize_handle( $handle ), |
| 45 |
$filesystem_path, |
| 46 |
$this->get_url( $handle ), |
| 47 |
$media |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
public function delete( string $handle ): void { |
| 52 |
$filesystem = $this->get_filesystem(); |
| 53 |
$path = $this->get_path( $handle ); |
| 54 |
|
| 55 |
if ( ! $filesystem->exists( $path ) ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$filesystem->delete( $path ); |
| 60 |
} |
| 61 |
|
| 62 |
private function get_filesystem(): \WP_Filesystem_Base { |
| 63 |
global $wp_filesystem; |
| 64 |
|
| 65 |
if ( empty( $wp_filesystem ) ) { |
| 66 |
require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 67 |
WP_Filesystem(); |
| 68 |
} |
| 69 |
|
| 70 |
return $wp_filesystem; |
| 71 |
} |
| 72 |
|
| 73 |
private function get_filesystem_path( $path ): string { |
| 74 |
$filesystem = $this->get_filesystem(); |
| 75 |
|
| 76 |
return str_replace( ABSPATH, $filesystem->abspath(), $path ); |
| 77 |
} |
| 78 |
|
| 79 |
private function get_url( string $handle ): string { |
| 80 |
$upload_dir = wp_upload_dir(); |
| 81 |
$sanitized_handle = $this->sanitize_handle( $handle ); |
| 82 |
$handle = $sanitized_handle . self::FILE_EXTENSION; |
| 83 |
|
| 84 |
return trailingslashit( $upload_dir['baseurl'] ) . self::DEFAULT_CSS_DIR . $handle; |
| 85 |
} |
| 86 |
|
| 87 |
private function get_path( string $handle ): string { |
| 88 |
$upload_dir = wp_upload_dir(); |
| 89 |
$sanitized_handle = $this->sanitize_handle( $handle ); |
| 90 |
$handle = $sanitized_handle . self::FILE_EXTENSION; |
| 91 |
|
| 92 |
return trailingslashit( $upload_dir['basedir'] ) . self::DEFAULT_CSS_DIR . $handle; |
| 93 |
} |
| 94 |
|
| 95 |
private function sanitize_handle( string $handle ): string { |
| 96 |
return preg_replace( '/[^a-zA-Z0-9_-]/', '', $handle ); |
| 97 |
} |
| 98 |
} |
| 99 |
|