]> git.mxchange.org Git - friendica-addons.git/blob - fancybox/fancybox.php
f3a566a5061a3337a6f650bffb758141c4c6e72a
[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.01
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(App $a, array &$b)
32 {
33         $matches = [];
34         $pattern = '#<div class="body-attach">.*?</div>#s';
35         $gallery = 'gallery';
36         if (array_key_exists('item', $b)) {
37                 $item = $b['item'];
38                 if (array_key_exists('uri-id', $item)) {
39                         $gallery = $gallery . '-' . $item['uri-id'];
40                 }
41         }
42         $html = $b['html'];
43         while (preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE)) {
44                 if (is_array($matches)) {
45                         $matches = $matches[0];
46                 }
47                 $part     = $matches[0];
48                 $replaced = str_replace('<a href', '<a data-fancybox="' . $gallery . '" href', $part);
49                 $replaced = str_replace('<div class="body-attach"', '<div class="body-attach done"', $replaced);
50                 $html     = str_replace($part, $replaced, $html);
51         }
52         $html      = str_replace('class="body-attach done"', 'class="body-attach"', $html);
53         $b['html'] = $html;
54 }