Ashuwp invitaion code – 邀请码生成器

简单实用的wordpress邀请码、注册码生成器

Ashuwp_Invitation_Code是一个wordpress插件,它有助于为您的网站添加邀请代码。 wordpress用户还可以配合一些日志限制访问代码来做一个付费阅读网站。

要使用此插件需要开启wordpress的注册功能。

代码版

纯代码实现邀请码 😎 的思路:

  1. 需要新建一个数据表来保存邀请码。
  2. 后台需要两个页面:邀请码列表、添加邀请码。
  3. 添加邀请码的时候需要能设置前缀,一次生成多个邀请码,邀请码长度可自定义,每个邀请码使用次数可设置。

数据表

code:邀请码、max:邀请码使用次数、users:使用这个验证码的所有用户、status:验证码是否可用。

Ashuwp invitaion code - 邀请码生成器

数据库操作

数据库操作部分代码包括: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 echo admin_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 echo admin_url( 'user-edit.php?user_id='.$user->ID ); ?>"><?php echo $user->user_login; ?></a>,
<?php
}
}
?>
</td>
<td>
<a href="<?php echo admin_url( 'admin.php?page=invitation_code&code_action=delete&code_id='.$code['id'] ); ?>">删除</a>
<?php
if(empty($code['status']) || !$code['status']){
?>
<a href="<?php echo admin_url( 'admin.php?page=invitation_code&code_action=active&code_id='.$code['id'] ); ?>">启用</a>
<?php
}else{
?>
<a href="<?php echo admin_url( 'admin.php?page=invitation_code&code_action=deactive&code_id='.$code['id'] ); ?>">禁用</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 echo admin_url( 'admin.php?page=invitation_code_add' ); ?>" 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. 增加邀请码

Ashuwp invitaion code - 邀请码生成器

2. 邀请码列表

Ashuwp invitaion code - 邀请码生成器

结束语

上面范例代码,仅做参考使用,可根据实际自行优化,具体如何使用邀请码也请自行斟酌。

相关推荐

隐藏分类以及文章但登陆后可见 – WordPress教程

下载权限
查看
  • 免费下载
    评论并刷新后下载
    登录后下载
  • {{attr.name}}:
您当前的等级为
登录后免费下载登录 小黑屋反思中,不准下载! 评论后刷新页面下载评论 支付以后下载 请先登录 您今天的下载次数(次)用完了,请明天再来 支付积分以后下载立即支付 支付以后下载立即支付 您当前的用户组不允许下载升级会员
您已获得下载权限 您可以每天下载资源次,今日剩余

给TA赞赏
共{{data.count}}人
人已赞赏
⚠️
Npcink上的部份代码及教程来源于互联网,仅供网友学习交流,若您喜欢本文可附上原文链接随意转载。
无意侵害您的权益,请发送邮件至 1355471563#qq.com 或点击右侧 私信:Muze 反馈,我们将尽快处理。
购物车
优惠劵
搜索