PluginProbe
Unique Headers / 2.1.1
Unique Headers v2.1.1
2.1.3 2.1 2.1.1 2.0.1 trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1 1.2 1.2.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.7 1.3.8 1.3.9 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 All 54 releases
unique-headers / src / AttachmentHelper.php

AttachmentHelper.php in Unique Headers 2.1.1, at src/AttachmentHelper.php

51 lines 1.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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