1
0
Fork 0
mirror of https://github.com/yunluo/gdk.git synced 2024-05-05 07:53:34 +02:00

优化代码

This commit is contained in:
云落 2020-03-26 22:24:00 +08:00
parent 3d36179f4b
commit be86126aa6
6 changed files with 53 additions and 232 deletions

View file

@ -80,13 +80,13 @@ class GDK_Points_Shortcodes
$notice .= '</div>';
} else {
if (GDK_Points::get_user_total_points($user_id, 'accepted') < $point) {
$notice .= '<fieldset id="hide_notice" class="fieldset"><legend class="legend">付费内容</legend>';
$notice .= '<fieldset id="hide_notice" class="fieldset ta-center"><legend class="legend ta-left">付费内容</legend>';
$notice .= '<p>当前隐藏内容需要支付</p><span class="cm-coin">' . $point . '金币</span>';
$notice .= '<p>您当前拥有<span class="red">' . GDK_Points::get_user_total_points($user_id, 'accepted') . '</span>金币,金币不足,请充值</p>';
$notice .= buy_points();
$notice .= '</fieldset>';
} else {
$notice .= '<fieldset id="hide_notice" class="fieldset"><legend class="legend">付费内容</legend>';
$notice .= '<fieldset id="hide_notice" class="fieldset ta-center"><legend class="legend ta-left">付费内容</legend>';
$notice .= '<p>当前隐藏内容需要支付</p><span class="cm-coin">' . $point . '金币</span>';
$notice .= '<p>您当前拥有<span class="red">' . GDK_Points::get_user_total_points($user_id, 'accepted') . '</span>金币</p>';
$notice .= '<p><button class="cm-btn primary" id="pay_points" data-point="' . $point . '" data-userid="' . $user_id . '" data-action="gdk_pay_buy" data-id="' . $pid . '">点击购买</button></p>';
@ -94,7 +94,7 @@ class GDK_Points_Shortcodes
}
}
} else {
$notice .= '<fieldset id="hide_notice" class="fieldset"><legend class="legend">付费内容</legend>';
$notice .= '<fieldset id="hide_notice" class="fieldset ta-center"><legend class="legend ta-left">付费内容</legend>';
$notice .= '<p>当前隐藏内容需要支付</p><span class="cm-coin">' . $point . '金币</span>';
$notice .= '<p>您当前尚未登陆,请登陆后查看</p>';
$notice .= weixin_login_btn();

View file

@ -1,5 +1,5 @@
<?php
error_reporting(0);
//error_reporting(0);
/**
* 微信(或易信)公共平台处理类
@ -15,9 +15,7 @@ class WeChat
private $__token = ""; //TOKEN值
private $__callback_function = null; //回调函数名称
private $__articles = array(); //图文信息array
public $debug = false; //是否调试状态
public $fromUser = ""; //当前消息的发送者
public $toUser = ""; //当前消息的接收者
@ -102,33 +100,16 @@ class WeChat
//解析POST数据(XML格式)
$object = simplexml_load_string($postData, 'SimpleXMLElement', LIBXML_NOCDATA);
$messgeType = trim($object->MsgType); //取得消息类型
$this->fromUser = "" . $object->FromUserName; //记录消息发送方(不是发送者的微信号而是一个加密后的OpenID)
$this->toUser = "" . $object->ToUserName; //记录消息接收方(就是公共平台的OpenID)
$this->fromUser = $object->FromUserName; //记录消息发送方(不是发送者的微信号而是一个加密后的OpenID)
$this->toUser = $object->ToUserName; //记录消息接收方(就是公共平台的OpenID)
//如果回调函数没有设置,则退出
if (!is_callable($this->callback_function)) {
return;
}
//根据不同的消息类型,分别处理
switch ($messgeType) {
case "text": //文本消息
//调用回调函数
call_user_func($this->callback_function, $this, "text", $object->Content, "", "");
break;
case "event": //事件
switch ($object->Event) {
case "subscribe": //订阅事件
call_user_func($this->callback_function, $this, "subscribe", $object->FromUserName, "", "");
break;
default:
//Unknow Event
break;
}
break;
default:
//Unknow msg type
break;
if ($messgeType == "text") {
call_user_func($this->callback_function, $this, "text", $object->Content, "", "");
}
}
@ -158,41 +139,6 @@ class WeChat
return $xmlText;
}
/**
* 形成 图文消息响应值
*
* @param string $toUser
* @param string $fromUser
* @param array $articles 一个array每个元素保存一条图文信息每个元素也是一个array, 有Title,Description,PicUrl,Url四个键值
*
* @return string
*/
protected function _newsResponse($toUser, $fromUser, $articles)
{
$xmlTemplate = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
";
$xmlText = sprintf($xmlTemplate, $toUser, $fromUser, time());
$xmlText .= '<ArticleCount>' . count($articles) . '</ArticleCount>';
$xmlText .= '<Articles>';
foreach ($articles as $article) {
$xmlText .= '<item>';
$xmlText .= '<Title><![CDATA[' . $article['Title'] . ']]></Title>';
$xmlText .= '<Description><![CDATA[' . $article['Description'] . ']]></Description>';
$xmlText .= '<PicUrl><![CDATA[' . $article['PicUrl'] . ']]></PicUrl>';
$xmlText .= '<Url><![CDATA[' . $article['Url'] . ']]></Url>';
$xmlText .= '</item>';
}
$xmlText .= '</Articles> </xml>';
return $xmlText;
}
/**
* 发送文本内容
*
@ -203,51 +149,21 @@ class WeChat
echo $this->textResponse($this->fromUser, $this->toUser, $content);
}
/**
* 添加一条图文信息
*
* @param string $title 标题
* @param string $description 内容
* @param string $url 网页链接URL
* @param string $pictureUrl 图片的URL
*/
public function addNews($title, $description, $url, $pictureUrl)
{
$article = array('Title' => $title,
'Description' => $description,
'PicUrl' => $pictureUrl,
'Url' => $url);
$this->articles[] = $article;
}
/**
* 发送图文信息
* 用法首先用addNews()函数一条一条地添加图文信息,添加完成后用本函数发送
*/
public function sendNews()
{
echo $this->newsResponse($this->fromUser, $this->toUser, $this->articles);
}
}
define("WX_WELCOME", '欢迎关注极客公园'); //欢迎词
define("POSTNUM", '5'); //文章数量
define("DEFAULT_THUMB", ''); //封面
add_action('pre_get_posts', 'wm_preprocess', 4);
add_action('pre_get_posts', 'wx_preprocess', 4);
/**
* 预处理函数
*
* @param $wp_query
*/
function wm_preprocess($wp_query)
function wx_preprocess($wp_query)
{
global $object;
$wx_token = trim(gdk_option('gdk_wxmp_token'));
if (!isset($object)) {
//创建一个WeChat类的实例, 回调函数名称为"onMessage",即消息处理函数
$object = new WeChat($wx_token, "onMessage");
//创建一个WeChat类的实例, 回调函数名称为"CaptchaMessage",即消息处理函数
$object = new WeChat($wx_token, "CaptchaMessage");
$object->process(); //处理消息
return;
}
@ -262,113 +178,12 @@ function wm_preprocess($wp_query)
* @param string $arg1
* @param string $arg2
*/
function onMessage(WeChat $object, $messageType, $content, $arg1, $arg2)
function CaptchaMessage(WeChat $object, $messageType, $content, $arg1, $arg2)
{
//处理subscribe消息
switch ($messageType) {
case "subscribe": //当用户关注
$object->addNews(WX_WELCOME, "", "", "");
$object->sendNews();
break;
case "text":
$keyword = trim($content);
switch ($keyword) {
case 'yzm':
case 'Yzm':
case 'yZm':
case 'yzM':
case 'YZM':
case '验证码':
$object->sendText('您的验证码为:【' . wx_captcha() . '】验证码有效期为2分钟请抓紧使用过期需重新申请');
break;
case 'r':
send_post($object, 'r');
break;
case "help":
case "h":
case "?":
case "":
case "":
$object->sendText(WX_WELCOME);
break;
default:
send_post($object, 'r');
break;
}
break;
default:
$object->sendText("暂无设置此功能"); //否则,显示出错信息
}
}
//获取博客文章
function wm_query_posts($q, $s = "")
{
global $wp_query;
$articles = [];
$query_base = array(
'ignore_sticky_posts' => true,
'posts_per_page' => POSTNUM,
'post_status' => 'publish',
);
if (empty($s)) {
switch ($q) {
case "n":
$query_more = array(
"order" => "DESC",
"orderby" => "date",
);
break;
case "r":
$query_more = array(
"orderby" => "rand",
);
break;
default:
$query_more = [];
break;
}
} else {
$query_more = array(
's' => $s,
);
}
$weixin_query_array = array_merge($query_base, $query_more);
$wp_query->query($weixin_query_array);
if (have_posts()) {
while (have_posts()) {
the_post();
global $post;
$title = get_the_title();
$excerpt = gdk_print_excerpt(120, $post, false);
$thumbnail_id = get_post_thumbnail_id($post->ID);
if ($thumbnail_id) {
$thumb = wp_get_attachment_image_src($thumbnail_id, 'full');
$thumb = $thumb[0];
} else {
$thumb = gdk_thumbnail_src();
}
if (empty(DEFAULT_THUMB) && !empty(DEFAULT_THUMB)) {
$thumb = DEFAULT_THUMB;
}
$link = get_permalink();
$articles[] = array($title, $excerpt, $link, $thumb);
if ($messageType == "text") {
$keyword = trim($content);
if (in_string($keyword, '验证码')) {
$object->sendText('您的验证码为:【' . wx_captcha() . '】验证码有效期为2分钟请抓紧使用过期需重新申请');
}
}
return $articles;
}
function send_post(WeChat $object, $type = '', $value = '')
{
$articles = wm_query_posts($type, $value);
if (empty($articles)) {
$no_post = '暂无相关文章';
$object->sendText($no_post);
}
foreach ($articles as $v) {
$object->addNews($v['0'], $v['1'], $v['2'], $v['3']);
}
$object->sendNews();
}

View file

@ -380,9 +380,9 @@ jQuery(function ($) {
function(a) {
a = $.trim(a);
if (a == '200') {
alert("测试成功,您的SMTP邮箱邮件发送已成功,Enjoy it");
alert("测试成功您的SMTP邮箱邮件发送已成功Enjoy it");
}else{
alert("测试失败,您的SMTP邮箱邮件响应失败,请重试");
alert("测试失败您的SMTP邮箱邮件响应失败请重试");
}
});
});
@ -401,7 +401,7 @@ jQuery(function ($) {
$(".install_new_version").show();
}
}else{
$(".get_new_version").after("检测失败,网络错误");
$(".get_new_version").after("检测失败网络错误");
}
});
});

