| 1 |
<?php |
| 2 |
/** |
| 3 |
* Attachment model class. |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Optml_Attachment_Model |
| 8 |
* |
| 9 |
* @since 4.0.0 |
| 10 |
*/ |
| 11 |
class Optml_Attachment_Model { |
| 12 |
use Optml_Dam_Offload_Utils; |
| 13 |
|
| 14 |
/** |
| 15 |
* The attachment ID. |
| 16 |
* |
| 17 |
* @var int |
| 18 |
*/ |
| 19 |
private $attachment_id; |
| 20 |
|
| 21 |
/** |
| 22 |
* The attachment file extension. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $extension; |
| 27 |
|
| 28 |
/** |
| 29 |
* The attachment file name including extension. |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
private $original_attached_file_name; |
| 34 |
|
| 35 |
/** |
| 36 |
* The attachment file name without extension. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
private $filename_no_ext; |
| 41 |
|
| 42 |
/** |
| 43 |
* The original attached full file path (even if it's scaled). |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
private $origianal_attached_file_path; |
| 48 |
|
| 49 |
/** |
| 50 |
* The original attached file directory path. |
| 51 |
* |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
private $dir_path; |
| 55 |
|
| 56 |
/** |
| 57 |
* Whether the attachment is scaled. |
| 58 |
* |
| 59 |
* @var bool |
| 60 |
*/ |
| 61 |
private $is_scaled = false; |
| 62 |
|
| 63 |
/** |
| 64 |
* The attached file main URL. |
| 65 |
* |
| 66 |
* @var string |
| 67 |
*/ |
| 68 |
private $main_attachment_url; |
| 69 |
|
| 70 |
/** |
| 71 |
* Whether the attachment is remote - offloaded, imported from DAM or legacy offloaded. |
| 72 |
* |
| 73 |
* @var bool |
| 74 |
*/ |
| 75 |
private $is_remote_attachment; |
| 76 |
|
| 77 |
/** |
| 78 |
* The attachment mime type. |
| 79 |
* |
| 80 |
* @var string |
| 81 |
*/ |
| 82 |
private $mime_type; |
| 83 |
|
| 84 |
/** |
| 85 |
* The attachment metadata. |
| 86 |
* |
| 87 |
* @var array |
| 88 |
*/ |
| 89 |
public $attachment_metadata; |
| 90 |
|
| 91 |
/** |
| 92 |
* Constructor. |
| 93 |
* |
| 94 |
* @param int $attachment_id Attachment ID. |
| 95 |
* |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function __construct( int $attachment_id ) { |
| 99 |
$this->attachment_id = $attachment_id; |
| 100 |
$this->attachment_metadata = wp_get_attachment_metadata( $this->attachment_id ); |
| 101 |
$this->mime_type = get_post_mime_type( $attachment_id ); |
| 102 |
|
| 103 |
$this->main_attachment_url = $this->is_image() ? wp_get_original_image_url( $attachment_id ) : wp_get_attachment_url( $attachment_id ); |
| 104 |
$this->origianal_attached_file_path = $this->setup_original_attached_file(); |
| 105 |
$this->dir_path = dirname( $this->origianal_attached_file_path ); |
| 106 |
$this->is_scaled = isset( $this->attachment_metadata['original_image'] ); |
| 107 |
|
| 108 |
$filename = $this->is_scaled && isset( $this->attachment_metadata['original_image'] ) ? |
| 109 |
$this->attachment_metadata['original_image'] : |
| 110 |
basename( $this->origianal_attached_file_path ); |
| 111 |
|
| 112 |
$this->original_attached_file_name = $filename; |
| 113 |
|
| 114 |
$file_parts = pathinfo( $filename ); |
| 115 |
|
| 116 |
$this->extension = isset( $file_parts['extension'] ) ? $file_parts['extension'] : ''; |
| 117 |
$this->filename_no_ext = $file_parts['filename']; |
| 118 |
$this->is_remote_attachment = $this->is_dam_imported_image( $this->attachment_id ) || |
| 119 |
$this->is_legacy_offloaded_attachment( $this->attachment_id ) || |
| 120 |
$this->is_new_offloaded_attachment( $this->attachment_id ); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Check if the attachment is scaled. |
| 125 |
* |
| 126 |
* @return bool |
| 127 |
*/ |
| 128 |
public function is_scaled() { |
| 129 |
return $this->is_scaled; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get attachment ID. |
| 134 |
* |
| 135 |
* @return int |
| 136 |
*/ |
| 137 |
public function get_attachment_id() { |
| 138 |
return $this->attachment_id; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get filename no extension. |
| 143 |
* |
| 144 |
* @return string |
| 145 |
*/ |
| 146 |
public function get_filename_no_ext() { |
| 147 |
return $this->filename_no_ext; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get filename with extension. |
| 152 |
* |
| 153 |
* @param bool $scaled Whether the filename is scaled. |
| 154 |
* |
| 155 |
* @return string |
| 156 |
*/ |
| 157 |
public function get_filename_with_ext( $scaled = false ) { |
| 158 |
if ( $scaled ) { |
| 159 |
return sprintf( '%s-scaled.%s', $this->filename_no_ext, $this->extension ); |
| 160 |
} |
| 161 |
|
| 162 |
return sprintf( '%s.%s', $this->filename_no_ext, $this->extension ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Get extension. |
| 167 |
* |
| 168 |
* @return string |
| 169 |
*/ |
| 170 |
public function get_extension() { |
| 171 |
return $this->extension; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Get source file path. |
| 176 |
* |
| 177 |
* Returns the original attached file path, if the attachment is scaled, it will return the unscaled file path. |
| 178 |
* |
| 179 |
* @return string |
| 180 |
*/ |
| 181 |
public function get_source_file_path() { |
| 182 |
return $this->origianal_attached_file_path; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Get dir path. |
| 187 |
* |
| 188 |
* @return string |
| 189 |
*/ |
| 190 |
public function get_dir_path() { |
| 191 |
return $this->dir_path; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Get the attachment file main url. |
| 196 |
* |
| 197 |
* @return string |
| 198 |
*/ |
| 199 |
public function get_main_url() { |
| 200 |
return $this->main_attachment_url; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Get attachment metadata. |
| 205 |
* |
| 206 |
* @return array |
| 207 |
*/ |
| 208 |
public function get_attachment_metadata() { |
| 209 |
return $this->attachment_metadata; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Get all image sizes paths. |
| 214 |
* |
| 215 |
* @return array |
| 216 |
*/ |
| 217 |
public function get_all_image_sizes_paths() { |
| 218 |
$paths = []; |
| 219 |
|
| 220 |
if ( ! isset( $this->attachment_metadata['sizes'] ) ) { |
| 221 |
return []; |
| 222 |
} |
| 223 |
|
| 224 |
foreach ( $this->attachment_metadata['sizes'] as $size => $size_data ) { |
| 225 |
$paths[ $size ] = $this->dir_path . '/' . $size_data['file']; |
| 226 |
} |
| 227 |
|
| 228 |
return $paths; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Get all image sizes URLs. |
| 233 |
* |
| 234 |
* @return array |
| 235 |
*/ |
| 236 |
public function get_all_image_sizes_urls() { |
| 237 |
$attachment_metadata = $this->attachment_metadata; |
| 238 |
|
| 239 |
$links = []; |
| 240 |
|
| 241 |
if ( ! isset( $attachment_metadata['sizes'] ) ) { |
| 242 |
return []; |
| 243 |
} |
| 244 |
|
| 245 |
foreach ( $attachment_metadata['sizes'] as $size => $size_data ) { |
| 246 |
$links[ $size ] = str_replace( $this->original_attached_file_name, $size_data['file'], $this->get_main_url() ); |
| 247 |
} |
| 248 |
|
| 249 |
return $links; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Get attached file. |
| 254 |
* |
| 255 |
* @return string |
| 256 |
*/ |
| 257 |
private function setup_original_attached_file() { |
| 258 |
$attachment_metadata = wp_get_attachment_metadata( $this->attachment_id ); |
| 259 |
$attached_file = get_attached_file( $this->attachment_id ); |
| 260 |
$file_name = basename( $attached_file ); |
| 261 |
|
| 262 |
if ( isset( $attachment_metadata['original_image'] ) ) { |
| 263 |
return str_replace( $file_name, $attachment_metadata['original_image'], $attached_file ); |
| 264 |
} |
| 265 |
|
| 266 |
return $attached_file; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Get metadata 'file' key prefix path. |
| 271 |
* |
| 272 |
* @return string |
| 273 |
*/ |
| 274 |
public function get_metadata_prefix_path() { |
| 275 |
if ( ! isset( $this->attachment_metadata['file'] ) ) { |
| 276 |
$attached_file = get_post_meta( $this->attachment_id, '_wp_attached_file', true ); |
| 277 |
|
| 278 |
return dirname( $attached_file ); |
| 279 |
} |
| 280 |
|
| 281 |
$file_path = $this->attachment_metadata['file']; |
| 282 |
|
| 283 |
return dirname( $file_path ); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Check if can be renamed/replaced. |
| 288 |
* |
| 289 |
* @return bool |
| 290 |
*/ |
| 291 |
public function can_be_renamed_or_replaced() { |
| 292 |
return ! $this->is_remote_attachment; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Get the attachment mime type. |
| 297 |
*/ |
| 298 |
public function get_attachment_mimetype() { |
| 299 |
return $this->mime_type; |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Check if the attachment is an image. |
| 304 |
*/ |
| 305 |
public function is_image() { |
| 306 |
return strpos( $this->mime_type, 'image' ) !== false; |
| 307 |
} |
| 308 |
} |
| 309 |
|