Facades
3 weeks ago
Traits
3 weeks ago
Arr.php
3 weeks ago
DataCaster.php
3 weeks ago
Flex.php
3 weeks ago
HigherOrderTapProxy.php
3 weeks ago
MediaAttachment.php
3 weeks ago
MessagesBag.php
3 weeks ago
Str.php
3 weeks ago
Url.php
3 weeks ago
Utils.php
3 weeks ago
MediaAttachment.php
113 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Helper for building normalized attachment metadata arrays from WordPress media IDs. |
| 5 | * Handles images, videos with posters, and external video URLs. |
| 6 | * Produces consistent attachment shapes for API resources and DTOs. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Supports |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Supports; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | class MediaAttachment |
| 16 | { |
| 17 | /** |
| 18 | * Make a video attachment. |
| 19 | * |
| 20 | * @param array|null $video The video data. |
| 21 | * |
| 22 | * @return array|null |
| 23 | * |
| 24 | * @since 1.0.0 |
| 25 | */ |
| 26 | public static function make_video($video) |
| 27 | { |
| 28 | if (empty($video)) { |
| 29 | return null; |
| 30 | } |
| 31 | // If the video comes from youtube or video providers then return it. |
| 32 | if (isset($video['url'])) { |
| 33 | return $video; |
| 34 | } |
| 35 | $video_id = $video['id'] ?? null; |
| 36 | $poster_id = $video['poster'] ?? null; |
| 37 | if (empty($video_id)) { |
| 38 | return null; |
| 39 | } |
| 40 | $video_attachment = static::make($video_id); |
| 41 | $poster_attachment = static::make($poster_id); |
| 42 | $video_attachment['poster'] = $poster_attachment; |
| 43 | return $video_attachment; |
| 44 | } |
| 45 | /** |
| 46 | * Generate an array of metadata about an image attachment. |
| 47 | * |
| 48 | * @param int|null $id Attachment ID. |
| 49 | * |
| 50 | * @return array|null Array of image metadata, or null if attachment is invalid. |
| 51 | * |
| 52 | * @since 1.0.0 |
| 53 | */ |
| 54 | public static function make($id) |
| 55 | { |
| 56 | if (empty($id)) { |
| 57 | return null; |
| 58 | } |
| 59 | $metadata = wp_get_attachment_metadata($id); |
| 60 | $attached_file = get_attached_file($id); |
| 61 | if (empty($metadata) || empty($attached_file)) { |
| 62 | return null; |
| 63 | } |
| 64 | $mime = get_post_mime_type($id); |
| 65 | $type = $mime && \stripos($mime, '/') !== \false ? \explode('/', $mime)[0] : ''; |
| 66 | $sizes = isset($metadata['sizes']) && \is_array($metadata['sizes']) ? static::get_formatted_sizes($id, $metadata['sizes']) : []; |
| 67 | $filename = \basename($attached_file) ?? ''; |
| 68 | $author_id = (string) get_post_field('post_author', $id); |
| 69 | $author_name = get_the_author_meta('display_name', $author_id); |
| 70 | return ['id' => (string) $id, 'filename' => $filename, 'url' => wp_get_attachment_url($id), 'sizes' => $sizes, 'height' => isset($metadata['height']) ? (int) $metadata['height'] : 0, 'width' => isset($metadata['width']) ? (int) $metadata['width'] : 0, 'filesize' => isset($metadata['filesize']) ? (int) $metadata['filesize'] : 0, 'mime' => $mime, 'type' => $type, 'thumb' => null, 'author' => $author_id, 'author_name' => $author_name, 'date' => get_the_date('c', $id)]; |
| 71 | } |
| 72 | /** |
| 73 | * Generate an array of metadata about multiple image attachments. |
| 74 | * |
| 75 | * @param array $ids The ids. |
| 76 | * |
| 77 | * @return array |
| 78 | * |
| 79 | * @since 1.0.0 |
| 80 | */ |
| 81 | public static function make_many(array $ids) |
| 82 | { |
| 83 | $attachments = []; |
| 84 | foreach ($ids as $id) { |
| 85 | $attachment = static::make($id); |
| 86 | if ($attachment) { |
| 87 | $attachments[] = $attachment; |
| 88 | } |
| 89 | } |
| 90 | return $attachments; |
| 91 | } |
| 92 | /** |
| 93 | * Get the formatted image sizes. |
| 94 | * |
| 95 | * @param int $attachment_id The attachment ID. |
| 96 | * @param array $sizes An array of size data from attachment metadata. |
| 97 | * |
| 98 | * @return array An associative array of formatted image sizes with height, width, URL, and orientation. |
| 99 | * |
| 100 | * @since 1.0.0 |
| 101 | */ |
| 102 | protected static function get_formatted_sizes($attachment_id, array $sizes) |
| 103 | { |
| 104 | $formatted_sizes = []; |
| 105 | foreach ($sizes as $size => $info) { |
| 106 | $height = isset($info['height']) ? (int) $info['height'] : 0; |
| 107 | $width = isset($info['width']) ? (int) $info['width'] : 0; |
| 108 | $formatted_sizes[$size] = ['height' => $height, 'width' => $width, 'url' => wp_get_attachment_image_url($attachment_id, $size), 'orientation' => $height >= $width ? 'portrait' : 'landscape']; |
| 109 | } |
| 110 | return $formatted_sizes; |
| 111 | } |
| 112 | } |
| 113 |