403Webshell
Server IP : 43.141.49.107  /  Your IP : 113.219.202.44
Web Server : Apache
System : Linux VM-8-5-opencloudos 6.6.34-9.oc9.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jun 19 19:35:45 CST 2024 x86_64
User : www ( 1000)
PHP Version : 8.1.27
Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /www/wwwroot/www.ucppt.com/wp-content/plugins/wpcom-member-pro/includes/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /www/wwwroot/www.ucppt.com/wp-content/plugins/wpcom-member-pro/includes/points.php
<?php
namespace WPCOM\Member;
use WPCOM\Themer\Session;

defined( 'ABSPATH' ) || exit;

class Points{
    protected static $logs_table = 'wpcom_points_logs';
    public static $points_unit = '<svg class="icon" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="0.9em" height="0.9em" fill="currentColor" style="vertical-align: -0.1em;margin-right: 1px;"><path d="M436.636 385.155c107.161 0 218.624-23.568 285.558-68.505v81.809h62.01V219.33c0-107.678-179.02-165.825-347.46-165.825-168.449 0-347.577 58.147-347.577 165.832v488.886c0 107.685 179.028 165.825 347.469 165.825v-61.903c-176.928 0-285.566-60.548-285.566-103.915V561.258c66.949 44.836 178.297 68.512 285.566 68.512v-62.024c-176.928 0-285.566-60.541-285.566-103.908V316.765c66.949 44.822 178.411 68.39 285.566 68.39zm0-269.732c176.935 0 285.558 60.548 285.558 103.915 0 43.366-108.63 103.914-285.558 103.914S151.07 262.704 151.07 219.338c.014-43.367 108.738-103.922 285.566-103.922zm270.642 310.84c-153.883 0-234.336 59.194-234.336 117.642v314.159c0 58.448 80.554 117.634 234.336 117.634 152.736 0 233.075-58.348 234.322-116.38h.114V543.912c-.107-58.455-80.56-117.648-234.436-117.648zm0 61.91c113.756 0 172.426 39.066 172.426 55.732 0 16.658-58.663 55.724-172.426 55.724s-172.426-39.066-172.426-55.724 58.662-55.731 172.426-55.731zm0 425.615c-113.764 0-172.426-39.066-172.426-55.724v-75.006c39.28 21.375 97.004 35.625 172.426 35.625 75.414 0 133.038-14.243 172.426-35.625v75.006c0 16.658-58.663 55.724-172.426 55.724zm0-157.123c-113.764 0-172.426-39.087-172.426-55.73v-75.007c39.28 21.375 97.004 35.61 172.426 35.61 75.414 0 133.038-14.235 172.426-35.61v75.006c0 16.644-58.663 55.724-172.426 55.724z"/></svg>';

    function __construct() {
        add_action('wpmx_wallet_items', array($this, 'wallet_items'), 20);
        add_action('wpmx_after_wallet_item_points', array($this, 'add_points_btn'));
        add_action('wpmx_wallet_tab_points', array($this, 'tab_points'));
        add_action('wpmx_wallet_tab_point-tasks', array($this, 'tab_point_tasks'));
        add_action('wpcom_themer_maybe_updated', function(){ self::init_database(); });
        add_action('wp_ajax_wpcom_buy_points_modal', array($this, 'buy_points_modal'));
        add_action('wp_ajax_nopriv_wpcom_buy_points_modal', array($this, 'buy_points_modal'));
        add_action('wp_ajax_wpcom_buy_points_data', array($this, 'buy_points_data'));
        add_action('wp_ajax_wpcom_points_custom_price', array($this, 'points_custom_price'));
        add_action('wp_ajax_wpcom_my_points', array($this, 'get_my_points'));
        add_action('wp_ajax_nopriv_wpcom_my_points', array($this, 'get_my_points'));
        add_action('wpcom_points_payment_complete', array($this, 'payment_complete'), 10, 2);
        add_action('wpcom_order_item_detail_points', array($this, 'show_in_order'));

        add_action('user_register', array($this, 'user_register'), 50);
        add_action('wpmx_user_register_points_task', array($this, 'user_register_points_task'));
        add_action('wp_login', array($this, 'wp_login'), 20, 2);
        add_action('template_redirect', array($this, 'set_login_cookie'));
        add_action('profile_update', array($this, 'updated_user_email'), 10, 3);
        add_action('updated_user_meta', array($this, 'updated_mobile_phone'), 10, 4);
        add_action('wpcom_follow_user', array($this, 'follow_user'), 10, 2);
        add_action('transition_post_status', array($this, 'transition_post_status'), 20, 3);
        add_action('wpcom_hearted_post', array($this, 'hearted_post'), 10, 3);
        add_action('wpcom_liked_post', array($this, 'liked_post'));
        add_action('wp_insert_comment', array($this, 'inserted_comment'), 10, 2);
        add_action('transition_comment_status', array($this, 'comment_status'), 10, 3 );
        add_action('updated_comment_meta', array($this, 'adopted_answer'), 10, 4);
        add_action('added_comment_meta', array($this, 'adopted_answer'), 10, 4);
        add_action('show_user_profile', array($this, 'edit_user_points' ) );
        add_action('edit_user_profile', array($this, 'edit_user_points' ) );
        add_action('personal_options_update', array($this, 'save_user_points' ) );
        add_action('edit_user_profile_update', array($this, 'save_user_points' ) );
        add_filter('user_row_actions', array($this, 'user_row_actions'), 10, 2);
        add_action('admin_print_footer_scripts-users.php', array($this, 'users_page_scripts'));
        add_action('wp_ajax_wpcom_points_byadmin', array($this, 'points_byadmin'));

        add_filter('wpcom_account_tabs', array($this, 'account_tabs'), 20);
        add_filter('wpmx_wallet_tabs', array($this, 'wallet_tabs'), 20);
        add_filter('wpcom_init_plugin_options', array($this, 'add_plugin_options'));
        add_filter('manage_users_columns', array($this, 'users_columns' ) );
        add_filter('manage_users_custom_column', array($this, 'users_column_value' ), 10, 3 );
        add_filter('manage_users_sortable_columns', array($this ,'users_sortable') );
        add_filter('pre_get_users', array($this, 'filter_users' ) );
    }

