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