From 474109a5df0a06f5343ab184838fe2d80e3872a8 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Sun, 11 Jan 2026 19:23:20 +0000
Subject: [PATCH] =Fixed timeline CRUD.js issue where this.activeItem was set null when we still needed it
---
inc/utility/Image.php | 67 +++++++++++++++++++++++++--------
1 files changed, 50 insertions(+), 17 deletions(-)
diff --git a/inc/utility/Image.php b/inc/utility/Image.php
index 8ecbd83..d9a9515 100644
--- a/inc/utility/Image.php
+++ b/inc/utility/Image.php
@@ -21,41 +21,74 @@
public function formatImage(int $ID, string $start = 'tiny', string $replace = 'large', bool $addLink = true, ?string $postSlug = null):string
{
- $return = $this->cache->remember(
+ $return = $this->cache->remember(
['ID' => $ID, 'start' => $start, 'replace' => $replace],
function() use ($ID, $start, $replace) {
- $img = wp_get_attachment_image_src($ID, $start);
- if (!$img) {
- return'';
+ // Define size order for progressive enhancement
+ $sizeOrder = ['tiny', 'medium', 'large', 'full'];
+ $startIndex = array_search($start, $sizeOrder);
+ $replaceIndex = array_search($replace, $sizeOrder);
+
+ // Fallback if invalid sizes provided
+ if ($startIndex === false) $startIndex = 0;
+ if ($replaceIndex === false) $replaceIndex = 2;
+
+ // Get all images up to the replace size
+ $images = [];
+ for ($i = $startIndex; $i <= $replaceIndex; $i++) {
+ $img = wp_get_attachment_image_src($ID, $sizeOrder[$i]);
+ if ($img) {
+ $images[$sizeOrder[$i]] = $img;
+ }
}
- $img = $img[0];
- $data = $this->getGallerySizes($ID, $replace);
+ if (empty($images)) return '';
-
-
+ // Use first available image as src
+ $firstImage = reset($images);
$alt = get_post_meta($ID, '_wp_attachment_image_alt', true);
- $alt = ($alt=='')? '' : ' alt="'.$alt.'" ';
- return '<img width="100%" height="auto" src="'.$img.'"'.$alt.$data.' loading="lazy" decoding="async">';
+ $alt = ($alt=='')? '' : ' alt="'.esc_attr($alt).'" ';
+
+ // Build srcset only with images from start to replace
+ $srcsetParts = [];
+ foreach ($images as $img) {
+ $srcsetParts[] = sprintf('%s %dw', $img[0], $img[1]);
+ }
+ $srcset = implode(', ', $srcsetParts);
+
+ return sprintf(
+ '<img src="%s"%s srcset="%s" sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 800px" loading="lazy" decoding="async">',
+ $firstImage[0],
+ $alt,
+ $srcset
+ );
}
);
- $aOpen = $aClose = '';
if ($addLink) {
if (!$postSlug) {
global $post;
$postSlug = $post->post_name;
}
+ $full = wp_get_attachment_image_src($ID, 'full');
$imgPost = get_post($ID);
- if (!$imgPost) {
- return $return;
- }
+ if (!$imgPost) return $return;
+
$imgSlug = $imgPost->post_name;
- $aOpen = '<a class="open-gallery" target="_blank" rel="nofollow" data-opens="gallery-'.$postSlug.'" data-focus="'.$postSlug.'-'.$imgSlug.'">';
- $aClose = '</a>';
+ $galleryAttrs = sprintf(
+ ' data-gallery="gallery-%s" data-focus="%s-%s" data-full="%s"',
+ $postSlug,
+ $postSlug,
+ $imgSlug,
+ $full[0]
+ );
+
+ // Add gallery attributes to img tag
+ $return = str_replace('<img', '<img'.$galleryAttrs, $return);
}
- return $aOpen.$return.$aClose;
+
+ return $return;
}
public function getGallerySizes(int $ID, string $replace):string
--
Gitblit v1.10.0