| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace RyanHellyer\UniqueHeaders; |
| 6 |
|
| 7 |
class AttachmentHelper |
| 8 |
{ |
| 9 |
public function getId(int $postId, string $name): int |
| 10 |
{ |
| 11 |
$attachmentId = (int) get_post_meta($postId, '_' . $name . '_id', true); |
| 12 |
|
| 13 |
if (!$attachmentId) { |
| 14 |
$attachmentId = (int) apply_filters('unique_header_fallback_images', $postId); |
| 15 |
} |
| 16 |
|
| 17 |
return $attachmentId; |
| 18 |
} |
| 19 |
|
| 20 |
public function getSrc(int $attachmentId): string |
| 21 |
{ |
| 22 |
$src = wp_get_attachment_image_src($attachmentId, 'full'); |
| 23 |
|
| 24 |
return $src[0] ?? ''; |
| 25 |
} |
| 26 |
|
| 27 |
public function getDimensions(int $attachmentId, string $dimension = 'width'): int |
| 28 |
{ |
| 29 |
$data = wp_get_attachment_image_src($attachmentId, 'full'); |
| 30 |
|
| 31 |
if ($dimension === 'width' && isset($data[1])) { |
| 32 |
return (int) $data[1]; |
| 33 |
} |
| 34 |
|
| 35 |
if ($dimension === 'height' && isset($data[2])) { |
| 36 |
return (int) $data[2]; |
| 37 |
} |
| 38 |
|
| 39 |
return 0; |
| 40 |
} |
| 41 |
|
| 42 |
public function getTitle(int $attachmentId): string |
| 43 |
{ |
| 44 |
return trim( |
| 45 |
wp_strip_all_tags( |
| 46 |
(string) get_post_meta($attachmentId, '_wp_attachment_image_alt', true) |
| 47 |
) |
| 48 |
); |
| 49 |
} |
| 50 |
} |
| 51 |
|