class-s3-controller.php
2 years ago
class-s3-media-item-size.php
2 years ago
class-wp-offload-media-api.php
2 years ago
class-s3-media-item-size.php
54 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Smush\Core\S3; |
| 4 | |
| 5 | use Smush\Core\File_System; |
| 6 | use Smush\Core\Media\Media_Item_Size; |
| 7 | |
| 8 | class S3_Media_Item_Size extends Media_Item_Size { |
| 9 | private $file_size; |
| 10 | /** |
| 11 | * @var File_System |
| 12 | */ |
| 13 | private $fs; |
| 14 | |
| 15 | public function __construct( $key, $attachment_id, $dir, $base_url, $wp_size_metadata ) { |
| 16 | parent::__construct( $key, $attachment_id, $dir, $base_url, $wp_size_metadata ); |
| 17 | |
| 18 | $this->fs = new File_System(); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * The media item always uses local paths, which is fine for most cases because we download the files for our operations. |
| 23 | * But to cover any edge cases, this method returns the remote path (filtered by WP offload media). The remote path supports read operations like filesize() and file_exists() |
| 24 | */ |
| 25 | public function get_file_path() { |
| 26 | $local_file_path = parent::get_file_path(); |
| 27 | if ( $this->fs->file_exists( $local_file_path ) ) { |
| 28 | return $local_file_path; |
| 29 | } |
| 30 | |
| 31 | $size_file_name = $this->get_file_name(); |
| 32 | $attached_file = get_attached_file( $this->get_attachment_id() ); |
| 33 | $attached_file_name = wp_basename( $attached_file ); |
| 34 | $maybe_remote_path = str_replace( $attached_file_name, $size_file_name, $attached_file ); |
| 35 | |
| 36 | return $this->fs->file_exists( $maybe_remote_path ) |
| 37 | ? $maybe_remote_path |
| 38 | : $local_file_path; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Because for some reason WP Offload media removes the size {@see \DeliciousBrains\WP_Offload_Media\Items\Media_Library_Item::update_filesize_after_download_local} |
| 43 | * |
| 44 | * @return int |
| 45 | */ |
| 46 | public function get_filesize() { |
| 47 | if ( is_null( $this->file_size ) || $this->file_size === 0 ) { |
| 48 | $this->file_size = $this->fs->filesize( $this->get_file_path() ); |
| 49 | } |
| 50 | |
| 51 | return $this->file_size; |
| 52 | } |
| 53 | } |
| 54 |