bottom = array('WeChatShare_Plugin', 'render'); Typecho_Plugin::factory('admin/write-page.php')->bottom = array('WeChatShare_Plugin', 'render'); Typecho_Plugin::factory('Widget_Contents_Post_Edit')->finishPublish = array('WeChatShare_Plugin','updateWxShare'); Typecho_Plugin::factory('Widget_Contents_Page_Edit')->finishPublish = array('WeChatShare_Plugin','updateWxShare'); Typecho_Plugin::factory('Widget_Archive')->footer = array('WeChatShare_Plugin','addWxShareScript'); return $info; } /** * 禁用插件方法,如果禁用失败,直接抛出异常 * * @static * @access public * @return void */ public static function deactivate(){ Helper::removeAction('wx-share'); } /** * 获取插件配置面板 * * @access public * @param Typecho_Widget_Helper_Form $form 配置面板 * @return void */ public static function config(Typecho_Widget_Helper_Form $form) { $options = Typecho_Widget::widget('Widget_Options'); $up_action_url = Typecho_Common::url('/index.php/action/wx-share?do=update-plugin', $options->siteUrl); /** 公众号配置 */ $wx_AppID = new Typecho_Widget_Helper_Form_Element_Text('wx_AppID', NULL, NULL, _t('APPID'),'请登录微信公众号获取'); $form->addInput($wx_AppID); $wx_AppSecret = new Typecho_Widget_Helper_Form_Element_Text('wx_AppSecret', NULL, NULL, _t('密钥'),'请登录微信公众号获取'); $form->addInput($wx_AppSecret); $wx_image = new Typecho_Widget_Helper_Form_Element_Text('wx_image', NULL, NULL, _t('默认图标URL'),'请注意图标大小不要超过32KB'); $form->addInput($wx_image); $access_token_expire_time = new Typecho_Widget_Helper_Form_Element_Hidden('access_token_expire_time', NULL, NULL, _t('AccessToken 过期时间'),'隐藏'); $form->addInput($access_token_expire_time); $access_token = new Typecho_Widget_Helper_Form_Element_Hidden('access_token', NULL, NULL, _t('AccessToken'),'隐藏'); $form->addInput($access_token); $jsapi_ticket_expire_time = new Typecho_Widget_Helper_Form_Element_Hidden('jsapi_ticket_expire_time', NULL, NULL, _t('JsapiTicket 过期时间'),'隐藏'); $form->addInput($jsapi_ticket_expire_time); $jsapi_ticket = new Typecho_Widget_Helper_Form_Element_Text('jsapi_ticket', NULL, NULL, _t('JsapiTicket'),'方便出错调试,不用填写自动更新'); $form->addInput($jsapi_ticket); } /** * 个人用户的配置面板 * * @access public * @param Typecho_Widget_Helper_Form $form * @return void */ public static function personalConfig(Typecho_Widget_Helper_Form $form){} /** * 添加微信分享信息输入框到编辑文章/页面的页面,添加自定义摘要自定义字段 */ public static function render() { $options = Typecho_Widget::widget('Widget_Options'); $wx_action_url = Typecho_Common::url('/index.php/action/wx-share?do=insert', $options->siteUrl); $cid_res = preg_match('/cid=(\d+)/',$_SERVER['REQUEST_URI'],$match); $description = '';$wx_title = '';$wx_description = '';$wx_url = '';$wx_image = ''; if($cid_res) { /** 取出数据 */ $db = Typecho_Db::get(); $prefix = $db->getPrefix(); $desc_res= $db->fetchAll($db->select()->from($prefix.'fields')->where('cid = ?', $match['1'])->where('name = ?', 'description')); $wx_share= $db->fetchAll($db->select()->from($prefix.'wx_share')->where('cid = ?', $match['1'])); if($desc_res) { $description = $desc_res[0]['str_value']; } if($wx_share) { $wx_title = $wx_share[0]['wx_title']; $wx_description = $wx_share[0]['wx_description']; $wx_url = $wx_share[0]['wx_url']; $wx_image = $wx_share[0]['wx_image']; } } $data = '
微信分享
  1. 摘要:
'; ?> getPrefix(); $wx_share = $db->fetchAll($db->select('wx_id,wx_url')->from('table.wx_share')->order('wx_id',Typecho_Db::SORT_DESC)->limit(1)); $wx_id = $wx_share[0]['wx_id']; !empty($wx_share[0]['wx_url']) || $data['wx_url'] = $class->permalink; $data['cid'] = $class->cid; //更新数据,执行后,返回收影响的行数。 $db->query($db->update($prefix.'wx_share')->rows($data)->where('wx_id = ?',$wx_id)); } /** * 将微信分享的需要的信息写入wx_share.js文件中 */ public static function addWxShareScript($archive) { echo ''; $options = Typecho_Widget::widget('Widget_Options'); $ajax_wx_share_url = Typecho_Common::url('/index.php/action/wx-share?do=ajax-get', $options->siteUrl); $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://'; $signature_url = $http_type . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // pjax $signature_url = preg_replace('/\?_pjax=.*/','',$signature_url); $version = self::_VERSION; $wx_script = <<'; } /* * WeChatShare插件安装方法 */ public static function wechatShareInstall() { $installDb = Typecho_Db::get(); $type = explode('_', $installDb->getAdapterName()); $type = array_pop($type); $prefix = $installDb->getPrefix(); $scripts = file_get_contents('usr/plugins/WeChatShare/'.$type.'.sql'); $scripts = str_replace('typecho_', $prefix, $scripts); $scripts = str_replace('%charset%', 'utf8', $scripts); $scripts = explode(';', $scripts); try { foreach ($scripts as $script) { $script = trim($script); if ($script) { $installDb->query($script, Typecho_Db::WRITE); } } return '建立微信分享数据表,插件启用成功,请去配置页面填写微信公众号APPID、密钥和默认图标url'; } catch (Typecho_Db_Exception $e) { $code = $e->getCode(); if(('Mysql' == $type && 1050 == $code) || ('Mysql' == $type && '42S01' == $code) || ('SQLite' == $type && ('HY000' == $code || 1 == $code))) { try { $script = 'SELECT `wx_id`, `wx_title`, `wx_url`, `wx_image`, `wx_description` ,`cid` from `' . $prefix . 'wx_share`'; $installDb->query($script, Typecho_Db::READ); return '检测到微信分享数据表,微信分享插件启用成功,请去配置页面填写微信公众号APPID、密钥和默认图标url'; } catch (Typecho_Db_Exception $e) { $code = $e->getCode(); throw new Typecho_Plugin_Exception('数据表检测失败,微信分享插件启用失败。错误号:'.$code); } } else { throw new Typecho_Plugin_Exception('数据表建立失败,微信分享插件启用失败。错误号:'.$code); } } } }