]> git.mxchange.org Git - friendica-addons.git/blobdiff - fancybox/fancybox.php
invidious/invidious.php aktualisiert
[friendica-addons.git] / fancybox / fancybox.php
index f3a566a5061a3337a6f650bffb758141c4c6e72a..1a9ff6344ff46205d977fef4129e72dc85fc99bc 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * Name: Fancybox
  * Description: Open media attachments of posts into a fancybox overlay.
- * Version: 1.01
+ * Version: 1.05
  * Author: Grischa Brockhaus <grischa@brockha.us>
  */
 
@@ -17,38 +17,44 @@ function fancybox_install()
        Hook::register('prepare_body_final', __FILE__, 'fancybox_render');
 }
 
-function fancybox_head(App $a, string &$b)
+function fancybox_head(string &$b)
 {
        DI::page()->registerStylesheet(__DIR__ . '/asset/fancybox/jquery.fancybox.min.css');
 }
 
-function fancybox_footer(App $a, string &$str)
+function fancybox_footer(string &$str)
 {
        DI::page()->registerFooterScript(__DIR__ . '/asset/fancybox/jquery.fancybox.min.js');
        DI::page()->registerFooterScript(__DIR__ . '/asset/fancybox/fancybox.config.js');
 }
 
-function fancybox_render(App $a, array &$b)
-{
-       $matches = [];
-       $pattern = '#<div class="body-attach">.*?</div>#s';
-       $gallery = 'gallery';
-       if (array_key_exists('item', $b)) {
-               $item = $b['item'];
-               if (array_key_exists('uri-id', $item)) {
-                       $gallery = $gallery . '-' . $item['uri-id'];
-               }
-       }
-       $html = $b['html'];
-       while (preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE)) {
-               if (is_array($matches)) {
-                       $matches = $matches[0];
+function fancybox_render(array &$b){
+       $gallery = 'gallery-' . $b['item']['uri-id'] ?? random_int(1000000, 10000000);
+
+       // performWithEscapedBlocks escapes block defined with 2nd par pattern that won't be processed.
+       // We don't want to touch images in class="type-link":
+       $b['html'] = \Friendica\Util\Strings::performWithEscapedBlocks(
+               $b['html'],
+               '#<div class="type-link">.*?</div>#s',
+               function ($text) use ($gallery) {
+                       // This processes images inlined in posts
+                       // Frio / Vier hooks für lightbox are un-hooked in fancybox-config.js. So this works for them, too!
+                       //if (!in_array(DI::app()->getCurrentTheme(),['vier','frio']))
+                       $text = preg_replace(
+                               '#<a[^>]*href="([^"]*)"[^>]*>(<img[^>]*src="[^"]*"[^>]*>)</a>#',
+                               '<a data-fancybox="' . $gallery . '" href="$1">$2</a>',
+                               $text);
+
+                       // Local content images attached:
+                       $text = preg_replace_callback(
+                               '#<div class="(body-attach|imagegrid-column)">.*?</div>#s',
+                               function ($matches) use ($gallery) {
+                                       return str_replace('<a href', '<a data-fancybox="' . $gallery . '" href', $matches[0]);
+                               },
+                               $text
+                       );
+
+                       return $text;
                }
-               $part     = $matches[0];
-               $replaced = str_replace('<a href', '<a data-fancybox="' . $gallery . '" href', $part);
-               $replaced = str_replace('<div class="body-attach"', '<div class="body-attach done"', $replaced);
-               $html     = str_replace($part, $replaced, $html);
-       }
-       $html      = str_replace('class="body-attach done"', 'class="body-attach"', $html);
-       $b['html'] = $html;
+       );
 }