2020-02-02 04:13:58 +01:00
< ? php
/**
* class - points - shortcodes . php
*/
2020-02-06 11:51:43 +01:00
class GDK_Points_Shortcodes {
2020-02-02 04:13:58 +01:00
/**
* 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' ] );
}
public static function points_users_list ( $atts , $content = null ) {
$options = shortcode_atts (
[
'limit' => 10 ,
'order_by' => 'points' ,
'order' => 'DESC'
],
$atts
);
extract ( $options );
$output = " " ;
2020-02-06 11:51:43 +01:00
$pointsusers = GDK_Points :: get_users ();
2020-02-02 04:13:58 +01:00
if ( sizeof ( $pointsusers ) > 0 ) {
foreach ( $pointsusers as $pointsuser ) {
2020-02-06 11:51:43 +01:00
$total = GDK_Points :: get_user_total_points ( $pointsuser );
2020-02-02 04:13:58 +01:00
$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">' ;
$output .= " " . $total . " 金币 " ;
$output .= '</span>' ;
$output .= '</div>' ;
}
} else {
$output .= '<p>No users</p>' ;
}
return $output ;
}
public static function points_user_points ( $atts , $content = null ) {
$output = " " ;
$options = shortcode_atts ([ 'id' => " " ],
$atts
);
extract ( $options );
if ( $id == " " ) {
$id = get_current_user_id ();
}
if ( $id !== 0 ) {
2020-02-06 11:51:43 +01:00
$points = GDK_Points :: get_user_total_points ( $id , 'accepted' );
2020-02-02 04:13:58 +01:00
$output .= $points ;
}
return $output ;
}
/*付费可见短代码开始*/
public static function pay ( $atts , $content = null ) {
global $wpdb ;
$user_id = get_current_user_id ();
$description = get_the_ID ();
2020-02-06 11:51:43 +01:00
$result = $wpdb -> get_row ( " SELECT description FROM " . GDK_Points_Database :: points_get_table ( " users " ) . " WHERE user_id= " . $user_id . " AND description= " . $description . " AND status='accepted' LIMIT 0, 3; " , ARRAY_A )[ 'description' ]; //验证是否支付
2020-02-02 04:13:58 +01:00
extract ( shortcode_atts ([ 'point' => " 10 " ], $atts ));
$notice = '' ;
$pay_content = get_post_meta ( $description , 'pay_content' , true );
if ( ! empty ( $pay_content ) && $pay_content != $content ) {
update_post_meta ( $description , 'pay_content' , $content , true );
} else {
add_post_meta ( $description , 'pay_content' , $content , true );
}
if ( is_user_logged_in ()) {
if ( $result == $description || current_user_can ( 'administrator' )) {
$notice .= '<div class="alert alert-info"">' ;
$notice .= $content ;
$notice .= '</div>' ;
} else {
2020-02-06 11:51:43 +01:00
if ( GDK_Points :: get_user_total_points ( $user_id , 'accepted' ) < $point ) {
2020-02-02 04:13:58 +01:00
$notice .= '<div class="alert alert-info"">' ;
$notice .= '<p style="color:red;">本段内容需要支付 ' . $point . '金币查看</p>' ;
2020-02-06 11:51:43 +01:00
$notice .= '<p style="color:red;">您当前拥有 <em><strong>' . GDK_Points :: get_user_total_points ( $user_id , 'accepted' ) . '</strong></em> 金币,您的金币不足,请充值</p>' ;
2020-02-05 07:47:55 +01:00
$notice .= '<p><a class="lhb" href="' . get_permalink ( gdk_page_id ( 'chongzhi' )) . '" target="_blank" rel="nofollow" data-original-title="立即充值" title="">立即充值</a></p>' ;
2020-02-02 04:13:58 +01:00
$notice .= '</div>' ;
} else {
$notice .= '<div id="pay_notice" class="alert alert-info"">' ;
2020-02-06 11:51:43 +01:00
$notice .= '<p style="color:red;">本段内容需要付费查看,您当前拥有 <em><strong>' . GDK_Points :: get_user_total_points ( $user_id , 'accepted' ) . '</strong></em> 金币</p>' ;
2020-02-02 04:13:58 +01:00
$notice .= '<p><a class="lhb" style="cursor: pointer;" onclick="pay_point();">点击购买</a></p>' ;
$notice .= '</div>' ;
$notice .= '<p id="pay_success"></p>' ;
echo ' < script type = " text/javascript " >
function pay_point () {
2020-02-06 11:51:43 +01:00
ajax . post ( " ' . admin_url('admin-ajax.php') . ' " , " action=gdk_pay_buy&point=' . $point . '&userid=' . $user_id . '&id=' . $description . ' " , function ( n ) {
2020-02-02 04:13:58 +01:00
null != n && ( document . getElementById ( " pay_notice " ) . style . display = " none " , document . getElementById ( " pay_success " ) . innerHTML = " <div class= \" alert alert-info \" > " + n + " </div> " );
});
} </ script > ' ;
}
}
} else {
global $wp ;
$current_url = home_url ( add_query_arg ([] , $wp -> request ));
$login_uri = '<a href="' . esc_url ( wp_login_url ( $current_url )) . '" data-original-title="点击登录">点击登录</a>' ;
$notice .= '<div class="alert alert-info"">' ;
$notice .= '<p style="color:red;">查看本段内容需要支付 ' . $point . ' 金币</p>' ;
$notice .= '<p style="color:red;">您尚未登录,请 ' . $login_uri . ' 或者 <a href="' . esc_url ( wp_registration_url ()) . '">立即注册</a> </p>' ;
$notice .= '</div>' ;
}
return $notice ;
}
/*付费可见短代码结束*/
/**
* Shortcode . 显示用户的金币细节
*/
public static function points_user_points_details ( $atts , $content = null ) {
$options = shortcode_atts (
[
'user_id' => '' ,
'order_by' => 'point_id' ,
'order' => 'DESC' ,
'description' => true
],
$atts
);
extract ( $options );
date_default_timezone_set ( 'Asia/Shanghai' );
if ( is_string ( $description ) && ( ( $description == '0' ) || ( strtolower ( $description ) == 'false' ) ) ) {
$description = false ;
}
$desc_th = '' ;
if ( $description ) {
$desc_th = '<th>描述</th>' ;
}
global $wp_query ;
$curauth = $wp_query -> get_queried_object ();
$user_id = $curauth -> ID ;
2020-02-06 11:51:43 +01:00
$points = GDK_Points :: get_points_by_user ( $user_id );
2020-02-02 04:13:58 +01:00
$output = '<table class="points_user_points_table">' .
'<tr>' .
'<th>日期时间' .
'<th>金币</th>' .
'<th>类别</th>' .
'<th>状态</th>' .
$desc_th .
'</tr>' ;
if ( $user_id !== 0 ) {
if ( sizeof ( $points ) > 0 ) {
foreach ( $points as $point ) {
$desc_td = '' ;
if ( $description ) {
$desc_td = '<td>' . $point -> description . '</td>' ;
}
if ( $point -> points > 0 ){ $leibie = '充值' ;} elseif ( $point -> points < 0 ){ $leibie = '消费' ;}
$output .= '<tr>' .
'<td>' . $point -> datetime . '</td>' .
'<td>' . $point -> points . '</td>' .
'<td>' . $leibie . '</td>' .
'<td>' . $point -> status . '</td>' .
$desc_td .
'</tr>' ;
}
}
}
$output .= '</table>' ;
return $output ;
}
}
2020-02-06 11:51:43 +01:00
GDK_Points_Shortcodes :: init ();