2022-11-24 22:48:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Name: Fancybox
|
|
|
|
* Description: Open media attachments of posts into a fancybox overlay.
|
|
|
|
* Version: 1.01
|
|
|
|
* Author: Grischa Brockhaus <grischa@brockha.us>
|
|
|
|
*/
|
|
|
|
|
|
|
|
use Friendica\App;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
function fancybox_head(App $a, string &$b)
|
|
|
|
{
|
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
|
|
|
|
2022-11-24 22:48:08 +00:00
|
|
|
function fancybox_footer(App $a, string &$str)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2022-11-25 01:05:38 +00:00
|
|
|
function fancybox_render(App $a, array &$b)
|
|
|
|
{
|
|
|
|
$matches = [];
|
|
|
|
$pattern = '#<div class="body-attach">.*?</div>#s';
|
|
|
|
$gallery = 'gallery';
|
|
|
|
if (array_key_exists('item', $b)) {
|
|
|
|
$item = $b['item'];
|
|
|
|
if (array_key_exists('uri-id', $item)) {
|
|
|
|
$gallery = $gallery . '-' . $item['uri-id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$html = $b['html'];
|
|
|
|
while (preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE)) {
|
2022-11-25 01:21:03 +00:00
|
|
|
if (is_array($matches)) {
|
|
|
|
$matches = $matches[0];
|
|
|
|
}
|
|
|
|
$part = $matches[0];
|
2022-11-25 01:05:38 +00:00
|
|
|
$replaced = str_replace('<a href', '<a data-fancybox="' . $gallery . '" href', $part);
|
|
|
|
$replaced = str_replace('<div class="body-attach"', '<div class="body-attach done"', $replaced);
|
2022-11-25 01:21:03 +00:00
|
|
|
$html = str_replace($part, $replaced, $html);
|
2022-11-25 01:05:38 +00:00
|
|
|
}
|
2022-11-25 01:21:03 +00:00
|
|
|
$html = str_replace('class="body-attach done"', 'class="body-attach"', $html);
|
2022-11-25 01:05:38 +00:00
|
|
|
$b['html'] = $html;
|
2022-11-24 22:48:08 +00:00
|
|
|
}
|