    function account_tabs($tabs){
        foreach($tabs as $tab){
            if($tab && $tab['slug'] === 'wallet'){
                return $tabs;
            }
        }

        $tabs[23] = array(
            'slug' => 'wallet',
            'title' => '我的积分',
            'icon' => 'wallet',
            'shadow' => 1
        );
        add_action('wpcom_account_tabs_wallet', array($this, 'account_tabs_wallet'));
        return $tabs;
    }

    function account_tabs_wallet(){
        global $wpcom_member;
        echo $wpcom_member->load_template('wallet', []);
    }

    function wallet_items($items){
        $user = wp_get_current_user();
        $points = get_user_option( '_wpcom_points', $user->ID );
        $points = $points ? $points : 0;
        $items[] = [
            'name' => 'points',
            'title' => '积分',
            'unit' => wpmx_icon('points', false),
            'value' => number_format($points, 0, '.', '')
        ];
        return $items;
    }

    function add_points_btn(){
        global $wpmx_options;
        $enable = isset($wpmx_options['buy_points']) && $wpmx_options['buy_points'] ? true : false;
        $tasks_url = add_query_arg(['tab' => 'point-tasks'], wpcom_subpage_url('wallet')); ?>
        <div class="points-action">
            <?php if($enable){ ?><button class="wpcom-btn btn-sm btn-points" type="button"><?php wpmx_icon('add');?>购买积分</button><?php } ?>
            <a class="points-action-link" href="<?php echo esc_url($tasks_url);?>">做任务赚积分<?php wpmx_icon('arrow-right-2');?></a>
        </div>
    <?php }

    function wallet_tabs($tabs){
        $tabs[] = [
            'name' => 'points',
            'label' => '积分记录',
        ];
        $tabs[] = [
            'name' => 'point-tasks',
            'label' => '积分任务',
        ];
        return $tabs;
    }

    function add_plugin_options($res){
        $res['points_enable'] = true;
        return $res;
    }

    public static function add_log($args){
        global $wpdb;
        $table = $wpdb->prefix . self::$logs_table;
        if(isset($args['user_id']) && $args['title'] && $args['value']){
            $log = array(
                'user_id' => $args['user_id'],
                'title' => $args['title'],
                'value' => $args['value'],
                'time' => current_time('mysql')
            );
            $format = array('%d', '%s', '%f', '%s');
            $wpdb->insert($table, $log, $format);
            return $wpdb->insert_id;
        }
    }

    function get_logs($user_id, $per_page=20, $paged=1){
        global $wpdb;
        $table = $wpdb->prefix . self::$logs_table;
        $offset = ($paged - 1) * $per_page;
        $logs = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table WHERE user_id = %d ORDER BY time DESC LIMIT %d, %d", $user_id, $offset, $per_page));
        return $logs;
    }

    function tab_points(){
        global $wpdb;
        $user = wp_get_current_user();
        $paged = get_query_var('pageid') ? get_query_var('pageid') : 1;
        $table = $wpdb->prefix . self::$logs_table;
        $per_page = 20;
        $total = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM $table WHERE user_id = %d", $user->ID));
        $pages = ceil($total / $per_page);
        $logs = $this->get_logs($user->ID, $per_page, $paged);
        ?>
        <table class="balance-log-table">
            <tr>
                <th>类型</th>
                <th>积分</th>
                <th>时间</th>
            </tr>
            <?php if (isset($logs) && !empty($logs)) {
                foreach($logs as $log){ ?>
                <tr>
                    <td><?php echo $log->title;?></td>
                    <td><?php echo ($log->value >= 0 ? '+' : '-') . wpmx_icon('points', false, 'balance-log-unit') . ($log->value < 0 ? number_format(-($log->value), 0, '.', '') : number_format($log->value, 0, '.', ''));?></td>
                    <td><?php echo $log->time;?></td>
                </tr>
            <?php }}else{ ?>
                <tr class="balance-log-empty">
                    <td colspan="3">暂无积分记录</td>
                </tr>
            <?php } ?>
        </table>
        <?php if (isset($pages) && $pages > 1) {
            $url = add_query_arg(['tab' => 'points'], wpcom_subpage_url('wallet'));
            wpcom_pagination(4, array('numpages' => $pages, 'paged' => $paged, 'url' => $url, 'paged_arg' => 'pageid', 'paged_type' => 'query'));
        }
    }

