WordPress教程

WordPress 纯代码实现用户积分系统

阿里云

去年开发了一款站长社区的 WordPress 主题,主题君用纯代码做了 WordPres 用户积分系统,答应过宁总要把教程写出来的,今天算是有时间整理了。

首先声明:核心代码搬运了 ashuwp 的教程,然后自己集成了一些自己的代码,代码比较复杂,没有一定基础的朋友不要轻易尝试。

整理思路:

也想出现在这里?联系我们
创客主机

1. 需要准备两个数据表。

(1) 积分动态表,用来记录所有用户的积分增减情况。

(2) 用户积分总表,用来记录用户的积分总量,当然用户积分总数可以记录到 usermeta 数据表中,所以这个表不是必须的。

2. 后台需要 3 个页面。

(1) 积分动态,从“积分动态表”中获取数据,展示用户积分动态。

(2) 用户积分,从“用户积分表”中获取数据,方便查看用户的积分总量。

(3) 积分增减页面,用于给用户增减积分。

下面教程开始吧:

一、新建数据表

1. 积分动态表 points_activity 中的字段有 id,用户 id,积分(异动数),描述,余额,时间。

2. 用户积分表 points 中的字段就两个:用户 id,积分数。

  1. //在第一次启用主题时执行。
  2. function ashuwp_load_theme() {
  3.   global $pagenow;
  4.   if ( is_admin() && 'themes.php' == $pagenow && isset( $_GET['activated'] ) ){
  5.     ashuwp_points_install();
  6.   }
  7. }
  8. add_action( 'load-themes.php', 'ashuwp_load_theme' );
  9. //新建数据表points_activity和points
  10. function ashuwp_points_install(){
  11.   global $wpdb;
  12.   $table_name = $wpdb->prefix . 'points_activity'; //积分动态表
  13.   $table_name2 = $wpdb->prefix . 'points'; //积分汇总表
  14.   $charset_collate = $wpdb->get_charset_collate();
  15.   if( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name ) :
  16.     $sql = " CREATE TABLE `".$table_name."` (
  17.       `id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  18.       `user_id` BIGINT(40),
  19.       `points` INT NOT NULL,
  20.       `description` longtext,
  21.       `balance` INT NOT NULL,
  22.       `date` DATETIME NOT NULL
  23.       ) $charset_collate;";
  24.     require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  25.     dbDelta($sql);
  26.   endif;
  27.   if( $wpdb->get_var("SHOW TABLES LIKE '$table_name2'") != $table_name2 ) :
  28.     $sql2 = " CREATE TABLE `".$table_name2."` (
  29.       `user_id` BIGINT(40) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  30.       `points` INT NOT NULL
  31.     ) $charset_collate;";
  32.     dbDelta($sql2);
  33.   endif;
  34. }

二、数据库操作函数

  1. //根据用户id获取某个用户的积分数
  2. function ashuwp_get_user_points($user_id){
  3.   global $wpdb;
  4.   $user = get_user_by('ID', $user_id);
  5.   if(!$user){
  6.     return 0;
  7.   }
  8.   $points_table = $wpdb->prefix . 'points';
  9.   $sql = "select points from ".$points_table." where user_id={$user_id}";
  10.   $result = $wpdb->get_var($sql);
  11.   if(!$result){
  12.     return 0;
  13.   }else{
  14.     return $result;
  15.   }
  16. }
  17. //更新(新增)用户积分.
  18. function ashuwp_update_user_points($user_id, $new_points){
  19.   global $wpdb;
  20.   $user = get_user_by('ID', $user_id);
  21.   if(!$user){
  22.     $msg = array(
  23.       'state' => 'error',
  24.       'msg' => 'User Error',
  25.     );
  26.     return $msg;
  27.   }
  28.   if( !is_numeric($new_points)|| $new_points<0){
  29.     $msg = array(
  30.       'state' => 'error',
  31.       'msg' => 'Points not number or points error',
  32.     );
  33.     return $msg;
  34.   }
  35.   $points_table = $wpdb->prefix . 'points';
  36.   $points_exist = $wpdb->get_var( "select count(*) from {$points_table} where user_id='{$user_id}'" );
  37.   if($points_exist){
  38.     $sql = "update {$points_table} set points='{$new_points}' where user_id='{$user_id}'";
  39.   }else{
  40.     $sql = "insert into {$points_table} ( user_id, points ) values( '{$user_id}', '{$new_points}' )";;
  41.   }
  42.   $result = $wpdb->query($sql);
  43.   if($result){
  44.     $msg = array(
  45.       'state' => 'succeed',
  46.       'msg' => 'Points Updated.',
  47.     );
  48.     return $msg;
  49.   }else{
  50.     $msg = array(
  51.       'state' => 'error',
  52.       'msg' => 'Points update failed.',
  53.     );
  54.     return $msg;
  55.   }
  56. }
  57. //从用户积分表获取数据,后台积分汇总页面需要获取数据。
  58. function ashuwp_get_points( $args=array() ){
  59.   global $wpdb;
  60.   $defaults = array(
  61.     'per_page' => '50',
  62.     'paged' => '1',
  63.   );
  64.   $args = wp_parse_args( $args, $defaults );
  65.   $page = (int)$args['paged'];
  66.   $per_page = (int)$args['per_page'];
  67.   if(!$page){
  68.     $page = 1;
  69.   }
  70.   if(!$per_page){
  71.     $per_page = 50;
  72.   }
  73.   $begin = $per_page*($page-1);
  74.   $end = $per_page*$page;
  75.   $points_table = $wpdb->prefix . 'points';
  76.   $sql = "select * from $points_table order by user_id asc limit $begin,$end";
  77.   $results = $wpdb->get_results($sql,'ARRAY_A');
  78.   return $results;
  79. }
  80. //统计数据条数,后台积分汇总页面需要获取数据。
  81. function ashuwp_count_points(){
  82.   global $wpdb;
  83.   $points_table = $wpdb->prefix . 'points';
  84.   $sql = "select count(*) from $points_table";
  85.   $results = $wpdb->get_var($sql);
  86.   return $results;
  87. }

然后是对积分动态表的操作仅需要增加即可,不需要删除。

  1. /*增加入一条积分动态*/
  2. function ashuwp_add_points_activity( $args=array() ){
  3.   global $wpdb;
  4.   $defaults = array(
  5.     'user_id' => '',
  6.     'action' => '',
  7.     'points' => '',
  8.     'description' => '',
  9.     'date' => ''
  10.   );
  11.   $args = wp_parse_args( $args, $defaults );
  12.   //判断用户id是否合法
  13.   $user = get_user_by('ID', $args['user_id']);
  14.   if(!$user){
  15.     $msg = array(
  16.       'state' => 'error',
  17.       'msg' => 'User Error',
  18.     );
  19.     return $msg;
  20.   }
  21.   //仅增加和减少两种操作。
  22.   if( !in_array( $args['action'], array( 'add', 'reduce' ) )){
  23.     $msg = array(
  24.       'state' => 'error',
  25.       'msg' => 'Operate Error',
  26.     );
  27.     return $msg;
  28.   }
  29.   //检测积分异动数是否合法
  30.   if( !is_int($args['points']) ){
  31.     $msg = array(
  32.       'state' => 'error',
  33.       'msg' => 'Points Error',
  34.     );
  35.     return $msg;
  36.   }
  37.   //处理描述
  38.   $args['description'] = sanitize_text_field($args['description']);
  39.   //处理异动数,和计算余额
  40.   $old_points = (int)ashuwp_get_user_points($args['user_id']);
  41.   if($args['action']=='add'){
  42.     $balance = $old_points+$args['points'];
  43.     $change = $args['points'];
  44.     if($balance!=$old_points){
  45.       //将新余额更新到用户积分表。
  46.       $update = ashuwp_update_user_points($args['user_id'],$balance);
  47.     }
  48.   }
  49.   if($args['action']=='reduce'){
  50.     $balance = $old_points-$args['points'];
  51.     $change = -$args['points']; //若是减少,数据库中保存为负数。
  52.     if($balance!=$old_points){
  53.       $update = ashuwp_update_user_points($args['user_id'],$balance);
  54.     }
  55.   }
  56.   if( ($balance!=$old_points) && $update['state'] != 'succeed' ){
  57.     $msg = array(
  58.       'state' => 'error',
  59.       'msg' => $update['msg'],
  60.     );
  61.     return $msg;
  62.   }
  63.   $table_name = $wpdb->prefix . 'points_activity';
  64.   //插入数据
  65.   $args['date'] = date( "Y-m-d H:i:s", time());
  66.   $sql = "insert into $table_name ( user_id, points, description, balance, date ) values( '{$args['user_id']}', '{$change}', '{$args['description']}', '{$balance}', '{$args['date']}' )";
  67.   $result = $wpdb->query($sql);
  68.   if($result){
  69.     $msg = array(
  70.       'state' => 'succeed',
  71.       'msg' => 'succeed!',
  72.     );
  73.     return $msg;
  74.   }else{
  75.     //若动态插入失败,将用户积分表回滚。
  76.     ashuwp_update_user_points($args['user_id'],$old_points);
  77.     $msg = array(
  78.       'state' => 'error',
  79.       'msg' => 'Insert Error',
  80.     );
  81.     return $msg;
  82.   }
  83. }
  84. /*从积分动态表中获取数据,后台页面中需要。
  85. * 需支持条件查询方便后台管理。
  86. */
  87. function ashuwp_get_points_activity( $args=array() ){
  88.   global $wpdb;
  89.   $defaults = array(
  90.     'user_id' => '',
  91.     'per_page' => '50',
  92.     'paged' => '1',
  93.     'action' => ''
  94.   );
  95.   $args = wp_parse_args( $args, $defaults );
  96.   //处理页码
  97.   $page = (int)$args['paged'];
  98.   $per_page = (int)$args['per_page'];
  99.   if(!$page){
  100.     $page = 1;
  101.   }
  102.   if(!$per_page){
  103.     $per_page = 50;
  104.   }
  105.   $begin = $per_page*($page-1);
  106.   $end = $per_page*$page;
  107.   $table_name = $wpdb->prefix . 'points_activity';
  108.   $sql = "select * from $table_name where 1=1 ";
  109.   //查询用户id
  110.   if($args['user_id']!=''){
  111.     $user_id = (int)$args['user_id'];
  112.     $sql .= "and user_id='{$user_id}' ";
  113.   }
  114.   //查询操作种类
  115.   if( in_array( $args['action'], array( 'add', 'reduce', 'remain' ) ) ){
  116.     if($args['action']=='add'){
  117.       $sql .= "and points>0 ";
  118.     }
  119.     if($args['action']=='reduce'){
  120.       $sql .= "and points<0 ";
  121.     }
  122.     if($args['action']=='remain'){
  123.       $sql .= "and points=0 ";
  124.     }
  125.   }
  126.   $sql .= "order by id desc limit $begin,$end";
  127.   $results = $wpdb->get_results($sql,'ARRAY_A');
  128.   return $results;
  129. }
  130. /*统计积分动态的记录数
  131. * 加入统计条件方便后台管理
  132. */
  133. function ashuwp_count_points_activity( $args=array() ){
  134.   global $wpdb;
  135.   $defaults = array(
  136.     'user_id' => '',
  137.     'action' => ''
  138.   );
  139.   $args = wp_parse_args( $args, $defaults );
  140.   $table_name = $wpdb->prefix . 'points_activity';
  141.   $sql = "select count(*) from $table_name where 1=1 ";
  142.   //统计用户
  143.   if($args['user_id']!=''){
  144.     $user_id = (int)$args['user_id'];
  145.     $sql .= "and user_id='{$user_id}' ";
  146.   }
  147.   //统计操作
  148.   if( in_array( $args['action'], array( 'add', 'reduce', 'remain' ) ) ){
  149.     if($args['action']=='add'){
  150.       $sql .= "and points>0 ";
  151.     }
  152.     if($args['action']=='reduce'){
  153.       $sql .= "and points<0 ";
  154.     }
  155.     if($args['action']=='remain'){
  156.       $sql .= "and points=0 ";
  157.     }
  158.   }
  159.   $results = $wpdb->get_var($sql);
  160.   return $results;
  161. }

三、新建后台积分页面

1. 积分动态页面。

后台页面设计到 class-wp-list-table 类的应用,就不一一加注释了。

  1. if(!class_exists('WP_List_Table')) {
  2.     require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
  3. }
  4. class Ashuwp_Points_Activity_Table extends WP_List_Table {
  5.   function __construct(){
  6.     parent::__construct( array(
  7.       'singular'  => 'Points Activity',
  8.       'plural'    => 'Points Activity',
  9.       'ajax'      => false
  10.     ) );
  11.   }
  12.   function column_default( $item, $column_name ) {
  13.     switch ( $column_name ){
  14.       case 'id':
  15.       case 'user':
  16.       case 'points':
  17.       case 'balance':
  18.       case 'description':
  19.       case 'date':
  20.         return $item[ $column_name ];
  21.       default:
  22.         return print_r($item,true);
  23.     }
  24.   }
  25.   function get_columns() {
  26.     $columns = array(
  27.       'id'       => 'ID',
  28.       'user'    => 'User',
  29.       'points'      => 'Points',
  30.       'balance'     => 'Balance',
  31.       'description' => 'Description',
  32.       'date'    => 'Date',
  33.     );
  34.     return $columns;
  35.   }
  36.   function format_activity( $datas ) {
  37.     $return_datas = array();
  38.     foreach( $datas as $data ){
  39.       $user = get_user_by('id', $data['user_id']);
  40.       $item_array = array();
  41.       $item_array['id'] = $data['id'];
  42.       $item_array['user'] = $user->user_login;
  43.       if($data['points']<0){
  44.         $item_array['points'] = '<span class="reduce">'.$data['points'].'</span>';
  45.       }elseif($data['points']>0){
  46.         $item_array['points'] = '<span class="add">+'.$data['points'].'</span>';
  47.       }else{
  48.         $item_array['points'] = '<span class="remain">'.$data['points'].'</span>';
  49.       }
  50.       $item_array['description'] = $data['description'];
  51.       $item_array['balance'] = $data['balance'];
  52.       $item_array['date'] = $data['date'];
  53.       $return_datas[] = $item_array;
  54.     }
  55.     return $return_datas;
  56.   }
  57.   function prepare_items() {
  58.     $this->_column_headers = $this->get_column_info();
  59.     $per_page     = $this->get_items_per_page( 'customers_per_page', 50 );
  60.     $current_page = $this->get_pagenum();
  61.     $total_items  = 0;
  62.     $args = array(
  63.       'per_page' => $per_page,
  64.       'paged' => $current_page,
  65.     );
  66.     if( isset( $_GET['user_name'] ) && !empty( trim($_GET['user_name']) ) ){
  67.       $user = get_user_by( 'login', trim($_GET['user_name']) );
  68.       if( !empty($user)){
  69.         $args['user_id'] = $user->ID;
  70.       }
  71.     }
  72.     if( isset( $_GET['action_name'] ) && !empty( trim($_GET['action_name']) ) ){
  73.       if( in_array( $_GET['action_name'], array( 'add', 'reduce', 'remain' ) ) ){
  74.         $args['action'] = $_GET['action_name'];
  75.       }
  76.     }
  77.     $total_items  = ashuwp_count_points_activity($args);
  78.     $datas = ashuwp_get_points_activity($args);
  79.     $this->items = $this->format_activity($datas);
  80.     $this->set_pagination_args( array(
  81.       'total_items' => $total_items,
  82.       'per_page'    => $per_page,
  83.       'total_pages' => ceil($total_items/$per_page)
  84.     ) );
  85.   }
  86. }
  87. class ashuwp_points_activity_admin {
  88.   static public $instance;
  89.   public $points_activity_obj;
  90.   private function __construct(){
  91.     add_filter( 'set-screen-option', array( $this, 'set_screen' ), 10, 3 );
  92.     add_action( 'admin_menu', array( $this, 'ashuwp_points_activity_menu') );
  93.   }
  94.   private function __clone() {
  95.   }
  96.   function ashuwp_points_activity_menu() {
  97.     //svg图
  98.     $dollor_ico = '';
  99.     //建立积分动态页面
  100.     $hook = add_menu_page( 'Points Activity', 'Points Activity', 'manage_options', 'points_activity', array(&$this, 'points_activity_list'),'data:image/svg+xml;base64,'.$dollor_ico, 9);
  101.     add_action( "load-$hook", array( $this, 'screen_option' ) );
  102.   }
  103.   function set_screen( $status, $option, $value ) {
  104.     return $value;
  105.   }
  106.   function screen_option() {
  107.     $option = 'per_page';
  108.     $args   = array(
  109.       'label'   => 'Customers',
  110.       'default' => 30,
  111.       'option'  => 'customers_per_page'
  112.     );
  113.     add_screen_option( $option, $args );
  114.     $this->points_activity_obj = new Ashuwp_Points_Activity_Table();
  115.   }
  116.   function points_activity_list(){
  117.     $all = ashuwp_count_points_activity();
  118.     $points_add = ashuwp_count_points_activity( array( 'action'=>'add' ) );
  119.     $points_reduce = ashuwp_count_points_activity( array( 'action'=>'reduce' ) );
  120.     $points_remain = ashuwp_count_points_activity( array( 'action'=>'remain' ) );
  121.   ?>
  122.     <div class="wrap">
  123.       <h1 class="wp-heading-inline">Points Activity</h1>
  124.       <a href="<?php echo admin_url( 'admin.php?page=points_activity_add' ); ?>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="page-title-action">Add/Reduce</a>
  125.       <?php
  126.       if ( ! empty( $_GET['user_name'] ) ) {
  127.         printf( '<span class="subtitle">' . __('Search results for &#8220;%s&#8221;') . '</span>', esc_html( $_GET['user_name'] ) );
  128.       }
  129.       ?>
  130.       <hr class="wp-header-end">
  131.       <ul class="subsubsub">
  132.         <?php
  133.         if( !empty( $_GET['action_name'] ) && in_array( trim($_GET['action_name']), array( 'add', 'reduce', 'remain' ) ) ){
  134.           $now = trim($_GET['action_name']);
  135.         }else{
  136.           $now = 'all';
  137.         }
  138.         $current = 'class="current"';
  139.         ?>
  140.         <li class="all"><a <?php if($now=='all'){ echo $current; } ?> href="<?php echo admin_url( 'admin.php?page=points_activity' ); ?>" rel="external nofollow" >All<span class="count">(<?php echo $all; ?>)</span></a> |</li>
  141.         <li class="add"><a <?php if($now=='add'){ echo $current; } ?> href="<?php echo admin_url( 'admin.php?page=points_activity&action_name=add' ); ?>" rel="external nofollow" >Add<span class="count">(<?php echo $points_add; ?>)</span></a> |</li>
  142.         <li class="reduce"><a <?php if($now=='reduce'){ echo $current; } ?> href="<?php echo admin_url( 'admin.php?page=points_activity&action_name=reduce' ); ?>" rel="external nofollow" >Reduce<span class="count">(<?php echo $points_reduce; ?>)</span></a> |</li>
  143.         <li class="remain"><a <?php if($now=='remain'){ echo $current; } ?> href="<?php echo admin_url( 'admin.php?page=points_activity&action_name=remain' ); ?>" rel="external nofollow" >Remain<span class="count">(<?php echo $points_remain; ?>)</span></a></li>
  144.       </ul>
  145.       <form id="points-activity-filter" method="get" action="">
  146.         <style>
  147.         th.column-id,
  148.         td.column-id,
  149.         th.column-user,
  150.         td.column-user,
  151.         th.column-points,
  152.         td.column-points,
  153.         th.column-balance,
  154.         td.column-balance {
  155.           width:10%;
  156.         }
  157.         .column-points .add {
  158.           color:#46b450;
  159.         }
  160.         .column-points .reduce {
  161.           color:#e74c3c;
  162.         }
  163.         </style>
  164.         <p class="search-box">
  165.           <label class="screen-reader-text" for="code-search-input">User Search</label>
  166.           <input type="search" id="code-search-input" name="user_name" value="" />
  167.           <?php submit_button( 'Search', 'button', false, false, array('id' => 'search-submit') ); ?>
  168.           <input type="hidden" name="page" value="points_activity" />
  169.         </p>
  170.         <?php
  171.         $this->points_activity_obj->prepare_items();
  172.         $this->points_activity_obj->display();
  173.         ?>
  174.       </form>
  175.     </div>
  176.   <?php
  177.   }
  178.   public static function get_instance() {
  179.     if ( ! isset( self::$instance ) ) {
  180.       self::$instance = new self();
  181.     }
  182.     return self::$instance;
  183.   }
  184. }
  185. ashuwp_points_activity_admin::get_instance();

2. 用户积分页面

与积分动态页面类似,用户积分页面也需要用表格展示,也不一一注释。

  1. if(!class_exists('WP_List_Table')) {
  2.     require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
  3. }
  4. class Ashuwp_User_Points_Table extends WP_List_Table {
  5.   function __construct(){
  6.     parent::__construct( array(
  7.       'singular'  => 'User Points',
  8.       'plural'    => 'User Points',
  9.       'ajax'      => false
  10.     ) );
  11.   }
  12.   function column_default( $item, $column_name ) {
  13.     switch ( $column_name ){
  14.       case 'user_id':
  15.       case 'user_login':
  16.       case 'points':
  17.         return $item[ $column_name ];
  18.       default:
  19.         return print_r($item,true);
  20.     }
  21.   }
  22.   function get_columns() {
  23.     $columns = array(
  24.       'user_id'       => 'User ID',
  25.       'user_login'    => 'User Name',
  26.       'points'      => 'Points',
  27.     );
  28.     return $columns;
  29.   }
  30.   function format_datas( $datas ) {
  31.     $return_datas = array();
  32.     foreach( $datas as $data ){
  33.       $user = get_user_by('id', $data['user_id']);
  34.       $item_array = array();
  35.       $item_array['user_id'] = $data['user_id'];
  36.       $item_array['user_login'] = $user->user_login;
  37.       $item_array['points'] = $data['points'];
  38.       $return_datas[] = $item_array;
  39.     }
  40.     return $return_datas;
  41.   }
  42.   function prepare_items() {
  43.     $this->_column_headers = $this->get_column_info();
  44.     $per_page     = $this->get_items_per_page( 'customers_per_page', 50 );
  45.     $current_page = $this->get_pagenum();
  46.     $total_items  = 0;
  47.     $args = array(
  48.       'per_page' => $per_page,
  49.       'paged' => $current_page,
  50.     );
  51.     $total_items  = ashuwp_count_points();
  52.     $datas = ashuwp_get_points($args);
  53.     $this->items = $this->format_datas($datas);
  54.     $this->set_pagination_args( array(
  55.       'total_items' => $total_items,
  56.       'per_page'    => $per_page,
  57.       'total_pages' => ceil($total_items/$per_page)
  58.     ) );
  59.   }
  60. }
  61. class ashuwp_user_points_admin {
  62.   static public $instance;
  63.   public $user_points_obj;
  64.   private function __construct(){
  65.     add_filter( 'set-screen-option', array( $this, 'set_screen' ), 10, 3 );
  66.     add_action( 'admin_menu', array( $this, 'ashuwp_user_points_menu') );
  67.   }
  68.   private function __clone() {
  69.   }
  70.   function ashuwp_user_points_menu() {
  71.     $hook = add_submenu_page('points_activity', 'User Points', 'User Points', 'manage_options', 'user_points', array(&$this, 'user_points'));
  72.     add_action( "load-$hook", array( $this, 'screen_option' ) );
  73.   }
  74.   function set_screen( $status, $option, $value ) {
  75.     return $value;
  76.   }
  77.   function screen_option() {
  78.     $option = 'per_page';
  79.     $args   = array(
  80.       'label'   => 'Customers',
  81.       'default' => 30,
  82.       'option'  => 'customers_per_page'
  83.     );
  84.     add_screen_option( $option, $args );
  85.     $this->user_points_obj = new Ashuwp_User_Points_Table();
  86.   }
  87.   function user_points(){
  88.     $all = ashuwp_count_points();
  89.   ?>
  90.     <div class="wrap">
  91.       <h1 class="wp-heading-inline">User Points</h1>
  92.       <a href="<?php echo admin_url( 'admin.php?page=points_activity_add' ); ?>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="page-title-action">Add/Reduce</a>
  93.       <hr class="wp-header-end">
  94.       <ul class="subsubsub">
  95.         <li class="all"><a class="current" href="<?php echo admin_url( 'admin.php?page=user_points' ); ?>" rel="external nofollow"  rel="external nofollow" >All<span class="count">(<?php echo $all; ?>)</span></a></li>
  96.       </ul>
  97.       <form id="points-activity-filter" method="get">
  98.         <?php
  99.         $this->user_points_obj->prepare_items();
  100.         $this->user_points_obj->display();
  101.         ?>
  102.       </form>
  103.     </div>
  104.   <?php
  105.   }
  106.   public static function get_instance() {
  107.     if ( ! isset( self::$instance ) ) {
  108.       self::$instance = new self();
  109.     }
  110.     return self::$instance;
  111.   }
  112. }
  113. ashuwp_user_points_admin::get_instance();

3. 积分增减页面

  1. if(!class_exists('WP_List_Table')) {
  2.     require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
  3. }
  4. class Ashuwp_User_Points_Table extends WP_List_Table {
  5.   function __construct(){
  6.     parent::__construct( array(
  7.       'singular'  => 'User Points',
  8.       'plural'    => 'User Points',
  9.       'ajax'      => false
  10.     ) );
  11.   }
  12.   function column_default( $item, $column_name ) {
  13.     switch ( $column_name ){
  14.       case 'user_id':
  15.       case 'user_login':
  16.       case 'points':
  17.         return $item[ $column_name ];
  18.       default:
  19.         return print_r($item,true);
  20.     }
  21.   }
  22.   function get_columns() {
  23.     $columns = array(
  24.       'user_id'       => 'User ID',
  25.       'user_login'    => 'User Name',
  26.       'points'      => 'Points',
  27.     );
  28.     return $columns;
  29.   }
  30.   function format_datas( $datas ) {
  31.     $return_datas = array();
  32.     foreach( $datas as $data ){
  33.       $user = get_user_by('id', $data['user_id']);
  34.       $item_array = array();
  35.       $item_array['user_id'] = $data['user_id'];
  36.       $item_array['user_login'] = $user->user_login;
  37.       $item_array['points'] = $data['points'];
  38.       $return_datas[] = $item_array;
  39.     }
  40.     return $return_datas;
  41.   }
  42.   function prepare_items() {
  43.     $this->_column_headers = $this->get_column_info();
  44.     $per_page     = $this->get_items_per_page( 'customers_per_page', 50 );
  45.     $current_page = $this->get_pagenum();
  46.     $total_items  = 0;
  47.     $args = array(
  48.       'per_page' => $per_page,
  49.       'paged' => $current_page,
  50.     );
  51.     $total_items  = ashuwp_count_points();
  52.     $datas = ashuwp_get_points($args);
  53.     $this->items = $this->format_datas($datas);
  54.     $this->set_pagination_args( array(
  55.       'total_items' => $total_items,
  56.       'per_page'    => $per_page,
  57.       'total_pages' => ceil($total_items/$per_page)
  58.     ) );
  59.   }
  60. }
  61. class ashuwp_user_points_admin {
  62.   static public $instance;
  63.   public $user_points_obj;
  64.   private function __construct(){
  65.     add_filter( 'set-screen-option', array( $this, 'set_screen' ), 10, 3 );
  66.     add_action( 'admin_menu', array( $this, 'ashuwp_user_points_menu') );
  67.   }
  68.   private function __clone() {
  69.   }
  70.   function ashuwp_user_points_menu() {
  71.     $hook = add_submenu_page('points_activity', 'User Points', 'User Points', 'manage_options', 'user_points', array(&$this, 'user_points'));
  72.     add_action( "load-$hook", array( $this, 'screen_option' ) );
  73.   }
  74.   function set_screen( $status, $option, $value ) {
  75.     return $value;
  76.   }
  77.   function screen_option() {
  78.     $option = 'per_page';
  79.     $args   = array(
  80.       'label'   => 'Customers',
  81.       'default' => 30,
  82.       'option'  => 'customers_per_page'
  83.     );
  84.     add_screen_option( $option, $args );
  85.     $this->user_points_obj = new Ashuwp_User_Points_Table();
  86.   }
  87.   function user_points(){
  88.     $all = ashuwp_count_points();
  89.   ?>
  90.     <div class="wrap">
  91.       <h1 class="wp-heading-inline">User Points</h1>
  92.       <a href="<?php echo admin_url( 'admin.php?page=points_activity_add' ); ?>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="page-title-action">Add/Reduce</a>
  93.       <hr class="wp-header-end">
  94.       <ul class="subsubsub">
  95.         <li class="all"><a class="current" href="<?php echo admin_url( 'admin.php?page=user_points' ); ?>" rel="external nofollow"  rel="external nofollow" >All<span class="count">(<?php echo $all; ?>)</span></a></li>
  96.       </ul>
  97.       <form id="points-activity-filter" method="get">
  98.         <?php
  99.         $this->user_points_obj->prepare_items();
  100.         $this->user_points_obj->display();
  101.         ?>
  102.       </form>
  103.     </div>
  104.   <?php
  105.   }
  106.   public static function get_instance() {
  107.     if ( ! isset( self::$instance ) ) {
  108.       self::$instance = new self();
  109.     }
  110.     return self::$instance;
  111.   }
  112. }
  113. ashuwp_user_points_admin::get_instance();

在遇到某个操作比如:充值成功、购买成功、发表评论,执行 ashuwp_add_points_activity 函数即可。

范例一:充值成功增加积分。

  1. //准备参数
  2. $user_id = 1; //用户id
  3. $points = 10; //积分数
  4. $desc = '充值:'.$points;
  5. $args = array(
  6.   'user_id' => $user_id,
  7.   'action' => 'add', //增加
  8.   'points' => $points,
  9.   'description' => $desc,
  10. );
  11. $chognzhi = ashuwp_add_points_activity($args);

四、结语

本教程的思路、代码仅供参考,代码为阿树从实际项目中剥离而来,在代码处理过程中,不敢保证不出错,特别是如果遇到有两个连续的 empty 请自行删除一个。

WordPress 纯代码实现用户积分系统

已有 245 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!