Skip to content

Commit

Permalink
Add workaround to cope with image links in JetPack tiled gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
arnowelzel committed Apr 4, 2024
1 parent 8b5d848 commit fb954a8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lightbox-photoswipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Lightbox with PhotoSwipe
Plugin URI: https://wordpress.org/plugins/lightbox-photoswipe/
Description: Lightbox with PhotoSwipe
Version: 5.2.5
Version: 5.2.6
Author: Arno Welzel
Author URI: http://arnowelzel.de
Text Domain: lightbox-photoswipe
Expand Down
8 changes: 6 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Contributors: awelzel
Tags: lightbox, photoswipe, attachments, images, gallery
Requires at least: 5.3
Tested up to: 6.4
Stable tag: 5.2.5
Stable tag: 5.2.6
Donate link: https://paypal.me/ArnoWelzel
License: GPLv2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -151,9 +151,13 @@ If you change any of the stylesheets or frontend scripts in `src/js` or `src/lib

== Changelog ==

= 5.2.6 =

* Additional workarounds to deal with image links and captions in JetPack tiled galleries.

= 5.2.5 =

* Fixed handling of image rotation when using original size images using the option .
* Fixed handling of image rotation when using original size images using the option.

= 5.2.4 =

Expand Down
28 changes: 23 additions & 5 deletions src/LightboxPhotoSwipe/LightboxPhotoSwipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
class LightboxPhotoSwipe
{
const VERSION = '5.2.5';
const VERSION = '5.2.6';
const SLUG = 'lightbox-photoswipe';
const META_VERSION = '18';
const CACHE_EXPIRE_IMG_DETAILS = 86400;
Expand Down Expand Up @@ -421,19 +421,30 @@ public function callbackProperties(array $matches)
$file = $realFile;
}

// Keep original file name for metadata retrieval
// Keep original file name variations for metadata retrieval
$fileOriginal = $file;
$fileOriginalNoScaled = $file;
$fileOriginalNoSize = $file;
$fileOriginalScaled = $file;

// If the "fix image links" option is set, try to remove size parameters from the image link.
// For example: "image-1024x768.jpg" will become "image.jpg"
$sizeMatcher = '/(-[0-9]+x[0-9]+\.)(?:.(?!-[0-9]+x[0-9]+\.)).+$/';
$fileFixed = preg_filter($sizeMatcher, '.', $file);
if ($fileFixed !== null && $fileFixed !== $file) {
$fileOriginalScaled = substr($fileFixed, 0, -1) . '-scaled.' . $extension;
}
if ('1' === $this->optionsManager->getOption('fix_links')) {
$fileFixed = preg_filter($sizeMatcher, '.', $file);
if ($fileFixed !== null && $fileFixed !== $file) {
$file = $fileFixed . $extension;
$matches[2] = preg_filter($sizeMatcher, '.', $matches[2]) . $extension;

if ($file !== $fileOriginal) {
$fileOriginalNoSize = $file;
}
}
}

// If the "fix scaled image links" option is set, try to remove "-scaled" from the image link.
// For example: "image-scaled.jpg" will become "image.jpg"
$scaledMatcher = '/(-scaled\.).+$/';
Expand All @@ -442,15 +453,22 @@ public function callbackProperties(array $matches)
if ($fileFixed !== null && $fileFixed !== $file) {
$file = $fileFixed . $extension;
$matches[2] = preg_filter($scaledMatcher, '.', $matches[2]) . $extension;

if ($file !== $fileOriginal) {
$fileOriginalNoScaled = $file;
}
}
}

// Try to get metadata from database
if ('1' === $this->optionsManager->getOption('usepostdata') && '1' === $this->optionsManager->getOption('show_caption')) {
$imgId = $wpdb->get_col(
$wpdb->prepare(
'SELECT post_id FROM '.$wpdb->postmeta.' WHERE meta_key = "_wp_attached_file" and meta_value = %s;',
str_replace ($uploadDir . '/', '', $fileOriginal)
'SELECT post_id FROM '.$wpdb->postmeta.' WHERE meta_key = "_wp_attached_file" and meta_value in (%s, %s, %s, %s);',
str_replace ($uploadDir . '/', '', $fileOriginal),
str_replace ($uploadDir . '/', '', $fileOriginalNoSize),
str_replace ($uploadDir . '/', '', $fileOriginalNoScaled),
str_replace ($uploadDir . '/', '', $fileOriginalScaled),
)
);
if (isset($imgId[0])) {
Expand Down

0 comments on commit fb954a8

Please sign in to comment.