| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gallery Image Function. |
| 4 |
* |
| 5 |
* Builds the front-end image gallery markup for a post or WooCommerce product. |
| 6 |
* WooCommerce products read their images from the standard product gallery meta, |
| 7 |
* while other post types use the theme's own "wpstream_theme_gallery" Meta Box |
| 8 |
* field. Output is rendered as FancyBox-enabled thumbnail grids. |
| 9 |
* |
| 10 |
* @package wpstream-theme |
| 11 |
*/ |
| 12 |
|
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
// Guard against redeclaration when function names overlap. |
| 20 |
if ( ! function_exists( 'wpstream_theme_image_gallery' ) ) { |
| 21 |
/** |
| 22 |
* Generate an image gallery HTML. |
| 23 |
* |
| 24 |
* @param int $post_id The ID of the post. |
| 25 |
* @param string $full_size The size of the full-size images. |
| 26 |
* @param int|null $images_count Optional cap on how many product gallery images to include. |
| 27 |
* @return string Returns the HTML for the image gallery. |
| 28 |
*/ |
| 29 |
function wpstream_theme_image_gallery( $post_id, $full_size = 'full', $images_count = null ) { |
| 30 |
// Get the post type. |
| 31 |
$post_type = get_post_type( $post_id ); |
| 32 |
|
| 33 |
// Products source their gallery from WooCommerce meta; everything else uses the theme field. |
| 34 |
if ( 'product' === $post_type ) { |
| 35 |
// If the post type is 'product', retrieve gallery images using WooCommerce custom field. |
| 36 |
// Featured image (thumbnail) becomes the first/hero image of the gallery. |
| 37 |
$product_image = get_post_meta( $post_id, '_thumbnail_id', true ); |
| 38 |
// Comma-separated list of additional product gallery attachment IDs. |
| 39 |
$gallery_images_source = get_post_meta( $post_id, '_product_image_gallery', true ); |
| 40 |
// get only a specific number of images, based on $images_count |
| 41 |
if ( $images_count && $gallery_images_source ) { |
| 42 |
// Split the CSV into an array of IDs. |
| 43 |
$gallery_images = explode( ',', $gallery_images_source ); |
| 44 |
// Limit to the requested number of images. |
| 45 |
$gallery_images = array_slice( $gallery_images, 0, $images_count ); |
| 46 |
// Put the featured image at the front of the list. |
| 47 |
$gallery_images = array_merge( array( $product_image ), $gallery_images ); |
| 48 |
} else { |
| 49 |
// No cap (or no gallery meta): use just the featured image. |
| 50 |
$gallery_images = array( $product_image ); |
| 51 |
} |
| 52 |
// Products use the dedicated single-product FancyBox layout (hero + row). |
| 53 |
return wpstream_theme_single_product_generate_fancybox( $gallery_images, $full_size ); |
| 54 |
} else { |
| 55 |
// For other post types, retrieve gallery images using the 'wpstream_theme_gallery' custom field. |
| 56 |
$gallery_images = null; |
| 57 |
// The gallery field is provided by the Meta Box plugin; only read it if available. |
| 58 |
if(function_exists('rwmb_meta')){ |
| 59 |
// rwmb_meta returns an id => attachment map for image_advanced fields. |
| 60 |
$gallery_images = rwmb_meta( 'wpstream_theme_gallery', array(), $post_id ); |
| 61 |
// Reduce the map down to a flat list of attachment IDs. |
| 62 |
$gallery_images = array_keys( $gallery_images ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
// Print a section heading only when there are images to show. |
| 67 |
if ( ! empty( $gallery_images ) ) { |
| 68 |
print '<h2 class="mb-30">' . esc_html__( 'Image Gallery', 'hello-wpstream' ) . '</h2>'; |
| 69 |
} |
| 70 |
|
| 71 |
// Generate the gallery HTML and return it as a string. |
| 72 |
return wpstream_theme_image_generate_fancybox( $gallery_images, $full_size ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! function_exists( 'wpstream_theme_image_generate_fancybox' ) ) { |
| 77 |
/** |
| 78 |
* Generate HTML for a gallery using FancyBox. |
| 79 |
* |
| 80 |
* @param array $image_ids Array of attachment IDs for the images in the gallery. |
| 81 |
* @param string $full_size The size of the full-size images. |
| 82 |
* @return string Returns the HTML for the gallery. |
| 83 |
*/ |
| 84 |
function wpstream_theme_image_generate_fancybox( $image_ids, $full_size = 'full' ) { |
| 85 |
// Nothing to render when there are no attachment IDs. |
| 86 |
if ( empty( $image_ids ) ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
// Open the gallery wrapper and a Bootstrap row. |
| 91 |
$output = '<div class="wpstream_theme_image_generate_fancybox">'; |
| 92 |
$output .= '<div class="row">'; |
| 93 |
// Generate links to open images in FancyBox. |
| 94 |
foreach ( $image_ids as $image_id ) { |
| 95 |
// Card-sized thumbnail source used for the visible <img>. |
| 96 |
$wpstream_featured_unit_cards = wp_get_attachment_image_src( $image_id, 'wpstream_featured_unit_cards' ); |
| 97 |
// Full-size source used for the FancyBox lightbox link. |
| 98 |
$image_src = wp_get_attachment_image_src( $image_id, $full_size ); |
| 99 |
// Alt text pulled from the attachment meta. |
| 100 |
$image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true ); |
| 101 |
// Open the responsive grid column for this item. |
| 102 |
$output .= '<div class="col-6 col-sm-6 col-md-4 col-lg-3 wpstream-gallery-items">'; |
| 103 |
// Only render the link/image when a valid source URL exists. |
| 104 |
if ( isset( $image_src[0] ) ) { |
| 105 |
|
| 106 |
// Zoom-in overlay icon shown on hover. |
| 107 |
$output .='<div class="wpstream_video_unit_video_play">'.wpstream_theme_get_svg_icon('zoom-in.svg').'</div>'; |
| 108 |
|
| 109 |
|
| 110 |
// FancyBox link pointing at the full-size image. |
| 111 |
$output .= '<a href="' . $image_src[0] . '" rel="data-fancybox-thumb" data-fancybox="gallery">'; |
| 112 |
// Hover overlay element. |
| 113 |
$output .='<div class="wpstream_video_unit_overlay"></div>'; |
| 114 |
// Visible thumbnail image. |
| 115 |
$output .= '<img class="w-100 h-100 rounded-3" src="' . $wpstream_featured_unit_cards[0] . '" alt="' . $image_alt . '">'; |
| 116 |
// Close the FancyBox link. |
| 117 |
$output .= '</a>'; |
| 118 |
} |
| 119 |
// Close this grid column. |
| 120 |
$output .= '</div>'; |
| 121 |
} |
| 122 |
// Close the row and gallery wrapper. |
| 123 |
$output .= '</div>'; |
| 124 |
$output .= '</div>'; |
| 125 |
|
| 126 |
return $output; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if ( ! function_exists( 'wpstream_theme_single_product_generate_fancybox' ) ) { |
| 131 |
/** |
| 132 |
* Generate HTML for a gallery using FancyBox. |
| 133 |
* |
| 134 |
* @param array $image_ids Array of attachment IDs for the images in the gallery. |
| 135 |
* @param string $full_size The size of the full-size images. |
| 136 |
* @return string Returns the HTML for the gallery. |
| 137 |
*/ |
| 138 |
function wpstream_theme_single_product_generate_fancybox( $image_ids, $full_size = 'full' ) { |
| 139 |
// Open the gallery wrapper. |
| 140 |
$output = '<div class="wpstream_theme_image_generate_fancybox">'; |
| 141 |
|
| 142 |
// Generate the first item |
| 143 |
// Hero image: full width when it is the only image, otherwise the wide 9-column slot. |
| 144 |
$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' ); |
| 145 |
|
| 146 |
// Generate the rest of the gallery images |
| 147 |
// Drop the hero image already rendered above. |
| 148 |
array_shift( $image_ids ); |
| 149 |
// Render any remaining images in a side/secondary row. |
| 150 |
if ( ! empty( $image_ids ) ) { |
| 151 |
$output .= '<div class="row col-6 col-lg-3 col-md-12 col-sm-12">'; |
| 152 |
// One FancyBox thumbnail per remaining image. |
| 153 |
foreach ( $image_ids as $image_id ) { |
| 154 |
$output .= wpstream_generate_fancybox_image_html( $image_id, $full_size, 'col-6 col-sm-6 col-md-4 col-lg-3' ); |
| 155 |
} |
| 156 |
$output .= '</div>'; |
| 157 |
} |
| 158 |
|
| 159 |
// Close the gallery wrapper. |
| 160 |
$output .= '</div>'; |
| 161 |
|
| 162 |
return $output; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// Guard against redeclaration. |
| 167 |
if ( ! function_exists( 'wpstream_generate_fancybox_image_html') ) { |
| 168 |
/** |
| 169 |
* Build the FancyBox markup for a single gallery image. |
| 170 |
* |
| 171 |
* @param int $image_id Attachment ID of the image. |
| 172 |
* @param string $full_size Registered image size to use for the source. |
| 173 |
* @param string $class Extra CSS classes for the item wrapper (column sizing). |
| 174 |
* @return string The item HTML, or '' when the image source is missing. |
| 175 |
*/ |
| 176 |
function wpstream_generate_fancybox_image_html( $image_id, $full_size, $class ) { |
| 177 |
// Thumbnail source for the visible <img> (same size used for both here). |
| 178 |
$wpstream_featured_unit_cards = wp_get_attachment_image_src( $image_id, $full_size ); |
| 179 |
// Source used for the FancyBox link target. |
| 180 |
$image_src = wp_get_attachment_image_src( $image_id, $full_size ); |
| 181 |
// Attachment alt text. |
| 182 |
$image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true ); |
| 183 |
|
| 184 |
// Skip images without a resolvable source. |
| 185 |
if ( ! isset( $image_src[0] ) ) { |
| 186 |
return ''; |
| 187 |
} |
| 188 |
|
| 189 |
// Item wrapper with caller-supplied column classes. |
| 190 |
$output = '<div class="wpstream-gallery-items ' . esc_attr( $class ) . '">'; |
| 191 |
// Zoom-in hover icon. |
| 192 |
$output .= '<div class="wpstream_video_unit_video_play">' . wpstream_theme_get_svg_icon('zoom-in.svg') . '</div>'; |
| 193 |
// FancyBox link to the full image (URL escaped). |
| 194 |
$output .= '<a href="' . esc_url( $image_src[0] ) . '" rel="data-fancybox-thumb" data-fancybox="gallery">'; |
| 195 |
// Hover overlay element. |
| 196 |
$output .= '<div class="wpstream_video_unit_overlay"></div>'; |
| 197 |
// Visible thumbnail image (source and alt escaped). |
| 198 |
$output .= '<img class="w-100 h-100 rounded-3" src="' . esc_url( $wpstream_featured_unit_cards[0] ) . '" alt="' . esc_attr( $image_alt ) . '">'; |
| 199 |
// Close the link and wrapper. |
| 200 |
$output .= '</a>'; |
| 201 |
$output .= '</div>'; |
| 202 |
|
| 203 |
return $output; |
| 204 |
} |
| 205 |
} |