| Server IP : 43.141.49.119 / Your IP : 113.219.202.141 Web Server : Apache System : Linux VM-8-5-opencloudos 6.6.34-9.oc9.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jun 19 19:35:45 CST 2024 x86_64 User : www ( 1000) PHP Version : 8.1.27 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /www/wwwroot/www.ucppt.com/wp-content/plugins/wpcom-vip-redeem/includes/ |
Upload File : |
<?php
defined( 'ABSPATH' ) || exit;
class WVRD_Redeem {
public static $lifetimes = array(
'week' => '周付',
'month' => '月付',
'season' => '季付',
'halfyear' => '半年付',
'year' => '年付',
'lifetime' => '终身',
);
/**
* 生成兑换码
*/
public static function generate_codes( $vip_type, $vip_lifetime, $count = 1, $expired_at = '', $remark = '' ) {
global $wpdb;
$table = $wpdb->prefix . WVRD_TABLE;
$codes = array();
$now = current_time( 'mysql' );
$exp = $expired_at ? date( 'Y-m-d H:i:s', strtotime( $expired_at ) ) : null;
for ( $i = 0; $i < intval( $count ); $i++ ) {
$code = strtoupper( implode( '-', str_split( substr( md5( uniqid( mt_rand(), true ) ), 0, 16 ), 4 ) ) );
$wpdb->insert( $table, array(
'code' => $code,
'vip_type' => $vip_type,
'vip_lifetime' => $vip_lifetime,
'status' => 0,
'expired_at' => $exp,
'remark' => sanitize_text_field( $remark ),
'created_at' => $now,
) );
$codes[] = $code;
}
return $codes;
}
/**
* 兑换码核销,开通会员
*/
public static function redeem( $code, $user_id ) {
global $wpdb;
$table = $wpdb->prefix . WVRD_TABLE;
if ( ! $user_id ) {
return new WP_Error( 'not_login', '请先登录后再兑换。' );
}
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE code = %s LIMIT 1", strtoupper( trim( $code ) ) ) );
if ( ! $row ) {
return new WP_Error( 'invalid', '兑换码不存在,请检查后重试。' );
}
if ( (int) $row->status === 1 ) {
return new WP_Error( 'used', '该兑换码已被使用。' );
}
if ( (int) $row->status === 2 ) {
return new WP_Error( 'disabled', '该兑换码已被禁用。' );
}
if ( $row->expired_at && strtotime( $row->expired_at ) < time() ) {
return new WP_Error( 'expired', '该兑换码已过期。' );
}
// 验证 vip_type 是否存在
$vip_types = \WPCOM\Member\VIP::get_vip_types();
if ( ! isset( $vip_types[ $row->vip_type ] ) ) {
return new WP_Error( 'invalid_type', '兑换码对应的会员类型不存在,请联系管理员。' );
}
// 计算到期日
$end_date = self::calc_end_date( $user_id, $row->vip_type, $row->vip_lifetime );
// 写入用户 VIP 数据
$begin_date = current_time( 'Y-m-d' );
$current_end = get_user_option( 'vip_end_date', $user_id );
if ( $current_end && strtotime( $current_end ) > time() ) {
$begin_date = $current_end;
}
update_user_option( $user_id, 'vip_type', $row->vip_type );
update_user_option( $user_id, 'vip_begin_date', $begin_date );
update_user_option( $user_id, 'vip_end_date', $end_date );
// 标记兑换码已使用
$wpdb->update( $table, array(
'status' => 1,
'used_by' => $user_id,
'used_at' => current_time( 'mysql' ),
), array( 'id' => $row->id ) );
$vip_title = $vip_types[ $row->vip_type ]['title'];
$lifetime_label = isset( self::$lifetimes[ $row->vip_lifetime ] ) ? self::$lifetimes[ $row->vip_lifetime ] : $row->vip_lifetime;
return array(
'vip_title' => $vip_title,
'lifetime' => $lifetime_label,
'end_date' => $end_date,
'end_date_fmt' => date( 'Y年m月d日', strtotime( $end_date ) ),
);
}
/**
* 计算新到期日(支持叠加)
*/
public static function calc_end_date( $user_id, $vip_type, $vip_lifetime ) {
$current_type = get_user_option( 'vip_type', $user_id );
$current_end = get_user_option( 'vip_end_date', $user_id );
$base = ( $current_type === $vip_type && $current_end && strtotime( $current_end ) > time() )
? $current_end
: current_time( 'Y-m-d' );
if ( $vip_lifetime === 'lifetime' ) {
return '9999-12-31';
}
$map = array(
'week' => '+7 days',
'month' => '+1 month',
'season' => '+3 months',
'halfyear' => '+6 months',
'year' => '+1 year',
);
$offset = isset( $map[ $vip_lifetime ] ) ? $map[ $vip_lifetime ] : '+1 month';
return date( 'Y-m-d', strtotime( $offset, strtotime( $base ) ) );
}
/**
* 获取兑换码列表(后台用)
*/
public static function get_codes( $args = array() ) {
global $wpdb;
$table = $wpdb->prefix . WVRD_TABLE;
$where = '1=1';
$values = array();
if ( ! empty( $args['status'] ) && $args['status'] !== 'all' ) {
$where .= ' AND status = %d';
$values[] = intval( $args['status'] );
}
if ( ! empty( $args['search'] ) ) {
$where .= ' AND code LIKE %s';
$values[] = '%' . $wpdb->esc_like( $args['search'] ) . '%';
}
$per_page = isset( $args['per_page'] ) ? intval( $args['per_page'] ) : 20;
$page = isset( $args['page'] ) ? max( 1, intval( $args['page'] ) ) : 1;
$offset = ( $page - 1 ) * $per_page;
$sql = $values ? $wpdb->prepare( "SELECT * FROM {$table} WHERE {$where} ORDER BY id DESC LIMIT %d OFFSET %d", array_merge( $values, array( $per_page, $offset ) ) )
: "SELECT * FROM {$table} WHERE {$where} ORDER BY id DESC LIMIT {$per_page} OFFSET {$offset}";
$total_sql = $values ? $wpdb->prepare( "SELECT COUNT(*) FROM {$table} WHERE {$where}", $values )
: "SELECT COUNT(*) FROM {$table} WHERE {$where}";
return array(
'items' => $wpdb->get_results( $sql ),
'total' => (int) $wpdb->get_var( $total_sql ),
);
}
/**
* 更新兑换码状态
*/
public static function update_status( $id, $status ) {
global $wpdb;
$table = $wpdb->prefix . WVRD_TABLE;
return $wpdb->update( $table, array( 'status' => intval( $status ) ), array( 'id' => intval( $id ) ) );
}
/**
* 删除兑换码
*/
public static function delete_code( $id ) {
global $wpdb;
$table = $wpdb->prefix . WVRD_TABLE;
return $wpdb->delete( $table, array( 'id' => intval( $id ) ) );
}
}