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
|
|
|
if( !$image )return; |
68
|
|
|
return sprintf( |
69
|
|
|
'<figure data-w="%s" data-h="%s" data-ps="%s" itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">' . |
70
|
|
|
'<a href="%s" itemprop="contentUrl"><img src="%s" data-src="%s" itemprop="thumbnail" alt="%s"/></a>' . |
71
|
|
|
'<figcaption itemprop="caption description">%s<span itemprop="copyrightHolder">%s</span></figcaption>' . |
72
|
|
|
'</figure>', |
73
|
|
|
$image->thumbnail['width'], |
74
|
|
|
$image->thumbnail['height'], |
75
|
|
|
$this->getPhotoswipeData( $image ), |
76
|
|
|
get_attachment_link( $attachment->ID ), |
77
|
|
|
$this->theme->imageUri( 'blank.gif' ), |
78
|
|
|
$image->thumbnail['url'], |
79
|
|
|
$image->alt, |
80
|
|
|
$image->caption, |
81
|
|
|
$image->copyright |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function renderGalleryPagination( WP_Query $query ) |
89
|
|
|
{ |
90
|
|
|
return paginate_links([ |
91
|
|
|
'before_page_number' => '<span class="screen-reader-text">' . __( 'Page', 'castor' ) . ' </span>', |
92
|
|
|
'current' => $query->query['paged'], |
93
|
|
|
'mid_size' => 1, |
94
|
|
|
'next_text' => __( 'Next', 'castor' ), |
95
|
|
|
'prev_text' => __( 'Previous', 'castor' ), |
96
|
|
|
'total' => $query->max_num_pages, |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
|
|
protected function getImagesPerPage( array $args ) |
104
|
|
|
{ |
105
|
|
|
$args = $this->normalizeArgs( $args ); |
106
|
|
|
|
107
|
|
|
return $this->postmeta->get( $args['per_page'], [ |
108
|
|
|
'fallback' => -1, |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param int $id |
114
|
|
|
* |
115
|
|
|
* @return null|object |
116
|
|
|
*/ |
117
|
|
|
protected function getImageSrc( $id ) |
118
|
|
|
{ |
119
|
|
|
$thumbnail = wp_get_attachment_image_src( $id, 'thumbnail' ); |
120
|
|
|
|
121
|
|
|
if( !$thumbnail )return; |
122
|
|
|
|
123
|
|
|
$medium = $this->normalizeImageSrc( wp_get_attachment_image_src( $id, 'medium' ), $thumbnail ); |
124
|
|
|
$large = $this->normalizeImageSrc( wp_get_attachment_image_src( $id, 'large' ), $medium ); |
125
|
|
|
|
126
|
|
|
return (object) [ |
127
|
|
|
'alt' => trim( strip_tags( get_post_meta( $id, '_wp_attachment_image_alt', true ))), |
128
|
|
|
'caption' => wp_get_attachment_caption( $id ), |
129
|
|
|
'copyright' => trim( strip_tags( get_post_meta( $id, '_copyright', true ))), |
130
|
|
|
'thumbnail' => $this->normalizeImageSrc( $thumbnail ), |
131
|
|
|
'medium' => $medium, |
132
|
|
|
'large' => $large, |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
|
|
protected function getMediaIds( array $args ) |
140
|
|
|
{ |
141
|
|
|
$args = $this->normalizeArgs( $args ); |
142
|
|
|
|
143
|
|
|
return wp_parse_id_list( $this->postmeta->get( $args['media'], [ |
144
|
|
|
'ID' => $this->postmeta->get( $args['gallery'] ), |
145
|
|
|
'single' => false, |
146
|
|
|
])); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return int |
151
|
|
|
*/ |
152
|
|
|
protected function getPaged() |
153
|
|
|
{ |
154
|
|
|
return intval( get_query_var(( is_front_page() ? 'page' : 'paged' ))) ?: 1; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
protected function getPhotoswipeData( $image ) |
161
|
|
|
{ |
162
|
|
|
return sprintf( '{o:{src:"%s",w:"%d",h:"%d"},m:{src:"%s",w:"%d",h:"%d"}}', |
163
|
|
|
$image->large['url'], |
164
|
|
|
$image->large['width'], |
165
|
|
|
$image->large['height'], |
166
|
|
|
$image->medium['url'], |
167
|
|
|
$image->medium['width'], |
168
|
|
|
$image->medium['height'] |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
protected function normalizeArgs( array $args = [] ) |
176
|
|
|
{ |
177
|
|
|
return shortcode_atts([ |
178
|
|
|
'gallery' => 'gallery', |
179
|
|
|
'media' => 'media', |
180
|
|
|
'per_page' => 'per_page', |
181
|
|
|
], $args ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param mixed $fallback |
186
|
|
|
* |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
|
|
protected function normalizeImageSrc( array $image, $fallback = false ) |
190
|
|
|
{ |
191
|
|
|
if( is_array( $fallback ) && count( array_diff( $image, $fallback )) < 2 ) { |
192
|
|
|
$image = $fallback; |
193
|
|
|
} |
194
|
|
|
return [ |
195
|
|
|
'url' => $image[0], |
196
|
|
|
'width' => $image[1], |
197
|
|
|
'height' => $image[2], |
198
|
|
|
]; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|