    function tab_point_tasks(){
        $current_tasks = self::get_task_points();
        if($current_tasks && !empty($current_tasks)){
            $all_tasks = self::get_point_tasks();
            foreach($current_tasks as $task_id => $task_point){
                if($task_id && $task_id !== 'u_0' && isset($all_tasks[$task_id])){
                    $task_point = str_replace('-', '~', $task_point);
                    echo '<div class="point-task-item"><div class="point-task-item-content"><h4 class="point-task-item-title">'.$all_tasks[$task_id]['title'].'</h4><div class="point-task-item-desc">' . wp_kses_post($all_tasks[$task_id]['desc']) . '</div></div><div class="point-task-item-points"> + ' . wpmx_icon('points', false) . '<span>' . $task_point . '</span></div></div>';
                }
            }
        }else{
            echo '<div class="balance-log-empty">暂无积分任务</div>';
        }
    }

    function get_points_packages(){
        global $wpmx_options;
        $points_item_num = isset($wpmx_options['points_item_num']) && is_array($wpmx_options['points_item_num']) ? $wpmx_options['points_item_num'] : [];
        $points_item_price = isset($wpmx_options['points_item_price']) && is_array($wpmx_options['points_item_price']) ? $wpmx_options['points_item_price'] : [];
        $packages = [];
        if(!empty($points_item_num)){
            foreach($points_item_num as $i => $num){
                if(is_numeric($num) && $num > 0 && isset($points_item_price[$i]) && is_numeric($points_item_price[$i]) && $points_item_price[$i] > 0){
                    $packages[] = [
                        'num' => number_format($num, 0, '.', ''),
                        'price' => number_format($points_item_price[$i], 2, '.', '')
                    ];
                }
            }
        }
        return apply_filters('wpcom_points_packages', $packages);
    }

    function buy_points_modal(){
        global $wpcom_member, $wpmx_options;
        $res = array('result' => 0);
        $user_id = get_current_user_id();
        $enable = isset($wpmx_options['buy_points']) && $wpmx_options['buy_points'] ? true : false;
        if(!$enable){
            $res['result'] = -2;
            $res['msg'] = '积分购买未开启';
        }else if($user_id){
            $packages = $this->get_points_packages();
            $args = array(
                'desc' => isset($wpmx_options['buy_points_desc']) ? $wpmx_options['buy_points_desc'] : '',
                'items' => $packages,
                'points_rate' => isset($wpmx_options['buy_points_rate']) && is_numeric($wpmx_options['buy_points_rate']) && $wpmx_options['buy_points_rate'] > 0 ? $wpmx_options['buy_points_rate'] : 0
            );
            $buy_points_custom = isset($wpmx_options['buy_points_custom']) && $wpmx_options['buy_points_custom'] == '1' ? true : false;
            $args['custom'] = $buy_points_custom;
            if($buy_points_custom){
                $args['custom_min'] = isset($wpmx_options['buy_points_min']) && is_numeric($wpmx_options['buy_points_min']) ? number_format($wpmx_options['buy_points_min'], 0, '.', '') : 0;
                $args['custom_max'] = isset($wpmx_options['buy_points_max']) && is_numeric($wpmx_options['buy_points_max']) ? number_format($wpmx_options['buy_points_max'], 0, '.', '') : 0;
            }
            $res['tpl'] = $wpcom_member->load_template('buy-points-modal', $args);
        }else{
            $res['result'] = -1;
        }
        wp_send_json($res);
    }

    function get_points_price($value){
        global $wpmx_options;
        $points_rate = isset($wpmx_options['buy_points_rate']) && is_numeric($wpmx_options['buy_points_rate']) && $wpmx_options['buy_points_rate'] > 0 ? $wpmx_options['buy_points_rate'] : 0;
        $custom_min = isset($wpmx_options['buy_points_min']) && is_numeric($wpmx_options['buy_points_min']) ? number_format($wpmx_options['buy_points_min'], 0, '.', '') : 1;
        $custom_max = isset($wpmx_options['buy_points_max']) && is_numeric($wpmx_options['buy_points_max']) ? number_format($wpmx_options['buy_points_max'], 0, '.', '') : PHP_INT_MAX;
        if($points_rate > 0 && $value >= $custom_min && $value <= $custom_max){
            $price = number_format($value / $points_rate, 2, '.', '');
            if($price > 0){
                return $price;
            }else{
                return new \WP_Error('points_price_error', '积分购买价格计算错误');
            }
        }else if($points_rate > 0 && $value >= $custom_min){
            return new \WP_Error('points_buy_max', '超过最大可购买积分数量');
        }else if($points_rate > 0 && $value <= $custom_max){
            return new \WP_Error('points_buy_min', '低于最低积分起购数量');
        }else{
            return new \WP_Error('points_rate_error', '积分汇率获取失败');
        }
    }

