140 lines
4.6 KiB
PHP
140 lines
4.6 KiB
PHP
<?php
|
||
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
|
||
/**
|
||
* FlexPhoto图片并列排版工具
|
||
*
|
||
* @package FlexPhoto
|
||
* @author XIGE
|
||
* @version 1.1.2
|
||
* @link https://www.sangushui.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?20261111c">
|
||
<?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;
|
||
}
|
||
}
|