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(App $a, array &$b){
32 $gallery = 'gallery-' . $b['item']['uri-id'] ?? random_int(1000000, 10000000);
34 // performWithEscapedBlocks escapes block defined with 2nd par pattern that won't be processed.
35 // We don't want to touch images in class="type-link":
36 $b['html'] = \Friendica\Util\Strings::performWithEscapedBlocks(
38 '#<div class="type-link">.*?</div>#s',
39 function ($text) use ($gallery) {
40 // This processes images inlined in posts
41 // Frio / Vier hooks für lightbox are un-hooked in fancybox-config.js. So this works for them, too!
42 //if (!in_array($a->getCurrentTheme(),['vier','frio']))
44 '#<a[^>]*href="([^"]*)"[^>]*>(<img[^>]*src="[^"]*"[^>]*>)</a>#',
45 '<a data-fancybox="' . $gallery . '" href="$1">$2</a>',
48 // Local content images attached:
49 $text = preg_replace_callback(
50 '#<div class="(body-attach|imagegrid-column)">.*?</div>#s',
51 function ($matches) use ($gallery) {
52 return str_replace('<a href', '<a data-fancybox="' . $gallery . '" href', $matches[0]);