    function points_custom_price(){
        $res = array('result' => -1);
        $value = isset($_POST['value']) ? number_format(sanitize_text_field($_POST['value']), 0, '.', '') : '';
        $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
        $user_id = get_current_user_id();
        if($user_id && $nonce && is_numeric($value) && wp_verify_nonce($nonce, 'points_custom')){
            $price = $this->get_points_price($value);
            if(!is_wp_error($price) && $price > 0){
                $res['result'] = 0;
                $res['price'] = $price;
            }else{
                $res['result'] = -3;
                if(is_wp_error($price)){
                    $res['msg'] = $price->get_error_message();
                }
            }
        }else{
            $res['result'] = -2;
        }
        wp_send_json($res);
    }

    function get_my_points(){
        $user = wp_get_current_user();
        $points = 0;
        $res = array('result' => 0, 'nonce' => wp_create_nonce('wpcom_payment_form'));

        if($user && isset($user->ID) && $user->ID){
            $points = get_user_option( '_wpcom_points', $user->ID );
            $points = $points ? $points : 0;
        }else{
            $res['result'] = -1;
        }

        $res['points'] = number_format($points, 0, '.', '');
        wp_send_json($res);
    }

    function buy_points_data(){
        $res = array('result' => 0);
        $value = isset($_POST['value']) ? number_format(sanitize_text_field($_POST['value']), 0, '.', '') : '';
        $price = isset($_POST['price']) ? number_format(sanitize_text_field($_POST['price']), 2, '.', '') : '';
        $nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : '';
        $nonce_name = $price === '' ? 'points_custom' : 'points_types';

        if($nonce && wp_verify_nonce($nonce, $nonce_name)){
            if($price){
                $packages = $this->get_points_packages();
                if($packages && count($packages) > 0){
                    $checked = false;
                    foreach($packages as $package){
                        if($package['num'] === $value && $package['price'] === $price){
                            $checked = true;
                            break;
                        }
                    }
                    if(!$checked) {
                        $price = new \WP_Error('points_package_price_error', '价格信息校验不通过');
                    }
                }else{
                    $price = new \WP_Error('points_package_price_error', '价格信息获取出错');
                }
            }else{
                $price = $this->get_points_price($value);
            }
            if ($value && !is_wp_error($price) && $price > 0) {
                $data = [
                    'points' => $value
                ];
                $items = array(
                    array(
                        'title' => '购买积分 ' . $value,
                        'price' => $price,
                        'url' => '',
                        'number' => 1,
                        'type' => 'points',
                        'type_id' => '',
                        'data' => $data
                    )
                );
                $res['total'] = $price;
                $res['items'] = $items;
                $res = Order::setup_order_data($res);
            } else {
                $res['result'] = -2;
                if(is_wp_error($price)){
                    $res['msg'] = $price->get_error_message();
                }else{
                    $res['msg'] = '购买出错,请检查输入积分数量或者联系管理员';
                }
            }
        }else{
            $res['result'] = -1;
            $res['msg'] = '购买请求失败,请稍后再试';
        }
        wp_send_json($res);
    }

    function payment_complete($item, $order){
        if($order && $order->user && $order->status === 'paid' && isset($item->price) && $item->price){
            $user_id = (int)$order->user;
            $data = $item->data ? json_decode($item->data) : null;
            $user = get_user_by('ID', $user_id);
            if($user_id && $user && isset($user->ID) && isset($data->points) && $data->points > 0){
                $log_title = '购买积分#' . $order->number;
                $this->use_points($data->points, $user->ID, $log_title);
            }
        }
    }

    public function show_in_order($item) {
        if ($item && $item->type === 'points') {
            $data = $item->data ? json_decode($item->data) : null; ?>
            <ul class="member-account-order">
                <li class="member-account-order-item">
                    <div class="member-account-order-label">购买项目:</div>
                    <div class="member-account-order-value">购买积分</div>
                </li>
                <li class="member-account-order-item">
                    <div class="member-account-order-label">购买积分:</div>
                    <div class="member-account-order-value"><?php echo isset($data->points) && $data->points > 0 ? number_format($data->points, 0, '.', '') : '-';?></div>
                </li>
            </ul>
        <?php }
    }

    public static function use_points($points, $user_id, $log_title){
        $points = number_format($points, 0, '.', '');
        if(is_numeric($points) && $user_id){
            $my_points = (float)get_user_option( '_wpcom_points', $user_id );
            $my_points = $my_points && $my_points > 0 ? $my_points : 0;
            if($my_points + $points >= 0){
                $my_points = number_format($my_points + $points, 0, '.', '');
                $update = update_user_option($user_id, '_wpcom_points', $my_points);
                if($update){
                    $log = array(
                        'user_id' => $user_id,
                        'title' => $log_title,
                        'value' => $points
                    );
                    self::add_log($log);
                }
                return $update;
            }
        }
    }

    function users_columns($columns){
        $columns['points'] = __('Points', WPCMP_TD);
        return $columns;
    }

    function users_column_value($val, $column_name, $user_id){
        if($column_name === 'points'){
            $points = (float)get_user_option( '_wpcom_points', $user_id );
            $points = $points ? $points : 0;
            $val = self::$points_unit . $points;
        }
        return $val;
    }

    function users_sortable($columns){
        $columns['points'] = 'points';
        return $columns;
    }

