Newlines added
[friendica-addons.git/.git] / fancybox / fancybox.php
1 <?php
2 /**
3  * Name: Fancybox
4  * Description: Open media attachments of posts into a fancybox overlay.
5  * Version: 1.05
6  * Author: Grischa Brockhaus <grischa@brockha.us>
7  */
8
9 use Friendica\App;
10 use Friendica\Core\Hook;
11 use Friendica\DI;
12
13 function fancybox_install()
14 {
15         Hook::register('head', __FILE__, 'fancybox_head');
16         Hook::register('footer', __FILE__, 'fancybox_footer');
17         Hook::register('prepare_body_final', __FILE__, 'fancybox_render');
18 }
19
20 function fancybox_head(string &$b)
21 {
22         DI::page()->registerStylesheet(__DIR__ . '/asset/fancybox/jquery.fancybox.min.css');
23 }
24
25 function fancybox_footer(string &$str)
26 {
27         DI::page()->registerFooterScript(__DIR__ . '/asset/fancybox/jquery.fancybox.min.js');
28         DI::page()->registerFooterScript(__DIR__ . '/asset/fancybox/fancybox.config.js');
29 }
30
31 function fancybox_render(array &$b){
32         $gallery = 'gallery-' . $b['item']['uri-id'] ?? random_int(1000000, 10000000);
33
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(
37                 $b['html'],
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(DI::app()->getCurrentTheme(),['vier','frio']))
43                         $text = preg_replace(
44                                 '#<a[^>]*href="([^"]*)"[^>]*>(<img[^>]*src="[^"]*"[^>]*>)</a>#',
45                                 '<a data-fancybox="' . $gallery . '" href="$1">$2</a>',
46                                 $text);
47
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]);
53                                 },
54                                 $text
55                         );
56
57                         return $text;
58                 }
59         );
60 }