Changeset 2734
- Timestamp:
- 03/10/2016 11:14:57 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-custom-stats/stats/user.php
r2657 r2734 10 10 class WPorg_GP_User_Stats { 11 11 12 function __construct() { 12 private $user_stats = array(); 13 14 public function __construct() { 13 15 global $wpdb, $gp_table_prefix; 14 16 15 add_action( 'gp_translation_created', array( $this, 'translation_created' ) ); 16 add_action( 'gp_translation_saved', array( $this, 'translation_saved' ) ); 17 add_action( 'gp_translation_created', array( $this, 'translation_updated' ) ); 18 add_action( 'gp_translation_saved', array( $this, 'translation_updated' ) ); 19 20 // DB Writes are delayed until shutdown to bulk-update the stats during imports. 21 add_action( 'shutdown', array( $this, 'write_stats_to_database' ) ); 17 22 18 23 $wpdb->user_translations_count = $gp_table_prefix . 'user_translations_count'; 19 24 } 20 25 21 function translation_created( $translation ) { 22 return $this->translation_saved( $translation, 'created' ); 23 } 24 25 function translation_saved( $translation, $action = 'saved' ) { 26 public function translation_updated( $translation ) { 26 27 if ( ! $translation->user_id ) { 27 28 return; … … 34 35 $this->bump_user_stat( $translation->user_id, $translation_set->locale, $translation_set->slug, 1, 0 ); 35 36 36 } elseif ( 'current' === $translation->status && ' created' === $action) {37 } elseif ( 'current' === $translation->status && 'gp_translation_created' === current_filter() ) { 37 38 // New translation suggested & approved 38 39 $this->bump_user_stat( $translation->user_id, $translation_set->locale, $translation_set->slug, 1, 1 ); … … 45 46 } 46 47 47 function bump_user_stat( $user_id, $locale, $locale_slug, $suggested = 0, $accepted = 0 ) { 48 global $wpdb; 49 $wpdb->query( $wpdb->prepare( 50 "INSERT INTO {$wpdb->user_translations_count} (`user_id`, `locale`, `locale_slug`, `suggested`, `accepted`) VALUES (%d, %s, %s, %d, %d) 51 ON DUPLICATE KEY UPDATE `suggested`=`suggested` + VALUES(`suggested`), `accepted`=`accepted` + VALUES(`accepted`)", 52 $user_id, $locale, $locale_slug, $suggested, $accepted 53 ) ); 48 private function bump_user_stat( $user_id, $locale, $locale_slug, $suggested = 0, $accepted = 0 ) { 49 $key = "$user_id,$locale,$locale_slug"; 50 51 if ( isset( $this->user_stats[ $key ] ) ) { 52 $this->user_stats[ $key ]->suggested += $suggested; 53 $this->user_stats[ $key ]->accepted += $accepted; 54 } else { 55 $this->user_stats[ $key ] = (object) array( 56 'suggested' => $suggested, 57 'accepted' => $accepted, 58 ); 59 } 54 60 } 55 61 62 public function write_stats_to_database() { 63 global $wpdb; 64 65 $values = array(); 66 foreach ( $this->user_stats as $key => $stats ) { 67 list( $user_id, $locale, $locale_slug ) = explode( ',', $key ); 68 69 $values[] = $wpdb->prepare( '(%d, %s, %s, %d, %d)', 70 $user_id, 71 $locale, 72 $locale_slug, 73 $stats->suggested, 74 $stats->accepted 75 ); 76 77 // If we're processing a large batch, add them as we go to avoid query lengths & memory limits. 78 if ( count( $values ) > 50 ) { 79 $r = $wpdb->query( 80 "INSERT INTO {$wpdb->user_translations_count} (`user_id`, `locale`, `locale_slug`, `suggested`, `accepted`) 81 VALUES " . implode( ', ', $values ) . " 82 ON DUPLICATE KEY UPDATE `suggested`=`suggested` + VALUES(`suggested`), `accepted`=`accepted` + VALUES(`accepted`)" 83 ); 84 error_log( print_r($r,true)); 85 $values = array(); 86 } 87 } 88 89 if ( $values ) { 90 $r = $wpdb->query( 91 "INSERT INTO {$wpdb->user_translations_count} (`user_id`, `locale`, `locale_slug`, `suggested`, `accepted`) 92 VALUES " . implode( ', ', $values ) . " 93 ON DUPLICATE KEY UPDATE `suggested`=`suggested` + VALUES(`suggested`), `accepted`=`accepted` + VALUES(`accepted`)" 94 ); 95 } 96 } 56 97 } 57 98
Note: See TracChangeset
for help on using the changeset viewer.