    function filter_users( $query ){
        global $pagenow, $wpdb;
        if (is_admin() && 'users.php' == $pagenow ) {
            if($query->get('orderby') === 'points'){
                $meta_key = $wpdb->get_blog_prefix() . '_wpcom_points';

                $query->set('meta_query', array(
                    'relation' => 'OR',
                    array(
                        'key'     => $meta_key,
                        'compare' => 'NOT EXISTS',
                    ),
                    array(
                        'key'     => $meta_key,
                        'type'    => 'NUMERIC',
                        'compare' => 'EXISTS',
                    ),
                ));

                $query->set('meta_key', $meta_key);
                $query->set('orderby', 'meta_value_num');
            }
        }
        return $query;
    }

    function user_row_actions($actions, $user){
        $actions['points'] = '<a class="j-add-points" href="#" title="添加积分" data-user="'.esc_attr($user->ID).'">积分</a>';
        return $actions;
    }

    function users_page_scripts(){
        wp_enqueue_script( 'jquery-ui-dialog' );
        wp_enqueue_style( 'wp-jquery-ui-dialog' ); ?>
        <div id="points-dialog">
            <form method="post" id="points-form">
                <?php wp_nonce_field( 'wpcom_users_points', 'wpcom_users_points_nonce' ); ?>
                <table class="form-table">
                    <tr>
                        <th><label for="points-user">添加用户</label></th>
                        <td>
                            <p class="points-user"></p>
                            <input type="hidden" name="points-user" class="regular-text" value="">
                            <p class="description">请核对添加用户ID是否正确</p>
                        </td>
                    </tr>
                    <tr>
                        <th><label for="points-value">添加积分</label></th>
                        <td>
                            <input type="number" step="1" name="points-value" class="regular-text" placeholder="请填写添加的积分">
                            <p class="description">必填,可为负,如果是负数的话则会扣减积分;积分需要为整数</p>
                        </td>
                    </tr>
                    <tr>
                        <th><label for="points-title">添加标题</label></th>
                        <td>
                            <input type="text" name="points-title" class="regular-text" placeholder="可选,用于积分记录里面显示">
                            <p class="description">如无特殊说明可留空,留空的话默认为:购买积分[线下转账]</p>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <p class="submit"><input type="submit" name="submit" class="button button-primary" value="立即添加"></p>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
        <script>
            jQuery(function($){
                const $wrap = $('#points-dialog');
                $wrap.dialog({
                        title: '添加积分',
                        dialogClass: 'wp-dialog',
                        autoOpen: false,
                        width: '680',
                        modal: true,
                        focus: false,
                        position: {
                            my: "center",
                            at: "center",
                            of: window
                        }
                    });
                $('.wp-list-table').on('click', '.j-add-points', function(){
                    let user = $(this).data('user');
                    if(user){
                        $wrap.find('[name="points-user"]').val(user);
                        $wrap.find('.points-user').html(user);
                        $wrap.dialog('open');
                    }
                    return false;
                });
                $wrap.on('submit', 'form', function (e) {
                    var $form = $(this);
                    var $submit = $form.find('input[type=submit]');
                    if($submit.hasClass('disabled')) return false;
                    $submit.addClass('disabled').val('正在添加...');
                    $.ajax({
                        url: ajaxurl+'?action=wpcom_points_byadmin',
                        type: 'post',
                        data: $form.serialize(),
                        dataType: 'json',
                        success: function (res) {
                            $submit.removeClass('disabled').val('立即充值');
                            $wrap.dialog('close');
                            if(res.result==0){
                                alert('添加成功,请刷新页面查看最新积分!');
                            }else{
                                alert(res.msg ? res.msg : '添加失败,请稍后再试!');
                            }
                        },
                        error: function () {
                            $submit.removeClass('disabled').val('立即添加');
                            $wrap.dialog('close');
                            alert('添加失败,请稍后再试!');
                        }
                    });
                    return false;
                });
            })
        </script>
        <?php
    }

    function points_byadmin(){
        $res = array('result' => 0);
        if(current_user_can('edit_user')){
            $user_id = isset($_POST['points-user']) ? sanitize_text_field($_POST['points-user']) : 0;
            $value = isset($_POST['points-value']) ? trim(sanitize_text_field($_POST['points-value'])) : 0;
            $title = isset($_POST['points-title']) ? trim(sanitize_text_field($_POST['points-title'])) : '';
            $nonce = isset($_POST['wpcom_users_points_nonce']) ? sanitize_text_field($_POST['wpcom_users_points_nonce']) : '';
            if($nonce && wp_verify_nonce($nonce, 'wpcom_users_points')){
                $value = number_format($value, 0, '.', '');
                if($value && $value != 0 && is_numeric($value)){
                    $user = get_user_by('ID', $user_id);
                    if($user && isset($user->ID) && $user->ID){
                        $log_title = trim($title) ? trim($title) : '购买积分[线下转账]';
                        if(!$this->use_points($value, $user->ID, $log_title)){
                            $res = array('result' => -5, 'msg' => '添加失败,请刷新页面核对用户积分是否变化');
                        }
                    }else{
                        $res = array('result' => -4, 'msg' => '添加积分用户获取失败');
                    }
                }else{
                    $res = array('result' => -3, 'msg' => '添加积分异常,请核对修改后重新提交');
                }
            }else{
                $res = array('result' => -2, 'msg' => '页面校验异常,请稍后再试');
            }
        }else{
            $res = array('result' => -1, 'msg' => '无积分添加权限');
        }
        wp_send_json($res);
    }

