From 70979cdb9416d16191c11cd65ba539edb4bf64ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=AA=E5=AD=90?= Date: Wed, 24 Jul 2024 16:19:21 +0800 Subject: [PATCH] 0.5.8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加了段代码的功能,以适配原版使用`[article id="123"]`或者`[article url="https://your-site.com/your-article-url"]` --- functions.php | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/functions.php b/functions.php index 41323ab..73c662b 100644 --- a/functions.php +++ b/functions.php @@ -522,4 +522,85 @@ function getParentCategory($categoryId) { // 计算两个时间之间相差的天数 function getDays($time1, $time2) { return floor(($time2 - $time1) / 86400); -} \ No newline at end of file +} + +//获取文章卡片 +function get_article_info($atts) { + $default_atts = array( + 'id' => '', + 'url' => '' + ); + $atts = array_merge($default_atts, $atts); + $db = Typecho_Db::get(); + if (!empty($atts['id'])) { + $post = $db->fetchRow($db->select()->from('table.contents') + ->where('cid = ?', $atts['id']) + ->limit(1)); + } elseif (!empty($atts['url'])) { + $post = $db->fetchRow($db->select()->from('table.contents') + ->where('permalink = ?', $atts['url']) + ->limit(1)); + } else { + return '请提供文章ID或URL'; + } + if (!$post) { + return '未找到文章'; + } + $post = Typecho_Widget::widget('Widget_Abstract_Contents')->push($post); + + // 获取缩略图 + $imageToDisplay = img_postthumb($post['cid']); + if (empty($imageToDisplay)) { + $imageToDisplay = '//pic.0tz.top/api'; // 设置一个默认图片路径 + } + + $output = '
'; + $output .= ''; + $output .= '' . $post['title'] . ''; + $output .= '' . Typecho_Common::subStr(strip_tags($post['text']), 0, 100, '...') . ''; + $output .= ''; + $output .= ''; + $output .= '
'; + return $output; +} + + +// 注册短代码 +function register_shortcodes() { + Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('ContentProcessor', 'process'); +} + +class ContentProcessor { + public static function process($content, $widget, $lastResult) { + $content = preg_replace_callback('/\[article\s+([^\]]+)\]/', function($matches) { + $atts = self::parse_atts($matches[1]); + return get_article_info($atts); + }, $content); + + return $content; + } + + // 解析短代码属性 + private static function parse_atts($text) { + $atts = array(); + $pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/'; + $text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text); + if (preg_match_all($pattern, $text, $match, PREG_SET_ORDER)) { + foreach ($match as $m) { + if (!empty($m[1])) + $atts[strtolower($m[1])] = stripcslashes($m[2]); + elseif (!empty($m[3])) + $atts[strtolower($m[3])] = stripcslashes($m[4]); + elseif (!empty($m[5])) + $atts[strtolower($m[5])] = stripcslashes($m[6]); + elseif (isset($m[7]) && strlen($m[7])) + $atts[] = stripcslashes($m[7]); + elseif (isset($m[8])) + $atts[] = stripcslashes($m[8]); + } + } + return $atts; + } +} + +register_shortcodes(); \ No newline at end of file