| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gallery Image Function |
| 4 |
* |
| 5 |
* @package wpstream-theme |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! function_exists( 'wpstream_theme_image_gallery' ) ) { |
| 9 |
/** |
| 10 |
* Generate an image gallery HTML. |
| 11 |
* |
| 12 |
* @param int $post_id The ID of the post. |
| 13 |
* @param string $full_size The size of the full-size images. |
| 14 |
* @return string Returns the HTML for the image gallery. |
| 15 |
*/ |
| 16 |
function wpstream_theme_image_gallery( $post_id, $full_size = 'full', $images_count = null ) { |
| 17 |
// Get the post type. |
| 18 |
$post_type = get_post_type( $post_id ); |
| 19 |
|
| 20 |
if ( 'product' === $post_type ) { |
| 21 |
// If the post type is 'product', retrieve gallery images using WooCommerce custom field. |
| 22 |
$product_image = get_post_meta( $post_id, '_thumbnail_id', true ); |
| 23 |
$gallery_images_source = get_post_meta( $post_id, '_product_image_gallery', true ); |
| 24 |
// get only a specific number of images, based on $images_count |
| 25 |
if ( $images_count && $gallery_images_source ) { |
| 26 |
$gallery_images = explode( ',', $gallery_images_source ); |
| 27 |
$gallery_images = array_slice( $gallery_images, 0, $images_count ); |
| 28 |
$gallery_images = array_merge( array( $product_image ), $gallery_images ); |
| 29 |
} else { |
| 30 |
$gallery_images = array( $product_image ); |
| 31 |
} |
| 32 |
return wpstream_theme_single_product_generate_fancybox( $gallery_images, $full_size ); |
| 33 |
} else { |
| 34 |
// For other post types, retrieve gallery images using the 'wpstream_theme_gallery' custom field. |
| 35 |
$gallery_images = null; |
| 36 |
if(function_exists('rwmb_meta')){ |
| 37 |
$gallery_images = rwmb_meta( 'wpstream_theme_gallery', array(), $post_id ); |
| 38 |
$gallery_images = array_keys( $gallery_images ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
if ( ! empty( $gallery_images ) ) { |
| 43 |
print '<h2 class="mb-30">' . esc_html__( 'Image Gallery', 'hello-wpstream' ) . '</h2>'; |
| 44 |
} |
| 45 |
|
| 46 |
// Generate the gallery HTML and return it as a string. |
| 47 |
return wpstream_theme_image_generate_fancybox( $gallery_images, $full_size ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! function_exists( 'wpstream_theme_image_generate_fancybox' ) ) { |
| 52 |
/** |
| 53 |
* Generate HTML for a gallery using FancyBox. |
| 54 |
* |
| 55 |
* @param array $image_ids Array of attachment IDs for the images in the gallery. |
| 56 |
* @param string $full_size The size of the full-size images. |
| 57 |
* @return string Returns the HTML for the gallery. |
| 58 |
*/ |
| 59 |
function wpstream_theme_image_generate_fancybox( $image_ids, $full_size = 'full' ) { |
| 60 |
if ( empty( $image_ids ) ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
$output = '<div class="wpstream_theme_image_generate_fancybox">'; |
| 65 |
$output .= '<div class="row">'; |
| 66 |
// Generate links to open images in FancyBox. |
| 67 |
foreach ( $image_ids as $image_id ) { |
| 68 |
$wpstream_featured_unit_cards = wp_get_attachment_image_src( $image_id, 'wpstream_featured_unit_cards' ); |
| 69 |
$image_src = wp_get_attachment_image_src( $image_id, $full_size ); |
| 70 |
$image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true ); |
| 71 |
$output .= '<div class="col-6 col-sm-6 col-md-4 col-lg-3 wpstream-gallery-items">'; |
| 72 |
if ( isset( $image_src[0] ) ) { |
| 73 |
|
| 74 |
$output .='<div class="wpstream_video_unit_video_play">'.wpstream_theme_get_svg_icon('zoom-in.svg').'</div>'; |
| 75 |
|
| 76 |
|
| 77 |
$output .= '<a href="' . $image_src[0] . '" rel="data-fancybox-thumb" data-fancybox="gallery">'; |
| 78 |
$output .='<div class="wpstream_video_unit_overlay"></div>'; |
| 79 |
$output .= '<img class="w-100 h-100 rounded-3" src="' . $wpstream_featured_unit_cards[0] . '" alt="' . $image_alt . '">'; |
| 80 |
$output .= '</a>'; |
| 81 |
} |
| 82 |
$output .= '</div>'; |
| 83 |
} |
| 84 |
$output .= '</div>'; |
| 85 |
$output .= '</div>'; |
| 86 |
|
| 87 |
return $output; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if ( ! function_exists( 'wpstream_theme_single_product_generate_fancybox' ) ) { |
| 92 |
/** |
| 93 |
* Generate HTML for a gallery using FancyBox. |
| 94 |
* |
| 95 |
* @param array $image_ids Array of attachment IDs for the images in the gallery. |
| 96 |
* @param string $full_size The size of the full-size images. |
| 97 |
* @return string Returns the HTML for the gallery. |
| 98 |
*/ |
| 99 |
function wpstream_theme_single_product_generate_fancybox( $image_ids, $full_size = 'full' ) { |
| 100 |
$output = '<div class="wpstream_theme_image_generate_fancybox">'; |
| 101 |
|
| 102 |
// Generate the first item |
| 103 |
$output .= wpstream_generate_fancybox_image_html( $image_ids[0], 'wpstream_featured_blog_image', count( $image_ids ) === 1 ? 'col-12' : 'col-12 col-sm-12 col-md-12 col-lg-9' ); |
| 104 |
|
| 105 |
// Generate the rest of the gallery images |
| 106 |
array_shift( $image_ids ); |
| 107 |
if ( ! empty( $image_ids ) ) { |
| 108 |
$output .= '<div class="row col-6 col-lg-3 col-md-12 col-sm-12">'; |
| 109 |
foreach ( $image_ids as $image_id ) { |
| 110 |
$output .= wpstream_generate_fancybox_image_html( $image_id, $full_size, 'col-6 col-sm-6 col-md-4 col-lg-3' ); |
| 111 |
} |
| 112 |
$output .= '</div>'; |
| 113 |
} |
| 114 |
|
| 115 |
$output .= '</div>'; |
| 116 |
|
| 117 |
return $output; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
if ( ! function_exists( 'wpstream_generate_fancybox_image_html') ) { |
| 122 |
function wpstream_generate_fancybox_image_html( $image_id, $full_size, $class ) { |
| 123 |
$wpstream_featured_unit_cards = wp_get_attachment_image_src( $image_id, $full_size ); |
| 124 |
$image_src = wp_get_attachment_image_src( $image_id, $full_size ); |
| 125 |
$image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true ); |
| 126 |
|
| 127 |
if ( ! isset( $image_src[0] ) ) { |
| 128 |
return ''; |
| 129 |
} |
| 130 |
|
| 131 |
$output = '<div class="wpstream-gallery-items ' . esc_attr( $class ) . '">'; |
| 132 |
$output .= '<div class="wpstream_video_unit_video_play">' . wpstream_theme_get_svg_icon('zoom-in.svg') . '</div>'; |
| 133 |
$output .= '<a href="' . esc_url( $image_src[0] ) . '" rel="data-fancybox-thumb" data-fancybox="gallery">'; |
| 134 |
$output .= '<div class="wpstream_video_unit_overlay"></div>'; |
| 135 |
$output .= '<img class="w-100 h-100 rounded-3" src="' . esc_url( $wpstream_featured_unit_cards[0] ) . '" alt="' . esc_attr( $image_alt ) . '">'; |
| 136 |
$output .= '</a>'; |
| 137 |
$output .= '</div>'; |
| 138 |
|
| 139 |
return $output; |
| 140 |
} |
| 141 |
} |