1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\Castor; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\Castor\Helpers\PostMeta; |
6
|
|
|
use GeminiLabs\Castor\Helpers\Utility; |
7
|
|
|
|
8
|
|
|
class Image |
9
|
|
|
{ |
10
|
|
|
public $postmeta; |
11
|
|
|
public $utility; |
12
|
|
|
|
13
|
|
|
public function __construct( PostMeta $postmeta, Utility $utility ) |
14
|
|
|
{ |
15
|
|
|
$this->postmeta = $postmeta; |
16
|
|
|
$this->utility = $utility; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param int|string $attachment |
21
|
|
|
* |
22
|
|
|
* @return null|object |
23
|
|
|
*/ |
24
|
|
|
public function get( $attachment ) |
25
|
|
|
{ |
26
|
|
|
if( !filter_var( $attachment, FILTER_VALIDATE_INT )) { |
27
|
|
|
$attachment = $this->postmeta->get( $attachment ); |
28
|
|
|
} |
29
|
|
|
if( !$attachment )return; |
30
|
|
|
|
31
|
|
|
if( $thumbnail = wp_get_attachment_image_src( $attachment, 'thumbnail' )) { |
32
|
|
|
$medium = $this->normalizeSrc( wp_get_attachment_image_src( $attachment, 'medium' ), $thumbnail ); |
33
|
|
|
$large = $this->normalizeSrc( wp_get_attachment_image_src( $attachment, 'large' ), $medium ); |
34
|
|
|
|
35
|
|
|
return (object) [ |
36
|
|
|
'alt' => wp_strip_all_tags( get_post_meta( $attachment, '_wp_attachment_image_alt', true ), true ), |
37
|
|
|
'caption' => wp_get_attachment_caption( $attachment ), |
38
|
|
|
'copyright' => wp_strip_all_tags( get_post_meta( $attachment, '_copyright', true ), true ), |
39
|
|
|
'large' => $large, |
40
|
|
|
'medium' => $medium, |
41
|
|
|
'permalink' => get_attachment_link( $attachment ), |
42
|
|
|
'thumbnail' => $this->normalizeSrc( $thumbnail ), |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param mixed $fallback |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
protected function normalizeSrc( array $image, $fallback = false ) |
53
|
|
|
{ |
54
|
|
|
if( is_array( $fallback ) && count( array_diff( $image, $fallback )) < 2 ) { |
55
|
|
|
$image = $fallback; |
56
|
|
|
} |
57
|
|
|
return [ |
58
|
|
|
'url' => $image[0], |
59
|
|
|
'width' => $image[1], |
60
|
|
|
'height' => $image[2], |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|