1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('ShortcodeParser', 'postCard');
class ShortcodeParser { public static function postCard($content, $widget) { $pattern = '/\[post\s+id=[""\']?(\d+)[""\']?\]/i'; return preg_replace_callback($pattern, function($matches) { return self::buildPostCard($matches[1]); }, $content); }
private static function buildPostCard($cid) { try { $db = Typecho_Db::get(); $options = Typecho_Widget::widget('Widget_Options'); $post = $db->fetchRow($db->select('title', 'text', 'slug', 'created', 'modified') ->from('table.contents') ->where('cid = ?', intval($cid)) ->where('type = ?', 'post') ->where('status = ?', 'publish') ->limit(1)); if (!$post) return '文章不存在'; $routeExists = (bool)Typecho_Router::get('post'); $permalink = $routeExists ? Typecho_Common::url( Typecho_Router::url( 'post', [ 'slug' => $post['slug'], 'category' => self::getFirstCategory($cid) ] ), $options->siteUrl ) : $options->siteUrl . '?cid=' . $cid;
$excerpt = self::getExcerpt($post['text'], 50); $thumb = $options->Thumb . '?url=' . self::getPostThumb($cid, $options) . '&w=200&h=140'; return <<<HTML <div class=""post_go""> <div class=""post_left""> <img decoding=""async"" src=""{$thumb}"" alt=""{$post['title']}""> <div class=""post_info""> <div class=""post_name_h2"">{$post['title']}</div> <p>{$excerpt}</p> </div> </div> <a class=""post_url"" href=""{$permalink}"">阅读全文 <i class=""bi bi-arrow-right ms-2""></i> </a> </div> HTML; } catch (Exception $e) { return '文章加载失败:'.$e->getMessage(); } }
private static function getFirstCategory($cid) { $db = Typecho_Db::get(); return $db->fetchRow($db->select('slug') ->from('table.metas') ->join('table.relationships', 'table.relationships.mid = table.metas.mid') ->where('table.relationships.cid = ?', $cid) ->where('table.metas.type = ?', 'category') ->limit(1))['slug'] ?? ''; } private static function getExcerpt($text, $length) { $cleanText = trim(strip_tags($text)); return Typecho_Common::subStr($cleanText, 0, $length, '...'); }
private static function getPostThumb($cid, $options) {
$uploadBase = defined('__TYPECHO_UPLOAD_URL__') ? rtrim(__TYPECHO_UPLOAD_URL__, '/') : $options->siteUrl; $default = $options->Image ?? ''; $db = Typecho_Db::get(); $attachments = $db->fetchAll($db->select('text') ->from('table.contents') ->where('parent = ?', $cid) ->where('type = ?', 'attachment')); foreach ($attachments as $attach) { $meta = unserialize($attach['text']); if (isset($meta['mime']) && strpos($meta['mime'], 'image/') === 0) { return Typecho_Common::url($meta['path'], $uploadBase); } } return $default; } }
|