wordpress用户积分的实现代码

WordPress教程 3817

对于开放了会员注册的wordpress网站而言,用户积分是提升会员活跃度的一个重要元素,给wordpress增添用户积分功能可以通过myCREDCubePoints等wordpress积分插件实现,同时也可以通过主题集成相关代码的方式实现,下面是阿树工作室分享的wordpress用户积分实现代码。

实现思路

需要准备两个数据表
  1. 积分动态表,用来记录所有用户的积分增减情况。
  2. 用户积分总表,用来记录用户的积分总量,当然用户积分总数可以记录到usermeta数据表中,所以这个表不是必须的。
后台需要3个页面
  1. 积分动态,从“积分动态表”中获取数据,展示用户积分动态。
  2. 用户积分,从“用户积分表”中获取数据,方便查看用户的积分总量。
  3. 积分增减页面,用于给用户增减积分。

操作步骤

新建数据表

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

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

在主题functions.php文件添加以下代码:

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

1、先是对用户积分表points的操作,用户积分所需要的操作仅需要更新、获取两个操作。

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

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

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

1、积分动态页面。

后台页面设计到class-wp-list-table类的应用。

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

2、用户积分页面

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

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

3、积分增减页面

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

实际应用

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

示例:充值成功增加积分。

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

温馨提示:本教程的思路、代码仅供参考,代码为原作者从实际项目中剥离而来,在代码处理过程中,难免会出现错误,因此如果遇到有两个连续的empty请自行删除一个。

教程原地址:http://www.ashuwp.com/courses/supplement/895.html

精品推荐: