Making WordPress.org


Ignore:
Timestamp:
08/19/2015 02:08:28 PM (11 years ago)
Author:
ocean90
Message:

WP i18n teams: Show all translation contributors and show Slack username of translation editors.

fixes #996.
fixes #1191.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wp-i18n-teams/wp-i18n-teams.php

    r1800 r1838  
    210210                }
    211211
    212                 $contributors = self::get_contributors( $locale );
     212                $contributors = $this->get_contributors( $locale );
    213213                $locale_data['validators'] = $contributors['validators'];
    214214                $locale_data['translators'] = $contributors['translators'];
    215215
    216216                return $locale_data;
     217        }
     218
     219        /**
     220         * Get the translators and validators for the given locale.
     221         *
     222         * @param GP_Locale $locale
     223         * @return array
     224         */
     225        public function get_contributors( $locale ) {
     226                $cache = wp_cache_get( 'contributors-data:' . $locale->wp_locale, 'wp-i18n-teams' );
     227                if ( false !== $cache ) {
     228                        return $cache;
     229                }
     230
     231                $contributors = array();
     232                $contributors['validators'] = $this->get_translation_editors( $locale );
     233                $contributors['translators'] = $this->get_translation_contributors( $locale );
     234
     235                wp_cache_set( 'contributors-data:' . $locale->wp_locale, $contributors, 'wp-i18n-teams', 2 * HOUR_IN_SECONDS );
     236
     237                return $contributors;
    217238        }
    218239
     
    249270
    250271        /**
    251          * Get the translators and validators for the given locale.
     272         * Get the translation editors for the given locale.
    252273         *
    253274         * @param GP_Locale $locale
    254275         * @return array
    255276         */
    256         public static function get_contributors( $locale ) {
    257                 require_once( API_WPORGPATH . 'core/credits/wp-credits.php' );
    258 
    259                 $credits = WP_Credits::factory( WP_CORE_LATEST_RELEASE, $locale );
    260                 $results = $credits->get_results();
    261 
    262                 $contributors = array(
    263                         'validators'  => ! empty( $results['groups']['validators']['data'] )  ? $results['groups']['validators']['data']  : array(),
    264                         'translators' => ! empty( $results['groups']['translators']['data'] ) ? $results['groups']['translators']['data'] : array(),
    265                 );
     277        private function get_translation_editors( $locale ) {
     278                global $wpdb;
     279
     280                $editors = array();
     281
     282                $subdomain = $wpdb->get_var( $wpdb->prepare( "SELECT subdomain FROM locales WHERE locale = %s", $locale->wp_locale ) );
     283                if ( ! $subdomain ) {
     284                        return $editors;
     285                }
     286
     287                $blog_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = '/'", "$subdomain.wordpress.org" ) );
     288                if ( ! $blog_id ) {
     289                        return $editors;
     290                }
     291
     292                $meta_key = $wpdb->base_prefix . intval( $blog_id ) . '_capabilities';
     293                $users = $wpdb->get_col( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$meta_key' AND meta_value LIKE '%translation_editor%'" );
     294                if ( ! $users ) {
     295                        return $editors;
     296                }
     297
     298                $user_data = $wpdb->get_results( "SELECT ID, user_nicename, display_name, user_email FROM $wpdb->users WHERE ID IN (" . implode( ',', $users ) . ")" );
     299                foreach ( $user_data as $user ) {
     300                        if ( $user->display_name && $user->display_name !== $user->user_nicename ) {
     301                                $editors[ $user->user_nicename ] = array(
     302                                        'display_name' => $user->display_name,
     303                                        'email'        => $user->user_email,
     304                                        'nice_name'    => $user->user_nicename,
     305                                        'slack'        => self::get_slack_username( $user->ID ),
     306                                );
     307                        } else {
     308                                $editors[ $user->user_nicename ] = array(
     309                                        'display_name' => $user->user_nicename,
     310                                        'email'        => $user->user_email,
     311                                        'nice_name'    => $user->user_nicename,
     312                                        'slack'        => self::get_slack_username( $user->ID ),
     313                                );
     314                        }
     315                }
     316
     317                uasort( $editors, array( $this, '_sort_display_name_callback' ) );
     318
     319                return $editors;
     320        }
     321
     322        /**
     323         * Get the translation contributors for the given locale.
     324         *
     325         * @param GP_Locale $locale
     326         * @return array
     327         */
     328        private function get_translation_contributors( $locale ) {
     329                global $wpdb;
     330
     331                $contributors = array();
     332
     333                $users = $wpdb->get_col( $wpdb->prepare(
     334                        "SELECT DISTINCT user_id FROM translate_user_translations_count WHERE accepted > 0 AND locale = %s",
     335                        $locale->slug
     336                ) );
     337
     338                if ( ! $users ) {
     339                        $contributors;
     340                }
     341
     342                $user_data = $wpdb->get_results( "SELECT user_nicename, display_name, user_email FROM $wpdb->users WHERE ID IN (" . implode( ',', $users ) . ")" );
     343                foreach ( $user_data as $user ) {
     344                        if ( $user->display_name && $user->display_name !== $user->user_nicename ) {
     345                                $contributors[ $user->user_nicename ] = array(
     346                                        'display_name' => $user->display_name,
     347                                        'nice_name'    => $user->user_nicename,
     348                                );
     349                        } else {
     350                                $contributors[ $user->user_nicename ] = array(
     351                                        'display_name' => $user->user_nicename,
     352                                        'nice_name'    => $user->user_nicename,
     353                                );
     354                        }
     355                }
     356
     357                uasort( $contributors, array( $this, '_sort_display_name_callback' ) );
    266358
    267359                return $contributors;
     
    318410                }
    319411        }
     412
     413        /**
     414         * Get the Slack username for a .org user.
     415         *
     416         * @param int $user_id
     417         *
     418         * @return string
     419         */
     420        protected static function get_slack_username( $user_id ) {
     421                global $wpdb;
     422
     423                $data = $wpdb->get_var( $wpdb->prepare( "SELECT profiledata FROM slack_users WHERE user_id = %d", $user_id ) );
     424                if ( $data && ( $data = json_decode( $data, true ) ) ) {
     425                        return $data['name'];
     426                }
     427
     428                return '';
     429        }
     430
     431        public function _sort_display_name_callback( $a, $b ) {
     432                return strnatcasecmp( $a['display_name'], $b['display_name'] );
     433        }
    320434}
    321435
Note: See TracChangeset for help on using the changeset viewer.