PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.0
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.0
6.2.1 6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / Supports / MediaAttachment.php
kirki / libraries / framework / Supports Last commit date
Facades 1 week ago Traits 1 month ago Arr.php 1 week ago DataCaster.php 1 month ago Fluent.php 1 week ago HigherOrderTapProxy.php 1 month ago MediaAttachment.php 1 month ago MessagesBag.php 1 week ago Somoy.php 1 week ago Str.php 1 week ago Url.php 1 month ago Utils.php 1 month 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