    function edit_user_points($user){
        if (!current_user_can('edit_user')) return;
        $points = (float)get_user_option( '_wpcom_points', $user->ID );
        $points = $points ? $points : 0; ?>
        <h3>积分设置</h3>
        <table class="form-table">
            <tr>
                <th><label for="vip_type">用户积分</label></th>
                <td>
                    <input type="number" name="user_points" class="regular-text" value="<?php echo $points !== '' ? $points : '';?>">
                    <p class="description">积分为整数,温馨提示:此处的积分修改不会产生积分记录,用户在网站前台“我的钱包/我的积分-积分记录”里面看不到记录,如需为用户添加积分建议通过用户列表操作</p>
                </td>
            </tr>
        </table>
    <?php }

    function save_user_points($user_id){
        if (!current_user_can('edit_user', $user_id)) return false;
        if (isset($_POST['user_points']) && trim($_POST['user_points']) !== '') {
            $points = sanitize_text_field(trim($_POST['user_points']));
            if(is_numeric($points)){
                $points = number_format($points, 0, '.', '');
                if($points || $points == 0) update_user_option($user_id, '_wpcom_points', $points);
            }
        }
    }

    function user_register($user_id){
        if($user_id){
            $points = self::get_task_points('u_0');
            if($points && $points > 0){
                self::use_points($points, $user_id, $this->get_task_title('u_0'));
                $today = current_time('Y-m-d');
                Session::set('_login_' . $user_id . '_' . $today, 1, 24*60*60);
            }

            // 新注册用户直接检查是否有邮箱或者手机号,有的话也增加积分,避免数据保存滞后,通过定时任务执行
            wp_schedule_single_event( time() + 3, 'wpmx_user_register_points_task', [$user_id, wp_rand(1000, 99999) . '_' . time()] );
        }
    }

    function user_register_points_task($user_id){
        if($user_id && $user = get_user_by('id', $user_id)){
            // user_status 判断是否待审核用户,待审核用户审核通过后增加积分
            if($user->user_status != 0) return false;

            if($user->user_email && !wpcom_is_empty_mail($user->user_email)){
                $this->email_or_mobile_task($user->ID);
            }

            if($user->mobile_phone && is_numeric($user->mobile_phone) && is_wpcom_enable_phone()){
                $this->email_or_mobile_task($user->ID, 'mobile_phone');
            }
        }
    }

    function wp_login($user_login, $user){
        if($user && $user->ID){
            $points = self::get_task_points('u_1');
            if($points && $points > 0){
                $today = current_time('Y-m-d');
                $key = '_login_' . $user->ID . '_' . $today;
                if(Session::get($key) != '1'){
                    self::use_points($points, $user->ID, $this->get_task_title('u_1'));
                    Session::set($key, 1, 24*60*60);
                }
            }
        }
    }

