1
0
Fork 0
mirror of https://github.com/yunluo/gdk.git synced 2024-05-12 11:17:12 +02:00
gdk/class/points/class-points-shortcodes.php

181 lines
6.8 KiB
PHP
Raw Normal View History

2020-02-02 04:13:58 +01:00
<?php
/**
2021-10-10 15:48:53 +02:00
* class-points-shortcodes.php.
2020-03-15 14:29:06 +01:00
*/
class GDK_Points_Shortcodes
{
/**
* Add shortcodes.
*/
public static function init()
{
add_shortcode('points_users_list', [__CLASS__, 'points_users_list']);
add_shortcode('points_user_points', [__CLASS__, 'points_user_points']);
add_shortcode('pay', [__CLASS__, 'pay']);
add_shortcode('points_user_points_details', [__CLASS__, 'points_user_points_details']);
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
public static function points_users_list($atts, $content = null)
{
$options = shortcode_atts(
[
2021-10-10 15:48:53 +02:00
'limit' => 10,
2020-03-15 14:29:06 +01:00
'order_by' => 'points',
2021-10-10 15:48:53 +02:00
'order' => 'DESC',
2020-03-15 14:29:06 +01:00
],
$atts
);
extract($options);
2021-10-10 15:48:53 +02:00
$output = '';
2020-03-15 14:29:06 +01:00
$pointsusers = GDK_Points::get_users();
if (sizeof($pointsusers) > 0) {
foreach ($pointsusers as $pointsuser) {
$total = GDK_Points::get_user_total_points($pointsuser);
$output .= '<div class="points-user">';
$output .= '<span style="font-weight:bold;width:100%;" class="points-user-username">';
$output .= get_user_meta($pointsuser, 'nickname', true);
$output .= ':</span>';
$output .= '<span class="points-user-points">';
2021-10-10 15:48:53 +02:00
$output .= ' '.$total.' 金币';
2020-03-15 14:29:06 +01:00
$output .= '</span>';
$output .= '</div>';
}
} else {
$output .= '<p>No users</p>';
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
return $output;
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
public static function points_user_points($atts, $content = null)
{
2021-10-10 15:48:53 +02:00
$output = '';
$options = shortcode_atts(
['id' => ''],
2020-03-15 14:29:06 +01:00
$atts
);
extract($options);
2021-10-10 15:48:53 +02:00
if ('' == $id) {
2020-03-15 14:29:06 +01:00
$id = get_current_user_id();
}
2021-10-10 15:48:53 +02:00
if (0 !== $id) {
2020-03-15 14:29:06 +01:00
$points = GDK_Points::get_user_total_points($id, 'accepted');
$output .= $points;
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
return $output;
}
2020-02-02 04:13:58 +01:00
2021-10-10 15:48:53 +02:00
// 付费可见短代码开始
2020-02-02 04:13:58 +01:00
2020-03-15 14:29:06 +01:00
public static function pay($atts, $content = null)
{
2021-10-10 15:48:53 +02:00
$content = do_shortcode($content);
2020-03-15 14:29:06 +01:00
global $wpdb;
$user_id = get_current_user_id();
2021-10-10 15:48:53 +02:00
$pid = get_the_ID();
$result = $wpdb->get_row('SELECT description FROM '.GDK_Points_Database::points_get_table('users').' WHERE user_id='.$user_id.' AND description='.$pid." AND status='accepted' LIMIT 0, 3;", ARRAY_A)['description']; //验证是否支付
extract(shortcode_atts(['point' => '10'], $atts));
2020-03-15 14:29:06 +01:00
$notice = '';
add_post_meta($pid, '_point_content', $content, true) or update_post_meta($pid, '_point_content', $content); //没有新建,有就更新
if (is_user_logged_in()) {
if ($result == $pid || current_user_can('administrator')) {
$notice .= '<div class="cm-alert success">';
$notice .= $content;
$notice .= '</div>';
2020-02-02 04:13:58 +01:00
} else {
2020-03-15 14:29:06 +01:00
if (GDK_Points::get_user_total_points($user_id, 'accepted') < $point) {
2020-03-26 15:24:00 +01:00
$notice .= '<fieldset id="hide_notice" class="fieldset ta-center"><legend class="legend ta-left">付费内容</legend>';
2021-10-10 15:48:53 +02:00
$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>';
2020-03-15 14:29:06 +01:00
$notice .= buy_points();
$notice .= '</fieldset>';
} else {
2020-03-26 15:24:00 +01:00
$notice .= '<fieldset id="hide_notice" class="fieldset ta-center"><legend class="legend ta-left">付费内容</legend>';
2021-10-10 15:48:53 +02:00
$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>';
2020-03-15 14:29:06 +01:00
$notice .= '</fieldset>';
}
2020-02-02 04:13:58 +01:00
}
2020-03-15 14:29:06 +01:00
} else {
2020-03-26 15:24:00 +01:00
$notice .= '<fieldset id="hide_notice" class="fieldset ta-center"><legend class="legend ta-left">付费内容</legend>';
2021-10-10 15:48:53 +02:00
$notice .= '<p>当前隐藏内容需要支付</p><span class="cm-coin">'.$point.'金币</span>';
2020-03-15 14:29:06 +01:00
$notice .= '<p>您当前尚未登陆,请登陆后查看</p>';
$notice .= weixin_login_btn();
$notice .= '</fieldset>';
2020-02-02 04:13:58 +01:00
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
return $notice;
2020-02-02 04:13:58 +01:00
}
2021-10-10 15:48:53 +02:00
// 付费可见短代码结束
2020-03-15 14:29:06 +01:00
/**
* Shortcode. 显示用户的金币细节
2021-10-10 15:48:53 +02:00
*
* @param mixed $atts
* @param null|mixed $content
2020-03-15 14:29:06 +01:00
*/
public static function points_user_points_details($atts, $content = null)
{
$options = shortcode_atts(
[
2021-10-10 15:48:53 +02:00
'user_id' => '',
'order_by' => 'point_id',
'order' => 'DESC',
2020-03-15 14:29:06 +01:00
'description' => true,
],
$atts
);
extract($options);
date_default_timezone_set('Asia/Shanghai');
2021-10-10 15:48:53 +02:00
if (is_string($description) && (('0' == $description) || ('false' == strtolower($description)))) {
2020-03-15 14:29:06 +01:00
$description = false;
}
2020-02-02 04:13:58 +01:00
2020-03-15 14:29:06 +01:00
$desc_th = '';
if ($description) {
$desc_th = '<th>描述</th>';
}
global $wp_query;
$curauth = $wp_query->get_queried_object();
$user_id = $curauth->ID;
2021-10-10 15:48:53 +02:00
$points = GDK_Points::get_points_by_user($user_id);
$output = '<table class="points_user_points_table">'.
'<tr>'.
'<th>日期时间'.
'<th>金币</th>'.
'<th>类别</th>'.
'<th>状态</th>'.
$desc_th.
2020-03-15 14:29:06 +01:00
'</tr>';
2021-10-10 15:48:53 +02:00
if (0 !== $user_id) {
2020-03-15 14:29:06 +01:00
if (sizeof($points) > 0) {
foreach ($points as $point) {
$desc_td = '';
if ($description) {
2021-10-10 15:48:53 +02:00
$desc_td = '<td>'.$point->description.'</td>';
}
if ($point->points > 0) {
$leibie = '充值';
} elseif ($point->points < 0) {
$leibie = '消费';
2020-03-15 14:29:06 +01:00
}
2021-10-10 15:48:53 +02:00
$output .= '<tr>'.
'<td>'.$point->datetime.'</td>'.
'<td>'.$point->points.'</td>'.
'<td>'.$leibie.'</td>'.
'<td>'.$point->status.'</td>'.
$desc_td.
2020-03-15 14:29:06 +01:00
'</tr>';
}
}
}
2020-02-02 04:13:58 +01:00
2020-03-15 14:29:06 +01:00
$output .= '</table>';
2020-02-02 04:13:58 +01:00
2020-03-15 14:29:06 +01:00
return $output;
}
2020-02-02 04:13:58 +01:00
}
2020-03-15 14:29:06 +01:00
GDK_Points_Shortcodes::init();