WordPress主题纯代码实现邀请码功能
网站实现邀请码功能有很多很多用处,具体什么用处,相信大家都知道,就不多说了,直接说怎么实现吧。以下代码来自某工作室 。
思路:
需要新建一个数据表来保存邀请码。
后台需要两个页面:邀请码列表、添加邀请码。
添加邀请码的时候需要能设置前缀,一次生成多个邀请码,邀请码长度可自定义,每个邀请码使用次数可设置。
数据表
code:邀请码、max:邀请码使用次数、users:使用这个验证码的所有用户、status:验证码是否可用。
WordPress主题纯代码实现邀请码功能
数据库操作
数据库操作部分代码包括:1. 建立数据库。2.对数据的获取、增加、删除、更改等操作。
<?php
//第一次启用主题时执行
function ashuwp_load_theme() {
global $pagenow;
if ( is_admin() && 'themes.php' == $pagenow && isset( $_GET['activated'] ) ){
ashuwp_invitation_code_install();
}
}
add_action( 'load-themes.php', 'ashuwp_load_theme' );
//建立数据表
function ashuwp_invitation_code_install(){
global $wpdb;
$table_name = $wpdb->prefix . 'invitation_code';
if( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name ) :
$sql = " CREATE TABLE `".$wpdb->prefix."invitation_code` (
`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`code` varchar(40),
`max` INT NOT NULL,
`users` varchar(20),
`status` tinyint
) ENGINE = MYISAM DEFAULT CHARSET=utf8;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
endif;
}
//插入数据
function ashuwp_insert_invitation_code( $code, $max = 1, $users, $status){
global $wpdb;
if($code==''){
return false;
}
$code = trim($code);
$code_exists = ashuwp_check_invitation_code($code);
if(!$code_exists){
$insert = "insert into ".$wpdb->prefix."invitation_code (code,max,users,status) values( '$code', '$max', '','1')";
$wpdb->query($insert);
return true;
}else{
return false;
}
}
//检查邀请码是否已存在
function ashuwp_check_invitation_code( $code ){
global $wpdb;
$sql = "select * from ".$wpdb->prefix."invitation_code where code='$code'";
$result = $wpdb->get_results($sql);
if(!empty($result)){
return true;
}else{
return false;
}
}
//获取邀请码
function ashuwp_get_invitation_code($per_page=50, $page=1){
global $wpdb;
$page = (int)$page;
$per_page = (int)$per_page;
if(!$page){
$page = 1;
}
if(!$per_page){
$per_page = 50;
}
$begin = $per_page*($page-1);
$end = $per_page*$page;
$sql = "select * from ".$wpdb->prefix."invitation_code limit $begin,$end";
$results = $wpdb->get_results($sql,'ARRAY_A');
return $results;
}
//邀请码的删除、启用、禁用。
function ashuwp_operation_invitation_code( $id, $action ){
global $wpdb;
$id = (int)$id;
if(!$id){
return false;
}
if(!in_array($action,array('delete','deactive','active'))){
return false;
}
if($action =='delete'){
$sql = "delete from ".$wpdb->prefix ."invitation_code where id='$id'";
}
if($action =='deactive'){
$sql = "update ".$wpdb->prefix ."invitation_code set status=0 where id='$id'";
}
if($action =='active'){
$sql = "update ".$wpdb->prefix ."invitation_code set status=1 where id='$id'";
}
$result = $wpdb->query($sql);
if($result){
return true;
}else{
return false;
}
}
网站后台
网站后台部分代码包括:1. 邀请码列表页面。2. 邀请码增加页面。
<?php
class ashuwp_invitation_code_admin {
static public $instance;
public function __construct(){
add_action( 'admin_menu', array(&$this, 'ashuwp_invitation_code_menu') );
}
function ashuwp_invitation_code_menu(){
add_menu_page( '邀请码', '邀请码', 'manage_options', 'invitation_code', array(&$this, 'invitation_code_list'),'',27);
add_submenu_page('invitation_code', '添加邀请码', '添加邀请码', 'manage_options', 'invitation_code_add', array(&$this, 'invitation_code_add'));
}
function invitation_code_list(){
if( isset($_GET['code_action']) && in_array($_GET['code_action'],array('delete','deactive','active')) && isset($_GET['code_id']) ){
$code_id = (int)$_GET['code_id'];
if(!$code_id){
return;
}
$result = ashuwp_operation_invitation_code( $code_id, $_GET['code_action'] );
}
$code_lists = ashuwp_get_invitation_code(999,1);
?>
<div class="wrap">
<h1 class="wp-heading-inline">邀请码</h1>
<a href="<?php%20echo%20admin_url('admin.php?page=invitation_code_add');?>" class="page-title-action">添加</a>
<hr class="wp-header-end">
<?php
if(isset($result)){
if($result){
?>
<div id="message" class="notice notice-success">操作成功。</div>
<?php
}else{
?>
<div id="message" class="notice notice-error">操作失败。</div>
<?php
}
}
?>
<ul class="subsubsub"><li class="all">全部<span class="count">(<?php echo count($code_lists); ?>)</span></ul>
<table class="wp-list-table widefat fixed">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">邀请码</th>
<th scope="col">统计(最大使用数/已使用)</th>
<th scope="col">用户</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<?php
if($code_lists){
foreach($code_lists as $code){
$users = array();
if(!empty($code['users'])){
$users = explode( ',', $code['users'] );
}
$used = count($users);
?>
<tr>
<td><?php echo $code['id']; ?></td>
<td>
<?php echo $code['code']; ?>
<?php
if(empty($code['status']) || !$code['status']){
echo '-已禁用';
}
?>
</td>
<td>
<?php echo $code['max'].'/'.$used; ?>
</td>
<td>
<?php
foreach( $users as $user_id ){
$user = get_user_by('id', $user_id);
if(!empty($user)){
?>
<a href="<?php%20echo%20admin_url(%20'user-edit.php?user_id='.$user->ID%20);%20?>"><?php echo $user->user_login; ?></a>,
<?php
}
}
?>
</td>
<td>
<a href="<?php%20echo%20admin_url(%20'admin.php?page=invitation_code&code_action=delete&code_id='.$code['id']%20);%20?>">删除</a>
<?php
if(empty($code['status']) || !$code['status']){
?>
<a href="<?php%20echo%20admin_url(%20'admin.php?page=invitation_code&code_action=active&code_id='.$code['id']%20);%20?>">启用</a>
<?php
}else{
?>
<a href="<?php%20echo%20admin_url(%20'admin.php?page=invitation_code&code_action=deactive&code_id='.$code['id']%20);%20?>">禁用</a>
<?php
}
?>
</td>
</tr>
<?php
}
}
?>
</tbody>
<tfoot>
<tr>
<th scope="col">ID</th>
<th scope="col">邀请码</th>
<th scope="col">统计</th>
<th scope="col">用户</th>
<th scope="col">操作</th>
</tr>
</tfoot>
</table>
<div class="tablenav bottom">
<div class="tablenav-pages">
<span class="pagination-links">
</span>
</div>
</div>
</div>
<?php
}
function invitation_code_add(){
$data_codes = ashuwp_get_invitation_code(999,1);
$code_list = array();
foreach($data_codes as $code){
$code_list[] = $code['code'];
}
if(isset($_REQUEST['submit']) && isset($_REQUEST['ashuwp_invitation_code_field']) && check_admin_referer('ashuwp_invitation_code_action', 'ashuwp_invitation_code_field') ) {
$code_prefix = '';
if(!empty($_POST['code_prefix'])){
$code_prefix = trim($_POST['code_prefix']);
}
$code_length = '';
if(!empty($_POST['code_length'])){
$code_length = (int)$_POST['code_length'];
}
if(!$code_length){
$code_length = 8;
}
$code_number = 1;
if(!empty($_POST['code_number'])){
$code_number = (int)$_POST['code_number'];
}
if(!$code_number){
$code_number = 1;
}
$code_counter = '';
if(!empty($_POST['code_counter'])){
$code_counter = (int)$_POST['code_counter'];
}
if(!$code_counter){
$code_counter = 1;
}
$i=1;
$code_tem = array();
while ( $i <= $code_number ){
$tem = strtoupper( $code_prefix . wp_generate_password( $code_length, false ) );
if(!in_array($tem,$code_list)){
$i++;
$code_tem[] = $tem;
ashuwp_insert_invitation_code( $tem, $code_counter, '', 1);
}
}
}
?>
<div class="wrap">
<h1 class="wp-heading-inline">添加邀请码</h1>
<a href="<?php%20echo%20admin_url(%20'admin.php?page=invitation_code_add'%20);%20?>" class="page-title-action">添加</a>
<hr class="wp-header-end">
<?php
if(!empty($code_tem)){
?>
<div id="message" class="notice notice-success">
<p>邀请码添加成功:</p>
<?php
foreach($code_tem as $text){
echo '<p>'.$text.'</p>';
}
?>
</div>
<?php
}
?>
<form action="" method="post">
<table class="form-table">
<tbody>
<tr>
<td><label for="code_prefix">邀请码前缀</label></td>
<td>
<input type="text" id="code_prefix" name="code_prefix" class="regular-text" value=""/>
<p class="description">前缀可不填。</p>
</td>
</tr>
<tr>
<td><label for="code_length">邀请码字符长度</label></td>
<td>
<input type="text" id="code_length" name="code_length" class="regular-text" value=""/>
<p class="description">字符长度不包括前缀,默认8个字符。</p>
</td>
</tr>
<tr>
<td><label for="code_number">邀请码个数</label></td>
<td>
<input type="text" id="code_number" name="code_number" class="regular-text" value=""/>
<p class="description">本次生成多少个邀请码,默认1个。</p>
</td>
</tr>
<tr>
<td><label for="code_counter">允许使用的次数</label></td>
<td>
<input type="text" id="code_counter" name="code_counter" class="regular-text" value=""/>
<p class="description">每个邀请码允许使用的次数,默认1次。</p>
</td>
</tr>
</tbody>
</table>
<p class="submit">
<?php wp_nonce_field( 'ashuwp_invitation_code_action','ashuwp_invitation_code_field' ); ?>
<input type="submit" name="submit" id="submit" class="button button-primary" value="生成邀请码">
</p>
</form>
</div>
<?php
}
}
$invitation_code = new ashuwp_invitation_code_admin();
效果展示
1. 增加邀请码
WordPress主题纯代码实现邀请码功能
2. 邀请码列表
WordPress主题纯代码实现邀请码功能
上面范例代码,仅做参考使用,可根据实际自行优化,具体如何使用邀请码也请自行斟酌。
本文链接:http://78moban.cn/post/9498.html
版权声明:站内所有文章皆来自网络转载,只供模板演示使用,并无任何其它意义!