1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\Castor\Helpers; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\Castor\Helpers\PostMeta; |
6
|
|
|
use GeminiLabs\Castor\Helpers\Theme; |
7
|
|
|
use WP_Post; |
8
|
|
|
use WP_Query; |
9
|
|
|
|
10
|
|
|
class Media |
11
|
|
|
{ |
12
|
|
|
public $postmeta; |
13
|
|
|
public $theme; |
14
|
|
|
|
15
|
|
|
public function __construct( PostMeta $postmeta, Theme $theme ) |
16
|
|
|
{ |
17
|
|
|
$this->postmeta = $postmeta; |
18
|
|
|
$this->theme = $theme; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public function gallery( array $args = [] ) |
25
|
|
|
{ |
26
|
|
|
$gallery = $this->getGallery( $args ); |
27
|
|
|
|
28
|
|
|
return $this->renderGallery( $gallery ) . $this->renderGalleryPagination( $gallery ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return WP_Query |
33
|
|
|
*/ |
34
|
|
|
public function getGallery( array $args ) |
35
|
|
|
{ |
36
|
|
|
$args = $this->normalizeArgs( $args ); |
37
|
|
|
|
38
|
|
|
return new WP_Query([ |
39
|
|
|
'orderby' => 'post__in', |
40
|
|
|
'paged' => $this->getPaged(), |
41
|
|
|
'post__in' => $this->getMediaIds( $args ), |
42
|
|
|
'post_mime_type' => 'image', |
43
|
|
|
'post_type' => 'attachment', |
44
|
|
|
'post_status' => 'inherit', |
45
|
|
|
'posts_per_page' => $this->getImagesPerPage( $args ), |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function renderGallery( WP_Query $gallery ) |
53
|
|
|
{ |
54
|
|
|
$images = array_reduce( $gallery->posts, function( $images, $attachment ) { |
55
|
|
|
return $images . $this->renderGalleryImage( $attachment ); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
return sprintf( '<div class="gallery-images" itemscope itemtype="http://schema.org/ImageGallery">%s</div>', $images ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return null|string |
63
|
|
|
*/ |
64
|
|
|
public function renderGalleryImage( WP_Post $attachment ) |
65
|
|
|
{ |
66
|
|
|
$image = $this->getImageSrc( $attachment->ID ); |
67
|
|
|
|
68
|
|
|
if( !$image )return; |
69
|
|
|
|
70
|
|
|
return sprintf( |
71
|
|
|
'<figure data-w="%s" data-h="%s" itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">' . |
72
|
|
|
'<a href="%s" itemprop="contentUrl" data-size="%sx%s" data-m="%s" data-m-size="%sx%s">' . |
73
|
|
|
'<img src="%s" data-src="%s" itemprop="thumbnail" alt="%s" />' . |
74
|
|
|
'</a>' . |
75
|
|
|
'<figcaption itemprop="caption description">%s<span itemprop="copyrightHolder">%s</span></figcaption>' . |
76
|
|
|
'</figure>', |
77
|
|
|
$image->thumbnail['width'], |
78
|
|
|
$image->thumbnail['height'], |
79
|
|
|
$image->large['url'], |
80
|
|
|
$image->large['width'], |
81
|
|
|
$image->large['height'], |
82
|
|
|
$image->medium['url'], |
83
|
|
|
$image->medium['width'], |
84
|
|
|
$image->medium['height'], |
85
|
|
|
$this->theme->imageUri( 'blank.gif' ), |
86
|
|
|
$image->thumbnail['url'], |
87
|
|
|
$image->alt, |
88
|
|
|
$image->caption, |
89
|
|
|
$image->copyright |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function renderGalleryPagination( WP_Query $query ) |
97
|
|
|
{ |
98
|
|
|
return paginate_links([ |
99
|
|
|
'before_page_number' => '<span class="screen-reader-text">' . __( 'Page', 'castor' ) . ' </span>', |
100
|
|
|
'current' => $query->query['paged'], |
101
|
|
|
'mid_size' => 1, |
102
|
|
|
'next_text' => __( 'Next', 'castor' ), |
103
|
|
|
'prev_text' => __( 'Previous', 'castor' ), |
104
|
|
|
'total' => $query->max_num_pages, |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
protected function getImagesPerPage( array $args ) |
112
|
|
|
{ |
113
|
|
|
$args = $this->normalizeArgs( $args ); |
114
|
|
|
|
115
|
|
|
return $this->postmeta->get( $args['per_page'], [ |
116
|
|
|
'fallback' => -1, |
117
|
|
|
]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param int $id |
122
|
|
|
* |
123
|
|
|
* @return null|object |
124
|
|
|
*/ |
125
|
|
|
protected function getImageSrc( $id ) |
126
|
|
|
{ |
127
|
|
|
$thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' ); |
128
|
|
|
|
129
|
|
|
if( !$thumbnail )return; |
130
|
|
|
|
131
|
|
|
$medium = $this->normalizeImageSrc( wp_get_attachment_image_src( $id, 'medium' ), $thumbnail ); |
132
|
|
|
$large = $this->normalizeImageSrc( wp_get_attachment_image_src( $id, 'large' ), $medium ); |
133
|
|
|
|
134
|
|
|
return (object) [ |
135
|
|
|
'alt' => trim( strip_tags( get_post_meta( $id, '_wp_attachment_image_alt', true ))), |
136
|
|
|
'caption' => wp_get_attachment_caption( $id ), |
137
|
|
|
'copyright' => trim( strip_tags( get_post_meta( $id, '_copyright', true ))), |
138
|
|
|
'thumbnail' => $this->normalizeImageSrc( $thumbnail ), |
139
|
|
|
'medium' => $medium, |
140
|
|
|
'large' => $large, |
141
|
|
|
]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
protected function getMediaIds( array $args ) |
148
|
|
|
{ |
149
|
|
|
$args = $this->normalizeArgs( $args ); |
150
|
|
|
|
151
|
|
|
return wp_parse_id_list( $this->postmeta->get( $args['media'], [ |
152
|
|
|
'ID' => $this->postmeta->get( $args['gallery'] ), |
153
|
|
|
'single' => false, |
154
|
|
|
])); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return int |
159
|
|
|
*/ |
160
|
|
|
protected function getPaged() |
161
|
|
|
{ |
162
|
|
|
return intval( get_query_var(( is_front_page() ? 'page' : 'paged' ))) ?: 1; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
protected function normalizeArgs( array $args = [] ) |
169
|
|
|
{ |
170
|
|
|
return shortcode_atts([ |
171
|
|
|
'gallery' => 'gallery', |
172
|
|
|
'media' => 'media', |
173
|
|
|
'per_page' => 'per_page', |
174
|
|
|
], $args ); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param mixed $fallback |
179
|
|
|
* |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
|
protected function normalizeImageSrc( array $image, $fallback = false ) |
183
|
|
|
{ |
184
|
|
|
if( is_array( $fallback ) && count( array_diff( $image, $fallback )) < 2 ) { |
185
|
|
|
$image = $fallback; |
186
|
|
|
} |
187
|
|
|
return [ |
188
|
|
|
'url' => $image[0], |
189
|
|
|
'width' => $image[1], |
190
|
|
|
'height' => $image[2], |
191
|
|
|
]; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|