View file

@ -759,17 +759,17 @@ function gdk_panlinks($links)
//一个简单可重复使用的邮件模板
function mail_temp($mail_title, $mail_cotent, $link, $link_title)
{
?>
<div style="width:500px;margin:auto">
<div style="background:#2695f3;color:#FFF;padding:20px 10px;"><?php echo $mail_title; ?></div>
<div style="padding:10px;margin:5px;border-bottom:dashed 1px #ddd;"><?php echo $mail_cotent; ?></div>
<a href="<?php echo $link; ?>" style="display:block;margin:auto;margin-top:40px;padding:10px;width:107px;outline:0;border:1px solid #2695f3;border-radius:25px;color:#2695f3;text-align:center;font-weight:700;font-size:14pxtext-decoration:none;" rel="noopener" target="_blank"><?php echo $link_title; ?></a>
$content = '<div style="width:500px;margin:auto">
<div style="background:#2695f3;color:#FFF;padding:20px 10px;">' . $mail_title . '</div>
<div style="padding:10px;margin:5px;border-bottom:dashed 1px #ddd;">' . $mail_cotent . '</div>
<a href="' . $link . '" style="display:block;margin:auto;margin-top:40px;padding:10px;width:120px;outline:0;border:1px solid #2695f3;border-radius:25px;color:#2695f3;text-align:center;font-weight:700;font-size:14px;text-decoration:none;" rel="noopener" target="_blank">' . $link_title . '</a>
<br><br>
<div style="color:#cecece;font-size: 12px;">本邮件为系统自动发送,请勿回复。<br>
如果不想被此类邮件打扰,请前往 <a style="color: #cecece;" href="<?php echo home_url(); ?>" rel="noopener" target="_blank"><?php echo get_option('blogname'); ?></a> 留言说明,由我们来操作处理。
</div>
</div>
<?php
如果不想被此类邮件打扰,请前往 <a style="color: #cecece;" href="' . home_url() . '" rel="noopener" target="_blank">' . get_option('blogname') . '</a> 留言说明,由我们来操作处理。
</div></div>';
return $content;
}
//获取所有站点分类id,带缓存
@ -829,10 +829,12 @@ function gdk_thumbnail_src()
{
global $post;
$gdk_thumbnail_src = '';
if ($values = get_post_custom_values('gdk_thumb')) { //输出自定义域图片地址
if ($values = get_post_custom_values('gdk_thumb')) {
//输出自定义域图片地址
$values = get_post_custom_values('gdk_thumb');
$gdk_thumbnail_src = $values[0];
} elseif (has_post_thumbnail()) { //如果有特色缩略图,则输出缩略图地址
} elseif (has_post_thumbnail()) {
//如果有特色缩略图,则输出缩略图地址
$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
$gdk_thumbnail_src = $thumbnail_src[0];
} else {
@ -840,7 +842,8 @@ function gdk_thumbnail_src()
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$gdk_thumbnail_src = $matches[1][0]; //获取该图片 src
if (empty($gdk_thumbnail_src)) { //如果日志中没有图片,则显示随机图片
if (empty($gdk_thumbnail_src)) {
//如果日志中没有图片,则显示随机图片
$random = mt_rand(1, 12);
$gdk_thumbnail_src = GDK_BASE_URL . 'assets/img/thumb/' . $random . '.jpg';
}
@ -861,7 +864,8 @@ function gdk_thumbnail_src()
function gdk_thumb_img($way, $width, $height, $style = '', $atrr = 'class="thumb_img"')
{
$url = gdk_thumbnail_src();
if ($way === 1) { //cdn
if ($way === 1) {
//cdn
$src = $url . '!' . $style;
} elseif ($way === 2) {
$src = GDK_BASE_URL . 'public/timthumb.php?src=' . $url . '&h=' . $height . '&w=' . $width . '&q=90&zc=1&ct=1';
@ -1028,7 +1032,8 @@ function payjs_notify()
//充值按钮
function buy_points()
{
if (is_user_logged_in()) { //logined
if (is_user_logged_in()) {
//logined
$result = '
<a data-fancybox="pay_fancybox" data-src="#pay_fancybox" href="javascript:;" class="cm-btn primary">点击充值</a>
<form id="pay_fancybox" name="pay_form" style="display: none; width: 100%; max-width: 500px;" class="pure-form">
@ -1049,7 +1054,8 @@ function buy_points()
<input data-fancybox-close type="button" id="submit_pay" data-action="pay_points" data-id="' . get_current_user_id() . '" class="cm-btn primary" value="提交">
</p>
</form>';
} else { // no login
} else {
// no login
$result = '<div class=\'cm-alert error\'>本页面需要您登录才可以操作,请先 ' . weixin_login_btn() . ' 或者<a href="' . esc_url(wp_registration_url()) . '">立即注册</a></div>';
}
return $result;
@ -1322,4 +1328,4 @@ function blank_open()
echo 'target="_blank"';
}
}
}

View file

@ -163,14 +163,14 @@ function gdk_link_go($content)
//邮箱SMTP设置
function gdk_smtp($phpmailer)
{
$phpmailer->FromName = gdk_option('gdk_smtp_mail'); //邮箱地址
$phpmailer->FromName = gdk_option('gdk_smtp_username'); //邮箱地址
$phpmailer->Host = gdk_option('gdk_smtp_host'); //服务器地址
$phpmailer->Port = gdk_option('gdk_smtp_port'); //端口
$phpmailer->Username = gdk_option('gdk_smtp_username'); //昵称
$phpmailer->Username = gdk_option('gdk_smtp_mail'); //昵称
$phpmailer->Password = gdk_option('gdk_smtp_password'); //密码
$phpmailer->From = gdk_option('gdk_smtp_mail'); //邮箱地址
$phpmailer->SMTPAuth = true;
$phpmailer->SMTPSecure = 'ssl';
$phpmailer->SMTPSecure = 'ssl';
$phpmailer->IsSMTP();
}

View file

@ -321,7 +321,7 @@ function log_login($username, $password)
if (is_wp_error($check)) {
$ua = getBrowser();
$agent = $ua['name'] . " " . $ua['version'];
$agent = $ua['name'];
$referrer = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : $_SERVER['PHP_SELF'];
if (strstr($referrer, 'wp-login')) {
@ -334,7 +334,7 @@ function log_login($username, $password)
$contact_errors = false;
// get the posted data
$name = "WordPress " . get_bloginfo('name');
$name = get_bloginfo('name');
$email_address = get_bloginfo('admin_email');
// write the email content
@ -342,23 +342,23 @@ function log_login($username, $password)
$header .= "Content-Type: text/html; charset=utf-8\n";
$header .= "From: $name <$email_address>\n";
$message = "Failed login attempt on <a href='" . get_site_url() . "/" . $ref . "' target='_blank'>" . $name . "</a><br>" . PHP_EOL;
$message .= 'IP: <a href="http://whatismyipaddress.com/ip/' . gdk_get_ip() . '" target="_blank">' . gdk_get_ip() . "</a><br>" . PHP_EOL;
$message .= 'WhoIs: <a href="https://who.is/whois-ip/ip-address/' . gdk_get_ip() . '" target="_blank">' . gdk_get_ip() . "</a><br>" . PHP_EOL;
$message = "<a href='" . home_url() . "' target='_blank'>" . $name . "</a> 登陆失败提醒<br>" . PHP_EOL;
$message .= "Browser: " . $agent . "<br>" . PHP_EOL;
$message .= "OS: " . $ua['platform'] . "<br>" . PHP_EOL;
$message .= 'IP: <a href="https://www.ipip.net/ip/'. gdk_get_ip() . '.html" target="_blank">'. gdk_get_ip() . '</a><br>' . PHP_EOL;
$message .= "Date: " . date('Y-m-d H:i:s') . "<br>" . PHP_EOL;
$message .= "Referrer: " . $referrer . "<br>" . PHP_EOL;
$message .= "User Agent: " . $ua['userAgent'] . "<br>" . PHP_EOL;
$message .= "Username: " . $username . "<br>" . PHP_EOL;
$message .= "Password: " . $password . "<br>" . PHP_EOL;
$subject = "Failed login attempt - " . $name;
$subject = "=?utf-8?B?" . base64_encode($subject) . "?=";
$subject = "登陆失败提醒 - " . $name;
$to = $email_address;
$mail_content = mail_temp($subject, $message , home_url(), $name );
if (!empty($to)) {
// send the email using wp_mail()
if (!wp_mail($to, $subject, $message, $header)) {
if (!wp_mail($to, $subject, $mail_content , $header)) {
$contact_errors = true;
}
}