2022-11-24 22:48:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Name: Fancybox
|
|
|
|
* Description: Open media attachments of posts into a fancybox overlay.
|
2022-12-18 23:52:31 +00:00
|
|
|
* Version: 1.05
|
2022-11-24 22:48:08 +00:00
|
|
|
* Author: Grischa Brockhaus <grischa@brockha.us>
|
2024-11-13 09:10:25 +00:00
|
|
|
* Status: Unsupported
|
2022-11-24 22:48:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
use Friendica\Core\Hook;
|
|
|
|
use Friendica\DI;
|
|
|
|
|
|
|
|
function fancybox_install()
|
|
|
|
{
|
2022-11-25 01:05:38 +00:00
|
|
|
Hook::register('head', __FILE__, 'fancybox_head');
|
|
|
|
Hook::register('footer', __FILE__, 'fancybox_footer');
|
|
|
|
Hook::register('prepare_body_final', __FILE__, 'fancybox_render');
|
2022-11-24 22:48:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 02:16:09 +00:00
|
|
|
function fancybox_head(string &$b)
|
2022-11-24 22:48:08 +00:00
|
|
|
{
|
2022-11-25 01:05:38 +00:00
|
|
|
DI::page()->registerStylesheet(__DIR__ . '/asset/fancybox/jquery.fancybox.min.css');
|
2022-11-24 22:48:08 +00:00
|
|
|
}
|
2022-11-25 01:05:38 +00:00
|
|
|
|
2023-01-14 02:16:09 +00:00
|
|
|
function fancybox_footer(string &$str)
|
2022-11-24 22:48:08 +00:00
|
|
|
{
|
2022-11-25 01:05:38 +00:00
|
|
|
DI::page()->registerFooterScript(__DIR__ . '/asset/fancybox/jquery.fancybox.min.js');
|
|
|
|
DI::page()->registerFooterScript(__DIR__ . '/asset/fancybox/fancybox.config.js');
|
2022-11-24 22:48:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 02:16:09 +00:00
|
|
|
function fancybox_render(array &$b){
|
2022-12-04 23:54:04 +00:00
|
|
|
$gallery = 'gallery-' . $b['item']['uri-id'] ?? random_int(1000000, 10000000);
|
|
|
|
|
2022-12-05 11:46:18 +00:00
|
|
|
// performWithEscapedBlocks escapes block defined with 2nd par pattern that won't be processed.
|
|
|
|
// We don't want to touch images in class="type-link":
|
|
|
|
$b['html'] = \Friendica\Util\Strings::performWithEscapedBlocks(
|
|
|
|
$b['html'],
|
2022-12-04 23:54:04 +00:00
|
|
|
'#<div class="type-link">.*?</div>#s',
|
2022-12-05 11:46:18 +00:00
|
|
|
function ($text) use ($gallery) {
|
|
|
|
// This processes images inlined in posts
|
|
|
|
// Frio / Vier hooks für lightbox are un-hooked in fancybox-config.js. So this works for them, too!
|
2024-12-11 21:47:10 +00:00
|
|
|
//if (!in_array(DI::appHelper()->getCurrentTheme(),['vier','frio']))
|
2022-12-05 11:46:18 +00:00
|
|
|
$text = preg_replace(
|
|
|
|
'#<a[^>]*href="([^"]*)"[^>]*>(<img[^>]*src="[^"]*"[^>]*>)</a>#',
|
|
|
|
'<a data-fancybox="' . $gallery . '" href="$1">$2</a>',
|
|
|
|
$text);
|
|
|
|
|
|
|
|
// Local content images attached:
|
|
|
|
$text = preg_replace_callback(
|
2022-12-14 17:58:00 +00:00
|
|
|
'#<div class="(body-attach|imagegrid-column)">.*?</div>#s',
|
2022-12-05 11:46:18 +00:00
|
|
|
function ($matches) use ($gallery) {
|
|
|
|
return str_replace('<a href', '<a data-fancybox="' . $gallery . '" href', $matches[0]);
|
|
|
|
},
|
|
|
|
$text
|
|
|
|
);
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
2022-12-04 23:54:04 +00:00
|
|
|
);
|
2022-11-25 01:23:16 +00:00
|
|
|
}
|