]> git.mxchange.org Git - friendica-addons.git/blob - fancybox/fancybox.php
AddOn Fancybox
[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 function fancybox_footer(App $a, string &$str)
25 {
26     DI::page()->registerFooterScript(__DIR__ . '/asset/fancybox/jquery.fancybox.min.js');
27     DI::page()->registerFooterScript(__DIR__ . '/asset/fancybox/fancybox.config.js');
28 }
29
30 /*
31
32 prepare_body_final
33
34 Called at the end of prepare_body(). Hook data:
35
36     item: item array (input)
37     html: converted item body (input/output)
38
39 */
40
41 function fancybox_render(App $a, array &$b) {
42     $matches = [];
43     $pattern='#<div class="body-attach">.*?</div>#s';
44     $gallery = 'gallery';
45     if (array_key_exists('item', $b)) {
46         $item = $b['item'];
47         if (array_key_exists('uri-id', $item)) {
48             $gallery = $gallery . '-' . $item['uri-id'];
49         }
50     }
51     $html = $b['html'];
52     while (preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE)) {
53         if (is_array($matches)) $matches=$matches[0];
54         $part = $matches[0];
55         $replaced = str_replace('<a href', '<a data-fancybox="' . $gallery . '" href', $part);
56         $replaced = str_replace('<div class="body-attach"', '<div class="body-attach done"', $replaced);
57         $html =str_replace($part, $replaced, $html);
58     }
59     $html = str_replace('class="body-attach done"', 'class="body-attach"', $html);
60     $b['html'] = $html;
61 }