4 * Description: Open media attachments of posts into a fancybox overlay.
6 * Author: Grischa Brockhaus <grischa@brockha.us>
10 use Friendica\Core\Hook;
13 function fancybox_install()
15 Hook::register('head', __FILE__, 'fancybox_head');
16 Hook::register('footer', __FILE__, 'fancybox_footer');
17 Hook::register('prepare_body_final', __FILE__, 'fancybox_render');
20 function fancybox_head(App $a, string &$b)
22 DI::page()->registerStylesheet(__DIR__ . '/asset/fancybox/jquery.fancybox.min.css');
25 function fancybox_footer(App $a, string &$str)
27 DI::page()->registerFooterScript(__DIR__ . '/asset/fancybox/jquery.fancybox.min.js');
28 DI::page()->registerFooterScript(__DIR__ . '/asset/fancybox/fancybox.config.js');
31 function fancybox_render($a, array &$b){
32 $gallery = 'gallery-' . $b['item']['uri-id'] ?? random_int(1000000, 10000000);
34 // prevent urls in <div class="type-link"> to be replaced
35 $b['html'] = preg_replace_callback(
36 '#<div class="type-link">.*?</div>#s',
37 function ($matches) use ($gallery) {
38 return str_replace('<a href', '<a data-nofancybox="" href', $matches[0]);
43 // This processes images inlined in posts
44 // Frio / Vier hooks für lightbox are un-hooked in fancybox-config.js. So this works for them, too!
45 //if (!in_array($a->getCurrentTheme(),['vier','frio']))
47 // normal post inline linked images
48 $b['html'] = preg_replace_callback(
49 '#<a[^>]*href="([^"]*)"[^>]*>(<img[^>]*src="[^"]*"[^>]*>)</a>#',
50 function ($matches) use ($gallery) {
51 // don't touch URLS marked as not "fancyable".. ;-)
52 if (preg_match('#data-nofancybox#', $matches[0]))
56 return '<a data-fancybox="' . $gallery . '" href="'. $matches[1] .'">' . $matches[2] .'</a>';
62 // Local content images attached:
63 $b['html'] = preg_replace_callback(
64 '#<div class="body-attach">.*?</div>#s',
65 function ($matches) use ($gallery) {
66 return str_replace('<a href', '<a data-fancybox="' . $gallery . '" href', $matches[0]);