Collection
1 day ago
Comment
1 day ago
Date
1 day ago
Media
1 day ago
Post
1 day ago
Term
1 day ago
User
1 day ago
Wrap
1 day ago
Actions.php
1 day ago
Aggregate.php
1 day ago
Append.php
1 day ago
ArrayToCollection.php
1 day ago
Avatar.php
1 day ago
AvatarUrl.php
1 day ago
BeforeAfter.php
1 day ago
BooleanLabel.php
1 day ago
CharacterLimit.php
1 day ago
CodeBlock.php
1 day ago
Color.php
1 day ago
ColumnFilter.php
1 day ago
Composite.php
1 day ago
ConditionalValue.php
1 day ago
ContentTypeLink.php
1 day ago
Count.php
1 day ago
CountLabelFormatter.php
1 day ago
DottedPassword.php
1 day ago
EmptyValue.php
1 day ago
ExplodeToCollection.php
1 day ago
ExtendedValueLink.php
1 day ago
FallBack.php
1 day ago
FallBackFormatter.php
1 day ago
FileLink.php
1 day ago
FileSizeReadable.php
1 day ago
ForeignId.php
1 day ago
FormattedJson.php
1 day ago
GroupedIdsToCollection.php
1 day ago
HasValue.php
1 day ago
HtmlEntityDecode.php
1 day ago
HumanReadableTime.php
1 day ago
HumanTimeDifference.php
1 day ago
Id.php
1 day ago
IdsToCollection.php
1 day ago
Image.php
1 day ago
ImageSize.php
1 day ago
ImageToCollection.php
1 day ago
ImageUrl.php
1 day ago
ImageUrlsFromContent.php
1 day ago
Implode.php
1 day ago
ImplodeRecursive.php
1 day ago
Kses.php
1 day ago
Latest.php
1 day ago
Linkable.php
1 day ago
Links.php
1 day ago
MapOptionLabel.php
1 day ago
MapToId.php
1 day ago
MapToLabel.php
1 day ago
MenuLink.php
1 day ago
Message.php
1 day ago
Meta.php
1 day ago
MetaCollection.php
1 day ago
MetaCount.php
1 day ago
MetaValueCollection.php
1 day ago
NoIcon.php
1 day ago
NullFormatter.php
1 day ago
NumberFormat.php
1 day ago
PathScope.php
1 day ago
PregReplace.php
1 day ago
Prepend.php
1 day ago
ReadingTime.php
1 day ago
RelationIdsCollection.php
1 day ago
SelectOptionMapper.php
1 day ago
SmallBlocks.php
1 day ago
StringSanitizer.php
1 day ago
StripTags.php
1 day ago
Suffix.php
1 day ago
TableRender.php
1 day ago
ToArray.php
1 day ago
ToggleValue.php
1 day ago
TotalSum.php
1 day ago
Trim.php
1 day ago
UrlDecode.php
1 day ago
UsedByMenu.php
1 day ago
WordCount.php
1 day ago
WordLimit.php
1 day ago
Wrapper.php
1 day ago
YesIcon.php
1 day ago
YesNoIcon.php
1 day ago
FileLink.php
65 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Formatter; |
| 6 | |
| 7 | use AC\Exception\ValueNotFoundException; |
| 8 | use AC\Formatter; |
| 9 | use AC\Helper; |
| 10 | use AC\Type\Value; |
| 11 | |
| 12 | class FileLink implements Formatter |
| 13 | { |
| 14 | private string $link_to; |
| 15 | |
| 16 | public function __construct(string $link_to = '') |
| 17 | { |
| 18 | $this->link_to = $link_to; |
| 19 | } |
| 20 | |
| 21 | public function format(Value $value): Value |
| 22 | { |
| 23 | $attachment_id = $value->get_value(); |
| 24 | |
| 25 | if (! is_numeric($attachment_id)) { |
| 26 | throw ValueNotFoundException::from_id($value->get_id()); |
| 27 | } |
| 28 | |
| 29 | $attachment_id = (int)$attachment_id; |
| 30 | |
| 31 | $attachment = get_attached_file($attachment_id); |
| 32 | |
| 33 | if (! $attachment) { |
| 34 | return $value->with_value('<em>' . __('Invalid attachment', 'codepress-admin-columns') . '</em>'); |
| 35 | } |
| 36 | |
| 37 | $label = esc_html(basename($attachment)); |
| 38 | |
| 39 | if ($this->link_to === 'download') { |
| 40 | $url = wp_get_attachment_url($attachment_id) ?: ''; |
| 41 | |
| 42 | return $value->with_value( |
| 43 | Helper\Html::create()->link($url, $label, ['download' => '']) |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | if ($this->link_to === 'edit') { |
| 48 | $edit_url = get_edit_post_link($attachment_id) ?: wp_get_attachment_url($attachment_id) ?: ''; |
| 49 | |
| 50 | return $value->with_value( |
| 51 | Helper\Html::create()->link($edit_url, $label) |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | return $value->with_value( |
| 56 | Helper\Html::create()->link( |
| 57 | wp_get_attachment_url($attachment_id) ?: '', |
| 58 | $label, |
| 59 | ['target' => '_blank'] |
| 60 | ) |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | } |
| 65 |