Audio
5 months ago
Video
5 months ago
AttachmentMetaData.php
5 months ago
AttachmentUrl.php
5 months ago
AudioPlayer.php
5 months ago
AvailableSizes.php
5 months ago
Dimensions.php
5 months ago
Download.php
5 months ago
ExifData.php
5 months ago
FileLinkToUrl.php
5 months ago
FileName.php
5 months ago
FileSize.php
5 months ago
Link.php
5 months ago
NestedAttachmentMetaData.php
5 months ago
NumberFormat.php
5 months ago
PostMimeType.php
5 months ago
PreviewViewLink.php
5 months ago
VideoEmbed.php
5 months ago
AvailableSizes.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Formatter\Media; |
| 6 | |
| 7 | use AC\Exception\ValueNotFoundException; |
| 8 | use AC\Formatter; |
| 9 | use AC\Helper; |
| 10 | use AC\Type\Value; |
| 11 | |
| 12 | class AvailableSizes implements Formatter |
| 13 | { |
| 14 | |
| 15 | private bool $include_missing_file_sizes; |
| 16 | |
| 17 | public function __construct(bool $include_missing_file_sizes) |
| 18 | { |
| 19 | $this->include_missing_file_sizes = $include_missing_file_sizes; |
| 20 | } |
| 21 | |
| 22 | public function format(Value $value): Value |
| 23 | { |
| 24 | $meta = get_post_meta($value->get_id(), '_wp_attachment_metadata', true); |
| 25 | $sizes = $meta['sizes'] ?? null; |
| 26 | |
| 27 | if ( ! $sizes) { |
| 28 | throw new ValueNotFoundException(); |
| 29 | } |
| 30 | |
| 31 | $paths = []; |
| 32 | |
| 33 | $available_sizes = $this->get_available_sizes($sizes); |
| 34 | |
| 35 | if ($available_sizes) { |
| 36 | $url = wp_get_attachment_url($value->get_id()); |
| 37 | $paths[] = Helper\Html::create()->tooltip( |
| 38 | Helper\Html::create()->link($url, __('original', 'codepress-admin-columns')), |
| 39 | basename($url) |
| 40 | ); |
| 41 | |
| 42 | foreach ($available_sizes as $size) { |
| 43 | $src = (array)wp_get_attachment_image_src($value->get_id(), $size); |
| 44 | |
| 45 | if ( ! empty($src[0])) { |
| 46 | $paths[] = Helper\Html::create()->tooltip(Helper\Html::create()->link($src[0], $size), basename($src[0])); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if ($this->include_missing_file_sizes) { |
| 52 | $missing = $this->get_missing_sizes($sizes); |
| 53 | |
| 54 | if ($missing) { |
| 55 | foreach ($missing as $size) { |
| 56 | $paths[] = Helper\Html::create()->tooltip( |
| 57 | $size, |
| 58 | sprintf( |
| 59 | __('Missing image file for size %s.', 'codepress-admin-columns'), |
| 60 | '<em>"' . $size . '"</em>' |
| 61 | ), |
| 62 | ['class' => 'ac-missing-size'] |
| 63 | ); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return $value->with_value( |
| 69 | "<div class='ac-image-sizes'>" . implode(Helper\Html::create()->divider(), $paths) . "</div>" |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | private function get_available_sizes(array $image_sizes): array |
| 74 | { |
| 75 | return array_intersect(array_keys($image_sizes), get_intermediate_image_sizes()); |
| 76 | } |
| 77 | |
| 78 | private function get_missing_sizes(array $image_sizes): array |
| 79 | { |
| 80 | global $_wp_additional_image_sizes; |
| 81 | |
| 82 | if (empty($_wp_additional_image_sizes)) { |
| 83 | return []; |
| 84 | } |
| 85 | |
| 86 | $additional_size = $_wp_additional_image_sizes; |
| 87 | |
| 88 | if (isset($additional_size['post-thumbnail'])) { |
| 89 | unset($additional_size['post-thumbnail']); |
| 90 | } |
| 91 | |
| 92 | // image does not have these additional sizes rendered yet |
| 93 | return array_diff(array_keys((array)$additional_size), array_keys($image_sizes)); |
| 94 | } |
| 95 | |
| 96 | } |