Advertisement
johncarlson21

Untitled

Oct 10th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.80 KB | None | 0 0
  1. function mgm_coupon_used_users($coupon_id = NULL){
  2.  
  3.    
  4.  
  5.     global $wpdb;
  6.  
  7.     @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', '2048M' ) );
  8.  
  9.     $users = array();
  10.  
  11.    
  12.  
  13.     if(!is_null($coupon_id)){
  14.  
  15.        
  16.  
  17.         $start = 0;
  18.  
  19.         $limit = 1000;     
  20.  
  21.        
  22.  
  23.         $qry  = "SELECT count(DISTINCT {$wpdb->users}.ID) ";
  24.  
  25.         $qry .= "FROM {$wpdb->users} ";
  26.  
  27.         $qry .= "INNER JOIN {$wpdb->usermeta} ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id ) ";
  28.  
  29.         $qry .= "INNER JOIN {$wpdb->usermeta} AS mt1 ON ( {$wpdb->users}.ID = mt1.user_id ) ";
  30.  
  31.         $qry .= "INNER JOIN {$wpdb->usermeta} AS mt2 ON ( {$wpdb->users}.ID = mt2.user_id ) ";
  32.  
  33.         $qry .= "WHERE 1 = 1 AND (({$wpdb->usermeta}.meta_key =  '_mgm_user_register_coupon' AND CAST( {$wpdb->usermeta}.meta_value AS UNSIGNED ) =  '{$coupon_id}') ";
  34.  
  35.         $qry .= "OR (mt1.meta_key =  '_mgm_user_upgrade_coupon' AND CAST( mt1.meta_value AS UNSIGNED ) =  '{$coupon_id}') ";
  36.  
  37.         $qry .= "OR (mt2.meta_key =  '_mgm_user_extend_coupon' AND CAST( mt2.meta_value AS UNSIGNED ) =  '{$coupon_id}')) ";       
  38.  
  39.        
  40.  
  41.         $count  = $wpdb->get_var($qry);
  42.  
  43.        
  44.  
  45.         if($count) {           
  46.  
  47.             // read again
  48.  
  49.             for( $i = $start; $i < $count; $i = $i + $limit ) {                    
  50.  
  51.                 $users = array_merge($users, (array)mgm_patch_partial_coupon_used_users($i, $limit, $coupon_id));
  52.  
  53.                 //a small delay of 0.01 second
  54.  
  55.                 usleep(10000);                 
  56.  
  57.             }
  58.  
  59.         }
  60.  
  61.     }
  62.  
  63.    
  64.  
  65.     return $users;
  66.  
  67. }
  68.  
  69. /**
  70.  
  71.  * patch partial coupon used users
  72.  
  73.  */
  74.  
  75. function mgm_patch_partial_coupon_used_users($start, $limit,$coupon_id = NULL) {
  76.  
  77.     global $wpdb;
  78.  
  79.  
  80.  
  81.     $results = array();
  82.  
  83.    
  84.  
  85.     if(!is_null($coupon_id)){
  86.  
  87.        
  88.  
  89.         $qry  = "SELECT DISTINCT SQL_CALC_FOUND_ROWS {$wpdb->users}.ID, {$wpdb->users}.display_name, {$wpdb->users}.user_email, {$wpdb->users}.user_login, ";
  90.  
  91.         $qry .= "(SELECT t.meta_value FROM {$wpdb->usermeta} t WHERE t.user_id = {$wpdb->users}.ID AND t.meta_key =  'mgm_member_options') mgm_member_options ";
  92.  
  93.         $qry .= "FROM {$wpdb->users} ";
  94.  
  95.         $qry .= "INNER JOIN {$wpdb->usermeta} ON ( {$wpdb->users}.ID = {$wpdb->usermeta}.user_id ) ";
  96.  
  97.         $qry .= "INNER JOIN {$wpdb->usermeta} AS mt1 ON ( {$wpdb->users}.ID = mt1.user_id ) ";
  98.  
  99.         $qry .= "INNER JOIN {$wpdb->usermeta} AS mt2 ON ( {$wpdb->users}.ID = mt2.user_id ) ";
  100.  
  101.         $qry .= "WHERE 1 = 1 AND (({$wpdb->usermeta}.meta_key =  '_mgm_user_register_coupon' AND CAST( {$wpdb->usermeta}.meta_value AS UNSIGNED ) =  '{$coupon_id}') ";
  102.  
  103.         $qry .= "OR (mt1.meta_key =  '_mgm_user_upgrade_coupon' AND CAST( mt1.meta_value AS UNSIGNED ) =  '{$coupon_id}') ";
  104.  
  105.         $qry .= "OR (mt2.meta_key =  '_mgm_user_extend_coupon' AND CAST( mt2.meta_value AS UNSIGNED ) =  '{$coupon_id}')) ";
  106.  
  107.         $qry .= "ORDER BY user_login ASC LIMIT ". $start.",".$limit;
  108.  
  109.    
  110.  
  111.         $results =  $wpdb->get_results($qry);
  112.  
  113.        
  114.  
  115.         return $results;
  116.  
  117.     }
  118.  
  119.     return $results;
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement