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