PluginProbe ʕ •ᴥ•ʔ
Photo Gallery by FooGallery : Responsive Image Gallery, Masonry Gallery & Carousel / 2.3.3
Photo Gallery by FooGallery : Responsive Image Gallery, Masonry Gallery & Carousel v2.3.3
trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / render-functions.php
foogallery / includes Last commit date
admin 2 years ago compatibility 2 years ago extensions 2 years ago foopluginbase 2 years ago public 2 years ago thumbs 2 years ago class-attachment-filters.php 2 years ago class-foogallery-animated-gif-support.php 2 years ago class-foogallery-attachment-custom-class.php 2 years ago class-foogallery-attachment.php 2 years ago class-foogallery-cache.php 2 years ago class-foogallery-common-fields.php 2 years ago class-foogallery-crop-position.php 2 years ago class-foogallery-datasource-media_library.php 2 years ago class-foogallery-debug.php 2 years ago class-foogallery-extensions-compatibility.php 2 years ago class-foogallery-force-https.php 2 years ago class-foogallery-lazyload.php 2 years ago class-foogallery-lightbox.php 2 years ago class-foogallery-paging.php 2 years ago class-foogallery-sitemaps.php 2 years ago class-foogallery-widget.php 2 years ago class-foogallery.php 2 years ago class-gallery-advanced-settings.php 2 years ago class-il8n.php 2 years ago class-override-thumbnail.php 2 years ago class-posttypes.php 2 years ago class-retina.php 2 years ago class-thumbnail-dimensions.php 2 years ago class-thumbnails.php 2 years ago class-version-check.php 2 years ago constants.php 2 years ago functions.php 2 years ago includes.php 2 years ago index.php 2 years ago render-functions.php 2 years ago
render-functions.php
607 lines
1 <?php
2 /**
3 *
4 * FooGallery helper functions for rendering HTML
5 * Created by Brad Vincent
6 * Date: 11/07/2017
7 *
8 * @since 1.4.0
9 */
10
11 /**
12 * Returns the attachment image source only
13 *
14 * @param FooGalleryAttachment $foogallery_attachment
15 * @param array $args
16 *
17 * @since 1.4.0
18 *
19 * @return string
20 */
21 function foogallery_attachment_html_image_src( $foogallery_attachment, $args = array() ) {
22 return apply_filters( 'foogallery_attachment_resize_thumbnail', $foogallery_attachment->url, $args, $foogallery_attachment );
23 }
24
25 /**
26 * Returns the attachment img HTML
27 *
28 * @param FooGalleryAttachment $foogallery_attachment
29 * @param array $args
30 *
31 * @since 1.4.0
32 *
33 * @return string
34 */
35 function foogallery_attachment_html_image( $foogallery_attachment, $args = array() ) {
36 $attr = foogallery_build_attachment_html_image_attributes( $foogallery_attachment, $args );
37
38 $html = foogallery_html_opening_tag( 'img', $attr );
39
40 return apply_filters( 'foogallery_attachment_html_image', $html, $args, $foogallery_attachment );
41 }
42
43 /**
44 * Returns the attachment img HTML
45 *
46 * @param FooGalleryAttachment $foogallery_attachment
47 * @param array $args
48 *
49 * @since 1.4.9
50 *
51 * @return array
52 */
53 function foogallery_build_attachment_html_image_attributes( $foogallery_attachment, $args = array() ) {
54 $attr['src'] = foogallery_process_image_url( foogallery_attachment_html_image_src( $foogallery_attachment, $args ) );
55
56 if ( ! empty( $foogallery_attachment->alt ) ) {
57 $attr['alt'] = $foogallery_attachment->alt;
58 }
59
60 if ( ! empty( $foogallery_attachment->caption ) ) {
61 $attr['title'] = $foogallery_attachment->caption;
62 }
63
64 //pull any custom attributes out the args
65 if ( isset( $args['image_attributes'] ) && is_array( $args['image_attributes'] ) ) {
66 $attr = array_merge( $attr, $args['image_attributes'] );
67 }
68
69 //check for width and height args and add those to the image
70 if ( isset( $args['width'] ) && intval( $args['width'] ) > 0 ) {
71 $attr['width'] = $args['width'];
72 }
73 if ( isset( $args['height'] ) && intval( $args['height'] ) > 0 ) {
74 $attr['height'] = $args['height'];
75 }
76
77 $attr = apply_filters( 'foogallery_attachment_html_image_attributes', $attr, $args, $foogallery_attachment );
78
79 if ( array_key_exists( 'class', $attr ) ) {
80 $attr['class'] .= ' fg-image';
81 } else {
82 $attr['class'] = 'fg-image';
83 }
84
85 // Always output the loading attribute on the img tags.
86 $attr['loading'] = 'eager';
87
88 return $attr;
89 }
90
91 /**
92 * Returns the attachment anchor HTML opening tag
93 *
94 * @param FooGalleryAttachment $foogallery_attachment
95 * @param array $args
96 *
97 * @since 1.4.0
98 *
99 * @return string
100 */
101 function foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args = array() ) {
102 $attr = foogallery_build_attachment_html_anchor_attributes( $foogallery_attachment, $args );
103
104 $html = foogallery_html_opening_tag( 'a', $attr );
105
106 return apply_filters( 'foogallery_attachment_html_anchor_opening', $html, $args, $foogallery_attachment );
107 }
108
109 /**
110 * Returns the array of attributes that will be used on the anchor for a FooGalleryAttachment
111 *
112 * @param FooGalleryAttachment $foogallery_attachment
113 * @param array $args
114 *
115 * @since 1.4.9
116 *
117 * @return array
118 */
119 function foogallery_build_attachment_html_anchor_attributes( $foogallery_attachment, $args = array() ) {
120 $arg_defaults = array(
121 'link' => 'image',
122 'custom_link' => $foogallery_attachment->custom_url
123 );
124
125 $args = wp_parse_args( $args, $arg_defaults );
126
127 $link = $args['link'];
128
129 if ( 'page' === $link ) {
130 // get the URL to the attachment page.
131 $url = get_attachment_link( $foogallery_attachment->ID );
132 } elseif ( 'custom' === $link ) {
133 $url = $args['custom_link'];
134 } else {
135 $url = $foogallery_attachment->url;
136 }
137
138 // fallback for images that might not have a custom url.
139 if ( empty( $url ) ) {
140 $url = $foogallery_attachment->url;
141 }
142
143 $attr = array();
144
145 // only add href and target attributes to the anchor if the link is NOT set to 'none'.
146 if ( 'none' !== $link ) {
147 $attr['href'] = foogallery_process_image_url( $url );
148 if ( ! empty( $foogallery_attachment->custom_target ) && 'default' !== $foogallery_attachment->custom_target ) {
149 $attr['target'] = $foogallery_attachment->custom_target;
150 }
151 }
152
153 if ( ! empty( $foogallery_attachment->caption ) ) {
154 $attr['data-caption-title'] = foogallery_sanitize_html( $foogallery_attachment->caption );
155 }
156
157 if ( ! empty( $foogallery_attachment->description ) ) {
158 $attr['data-caption-desc'] = foogallery_sanitize_html( $foogallery_attachment->description );
159 }
160
161 if ( isset( $foogallery_attachment->caption_title ) ) {
162 $attr['data-caption-title'] = foogallery_sanitize_html( $foogallery_attachment->caption_title );
163 }
164
165 if ( isset( $foogallery_attachment->caption_desc ) ) {
166 $attr['data-caption-desc'] = foogallery_sanitize_html( $foogallery_attachment->caption_desc );
167 }
168
169 // set the ID attribute for the attachment.
170 if ( $foogallery_attachment->ID > 0 ) {
171 $attribute_key = foogallery_get_setting( 'attachment_id_attribute', 'data-attachment-id' );
172 $attr[ $attribute_key ] = $foogallery_attachment->ID;
173 }
174
175 // pull any custom attributes out the args.
176 if ( isset( $args['link_attributes'] ) && is_array( $args['link_attributes'] ) ) {
177 $attr = array_merge( $attr, $args['link_attributes'] );
178 }
179
180 $attr = apply_filters( 'foogallery_attachment_html_link_attributes', $attr, $args, $foogallery_attachment );
181
182 // always add the fg-thumb class.
183 if ( array_key_exists( 'class', $attr ) && ! empty( $attr['class'] ) ) {
184 $attr['class'] .= ' fg-thumb';
185 } else {
186 $attr['class'] = 'fg-thumb';
187 }
188
189 return $attr;
190 }
191
192 /**
193 * Returns the attachment anchor HTML
194 *
195 * @param FooGalleryAttachment $foogallery_attachment
196 * @param array $args
197 * @param bool $output_image
198 * @param bool $output_closing_tag
199 *
200 * @since 1.4.0
201 *
202 * @return string
203 */
204 function foogallery_attachment_html_anchor( $foogallery_attachment, $args = array(), $output_image = true, $output_closing_tag = true ) {
205 if ( empty ( $foogallery_attachment->url ) ) {
206 return '';
207 }
208
209 $html = foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args );
210
211 if ( $output_image ) {
212 $html .= foogallery_attachment_html_image( $foogallery_attachment, $args );;
213 }
214
215 if ( $output_closing_tag ) {
216 $html .= '</a>';
217 }
218
219 return apply_filters( 'foogallery_attachment_html_anchor', $html, $args, $foogallery_attachment );
220 }
221
222 /**
223 * Builds up the captions for an attachment
224 *
225 * @param FooGalleryAttachment $foogallery_attachment
226 * @param array $args
227 *
228 * @since 1.4.9
229 *
230 * @return array|bool
231 */
232 function foogallery_build_attachment_html_caption( &$foogallery_attachment, $args = array() ) {
233
234 //allow for custom captions to be set
235 $captions = apply_filters( 'foogallery_build_attachment_html_caption_custom', false, $foogallery_attachment, $args);
236
237 if ( false === $captions ) {
238
239 $captions = array();
240
241 $preset = foogallery_gallery_template_setting( 'caption_preset', 'fg-custom' );
242
243 if ( 'none' !== $preset ) {
244
245 $show_caption_title = false;
246 $show_caption_desc = false;
247
248 $caption_title_source = foogallery_gallery_template_setting( 'caption_title_source', '' );
249
250 //if we need to use the settings, then make sure our source is false
251 if ( empty( $caption_title_source ) ) {
252 $caption_title_source = false;
253 }
254
255 if ( 'fg-custom' === $preset ) {
256 $show_caption_title = $caption_title_source !== 'none';
257 } else {
258 //always show both title and desc for the presets
259 $show_caption_title = true;
260 }
261
262 //get the correct captions
263 if ( $foogallery_attachment->_post ) {
264 $caption_title = foogallery_get_caption_title_for_attachment($foogallery_attachment->_post, $caption_title_source);
265 } else {
266 $caption_title = foogallery_get_caption_by_source($foogallery_attachment, $caption_title_source, 'title');
267 }
268
269 $caption_desc_source = foogallery_gallery_template_setting('caption_desc_source', '');
270
271 //if we need to use the settings, then make sure our source is false
272 if ( empty( $caption_desc_source ) ) {
273 $caption_desc_source = false;
274 }
275
276 if ( 'fg-custom' === $preset ) {
277 $show_caption_desc = $caption_desc_source !== 'none';
278 } else {
279 //always show both title and desc for the presets
280 $show_caption_desc = true;
281 }
282
283 if ( $foogallery_attachment->_post ) {
284 $caption_desc = foogallery_get_caption_desc_for_attachment($foogallery_attachment->_post, $caption_desc_source);
285 } else {
286 $caption_desc = foogallery_get_caption_by_source( $foogallery_attachment, $caption_desc_source, 'desc' );
287 }
288
289 if ( $caption_title && $show_caption_title ) {
290 $captions['title'] = $foogallery_attachment->caption_title = $caption_title;
291 }
292 if ( $caption_desc && $show_caption_desc ) {
293 $captions['desc'] = $foogallery_attachment->caption_desc = $caption_desc;
294 }
295
296 } else {
297 $captions = false;
298 }
299 }
300
301 //extra sanitization for HTML captions
302 if ( isset( $args['override_caption_title'] ) ) {
303 $captions['override_title'] = foogallery_sanitize_html( $args['override_caption_title'] );
304 }
305 if ( isset( $args['override_caption_desc']) ) {
306 $captions['override_desc'] = foogallery_sanitize_html( $args['override_caption_desc'] );
307 }
308
309 //extra sanitization for HTML captions
310 if ( !empty( $captions['title']) ) {
311 $captions['title'] = foogallery_sanitize_html( $captions['title'] );
312 }
313 if ( !empty( $captions['desc']) ) {
314 $captions['desc'] = foogallery_sanitize_html( $captions['desc'] );
315 }
316
317 return apply_filters( 'foogallery_build_attachment_html_caption', $captions, $foogallery_attachment, $args );
318 }
319
320 /**
321 * Returns generic html for captions
322 *
323 * @param FooGalleryAttachment $foogallery_attachment
324 * @param array $args
325 *
326 * @since 1.4.0
327 *
328 * @return string
329 */
330 function foogallery_attachment_html_caption( $foogallery_attachment, $args = array() ) {
331 $captions = foogallery_build_attachment_html_caption( $foogallery_attachment, $args );
332 $html = '';
333
334 if ( $captions !== false ) {
335
336 $caption_title = null;
337 $caption_desc = null;
338
339 $html = '<figcaption class="fg-caption"><div class="fg-caption-inner">';
340
341 if ( array_key_exists( 'override_title', $captions ) ) {
342 $caption_title = $captions['override_title'];
343 } else if ( array_key_exists( 'title', $captions ) ) {
344 $caption_title = $captions['title'];
345 }
346 if ( array_key_exists( 'override_desc', $captions ) ) {
347 $caption_desc = $captions['override_desc'];
348 } else if ( array_key_exists( 'desc', $captions ) ) {
349 $caption_desc = $captions['desc'];
350 }
351
352 if ( !empty( $caption_title ) ) {
353 $html .= '<div class="fg-caption-title">' . $caption_title . '</div>';
354 }
355 if ( !empty( $caption_desc ) ) {
356 $html .= '<div class="fg-caption-desc">' . $caption_desc . '</div>';
357 }
358
359 $html .= '</div></figcaption>';
360 }
361
362 return apply_filters( 'foogallery_attachment_html_caption', $html, $foogallery_attachment, $args );
363 }
364
365 /**
366 * Returns the attachment item opening HTML
367 *
368 * @param FooGalleryAttachment $foogallery_attachment
369 * @param array $args
370 *
371 * @since 1.4.0
372 *
373 * @return string
374 */
375 function foogallery_attachment_html_item_opening($foogallery_attachment, $args = array() ) {
376
377 $classes[] = 'fg-item';
378
379 //set the type of an item
380 $classes['type'] = 'fg-type-' . $foogallery_attachment->type;
381
382 //let others add to the item classes
383 $classes = apply_filters( 'foogallery_attachment_html_item_classes', $classes, $foogallery_attachment, $args );
384
385 $class_list = '';
386 if ( is_array( $classes ) ) {
387 $class_list = implode( ' ', $classes );
388 }
389
390 $attachment_item_figure_class = apply_filters( 'foogallery_attachment_html_item_figure_class', 'fg-item-inner', $foogallery_attachment, $args );
391 $html = '<div class="' . $class_list . '"><figure class="'. esc_attr( $attachment_item_figure_class ) . '">';
392 return apply_filters( 'foogallery_attachment_html_item_opening', $html, $foogallery_attachment, $args );
393 }
394
395 /**
396 * Returns generic html for an attachment
397 *
398 * @param FooGalleryAttachment $foogallery_attachment
399 * @param array $args
400 *
401 * @since 1.4.0
402 *
403 * @return string
404 */
405 function foogallery_attachment_html( $foogallery_attachment, $args = array() ) {
406
407 //check if no arguments were passed in, and build them up if so
408 if ( empty( $args ) ) {
409 $args = foogallery_gallery_template_arguments();
410 }
411
412 //allow width and height arguments to be overridden
413 $override_width = foogallery_gallery_template_setting( 'override_width', false );
414 if ( $override_width !== false && intval( $override_width ) > 0 ) {
415 $args['width'] = $override_width;
416 }
417 $override_height = foogallery_gallery_template_setting( 'override_height', false );
418 if ( $override_height !== false && intval( $override_height ) > 0 ) {
419 $args['height'] = $override_height;
420 }
421
422 $caption = foogallery_attachment_html_caption( $foogallery_attachment, $args );
423
424 $html = foogallery_attachment_html_item_opening( $foogallery_attachment, $args );
425 $html .= foogallery_attachment_html_anchor_opening( $foogallery_attachment, $args );
426 $html .= '<span class="fg-image-wrap">';
427 $html .= foogallery_attachment_html_image( $foogallery_attachment, $args );
428 $html .= '</span>';
429 $html .= '<span class="fg-image-overlay"></span>';
430 $html .= '</a>';
431 $html .= $caption;
432 $html .= '</figure><div class="fg-loader"></div></div>';
433 return $html;
434 }
435
436 /**
437 * Get the foogallery template arguments for the current foogallery that is being output to the frontend
438 *
439 * @return array
440 */
441 function foogallery_gallery_template_arguments() {
442 global $current_foogallery_template;
443
444 return apply_filters( 'foogallery_gallery_template_arguments-' . $current_foogallery_template, array() );
445 }
446
447 /**
448 * Build up an object that will be encoded to JSON for a FooGallery Attachment
449 *
450 * @param FooGalleryAttachment $foogallery_attachment
451 * @param array $args
452 *
453 * @since 1.6.0
454 *
455 * @returns string
456 * @return string
457 */
458 function foogallery_build_json_object_from_attachment( $foogallery_attachment, $args = array() ) {
459 if ( isset( $foogallery_attachment ) ) {
460
461 //check if no arguments were passed in, and build them up if so
462 if ( empty( $args ) ) {
463 $args = foogallery_gallery_template_arguments();
464 }
465
466 $captions = foogallery_build_attachment_html_caption( $foogallery_attachment, $args );
467 $anchor_attributes = foogallery_build_attachment_html_anchor_attributes( $foogallery_attachment, $args );
468 $image_attributes = foogallery_build_attachment_html_image_attributes( $foogallery_attachment, $args );
469
470 if ( array_key_exists( 'data-src-fg', $image_attributes ) ) {
471 $src = $image_attributes['data-src-fg'];
472 } else if (array_key_exists( 'src', $image_attributes ) ) {
473 $src = $image_attributes['src'];
474 }
475
476 if ( array_key_exists( 'data-srcset-fg', $image_attributes ) ) {
477 $srcset = $image_attributes['data-srcset-fg'];
478 } else if ( array_key_exists( 'srcset', $image_attributes ) ) {
479 $srcset = $image_attributes['srcset'];
480 }
481
482 $json_object = new stdClass();
483
484 if ( array_key_exists( 'href', $anchor_attributes ) ) {
485 $json_object->href = $anchor_attributes['href'];
486 }
487 if ( array_key_exists( 'data-type', $anchor_attributes ) ) {
488 $json_object->type = $anchor_attributes['data-type'];
489 }
490 if ( isset( $src ) ) {
491 $json_object->src = $src;
492 }
493 if ( isset( $srcset ) ) {
494 $json_object->srcset = $srcset;
495 }
496 if ( array_key_exists( 'width', $image_attributes ) ) {
497 $json_object->width = $image_attributes['width'];
498 }
499 if ( array_key_exists( 'height', $image_attributes ) ) {
500 $json_object->height = $image_attributes['height'];
501 }
502
503 $json_object->alt = $foogallery_attachment->alt;
504
505 if ( $foogallery_attachment->ID > 0 ) {
506 $json_object->id = $foogallery_attachment->ID;
507 }
508
509 if ( $captions !== false ) {
510 if ( array_key_exists( 'title', $captions ) ) {
511 $json_object->caption = $json_object->title = $captions['title'];
512 }
513 if ( array_key_exists( 'desc', $captions ) ) {
514 $json_object->description = $captions['desc'];
515 }
516 }
517
518 $json_object->attr = new stdClass();
519 $json_object->attr->anchor = foogallery_create_anchor_for_json_object( $anchor_attributes );
520
521 $json_object = apply_filters( 'foogallery_build_attachment_json', $json_object, $foogallery_attachment, $args, $anchor_attributes, $image_attributes, $captions );
522
523 return $json_object;
524 }
525
526 return false;
527 }
528
529 /**
530 * Build up the anchor object that is used within the json object
531 *
532 * @param $anchor_attributes
533 *
534 * @return stdClass
535 */
536 function foogallery_create_anchor_for_json_object( $anchor_attributes ) {
537 //unset a number of keys in the array that are already set on the high-level item
538 unset( $anchor_attributes['href'] );
539 unset( $anchor_attributes['data-type'] );
540
541 //create the anchor object
542 $object = new stdClass();
543
544 //loop through the anchor attributes and set them on the object
545 foreach ( $anchor_attributes as $key => $value ) {
546 $object->{$key} = $value;
547 }
548 return $object;
549 }
550
551 /**
552 * Build up a JSON string for a FooGallery Attachment
553 *
554 * @param FooGalleryAttachment $foogallery_attachment
555 * @param array $args
556 *
557 * @since 1.4.9
558 *
559 * @returns string
560 * @return string
561 */
562 function foogallery_build_json_from_attachment( $foogallery_attachment, $args = array() ) {
563 if ( isset( $foogallery_attachment ) ) {
564
565 $json_object = foogallery_build_json_object_from_attachment( $foogallery_attachment, $args );
566
567 return foogallery_json_encode( $json_object );
568 }
569
570 return '';
571 }
572
573 /**
574 * Renders a script block with the JSON output for the attachments in a gallery
575 *
576 * @param $gallery FooGallery
577 * @param $attachments FooGalleryAttachment[]
578 */
579 function foogallery_render_script_block_for_json_items( $gallery, $attachments ) {
580 if ( count( $attachments ) > 0 ) {
581 $attachments_json = array_map( 'foogallery_build_json_from_attachment', $attachments );
582 echo '<script type="text/javascript">';
583 echo ' window["' . $gallery->container_id() . '_items"] = [';
584 echo implode( ', ', $attachments_json );
585 echo ' ];';
586 echo '</script>';
587 }
588 }
589
590 /**
591 * Generates the HTML for a tag
592 *
593 * @param $tag
594 * @param $attributes
595 *
596 * @return string
597 */
598 function foogallery_html_opening_tag( $tag, $attributes ) {
599 $html = '<' . $tag;
600 foreach ( $attributes as $name => $value ) {
601 if ( empty( $name ) || empty( $value ) ) continue;
602 $name = str_replace(' ', '', $name); //ensure we have no spaces!
603 $html .= " $name=" . '"' . foogallery_esc_attr($value) . '"';
604 }
605 $html .= '>';
606 return $html;
607 }