167 lines
5.8 KiB
PHP
167 lines
5.8 KiB
PHP
<?php
|
||
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
|
||
/**
|
||
* 图片横排
|
||
*
|
||
* @package FlexPhoto
|
||
* @author 石头厝
|
||
* @version 1.1.4
|
||
* @link https://www.shitoucuo.com/
|
||
*/
|
||
class FlexPhoto_Plugin implements Typecho_Plugin_Interface
|
||
{
|
||
/**
|
||
* 激活插件方法,如果激活失败,直接抛出异常
|
||
*
|
||
* @access public
|
||
* @return void
|
||
* @throws Typecho_Plugin_Exception
|
||
*/
|
||
public static function activate()
|
||
{
|
||
Typecho_Plugin::factory('Widget_Archive')->header = array('FlexPhoto_Plugin', 'skinhead');
|
||
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('FlexPhoto_Plugin','tutu');
|
||
Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('FlexPhoto_Plugin','tus');
|
||
}
|
||
|
||
/**
|
||
* 禁用插件方法,如果禁用失败,直接抛出异常
|
||
*
|
||
* @static
|
||
* @access public
|
||
* @return void
|
||
* @throws Typecho_Plugin_Exception
|
||
*/
|
||
public static function deactivate(){}
|
||
|
||
/**
|
||
* 获取插件配置面板
|
||
*
|
||
* @access public
|
||
* @param Typecho_Widget_Helper_Form $form 配置面板
|
||
* @return void
|
||
*/
|
||
public static function config(Typecho_Widget_Helper_Form $form)
|
||
{
|
||
}
|
||
|
||
/**
|
||
* 个人用户的配置面板
|
||
*
|
||
* @access public
|
||
* @param Typecho_Widget_Helper_Form $form
|
||
* @return void
|
||
*/
|
||
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
|
||
|
||
public static function skinhead(){
|
||
?>
|
||
<link rel="stylesheet" href="<?php Helper::options()->pluginUrl(); ?>/FlexPhoto/style.css?20261203b">
|
||
<script>
|
||
(function () {
|
||
/* 灯箱互斥:本主题默认只有 ViewImage 一个灯箱,画廊缩略图由它接管即可。
|
||
若站点额外装了 Fancybox(data-fancybox 属性本来就是给它用的),
|
||
就让 Fancybox 负责画廊,缩略图打上 no-view 让 ViewImage 跳过,
|
||
避免一次点击弹出两层。 */
|
||
function guard() {
|
||
if (!window.Fancybox) { return; }
|
||
var imgs = document.querySelectorAll('.flexphoto img');
|
||
for (var i = 0; i < imgs.length; i++) {
|
||
imgs[i].setAttribute('no-view', '');
|
||
}
|
||
}
|
||
|
||
if (document.readyState === 'loading') {
|
||
document.addEventListener('DOMContentLoaded', guard);
|
||
} else {
|
||
guard();
|
||
}
|
||
/* Fancybox 可能异步加载,加载完再兜一次 */
|
||
window.addEventListener('load', guard);
|
||
/* 分页/无限滚动后新插入的图同样处理 */
|
||
if (window.MutationObserver) {
|
||
new MutationObserver(guard).observe(document.body, {childList: true, subtree: true});
|
||
}
|
||
})();
|
||
</script>
|
||
<?php
|
||
}
|
||
public static function tutu($data, $ojbk,$last)
|
||
{
|
||
$text = empty($last)?$data:$last;
|
||
$text = self::parsePhotoSet($text);
|
||
return $text;
|
||
}
|
||
static public function parsePhotoSet($content)
|
||
{
|
||
$regx = '/\{flex(.*?)}(.*?)\/flex\}/s';
|
||
$new = preg_replace_callback($regx, array('FlexPhoto_Plugin', 'parsePhotoSetCallBack'), $content);
|
||
$new = preg_replace_callback($regx, array('FlexPhoto_Plugin', 'parsePhotoSetCol'), $new);
|
||
|
||
$reg='/(<p>)?\{flex(.*?)\}(.*?)\{\/flex\}(<\/p>)?(<br>)?/s';
|
||
|
||
$rp = '<div${2}>${3}</div>';
|
||
$new=preg_replace($reg, $rp, $new);
|
||
return $new;
|
||
}
|
||
private static function parsePhotoSetCallBack($match)
|
||
{
|
||
$new='{flex'.$match[1].'}'. str_replace(['<br>', '<br/>', '<br />', '<p>', '</p>'], '', $match[2]) .'/flex}';
|
||
|
||
/* 逐个匹配 img 标签,保留原标签里的关键信息(data-src / alt),
|
||
并自行生成 Fancybox 触发元素,避免依赖主题对 img 属性顺序的强制要求 */
|
||
$new = preg_replace_callback('/<img\b[^>]*>/i', array('FlexPhoto_Plugin', 'buildPhotoItem'), $new);
|
||
return $new;
|
||
}
|
||
|
||
private static function buildPhotoItem($match)
|
||
{
|
||
$tag = $match[0];
|
||
|
||
$src = '';
|
||
if (preg_match('/\bdata-src=["\']([^"\']+)["\']/i', $tag, $m)) {
|
||
$src = trim($m[1]);
|
||
} elseif (preg_match('/\bsrc=["\']([^"\']+)["\']/i', $tag, $m)) {
|
||
$src = trim($m[1]);
|
||
}
|
||
if ($src === '') {
|
||
return '';
|
||
}
|
||
|
||
$alt = preg_match('/\balt=["\']([^"\']*)["\']/i', $tag, $m) ? $m[1] : '';
|
||
|
||
// 先解码再编码,避免已转义的地址被二次转义
|
||
$src = htmlspecialchars(html_entity_decode($src, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');
|
||
$alt = htmlspecialchars(html_entity_decode($alt, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');
|
||
|
||
return '<figure><a class="flexphoto-link" data-fancybox="gallery" href="'.$src.'" data-caption="'.$alt.'">'
|
||
. '<img alt="'.$alt.'" src="'.$src.'" loading="lazy"></a></figure>';
|
||
}
|
||
private static function parsePhotoSetCol($match)
|
||
{
|
||
if(empty($match[1])){
|
||
return '{flex class="flexphoto"}'.$match[2].'/flex}';
|
||
}
|
||
|
||
preg_match_all('/<img\s*[^>]*>/i', $match[2], $matches);
|
||
// 返回匹配到的数量
|
||
$num = count($matches[0]);
|
||
|
||
$set = preg_replace('/(.*?)set="(.*?)"/', '$2', $match[1]);
|
||
$set = explode(",", $set);
|
||
$mobile = trim($set[0])==0 ? $num : trim($set[0]);
|
||
$tablet = trim($set[1])==0 ? $num : trim($set[1]);
|
||
$desktop = trim($set[2])==0 ? $num : trim($set[2]);
|
||
|
||
|
||
return '{flex class="flexphoto diycol" style="--desktop-col: '.$desktop.'; --tablet-col: '.$tablet.'; --mobile-col: '.$mobile.';"}'.$match[2].'/flex}';
|
||
}
|
||
|
||
public static function tus($text, $ojbk)
|
||
{
|
||
$text = str_replace('{flex}', '', $text);
|
||
$text = str_replace('{/flex}', '', $text);
|
||
return $text;
|
||
}
|
||
}
|