Slanted 主题扩展插件 * * 浏览量统计部分来自 willin kan 的 Views 插件 * * @package SlantedExtend * @author DT27 * @version 1.0.0 * @link https://dt27.org/SlantedExtend/ */ class SlantedExtend_Plugin implements Typecho_Plugin_Interface { /** * 激活插件方法,如果激活失败,直接抛出异常 * * @access public * @return void * @throws Typecho_Plugin_Exception */ public static function activate() { //独立页面顶部标题 Typecho_Plugin::factory('admin/write-page.php')->bottom = array('SlantedExtend_Plugin', 'headingJS'); //文章缩略图 Typecho_Plugin::factory('admin/write-post.php')->bottom = array('SlantedExtend_Plugin', 'thumbJS'); //原创文章 Typecho_Plugin::factory('admin/write-post.php')->option = array('SlantedExtend_Plugin', 'originalHtml'); //原创文章 Typecho_Plugin::factory('admin/write-post.php')->bottom = array('SlantedExtend_Plugin', 'originalJS'); //浏览量统计 Typecho_Plugin::factory('Widget_Archive')->beforeRender = array('SlantedExtend_Plugin', 'viewsCounter'); $db = Typecho_Db::get(); $prefix = $db->getPrefix(); // contents 表中若无 views 字段则添加 if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) $db->query('ALTER TABLE `'. $prefix .'contents` ADD `views` INT(10) DEFAULT 0;'); } /** * 禁用插件方法,如果禁用失败,直接抛出异常 * * @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){} /** * 插件实现方法 * * @access public * @return void */ public static function slantedExtendFields() { } /** * 文章缩略图自定义字段扩展 * * @access public * @return void */ public static function thumbJS() { ?>
原创文章将在文章底部显示转载提示信息
'; } /** * * 原创文章选项 * * @access public * @return void */ public static function originalJS(){ ?> is('single')) { $db = Typecho_Db::get(); $cid = Typecho_Widget::widget('Widget_Archive')->cid; $row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid)); $db->query($db->update('table.contents')->rows(array('views' => (int)$row['views']+1))->where('cid = ?', $cid)); } } /** * 输出访问次数 * * 语法: Views_Plugin::theViews(); * 输出: '访问: xx,xxx 次' * * 语法: Views_Plugin::theViews('有 ', ' 次点击'); * 输出: '有 xx,xxx 次点击' * * @access public * @param string $before 前字串 * @param string $after 后字串 * @param bool $echo 是否显示 (0 用于运算,不显示) * @return string */ public static function theViews($before = '浏览量: ', $after = ' ', $echo = 1) { $db = Typecho_Db::get(); $cid = Typecho_Widget::widget('Widget_Archive')->cid; $row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid)); if ($echo) echo $before, number_format($row['views']), $after; else return $row['views']; } /** * 输出最受欢迎文章 * * 语法: Views_Plugin::theMostViewed(); * * @access public * @param int $limit 文章数目 * @param int $show 是否显示浏览量 1 是, 0 否 * @param string $before 前字串 * @param string $after 后字串 * @return string */ public static function theMostViewed($limit = 10, $show = 1, $before = ' ( 浏览量: ', $after = ' ) ') { $db = Typecho_Db::get(); $options = Typecho_Widget::widget('Widget_Options'); $limit = is_numeric($limit) ? $limit : 10; $posts = $db->fetchAll($db->select()->from('table.contents') ->where('type = ? AND status = ? AND password IS NULL', 'post', 'publish') ->order('views', Typecho_Db::SORT_DESC) ->limit($limit) ); if ($posts) { foreach ($posts as $post) { $result = Typecho_Widget::widget('Widget_Abstract_Contents')->push($post); $post_views = number_format($result['views']); $post_title = htmlspecialchars($result['title']); $permalink = $result['permalink']; echo "