]> git.mxchange.org Git - friendica-addons.git/blob - fancybox/fancybox.php
Version 1.03
[friendica-addons.git] / fancybox / fancybox.php
1 <?php
2 /**
3  * Name: Fancybox
4  * Description: Open media attachments of posts into a fancybox overlay.
5  * Version: 1.03
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(App $a, string &$b)
21 {
22         DI::page()->registerStylesheet(__DIR__ . '/asset/fancybox/jquery.fancybox.min.css');
23 }
24
25 function fancybox_footer(App $a, 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($a, array &$b){
32         $gallery = 'gallery-' . $b['item']['uri-id'] ?? random_int(1000000, 10000000);
33
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]);
39                 },
40                 $b['html']
41         );
42
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']))
46         {
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]))
53                                 {
54                                         return $matches[0];
55                                 }
56                                 return '<a data-fancybox="' . $gallery . '" href="'. $matches[1] .'">' . $matches[2] .'</a>';
57                         },
58                         $b['html']
59                 );
60         }
61
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]);
67                 },
68                 $b['html']
69         );
70 }