| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace FL\Assistant\Data\Transformers; |
| 5 |
|
| 6 |
use FL\Assistant\Data\Repository\NotationsRepository; |
| 7 |
|
| 8 |
|
| 9 |
class AttachmentTransformer { |
| 10 |
|
| 11 |
protected $notations; |
| 12 |
|
| 13 |
public function __construct( |
| 14 |
NotationsRepository $notations |
| 15 |
) { |
| 16 |
$this->notations = $notations; |
| 17 |
} |
| 18 |
|
| 19 |
public function __invoke( \WP_Post $attachment ) { |
| 20 |
$size = wp_get_attachment_image_src( $attachment->ID, 'medium' ); |
| 21 |
$meta = wp_prepare_attachment_for_js( $attachment->ID ); |
| 22 |
$thumb_src = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' ); |
| 23 |
$thumb = $thumb_src ? $thumb_src[0] : null; |
| 24 |
|
| 25 |
$response = [ |
| 26 |
'alt' => $meta['alt'], |
| 27 |
'author' => get_the_author_meta( 'display_name', $attachment->post_author ), |
| 28 |
'commentsAllowed' => 'open' === $attachment->comment_status ? true : false, |
| 29 |
'date' => get_the_date( '', $attachment ), |
| 30 |
'description' => $meta['description'], |
| 31 |
'editUrl' => get_edit_post_link( $attachment->ID, '' ), |
| 32 |
'filesize' => $meta['filesizeHumanReadable'], |
| 33 |
'filename' => $meta['filename'], |
| 34 |
'id' => $attachment->ID, |
| 35 |
'labels' => [], |
| 36 |
'mime' => $meta['mime'], |
| 37 |
'permalink' => get_permalink( $attachment ), |
| 38 |
'sizes' => isset( $meta['sizes'] ) ? $meta['sizes'] : [], |
| 39 |
'slug' => $attachment->post_name, |
| 40 |
'subtype' => $meta['subtype'], |
| 41 |
'thumbnail' => $thumb, |
| 42 |
'title' => $meta['title'], |
| 43 |
'type' => $meta['type'], |
| 44 |
'url' => $meta['url'], |
| 45 |
'urls' => [ |
| 46 |
'medium' => $size ? $size[0] : null, |
| 47 |
], |
| 48 |
'height' => $meta['height'], |
| 49 |
'width' => $meta['width'], |
| 50 |
'orientation' => isset( $meta['orientation'] ) ? $meta['orientation'] : null, |
| 51 |
]; |
| 52 |
|
| 53 |
// Labels |
| 54 |
$labels = $this->notations->get_labels( 'post', $attachment->ID ); |
| 55 |
foreach ( $labels as $label ) { |
| 56 |
$response['labels'][] = $label['label_id']; |
| 57 |
} |
| 58 |
|
| 59 |
return $response; |
| 60 |
} |
| 61 |
|
| 62 |
} |
| 63 |
|