Completed
Push — develop ( 4145c9...286694 )
by Paul
04:35
created

Gallery::renderImageCaption()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 4
nop 1
1
<?php
2
3
namespace GeminiLabs\Castor;
4
5
use GeminiLabs\Castor\Helpers\PostMeta;
6
use GeminiLabs\Castor\Helpers\Theme;
7
use GeminiLabs\Castor\Helpers\Utility;
8
use GeminiLabs\Castor\Image;
9
use WP_Post;
10
use WP_Query;
11
12
class Gallery
13
{
14
	public $gallery;
15
16
	protected $args;
17
	protected $image;
18
	protected $postmeta;
19
	protected $theme;
20
	protected $utility;
21
22
	public function __construct( Image $image, PostMeta $postmeta, Theme $theme, Utility $utility )
23
	{
24
		$this->image    = $image;
25
		$this->postmeta = $postmeta;
26
		$this->theme    = $theme;
27
		$this->utility  = $utility;
28
	}
29
30
	/**
31
	 * @return WP_Query
32
	 */
33
	public function get( array $args = [] )
34
	{
35
		$this->normalizeArgs( $args );
36
37
		$this->gallery = new WP_Query([
38
			'orderby'        => 'post__in',
39
			'paged'          => $this->getPaged(),
40
			'post__in'       => $this->args['media'],
41
			'post_mime_type' => 'image',
42
			'post_type'      => 'attachment',
43
			'post_status'    => 'inherit',
44
			'posts_per_page' => $this->args['images_per_page'],
45
		]);
46
47
		return $this;
48
	}
49
50
	/**
51
	 * @return string
52
	 */
53
	public function render()
54
	{
55
		if( empty( $this->args['media'] ))return;
56
		$images = array_reduce( $this->gallery->posts, function( $images, $attachment ) {
57
			return $images . $this->renderImage( $attachment );
58
		});
59
		return sprintf( '<div class="gallery-images" itemscope itemtype="http://schema.org/ImageGallery">%s</div>%s',
60
			$images,
61
			$this->renderPagination()
62
		);
63
	}
64
65
	/**
66
	 * @return null|string
67
	 */
68
	public function renderImage( WP_Post $attachment )
69
	{
70
		$image = $this->image->get( $attachment->ID )->image;
71
72
		if( !$image )return;
73
		return sprintf(
74
			'<figure class="gallery-image" data-w="%s" data-h="%s" data-ps=\'%s\' itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">' .
75
				'%s%s' .
76
			'</figure>',
77
			$image->thumbnail['width'],
78
			$image->thumbnail['height'],
79
			$this->getPhotoswipeData( $image ),
80
			$this->renderImageTag( $image ),
81
			$this->renderImageCaption( $image )
82
		);
83
	}
84
85
	/**
86
	 * @return null|string
87
	 */
88
	public function renderPagination()
89
	{
90
		if( !$this->args['pagination'] )return;
91
		return paginate_links([
92
			'before_page_number' => '<span class="screen-reader-text">' . __( 'Page', 'castor' ) . ' </span>',
93
			'current'            => $this->gallery->query['paged'],
94
			'mid_size'           => 1,
95
			'next_text'          => __( 'Next', 'castor' ),
96
			'prev_text'          => __( 'Previous', 'castor' ),
97
			'total'              => $this->gallery->max_num_pages,
98
		]);
99
	}
100
101
	/**
102
	 * @param string $key
103
	 * @param mixed  $value
104
	 *
105
	 * @return bool
106
	 */
107
	protected function getBoolValue( $key, $value = null )
108
	{
109
		$bool = $this->getValue( $key, $value );
110
111
		if( is_null( filter_var( $bool, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ))) {
112
			$bool = $this->postmeta->get( $bool );
113
		}
114
		return wp_validate_boolean( $bool );
115
	}
116
117
	/**
118
	 * @param mixed $value
119
	 *
120
	 * @return int
121
	 */
122
	protected function getGalleryArg( $value = null )
123
	{
124
		$gallery = $this->getValue( 'gallery', $value );
125
126
		if( !is_numeric( $gallery ) && is_string( $gallery )) {
127
			$gallery = intval( $this->postmeta->get( $gallery ));
128
		}
129
		return !is_null( get_post( $gallery ))
130
			? intval( $gallery )
131
			: 0;
132
	}
133
134
	/**
135
	 * @param mixed $value
136
	 *
137
	 * @return int
138
	 */
139
	protected function getImagesPerPageArg( $value = null )
140
	{
141
		$perPage = $this->getValue( 'images_per_page', $value );
142
143
		if( !is_numeric( $perPage ) && is_string( $perPage )) {
144
			$perPage = $this->postmeta->get( $perPage );
145
		}
146
		return !!intval( $perPage )
147
			? $perPage
148
			: -1;
149
	}
150
151
	/**
152
	 * @param mixed $value
153
	 *
154
	 * @return bool
155
	 */
156
	protected function getLazyloadArg( $value = null )
157
	{
158
		return $this->getBoolValue( 'lazyload', $value );
159
	}
160
161
	/**
162
	 * @param mixed $value
163
	 *
164
	 * @return array
165
	 */
166
	protected function getMediaArg( $value = null )
167
	{
168
		$media = $this->getValue( 'media', $value );
169
170
		if( is_string( $media )) {
171
			$media = $this->postmeta->get( $media, [
172
				'ID'     => $this->getGalleryArg(),
173
				'single' => false,
174
			]);
175
		}
176
		return is_array( $media )
177
			? wp_parse_id_list( $media )
178
			: [];
179
	}
180
181
	/**
182
	 * @return int
183
	 */
184
	protected function getPaged()
185
	{
186
		return intval( get_query_var(( is_front_page() ? 'page' : 'paged' ))) ?: 1;
187
	}
188
189
	/**
190
	 * @param mixed $value
191
	 *
192
	 * @return bool
193
	 */
194
	protected function getPaginationArg( $value = null )
195
	{
196
		return $this->getBoolValue( 'pagination', $value );
197
	}
198
199
	/**
200
	 * @param mixed $value
201
	 *
202
	 * @return bool
203
	 */
204
	protected function getPermalinksArg( $value = null )
205
	{
206
		return $this->getBoolValue( 'permalinks', $value );
207
	}
208
209
	/**
210
	 * @return string
211
	 */
212
	protected function getPhotoswipeData( $image )
213
	{
214
		return sprintf( '{"l":{"src":"%s","w":%d,"h":%d},"m":{"src":"%s","w":%d,"h":%d}}',
215
			$image->large['url'],
216
			$image->large['width'],
217
			$image->large['height'],
218
			$image->medium['url'],
219
			$image->medium['width'],
220
			$image->medium['height']
221
		);
222
	}
223
224
	/**
225
	 * @param string $key
226
	 * @param mixed  $value
227
	 *
228
	 * @return mixed
229
	 */
230
	protected function getValue( $key, $value = null )
231
	{
232
		if( is_null( $value ) && isset( $this->args[$key] )) {
233
			$value = $this->args[$key];
234
		}
235
		return $value;
236
	}
237
238
	/**
239
	 * @return array
240
	 */
241
	protected function normalizeArgs( array $args = [] )
242
	{
243
		$defaults = [
244
			'gallery',         // (string) meta_key | (int) post_id
245
			'lazyload',        // (string) meta_key | (bool)
246
			'media',           // (string) meta_key | (array) post_ids
247
			'pagination',      // (string) meta_key | (bool)
248
			'images_per_page', // (string) meta_key | (int) number
249
			'permalinks',      // (string) meta_key | (bool)
250
		];
251
252
		$this->args = shortcode_atts( array_combine( $defaults, $defaults ), $args );
253
254
		array_walk( $this->args, function( &$value, $key ) {
255
			$method = $this->utility->buildMethodName( $key . '_arg' );
256
			if( method_exists( $this, $method )) {
257
				$value = call_user_func([ $this, $method ], $value );
258
			}
259
		});
260
261
		return $this->args;
262
	}
263
264
	/**
265
	 * @param object $image
266
	 * @return null|string
267
	 */
268
	protected function renderImageCaption( $image )
269
	{
270
		if( !empty( $image->copyright )) {
271
			$image->caption .= sprintf( ' <span itemprop="copyrightHolder">%s</span>', $image->copyright );
272
		}
273
		if( empty( $image->caption ))return;
274
		return sprintf( '<figcaption itemprop="caption description">%s</figcaption>', $image->caption );
275
	}
276
277
	/**
278
	 * @param object $image
279
	 * @return null|string
280
	 */
281
	protected function renderImageTag( $image )
282
	{
283
		$imgSrc = $this->getLazyloadArg()
284
			? $this->theme->imageUri( 'blank.gif' )
285
			: $image->thumbnail['url'];
286
287
		$imgTag = sprintf( '<img src="%s" data-src="%s" itemprop="thumbnail" alt="%s"/>',
288
			$imgSrc,
289
			$image->thumbnail['url'],
290
			$image->alt
291
		);
292
293
		return $this->getPermalinksArg()
294
			? sprintf( '<a href="%s" itemprop="contentUrl">%s</a>', $image->permalink, $imgTag )
295
			: $imgTag;
296
	}
297
}
298