wordpress用户积分的实现
敬告:代码供参考。
思路梳理:
1. 需要准备两个数据表。
(1) 积分动态表,用来记录所有用户的积分增减情况。
(2) 用户积分总表,用来记录用户的积分总量,当然用户积分总数可以记录到usermeta数据表中,所以这个表不是必须的。
2. 后台需要3个页面。
(1) 积分动态,从“积分动态表”中获取数据,展示用户积分动态。
(2) 用户积分,从“用户积分表”中获取数据,方便查看用户的积分总量。
(3) 积分增减页面,用于给用户增减积分。
一、新建数据表。
1. 积分动态表points_activity中的字段有id,用户id,积分(异动数),描述,余额,时间。
2. 用户积分表points中的字段就两个:用户id,积分数。
- //在第一次启用主题时执行。
- ashuwp_load_theme() {
- $pagenow;
- ( is_admin() && ‘themes.php’ == $pagenow && isset( $_GET[‘activated’] ) ){
- ashuwp_points_install();
- }
- }
- add_action( ‘load-themes.php’, ‘ashuwp_load_theme’ );
- //新建数据表points_activity和points
- ashuwp_points_install(){
- $wpdb;
- $table_name = $wpdb->prefix . ‘points_activity’; //积分动态表
- $table_name2 = $wpdb->prefix . ‘points’; //积分汇总表
- $charset_collate = $wpdb->get_charset_collate();
- ( $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;”;
- (ABSPATH . ‘wp-admin/includes/upgrade.php’);
- dbDelta($sql);
- ;
- ( $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);
- ;
- }
二、数据库操作函数
先是对用户积分表points的操作,用户积分所需要的操作仅需要更新、获取两个操作。
- //根据用户id获取某个用户的积分数
- ashuwp_get_user_points($user_id){
- $wpdb;
- $user = get_user_by(‘ID’, $user_id);
- (!$user){
- 0;
- }
- $points_table = $wpdb->prefix . ‘points’;
- $sql = “select points from “.$points_table.” where user_id={$user_id}”;
- $result = $wpdb->get_var($sql);
- (!$result){
- 0;
- }{
- $result;
- }
- }
- //更新(新增)用户积分.
- ashuwp_update_user_points($user_id, $new_points){
- $wpdb;
- $user = get_user_by(‘ID’, $user_id);
- (!$user){
- $msg = (
- ‘state’ => ‘error’,
- ‘msg’ => ‘User Error’,
- );
- $msg;
- }
- ( !is_numeric($new_points)|| $new_points<0){
- $msg = (
- ‘state’ => ‘error’,
- ‘msg’ => ‘Points not number points error’,
- );
- $msg;
- }
- $points_table = $wpdb->prefix . ‘points’;
- $points_exist = $wpdb->get_var( “select count(*) from {$points_table} where user_id='{$user_id}'” );
- ($points_exist){
- $sql = “update {$points_table} set points='{$new_points}’ where user_id='{$user_id}'”;
- }{
- $sql = “insert into {$points_table} ( user_id, points ) values( ‘{$user_id}’, ‘{$new_points}’ )”;;
- }
- $result = $wpdb->query($sql);
- ($result){
- $msg = (
- ‘state’ => ‘succeed’,
- ‘msg’ => ‘Points Updated.’,
- );
- $msg;
- }{
- $msg = (
- ‘state’ => ‘error’,
- ‘msg’ => ‘Points update failed.’,
- );
- $msg;
- }
- }
- //从用户积分表获取数据,后台积分汇总页面需要获取数据。
- ashuwp_get_points( $args=() ){
- $wpdb;
- $defaults = (
- ‘per_page’ => ’50’,
- ‘paged’ => ‘1’,
- );
- $args = wp_parse_args( $args, $defaults );
- $page = (int)$args[‘paged’];
- $per_page = (int)$args[‘per_page’];
- (!$page){
- $page = 1;
- }
- (!$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’);
- $results;
- }
- //统计数据条数,后台积分汇总页面需要获取数据。
- ashuwp_count_points(){
- $wpdb;
- $points_table = $wpdb->prefix . ‘points’;
- $sql = “select count(*) from $points_table”;
- $results = $wpdb->get_var($sql);
- $results;
- }
然后是对积分动态表的操作仅需要增加即可,不需要删除。
- /*增加入一条积分动态*/
- ashuwp_add_points_activity( $args=() ){
- $wpdb;
- $defaults = (
- ‘user_id’ => ”,
- ‘action’ => ”,
- ‘points’ => ”,
- ‘description’ => ”,
- ‘date‘ => ”
- );
- $args = wp_parse_args( $args, $defaults );
- //判断用户id是否合法
- $user = get_user_by(‘ID’, $args[‘user_id’]);
- (!$user){
- $msg = (
- ‘state’ => ‘error’,
- ‘msg’ => ‘User Error’,
- );
- $msg;
- }
- //仅增加和减少两种操作。
- ( !in_array( $args[‘action’], ( ‘add’, ‘reduce’ ) )){
- $msg = (
- ‘state’ => ‘error’,
- ‘msg’ => ‘Operate Error’,
- );
- $msg;
- }
- //检测积分异动数是否合法
- ( !is_int($args[‘points’]) ){
- $msg = (
- ‘state’ => ‘error’,
- ‘msg’ => ‘Points Error’,
- );
- $msg;
- }
- //处理描述
- $args[‘description’] = sanitize_text_field($args[‘description’]);
- //处理异动数,和计算余额
- $old_points = (int)ashuwp_get_user_points($args[‘user_id’]);
- ($args[‘action’]==’add’){
- $balance = $old_points+$args[‘points’];
- $change = $args[‘points’];
- ($balance!=$old_points){
- //将新余额更新到用户积分表。
- $update = ashuwp_update_user_points($args[‘user_id’],$balance);
- }
- }
- ($args[‘action’]==’reduce’){
- $balance = $old_points–$args[‘points’];
- $change = –$args[‘points’]; //若是减少,数据库中保存为负数。
- ($balance!=$old_points){
- $update = ashuwp_update_user_points($args[‘user_id’],$balance);
- }
- }
- ( ($balance!=$old_points) && $update[‘state’] != ‘succeed’ ){
- $msg = (
- ‘state’ => ‘error’,
- ‘msg’ => $update[‘msg’],
- );
- $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);
- ($result){
- $msg = (
- ‘state’ => ‘succeed’,
- ‘msg’ => ‘succeed!’,
- );
- $msg;
- }{
- //若动态插入失败,将用户积分表回滚。
- ashuwp_update_user_points($args[‘user_id’],$old_points);
- $msg = (
- ‘state’ => ‘error’,
- ‘msg’ => ‘Insert Error’,
- );
- $msg;
- }
- }
- /*从积分动态表中获取数据,后台页面中需要。
- * 需支持条件查询方便后台管理。
- */
- ashuwp_get_points_activity( $args=() ){
- $wpdb;
- $defaults = (
- ‘user_id’ => ”,
- ‘per_page’ => ’50’,
- ‘paged’ => ‘1’,
- ‘action’ => ”
- );
- $args = wp_parse_args( $args, $defaults );
- //处理页码
- $page = (int)$args[‘paged’];
- $per_page = (int)$args[‘per_page’];
- (!$page){
- $page = 1;
- }
- (!$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
- ($args[‘user_id’]!=”){
- $user_id = (int)$args[‘user_id’];
- $sql .= “and user_id='{$user_id}’ “;
- }
- //查询操作种类
- ( in_array( $args[‘action’], ( ‘add’, ‘reduce’, ‘remain’ ) ) ){
- ($args[‘action’]==’add’){
- $sql .= “and points>0 “;
- }
- ($args[‘action’]==’reduce’){
- $sql .= “and points<0 “;
- }
- ($args[‘action’]==’remain’){
- $sql .= “and points=0 “;
- }
- }
- $sql .= “order by id desc limit $begin,$end”;
- $results = $wpdb->get_results($sql,’ARRAY_A’);
- $results;
- }
- /*统计积分动态的记录数
- * 加入统计条件方便后台管理
- */
- ashuwp_count_points_activity( $args=() ){
- $wpdb;
- $defaults = (
- ‘user_id’ => ”,
- ‘action’ => ”
- );
- $args = wp_parse_args( $args, $defaults );
- $table_name = $wpdb->prefix . ‘points_activity’;
- $sql = “select count(*) from $table_name where 1=1 “;
- //统计用户
- ($args[‘user_id’]!=”){
- $user_id = (int)$args[‘user_id’];
- $sql .= “and user_id='{$user_id}’ “;
- }
- //统计操作
- ( in_array( $args[‘action’], ( ‘add’, ‘reduce’, ‘remain’ ) ) ){
- ($args[‘action’]==’add’){
- $sql .= “and points>0 “;
- }
- ($args[‘action’]==’reduce’){
- $sql .= “and points<0 “;
- }
- ($args[‘action’]==’remain’){
- $sql .= “and points=0 “;
- }
- }
- $results = $wpdb->get_var($sql);
- $results;
- }
三、建立后台页面
1. 积分动态页面。
后台页面设计到class-wp-list-table类的应用,就不一一加注释了。
- (!class_exists(‘WP_List_Table’)) {
- ( ABSPATH . ‘wp-admin/includes/-wp-list-table.php’ );
- }
- Ashuwp_Points_Activity_Table WP_List_Table {
- __construct(){
- parent::__construct( (
- ‘singular’ => ‘Points Activity’,
- ‘plural’ => ‘Points Activity’,
- ‘ajax’ => false
- ) );
- }
- column_default( $item, $column_name ) {
- ( $column_name ){
- ‘id’:
- ‘user’:
- ‘points’:
- ‘balance’:
- ‘description’:
- ‘date‘:
- $item[ $column_name ];
- :
- print_r($item,true);
- }
- }
- get_columns() {
- $columns = (
- ‘id’ => ‘ID’,
- ‘user’ => ‘User’,
- ‘points’ => ‘Points’,
- ‘balance’ => ‘Balance’,
- ‘description’ => ‘Description’,
- ‘date‘ => ‘Date‘,
- );
- $columns;
- }
- format_activity( $datas ) {
- $return_datas = ();
- ( $datas $data ){
- $user = get_user_by(‘id’, $data[‘user_id’]);
- $item_array = ();
- $item_array[‘id’] = $data[‘id’];
- $item_array[‘user’] = $user->user_login;
- ($data[‘points’]<0){
- $item_array[‘points’] = ‘<span =“reduce”>’.$data[‘points’].'</span>’;
- }($data[‘points’]>0){
- $item_array[‘points’] = ‘<span =“add”>+’.$data[‘points’].'</span>’;
- }{
- $item_array[‘points’] = ‘<span =“remain”>’.$data[‘points’].'</span>’;
- }
- $item_array[‘description’] = $data[‘description’];
- $item_array[‘balance’] = $data[‘balance’];
- $item_array[‘date‘] = $data[‘date‘];
- $return_datas[] = $item_array;
- }
- $return_datas;
- }
- 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 = (
- ‘per_page’ => $per_page,
- ‘paged’ => $current_page,
- );
- ( isset( $_GET[‘user_name’] ) && !( trim($_GET[‘user_name’]) ) ){
- $user = get_user_by( ‘login’, trim($_GET[‘user_name’]) );
- ( !($user)){
- $args[‘user_id’] = $user->ID;
- }
- }
- ( isset( $_GET[‘action_name’] ) && !( trim($_GET[‘action_name’]) ) ){
- ( in_array( $_GET[‘action_name’], ( ‘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( (
- ‘total_items’ => $total_items,
- ‘per_page’ => $per_page,
- ‘total_pages’ => ceil($total_items/$per_page)
- ) );
- }
- }
- ashuwp_points_activity_admin {
- $instance;
- $points_activity_obj;
- __construct(){
- add_filter( ‘set-screen-option’, ( $this, ‘set_screen’ ), 10, 3 );
- add_action( ‘admin_menu’, ( $this, ‘ashuwp_points_activity_menu’) );
- }
- __clone() {
- }
- ashuwp_points_activity_menu() {
- //svg图
- $dollor_ico = ‘PHN2ZyB2ZXJzaW9uPSIxLjEiIGlkPSJFYmVuZV8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCIKCSB3aWR0aD0iMTAwcHgiIGhlaWdodD0iMTAwcHgiIHZpZXdCb3g9IjAgMCAxMDAgMTAwIiBlbmFibGUtYmFja2dyb3VuZD0ibmV3IDAgMCAxMDAgMTAwIiB4bWw6c3BhY2U9InByZXNlcnZlIj4KPGNpcmNsZSBmaWxsPSIjQjJCMkIyIiBjeD0iNTAiIGN5PSI1MCIgcj0iNTAiLz4KPGc+Cgk8cGF0aCBkPSJNNDYuNzQ1LDc5LjUzOGMtMy44NjYtMC40MDItOC40NTgtMS40NDktMTIuMzI0LTMuNzA1bDIuMjU1LTYuNDQ0YzIuODE5LDIuMDk1LDYuNjg2LDMuNzg2LDEwLjM5MSw0LjM1bDEuMjA4LTIyLjg3NgoJCWMtNS45NjEtNS4wNzUtMTIuMjQ0LTEwLjM5MS0xMi4yNDQtMTguNzY5YzAtOC41MzksNi4yODMtMTMuMjEsMTMuOTM2LTEzLjc3NGwwLjQ4My04Ljc4aDUuMzE2bC0wLjQ4Myw5LjAyMQoJCWMyLjU3OCwwLjMyMiw1LjU1OSwxLjEyOCw4Ljg2MSwyLjQ5N2wtMS44NTMsNS42MzljLTIuMDE1LTEuMDQ3LTQuNzUzLTEuOTMzLTcuMzMxLTIuMzM2bC0xLjIwOCwyMS41ODgKCQljNi4xMjIsNS4xNTUsMTIuNjQ2LDEwLjcxMywxMi42NDYsMTkuNjU1YzAsOC40NTctNi4wNDEsMTMuMjktMTQuNDE5LDE0LjAxNmwtMC41NjMsMTAuMTQ5aC01LjE1NUw0Ni43NDUsNzkuNTM4eiBNNDguNzU5LDQxLjU5OQoJCWwwLjg4Ni0xNy4yMzhjLTMuNTQ0LDAuNjQ1LTYuMzY0LDIuOS02LjM2NCw3LjE2OUM0My4yODEsMzUuNDc3LDQ1LjYxOCwzOC42MTksNDguNzU5LDQxLjU5OXogTTUzLjI3LDU1LjEzMmwtMC45NjcsMTguNjA2CgkJYzQuMTg5LTAuODA1LDYuODQ4LTMuNzA1LDYuODQ4LTcuODk0UzU2LjY1Myw1OC4zNTQsNTMuMjcsNTUuMTMyeiIvPgo8L2c+Cjwvc3ZnPg==’;
- //建立积分动态页面
- $hook = add_menu_page( ‘Points Activity’, ‘Points Activity’, ‘manage_options’, ‘points_activity’, (&$this, ‘points_activity_list’),’data:image/svg+xml;base64,’.$dollor_ico, 9);
- add_action( “load-$hook”, ( $this, ‘screen_option’ ) );
- }
- set_screen( $status, $option, $value ) {
- $value;
- }
- screen_option() {
- $option = ‘per_page’;
- $args = (
- ‘label’ => ‘Customers’,
- ” => 30,
- ‘option’ => ‘customers_per_page’
- );
- add_screen_option( $option, $args );
- $this->points_activity_obj = Ashuwp_Points_Activity_Table();
- }
- points_activity_list(){
- $all = ashuwp_count_points_activity();
- $points_add = ashuwp_count_points_activity( ( ‘action’=>’add’ ) );
- $points_reduce = ashuwp_count_points_activity( ( ‘action’=>’reduce’ ) );
- $points_remain = ashuwp_count_points_activity( ( ‘action’=>’remain’ ) );
- ?>
- <div =“wrap”>
- <h1 =“wp-heading-inline”>Points Activity</h1>
- <a href=“<?php echo admin_url( ‘admin.php?page=points_activity_add’ ); ?>” =“page-title-action”>Add/Reduce</a>
- <?php
- ( ! ( $_GET[‘user_name’] ) ) {
- printf( ‘<span =“subtitle”>’ . __(‘Search results “%s”’) . ‘</span>’, esc_html( $_GET[‘user_name’] ) );
- }
- ?>
- <hr =“wp-header-end”>
- <ul =“subsubsub”>
- <?php
- ( !( $_GET[‘action_name’] ) && in_array( trim($_GET[‘action_name’]), ( ‘add’, ‘reduce’, ‘remain’ ) ) ){
- $now = trim($_GET[‘action_name’]);
- }{
- $now = ‘all’;
- }
- $current = ‘=“current”‘;
- ?>
- <li =“all”><a <?php ($now==’all’){ echo $current; } ?> href=“<?php echo admin_url( ‘admin.php?page=points_activity’ ); ?>”>All<span =“count”>(<?php echo $all; ?>)</span></a> |</li>
- <li =“add”><a <?php ($now==’add’){ echo $current; } ?> href=“<?php echo admin_url( ‘admin.php?page=points_activity&action_name=add’ ); ?>”>Add<span =“count”>(<?php echo $points_add; ?>)</span></a> |</li>
- <li =“reduce”><a <?php ($now==’reduce’){ echo $current; } ?> href=“<?php echo admin_url( ‘admin.php?page=points_activity&action_name=reduce’ ); ?>”>Reduce<span =“count”>(<?php echo $points_reduce; ?>)</span></a> |</li>
- <li =“remain”><a <?php ($now==’remain’){ echo $current; } ?> href=“<?php echo admin_url( ‘admin.php?page=points_activity&action_name=remain’ ); ?>”>Remain<span =“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 =“search-box”>
- <label =“screen-reader-text” =“code-search-input”>User Search</label>
- <input type=“search” id=“code-search-input” name=“user_name” value=“” />
- <?php submit_button( ‘Search’, ‘button’, false, false, (‘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
- }
- get_instance() {
- ( ! isset( self::$instance ) ) {
- self::$instance = self();
- }
- self::$instance;
- }
- }
- ashuwp_points_activity_admin::get_instance();
2. 用户积分页面
与积分动态页面类似,用户积分页面也需要用表格展示,也不一一注释。
- (!class_exists(‘WP_List_Table’)) {
- ( ABSPATH . ‘wp-admin/includes/-wp-list-table.php’ );
- }
- Ashuwp_User_Points_Table WP_List_Table {
- __construct(){
- parent::__construct( (
- ‘singular’ => ‘User Points’,
- ‘plural’ => ‘User Points’,
- ‘ajax’ => false
- ) );
- }
- column_default( $item, $column_name ) {
- ( $column_name ){
- ‘user_id’:
- ‘user_login’:
- ‘points’:
- $item[ $column_name ];
- :
- print_r($item,true);
- }
- }
- get_columns() {
- $columns = (
- ‘user_id’ => ‘User ID’,
- ‘user_login’ => ‘User Name’,
- ‘points’ => ‘Points’,
- );
- $columns;
- }
- format_datas( $datas ) {
- $return_datas = ();
- ( $datas $data ){
- $user = get_user_by(‘id’, $data[‘user_id’]);
- $item_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_datas;
- }
- 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 = (
- ‘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( (
- ‘total_items’ => $total_items,
- ‘per_page’ => $per_page,
- ‘total_pages’ => ceil($total_items/$per_page)
- ) );
- }
- }
- ashuwp_user_points_admin {
- $instance;
- $user_points_obj;
- __construct(){
- add_filter( ‘set-screen-option’, ( $this, ‘set_screen’ ), 10, 3 );
- add_action( ‘admin_menu’, ( $this, ‘ashuwp_user_points_menu’) );
- }
- __clone() {
- }
- ashuwp_user_points_menu() {
- $hook = add_submenu_page(‘points_activity’, ‘User Points’, ‘User Points’, ‘manage_options’, ‘user_points’, (&$this, ‘user_points’));
- add_action( “load-$hook”, ( $this, ‘screen_option’ ) );
- }
- set_screen( $status, $option, $value ) {
- $value;
- }
- screen_option() {
- $option = ‘per_page’;
- $args = (
- ‘label’ => ‘Customers’,
- ” => 30,
- ‘option’ => ‘customers_per_page’
- );
- add_screen_option( $option, $args );
- $this->user_points_obj = Ashuwp_User_Points_Table();
- }
- user_points(){
- $all = ashuwp_count_points();
- ?>
- <div =“wrap”>
- <h1 =“wp-heading-inline”>User Points</h1>
- <a href=“<?php echo admin_url( ‘admin.php?page=points_activity_add’ ); ?>” =“page-title-action”>Add/Reduce</a>
- <hr =“wp-header-end”>
- <ul =“subsubsub”>
- <li =“all”><a =“current” href=“<?php echo admin_url( ‘admin.php?page=user_points’ ); ?>”>All<span =“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
- }
- get_instance() {
- ( ! isset( self::$instance ) ) {
- self::$instance = self();
- }
- self::$instance;
- }
- }
- ashuwp_user_points_admin::get_instance();
3. 积分增减页面
- (!class_exists(‘WP_List_Table’)) {
- ( ABSPATH . ‘wp-admin/includes/-wp-list-table.php’ );
- }
- Ashuwp_User_Points_Table WP_List_Table {
- __construct(){
- parent::__construct( (
- ‘singular’ => ‘User Points’,
- ‘plural’ => ‘User Points’,
- ‘ajax’ => false
- ) );
- }
- column_default( $item, $column_name ) {
- ( $column_name ){
- ‘user_id’:
- ‘user_login’:
- ‘points’:
- $item[ $column_name ];
- :
- print_r($item,true);
- }
- }
- get_columns() {
- $columns = (
- ‘user_id’ => ‘User ID’,
- ‘user_login’ => ‘User Name’,
- ‘points’ => ‘Points’,
- );
- $columns;
- }
- format_datas( $datas ) {
- $return_datas = ();
- ( $datas $data ){
- $user = get_user_by(‘id’, $data[‘user_id’]);
- $item_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_datas;
- }
- 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 = (
- ‘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( (
- ‘total_items’ => $total_items,
- ‘per_page’ => $per_page,
- ‘total_pages’ => ceil($total_items/$per_page)
- ) );
- }
- }
- ashuwp_user_points_admin {
- $instance;
- $user_points_obj;
- __construct(){
- add_filter( ‘set-screen-option’, ( $this, ‘set_screen’ ), 10, 3 );
- add_action( ‘admin_menu’, ( $this, ‘ashuwp_user_points_menu’) );
- }
- __clone() {
- }
- ashuwp_user_points_menu() {
- $hook = add_submenu_page(‘points_activity’, ‘User Points’, ‘User Points’, ‘manage_options’, ‘user_points’, (&$this, ‘user_points’));
- add_action( “load-$hook”, ( $this, ‘screen_option’ ) );
- }
- set_screen( $status, $option, $value ) {
- $value;
- }
- screen_option() {
- $option = ‘per_page’;
- $args = (
- ‘label’ => ‘Customers’,
- ” => 30,
- ‘option’ => ‘customers_per_page’
- );
- add_screen_option( $option, $args );
- $this->user_points_obj = Ashuwp_User_Points_Table();
- }
- user_points(){
- $all = ashuwp_count_points();
- ?>
- <div =“wrap”>
- <h1 =“wp-heading-inline”>User Points</h1>
- <a href=“<?php echo admin_url( ‘admin.php?page=points_activity_add’ ); ?>” =“page-title-action”>Add/Reduce</a>
- <hr =“wp-header-end”>
- <ul =“subsubsub”>
- <li =“all”><a =“current” href=“<?php echo admin_url( ‘admin.php?page=user_points’ ); ?>”>All<span =“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
- }
- get_instance() {
- ( ! isset( self::$instance ) ) {
- self::$instance = self();
- }
- self::$instance;
- }
- }
- ashuwp_user_points_admin::get_instance();
四、实际应用
在遇到某个操作比如:充值成功、购买成功、发表评论,执行ashuwp_add_points_activity函数即可。
范例一:充值成功增加积分。
- //准备参数
- $user_id = 1; //用户id
- $points = 10; //积分数
- $desc = ‘充值:’.$points;
- $args = (
- ‘user_id’ => $user_id,
- ‘action’ => ‘add’, //增加
- ‘points’ => $points,
- ‘description’ => $desc,
- );
- $chognzhi = ashuwp_add_points_activity($args);
五、结语
本教程的思路、代码仅供参考,代码为阿树从实际项目中剥离而来,在代码处理过程中,不敢保证不出错,特别是如果遇到有两个连续的empty请自行删除一个。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END