    function set_login_cookie(){
        $user = wp_get_current_user();
        if ($user && isset($user->ID) && $user->ID) {
            $today = current_time('Y-m-d');
            $cookie_key = '_today_login_' . $user->ID;

            // 有今日登录的cookie,直接返回
            if(isset($_COOKIE[$cookie_key]) && $_COOKIE[$cookie_key] === $today){
                return ;
            }

            $this->wp_login('', $user);
            @setcookie($cookie_key, $today, time()+24*60*60, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
        }
    }

    function updated_user_email($user_id, $olddata, $userdata){
        // user_email 判断邮箱变更
        if($user_id && isset($userdata['user_email']) && $userdata['user_email'] !== $olddata->user_email && !wpcom_is_empty_mail($userdata['user_email'])){
            $this->email_or_mobile_task($user_id);
        }

        // user_status 判断审核状态变更,待审核用户审核通过后增加积分
        if(isset($userdata['user_status']) && $userdata['user_status'] == 0 && $userdata['user_status'] != $olddata->user_status){
            $this->email_or_mobile_task($user_id);
        }
    }

    function updated_mobile_phone($meta_id, $user_id, $meta_key, $meta_value){
        if($user_id && $meta_key === 'mobile_phone' && $meta_value){
            $this->email_or_mobile_task($user_id, 'mobile_phone');
        }
    }

    function email_or_mobile_task($user_id, $type = 'user_email'){
        global $wpdb;
        $key = $type === 'user_email' ? 'u_2' : 'u_3';
        $points = self::get_task_points($key);
        if($points && $points > 0){
            $task = get_user_meta($user_id, $wpdb->get_blog_prefix() . $type . '_task', true);
            if($task != '1'){
                self::use_points($points, $user_id, $this->get_task_title($key));
                update_user_option($user_id, $type . '_task', 1);
            }
        }
    }

    function follow_user($user, $followed){
        if($user && $followed){
            $points = self::get_task_points('u_4');
            if($points && $points > 0){
                $_user = get_user_by('ID', $user);
                $_followed = get_user_by('ID', $followed);
                if($_user && $_followed){
                    $key = '_follow_' . $user . '_' . $followed;
                    if(Session::get($key) != '1'){
                        self::use_points($points, $_followed->ID, $this->get_task_title('u_4'));
                        Session::set($key, 1, 30*24*60*60);
                    }
                }
            }
        }
    }

    function transition_post_status($new_status, $old_status, $post){
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
        if ($new_status !== 'publish' || $old_status === 'publish') return false;
        if ($post->post_type === 'post') {
            $points = self::get_task_points('p_0');
            if($points && $points > 0){
                $key = '_post_publish_' . $post->ID;
                if(Session::get($key) != '1'){
                    self::use_points($points, $post->post_author, $this->get_task_title('p_0'));
                    Session::set($key, 1, 30*24*60*60);
                }
            }
        }else if ($post->post_type === 'qa_post') {
            $points = self::get_task_points('q_0');
            $key = '_qa_post_publish_' . $post->ID;
            if(Session::get($key) != '1'){
                self::use_points($points, $post->post_author, $this->get_task_title('q_0'));
                Session::set($key, 1, 30*24*60*60);
            }
        }
    }

    function hearted_post($post_id, $user_id, $is_hearted){
        if($post_id && $user_id && $is_hearted){
            $points = self::get_task_points('p_1');
            if($points && $points > 0){
                $key = '_post_heart_' . $post_id . '_' . $user_id;
                $post = get_post($post_id);
                if($post && $post->post_author && $user_id != $post->post_author && Session::get($key) != '1'){
                    self::use_points($points, $post->post_author, $this->get_task_title('p_1'));
                    Session::set($key, 1, 30*24*60*60);
                }
            }
        }
    }

    function liked_post($post_id){
        if($post_id){
            $points = self::get_task_points('p_2');
            if($points && $points > 0){
                $key = 'post_like_' . $post_id;
                $post = get_post($post_id);
                $user_id = get_current_user_id();
                if($post && $post->post_author && (!$user_id || $user_id != $post->post_author) && Session::get($key) != '1'){
                    self::use_points($points, $post->post_author, $this->get_task_title('p_2'));
                    Session::set($key, 1, 30*24*60*60);
                }
            }
        }
    }

    function inserted_comment($comment_ID, $comment){
        if($comment_ID && $comment->user_id && ($comment->comment_approved === 'approve' || $comment->comment_approved == 1)) {
            if($comment->comment_type === 'comment'){
                $points = self::get_task_points('p_4');
                if($points && $points > 0){
                    $post_author = get_post_field( 'post_author', $comment->comment_post_ID );
                    $key = '_comment_' . $comment->user_id . '_' . $comment_ID;
                    if($post_author && $comment->user_id != $post_author && Session::get($key) != '1'){
                        self::use_points($points, $comment->user_id, $this->get_task_title('p_4'));
                        Session::set($key, 1, 24*60*60);
                    }
                }
            }else if($comment->comment_type === 'answer'){
                $points = self::get_task_points('q_1');
                if($points && $points > 0){
                    $post_author = get_post_field( 'post_author', $comment->comment_post_ID );
                    $key = '_answer_' . $comment->user_id . '_' . $comment_ID;
                    if($post_author && $comment->user_id != $post_author && Session::get($key) != '1'){
                        self::use_points($points, $comment->user_id, $this->get_task_title('q_1'));
                        Session::set($key, 1, 24*60*60);
                    }
                }
            }

        }
        if($comment_ID && $comment->comment_post_ID && ($comment->comment_approved === 'approve' || $comment->comment_approved == 1)){
            if($comment->comment_type === 'comment'){
                $_points = self::get_task_points('p_3');
                if($_points && $_points > 0){
                    $post_author = get_post_field( 'post_author', $comment->comment_post_ID );
                    $key = '_commented_' . $post_author . '_' . $comment_ID;
                    if($post_author && (!$comment->user_id || $comment->user_id != $post_author) && Session::get($key) != '1'){
                        self::use_points($_points, $post_author, $this->get_task_title('p_3'));
                        Session::set($key, 1, 24*60*60);
                    }
                }
            }else if($comment->comment_type === 'answer'){
                $_points = self::get_task_points('q_2');
                if($_points && $_points > 0){
                    $post_author = get_post_field( 'post_author', $comment->comment_post_ID );
                    $key = '_answered_' . $post_author . '_' . $comment_ID;
                    if($post_author && (!$comment->user_id || $comment->user_id != $post_author) && Session::get($key) != '1'){
                        self::use_points($_points, $post_author, $this->get_task_title('q_2'));
                        Session::set($key, 1, 24*60*60);
                    }
                }
            }
        }
    }

    function comment_status($new_status, $old_status, $comment){
        if($comment->comment_ID && ($new_status === 'approve' || $new_status === 'approved' || $new_status == 1) && $new_status != $old_status){
            $this->inserted_comment($comment->comment_ID, $comment);
        }
    }

    function adopted_answer($meta_id, $comment_id, $meta_key, $meta_value){
        if($meta_key === 'adopted_answer' && $meta_value == 1){
            $comment = get_comment($comment_id);
            if($comment && isset($comment->user_id) && $comment->user_id && $comment->comment_type === 'answer'){
                $points = self::get_task_points('q_3');
                if($points && $points > 0){
                    $post_author = get_post_field( 'post_author', $comment->comment_post_ID );
                    $key = '_adopted_answer_' . $comment->user_id . '_' . $comment->comment_post_ID;
                    if($post_author && $comment->user_id != $post_author && Session::get($key) != '1'){
                        self::use_points($points, $comment->user_id, $this->get_task_title('q_3'));
                        Session::set($key, 1, 30*24*60*60);
                    }
                }
            }
        }
    }

    /**
     * 获取配置的积分任务分值
     */
    public static function get_task_points($task_id = ''){
        global $wpmx_options;
        $tasks = [];
        if(isset($wpmx_options['task_id']) && !empty($wpmx_options['task_id'])){
            foreach($wpmx_options['task_id'] as $i => $task){
                if(isset($wpmx_options['task_points'][$i])){
                    // 支持范围配置,如 "10-20",保存原始值以便列表展示
                    $task_points = trim($wpmx_options['task_points'][$i]);
                    $task_points = str_replace('-', '-', $task_points);
                    $task_points = str_replace('—', '-', $task_points);
                    $tasks[$task] = $task_points;
                }
            }
        }

        // 如果请求单个任务,则返回数值(支持区间随机)
        if($task_id !== '') {
            $points = 0;
            if(isset($tasks[$task_id])){
                $val = $tasks[$task_id];
                // 区间格式 例如 10-20
                if(preg_match('/^(\d+)\s*-\s*(\d+)$/', $val, $m)){
                    $min = intval($m[1]);
                    $max = intval($m[2]);
                    if($min > $max){ $tmp = $min; $min = $max; $max = $tmp; }
                    $min = max(0, $min);
                    $max = max($min, $max);
                    $points = rand($min, $max);
                } else {
                    $points = intval($val);
                }
            }
            $points = $points > 0 ? $points : 0;
            return apply_filters( 'wpcom_task_points', $points, $task_id);
        }else{
            // 返回任务映射(值可能为数字或区间字符串),用于列表展示
            return apply_filters( 'wpcom_get_task_points', $tasks);
        }
    }

    function get_task_title($id){
        $tasks = self::get_point_tasks($id);
        $title = '';
        if($tasks && isset($tasks['title'])){
            $title = '「积分奖励」' . $tasks['title'];
        }
        return apply_filters('wpcom_point_task_title', $title, $id);
    }

    /**
     * 获取积分任务信息
     */
    public static function get_point_tasks($task_id = ''){
        $tasks = apply_filters('wpcom_get_point_tasks', [
            'u_0' => [
                'title' => '新用户注册',
                'desc' => '新用户注册将自动获取积分'
            ],
            'u_1' => [
                'title' => '每日登录',
                'desc' => '每日通过登录页面登录网站'
            ],
            'u_2' => [
                'title' => '绑定电子邮箱',
                'desc' => '未绑定电子邮箱的用户首次绑定电子邮箱'
            ],
            'u_3' => [
                'title' => '绑定手机号码',
                'desc' => '未绑定手机号码的用户首次绑定手机号码'
            ],
            'u_4' => [
                'title' => '被粉丝关注',
                'desc' => '有新粉丝关注了你'
            ],
            'p_0' => [
                'title' => '发布文章',
                'desc' => '发布新文章并审核通过'
            ],
            'p_1' => [
                'title' => '文章被收藏',
                'desc' => '发布的文章有其他用户收藏'
            ],
            'p_2' => [
                'title' => '文章被点赞',
                'desc' => '发布的文章有其他用户点赞'
            ],
            'p_3' => [
                'title' => '文章被评论',
                'desc' => '发布的文章有其他用户评论'
            ],
            'p_4' => [
                'title' => '发表文章评论',
                'desc' => '在文章下面发表了评论'
            ],
            'q_0' => [
                'title' => '发布问题',
                'desc' => '发布了新的问题并审核通过'
            ],
            'q_1' => [
                'title' => '回答问题',
                'desc' => '回答了问题并审核通过'
            ],
            'q_2' => [
                'title' => '问题被回答',
                'desc' => '发布的问题有其他用户回答'
            ],
            'q_3' => [
                'title' => '回答被采纳为最佳',
                'desc' => '回答其他用户的问题,你的回答被采纳为最佳回答'
            ]
        ]);
        return $task_id && isset($tasks[$task_id]) ? $tasks[$task_id] : $tasks;
    }

    private static function init_database(){
        global $wpdb;
        $logs_table = $wpdb->prefix . self::$logs_table;
        if( $wpdb->get_var("SHOW TABLES LIKE '$logs_table'") != $logs_table ){
            $charset_collate = $wpdb->get_charset_collate();
            require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
            $create_sql = "CREATE TABLE $logs_table (".
                "ID BIGINT(20) NOT NULL auto_increment,".
                "user_id BIGINT(20) NOT NULL,".
                "title longtext,".
                "value decimal(10,2) NOT NULL,".
                "time datetime,".
                "PRIMARY KEY (ID)) $charset_collate;";
            dbDelta( $create_sql );
        }
    }
}

new Points();

Youez - 2016 - github.com/yon3zu
LinuXploit