Plugin Directory

Changeset 1686938


Ignore:
Timestamp:
06/28/2017 01:07:30 PM (9 years ago)
Author:
Safronik
Message:

Fix: Users check

Location:
cleantalk-spam-protect/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • cleantalk-spam-protect/trunk/assets/js/cleantalk-users-checkspam.js

    r1667018 r1686938  
    1 var working = false;
    2 var ajax_nonce = ctUsersCheck.ct_ajax_nonce;
     1// Printf for JS
     2String.prototype.printf = function(){
     3    var formatted = this;
     4    for( var arg in arguments ) {
     5                var before_formatted = formatted.substring(0, formatted.indexOf("%s", 0));
     6                var after_formatted  = formatted.substring(formatted.indexOf("%s", 0)+2, formatted.length);
     7                formatted = before_formatted + arguments[arg] + after_formatted;
     8    }
     9    return formatted;
     10};
     11
     12// Flags
     13var ct_working = false,
     14        ct_new_check = true,
     15        ct_cooling_down_flag = false;
     16// Settings
    317var ct_cool_down_time = 65000,
    418        ct_requests_counter = 0,
    5         ct_max_requests = 95,
    6         ct_cooling_down_flag = false;
    7 var ct_new_check = true;
    8        
    9 String.prototype.format = String.prototype.f = function (){
    10     var args = arguments;
    11     return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n){
    12         if (m == "{{") { return "{"; }
    13         if (m == "}}") { return "}"; }
    14         return args[n];
    15     });
    16 };
     19        ct_max_requests = 95;
     20// Variables
     21var ct_ajax_nonce = ctUsersCheck.ct_ajax_nonce,
     22        ct_users_total = 0,
     23        ct_users_checked = 0,
     24        ct_users_spam = 0,
     25        ct_users_bad = 0,
     26        ct_unchecked = 'unset';
    1727
    1828var close_animate=true;
    … …  
    3646        var data = {
    3747                'action': 'ajax_clear_users',
    38                 'security': ajax_nonce
     48                'security': ct_ajax_nonce
    3949        };
    4050       
    … …  
    7383        var data = {
    7484                'action': 'ajax_check_users',
    75                 'security': ajax_nonce,
    76                 'new_check': ct_new_check
     85                'security': ct_ajax_nonce,
     86                'new_check': ct_new_check,
     87                'unchecked': ct_unchecked
    7788        };
    7889       
    … …  
    8293                data: data,
    8394                success: function(msg){                 
    84                         ct_new_check = false;
    85                         if(parseInt(msg)==1){
    86                                 ct_send_users();
    87                         }else if(parseInt(msg)==0){
    88                                 working=false;
    89                                 jQuery('#ct_working_message').hide();
     95                        var msg_obj = JSON.parse(msg);
     96                       
     97                        if(parseInt(msg_obj.error)){
     98                                ct_working=false;
     99                                alert(msg.error_message);
    90100                                location.href='users.php?page=ct_check_users&ct_worked=1';
    91101                        }else{
    92                                 working=false;
    93                                 alert(msg);
    94                                 location.href='users.php?page=ct_check_users&ct_worked=1';
     102                                ct_new_check = false;
     103                                if(parseInt(msg_obj.end) == 0){
     104                                        ct_users_checked += msg_obj.checked;
     105                                        ct_users_spam += msg_obj.spam;
     106                                        ct_users_bad += msg_obj.bad;
     107                                        ct_unchecked = ct_users_total - ct_users_checked - ct_users_bad;
     108                                        var status_string = String(ctUsersCheck.ct_status_string);
     109                                        var status_string = status_string.printf(ct_users_total, ct_users_checked, ct_users_spam, ct_users_bad);
     110                                        if(parseInt(ct_users_spam) > 0)
     111                                                status_string += ctUsersCheck.ct_status_string_warning;
     112                                        jQuery('#ct_checking_users_status').html(status_string);
     113                                        ct_send_users();
     114                                }else if(parseInt(msg_obj.end) == 1){
     115                                        ct_working=false;
     116                                        jQuery('#ct_working_message').hide();
     117                                        location.href='users.php?page=ct_check_users&ct_worked=1';
     118                                }
    95119                        }
    96120                },
    … …  
    106130function ct_show_users_info(){
    107131       
    108         if(working){
     132        if(ct_working){
    109133               
    110134                if(ct_cooling_down_flag == true){
    … …  
    116140                }
    117141               
    118                 var data = {
    119                         'action': 'ajax_info_users',
    120                         'security': ajax_nonce
    121                 };
    122                 jQuery.ajax({
    123                         type: "POST",
    124                         url: ajaxurl,
    125                         data: data,
    126                         success: function(msg){
    127                                 jQuery('#ct_checking_users_status').html(msg);
    128                                 setTimeout(ct_show_users_info, 3000);
    129                         },
    130                         error: function (jqXHR, textStatus, errorThrown){
    131                                 jQuery('#ct_error_message').show();
    132                                 jQuery('#cleantalk_ajax_error').html(textStatus);
    133                                 jQuery('#cleantalk_js_func').html('Show users');
    134                                 setTimeout(ct_show_users_info(), 3000);
    135                         },
    136             timeout: 5000
    137                 });
     142                setTimeout(ct_show_users_info, 3000);
     143               
     144                if(!ct_users_total){
     145                        var data = {
     146                                'action': 'ajax_info_users',
     147                                'security': ct_ajax_nonce
     148                        };
     149                        jQuery.ajax({
     150                                type: "POST",
     151                                url: ajaxurl,
     152                                data: data,
     153                                success: function(msg){
     154                                        var msg = jQuery.parseJSON(msg);
     155                                        jQuery('#ct_checking_users_status').html(msg.message);
     156                                        ct_users_total = msg.total;
     157                                },
     158                                error: function (jqXHR, textStatus, errorThrown){
     159                                        jQuery('#ct_error_message').show();
     160                                        jQuery('#cleantalk_ajax_error').html(textStatus);
     161                                        jQuery('#cleantalk_js_func').html('Show users');
     162                                        setTimeout(ct_show_users_info(), 3000);
     163                                },
     164                                timeout: 15000
     165                        });
     166                }
    138167        }
    139168}
    … …  
    142171        var data = {
    143172                'action': 'ajax_insert_users',
    144                 'security': ajax_nonce
     173                'security': ct_ajax_nonce
    145174        };
    146175       
    … …  
    158187        var data = {
    159188                'action': 'ajax_delete_all_users',
    160                 'security': ajax_nonce
     189                'security': ct_ajax_nonce
    161190        };
    162191       
    … …  
    194223        var data = {
    195224                'action': 'ajax_delete_checked_users',
    196                 'security': ajax_nonce,
     225                'security': ct_ajax_nonce,
    197226                'ids':ids
    198227        };
    … …  
    225254                var data = {
    226255                        'action': 'ajax_delete_checked_users',
    227                         'security': ajax_nonce,
     256                        'security': ct_ajax_nonce,
    228257                        'ids':ids
    229258                };
    … …  
    259288                jQuery('#ct_working_message').show();
    260289                jQuery('#ct_preloader').show();
    261                 working=true;
     290                ct_working=true;
    262291                ct_clear_users();
    263292                ct_show_users_info();
    … …  
    321350                var data = {
    322351                        'action': 'ajax_ct_approve_user',
    323                         'security': ajax_nonce,
     352                        'security': ct_ajax_nonce,
    324353                        'id': ct_id
    325354                };
    … …  
    338367                var data = {
    339368                        'action': 'ct_feedback_user',
    340                         'security': ajax_nonce,
     369                        'security': ct_ajax_nonce,
    341370                        'user_id': ct_id,
    342371                        'status': 'approve'
    … …  
    369398                var data = {
    370399                        'action': 'ajax_ct_get_csv_file',
    371                         'security': ajax_nonce,
     400                        'security': ct_ajax_nonce,
    372401                        'filename': ctUsersCheck.ct_csv_filename
    373402                };
  • cleantalk-spam-protect/trunk/inc/cleantalk-admin.php

    r1685423 r1686938  
    275275                        'ct_confirm_deletion_checked' => __('Delete checked users?', 'cleantalk'),
    276276                        'ct_csv_filename'             => "user_check_by_".$current_user->user_login,
    277                         'ct_bad_csv'                  => __("File doesn't exist. File will be generated while checking. Please, press \"Check for spam\".")
     277                        'ct_bad_csv'                  => __("File doesn't exist. File will be generated while checking. Please, press \"Check for spam\"."),
     278                        'ct_status_string'            => __("Total users %s, checked %s, found %s spam users and %s bad users (without IP or email)", 'cleantalk'),
     279                        'ct_status_string_warning'    => "<p>".__("Please do backup of WordPress database before delete any accounts!", 'cleantalk')."</p>"
    278280                ));
    279281                wp_localize_script( 'jquery', 'ctUsersScreen', array(
  • cleantalk-spam-protect/trunk/inc/cleantalk-users.php

    r1681278 r1686938  
    2828        , ARRAY_A);
    2929        $cnt_spam1=$r[0]['cnt'];
     30       
     31        // Parsing status message
     32        $status = ct_ajax_info_users(true);
     33        $status = json_decode($status, true);
     34        $status_message = $status['message'];
    3035?>
    3136        <div class="wrap">
    … …  
    4853                               
    4954                <!-- Main info -->
    50                 <h3 id="ct_checking_users_status" style="text-align:center;width:90%;"><?php ct_ajax_info_users(true);?></h3>
     55                <h3 id="ct_checking_users_status" style="text-align:center;width:90%;"><?php echo $status_message; ?></h3>
    5156               
    5257                <!-- Cooling notice -->
    … …  
    273278        $u = get_users($args_unchecked);
    274279       
     280        $check_result = array(
     281                'end' => 0,
     282                'checked' => 0,
     283                'spam' => 0,
     284                'bad' => 0,
     285                'error' => 0
     286        );
     287       
    275288    if(count($u) > 0){
    276289               
    … …  
    301314                       
    302315                        if(empty($curr_ip) && empty($curr_email)){
     316                                $check_result['bad']++;
    303317                                update_user_meta($u[$i]->ID,'ct_bad','1',true);
    304318                                unset($u[$i]);
    … …  
    314328                }
    315329               
    316                 // Drop if data empty
    317                 if(count($data) == 0)
    318                         die('1');
     330                // Drop if data empty and there's no users to check
     331                if(count($data) == 0){
     332                       
     333                        if($_POST['unchecked'] === 0)
     334                                $check_result['end'] = 1;
     335
     336                        print json_encode($check_result);
     337                        die();
     338                }
    319339               
    320340                $data=implode(',',$data);
    … …  
    332352               
    333353                if(empty($result)){
    334                         echo "Connection error";
     354                        $check_result['error'] = 1;
     355                        $check_result['error_message'] = __('Connection error', 'cleantalk');
     356                        print json_encode($check_result);
    335357                }else{
    336358                       
    337359                        $result = json_decode($result);         
    338360                       
    339                         if(isset($result->error_message))
     361                        if(isset($result->error_message)){
     362                                $check_result['error'] = 1;
     363                                $check_result['error_message'] = __('Server response: ', 'cleantalk').$result->error_message;;
    340364                                print "Server response: ".$result->error_message;
    341                         else{
     365                        }else{
    342366                               
    343367                                // Opening CSV file
    … …  
    355379                                for($i=0;$i<sizeof($u);$i++){
    356380                                       
     381                                        $check_result['checked']++;
    357382                                        update_user_meta($u[$i]->ID,'ct_checked',date("Y-m-d H:m:s"),true);
    358383                                       
    … …  
    378403                                       
    379404                                        if ($mark_spam_ip || $mark_spam_email){
     405                                                $check_result['spam']++;
    380406                                                update_user_meta($u[$i]->ID,'ct_marked_as_spam','1',true);
    381407                                                $text .= $u[$i]->user_login.',';
    … …  
    386412                                }
    387413                                fwrite($file_desc, $text);
    388                                 fclose($file_desc);             
    389                                 print 1;
     414                                fclose($file_desc);
     415                                print json_encode($check_result);
    390416                        }
    391417                }
    392         }else
    393                 print 0;
    394 
     418        }else{
     419                $check_result['end'] = 1;
     420                print json_encode($check_result);
     421        }
    395422        die;
    396423}
    … …  
    398425function ct_ajax_info_users($direct_call = false)
    399426{
    400     if (!$direct_call) {
     427    if (!$direct_call)
    401428            check_ajax_referer( 'ct_secret_nonce', 'security' );
    402     }
    403 
    404                 global $wpdb;
    405        
    406                 // All users
    407                 // $r=$wpdb->get_results("
    408                         // SELECT
    409                                 // COUNT(ID) AS cnt
    410                         // FROM $wpdb->users
    411                 // ");
    412                 // $cnt = $r[0]->cnt;
    413                 $args = array(
    414                         'total_users' => true
    415                 );
    416                 $user_query = new WP_User_Query($args);
    417                 $cnt = $user_query->get_total();
    418                
    419                 // Checked
    420                 $r=$wpdb->get_results("select distinct count($wpdb->users.ID) as cnt from $wpdb->users inner join $wpdb->usermeta on $wpdb->users.ID=$wpdb->usermeta.user_id where $wpdb->usermeta.meta_key='ct_checked' or $wpdb->usermeta.meta_key='ct_hash';");
    421                 $cnt_checked = $r[0]->cnt;
    422                
    423                 //Spam
    424                 $r=$wpdb->get_results("select distinct count($wpdb->users.ID) as cnt from $wpdb->users inner join $wpdb->usermeta on $wpdb->users.ID=$wpdb->usermeta.user_id where $wpdb->usermeta.meta_key='ct_marked_as_spam';", ARRAY_A);
    425                 $cnt_spam = $r[0]['cnt'];
    426                
    427                 //Bad
    428                 $r=$wpdb->get_results("select distinct count($wpdb->users.ID) as cnt from $wpdb->users inner join $wpdb->usermeta on $wpdb->users.ID=$wpdb->usermeta.user_id where $wpdb->usermeta.meta_key='ct_bad';", ARRAY_A);
    429                 $cnt_bad = $r[0]['cnt'];
    430                        
    431         printf (__("Total users %s, checked %s, found %s spam users and %s bad users (without IP or email)", 'cleantalk'), $cnt, $cnt_checked, $cnt_spam, $cnt_bad);
     429
     430        global $wpdb;
     431
     432        // All users
     433        $r=$wpdb->get_results("
     434                SELECT
     435                        COUNT(ID) AS cnt
     436                        FROM $wpdb->users
     437        ");
     438        $cnt = $r[0]->cnt;
     439       
     440        // Checked
     441        $r=$wpdb->get_results("select distinct count($wpdb->users.ID) as cnt from $wpdb->users inner join $wpdb->usermeta on $wpdb->users.ID=$wpdb->usermeta.user_id where $wpdb->usermeta.meta_key='ct_checked' or $wpdb->usermeta.meta_key='ct_hash';");
     442        $cnt_checked = $r[0]->cnt;
     443       
     444        //Spam
     445        $r=$wpdb->get_results("select distinct count($wpdb->users.ID) as cnt from $wpdb->users inner join $wpdb->usermeta on $wpdb->users.ID=$wpdb->usermeta.user_id where $wpdb->usermeta.meta_key='ct_marked_as_spam';", ARRAY_A);
     446        $cnt_spam = $r[0]['cnt'];
     447       
     448        //Bad
     449        $r=$wpdb->get_results("select distinct count($wpdb->users.ID) as cnt from $wpdb->users inner join $wpdb->usermeta on $wpdb->users.ID=$wpdb->usermeta.user_id where $wpdb->usermeta.meta_key='ct_bad';", ARRAY_A);
     450        $cnt_bad = $r[0]['cnt'];
     451       
     452        $return = array(
     453                'message' => '',
     454                'total' => $cnt
     455        );
     456       
     457        $return['message'] .= sprintf (__("Total users %s, checked %s, found %s spam users and %s bad users (without IP or email)", 'cleantalk'), $cnt, $cnt_checked, $cnt_spam, $cnt_bad);
     458       
    432459    $backup_notice = '&nbsp;';
    433460    if ($cnt_spam > 0) {
    434461        $backup_notice = __("Please do backup of WordPress database before delete any accounts!", 'cleantalk');
    435462    }
    436         print "<p>$backup_notice</p>";
    437 
    438     if (!$direct_call)
    439             die();
    440 
     463        $return['message'] .= "<p>$backup_notice</p>";
     464       
     465        if($direct_call){
     466                return json_encode($return);
     467        }else{
     468                echo json_encode($return);
     469                die();
     470        }           
     471       
    441472    return null;
    442473}
Note: See TracChangeset for help on using the changeset viewer.