Making WordPress.org

Ticket #340: 340.2.diff

File 340.2.diff, 8.2 KB (added by iandunn, 11 years ago)

Refactor 340.diff to access data directly instead of using API

  • global.wordpress.org/public_html/wp-content/plugins/css/front.css

     
     1.translators-info table {
     2        border: 1px solid #ccc;
     3        border-width: 1px 1px 1px 0;
     4        border-spacing: 0;
     5        margin: .7em 0 1.5em;
     6        width: 100%;
     7}
     8
     9.translators-info th {
     10        background: #ccc;
     11        border: 1px solid #ccc;
     12        border-width: 1px 0 0 1px;
     13        padding: 6px 15px;
     14}
     15
     16.translators-info td {
     17        border: 1px solid #ccc;
     18        border-width: 1px 0 0 1px;
     19        padding: 6px 15px;
     20}
     21
     22
     23.i18n-label.latest {
     24        color: #468847;
     25}
     26.i18n-label.minor-behind {
     27        color: #F7D708;
     28}
     29.i18n-label.major-behind-one {
     30        color: #E6A45A;
     31}
     32.i18n-label.major-behind-many {
     33        color: #e38587;
     34}
     35
     36
     37.version.latest {
     38        background: #d2e7ca;
     39}
     40.version.minor-behind {
     41        background: #fef0c1;
     42}
     43.version.major-behind-one {
     44        background: #E6A45A;
     45}
     46.version.major-behind-many {
     47        background: #e38587;
     48}
  • global.wordpress.org/public_html/wp-content/plugins/inc/api.php

     
     1<?php
     2
     3class WP_I18n_Team_Api {
     4        public static function get_sites() {
     5                $locales = GP_Locales::locales();
     6                $locales = array_filter( $locales, array( __CLASS__, 'filter_locale_for_wp' ) );
     7                usort( $locales, array( __CLASS__, 'sort_locales' ) );
     8
     9                return $locales;
     10        }
     11
     12        /* HELPER FUNCTIONS */
     13
     14        public static function filter_locale_for_wp( $element ) {
     15                if ( ! isset( $element->wp_locale ) ) {
     16                        return false;
     17                }
     18
     19                return true;
     20        }
     21
     22        public static function sort_locales( $a, $b ) {
     23                return strcmp( $a->english_name, $b->english_name );
     24        }
     25
     26}
  • global.wordpress.org/public_html/wp-content/plugins/inc/crawler.php

     
     1<?php
     2
     3class WP_I18n_Team_Crawler {
     4
     5        public static function get_locale( $slug, $wp_locale ) {
     6                $locale_data = array(
     7                        'version' => self::get_latest_version( $wp_locale ),
     8                        'url'     => self::get_locale_url( $slug ),
     9                );
     10
     11                return $locale_data;
     12        }
     13
     14        private static function get_latest_version( $wp_locale ) {
     15                global $rosetta;
     16
     17                // Temporarily override the Rosetta locale and get the latest release
     18                $overridden_locale              = $rosetta->rosetta->locale;
     19                $rosetta->rosetta->locale       = $wp_locale;
     20                $rosetta->rosetta->releases_dir = trailingslashit( $rosetta->rosetta->root_dir ) . trailingslashit( $rosetta->rosetta->releases_subdir ) . $wp_locale;
     21                $latest_release                 = $rosetta->rosetta->get_latest_release();
     22
     23                // Reset the original locale
     24                $rosetta->rosetta->locale       = $overridden_locale;
     25                $rosetta->rosetta->releases_dir = trailingslashit( $rosetta->rosetta->root_dir ) . trailingslashit( $rosetta->rosetta->releases_subdir ) . $overridden_locale;
     26
     27                // Extract the version
     28                if ( empty( $latest_release['version'] ) ) {
     29                        $latest_version = false;
     30                } else {
     31                        $latest_version = $latest_release['version'];
     32                }
     33
     34                return $latest_version;
     35        }
     36
     37        private static function get_locale_url( $slug ) {
     38                switch ( $slug ) {
     39                        case 'es-cl':
     40                                $subdomain = 'cl';
     41                        break;
     42                        case 'es-pe':
     43                                $subdomain = 'pe';
     44                        break;
     45                        case 'es-ve':
     46                                $subdomain = 've';
     47                        break;
     48                        case 'pt-br':
     49                                $subdomain = 'br';
     50                        break;
     51                        case 'sa-in':
     52                                $subdomain = 'sa';
     53                        break;
     54                        case 'zh-cn':
     55                                $subdomain = 'cn';
     56                        break;
     57                        case 'zh-tw':
     58                                $subdomain = 'tw';
     59                        break;
     60                        default:
     61                                $subdomain = $slug;
     62                        break;
     63                }
     64
     65                if ( domain_exists( $subdomain . '.wordpress.org', '/' ) ) {
     66                        $url = 'http://' . $subdomain . '.wordpress.org';
     67                } else {
     68                        $url = false;
     69                }
     70
     71                return $url;
     72        }
     73
     74}
  • global.wordpress.org/public_html/wp-content/plugins/wp-i18n-team-crawler.php

     
     1<?php
     2/*
     3Plugin Name: WP I18N Teams
     4Plugin URI: 
     5Description: Scans through a few APIs to generate a list of all languages/members
     6Version:     0.7
     7License:     GPLv2 or later
     8Author:      Marko Heijnen
     9Author URI:  http://www.markoheijnen.com
     10Text Domain: wporg
     11*/
     12
     13include 'inc/api.php';
     14include 'inc/crawler.php';
     15
     16class WP_I18n_Teams {
     17        static $version = '0.7';
     18
     19        public function __construct() {
     20                add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
     21                add_shortcode( 'wp-i18n-team',    array( $this, 'all_information' ) );
     22        }
     23
     24        public function enqueue_assets() {
     25                wp_enqueue_style( 'wp-i18n-team', plugins_url( 'css/front.css', __FILE__ ), array(), self::$version );
     26        }
     27
     28        public function all_information( $args ) {
     29                $no_sites = $no_downloads = $latest = $minor_behind = $major_behind_one = $major_behind_many = 0;
     30
     31                $sites = WP_I18n_Team_Api::get_sites();
     32
     33                $table  = '<table>';
     34                $table .= '<thead>';
     35                $table .= '<tr>';
     36                $table .= '<th>' . __( 'Locale Name', 'wporg' ) . '</th>';
     37                $table .= '<th>' . __( 'Native Name', 'wporg' ) . '</th>';
     38                $table .= '<th>' . __( 'Locale Code', 'wporg' ) . '</th>';
     39                $table .= '<th>' . __( 'WordPress Locale', 'wporg' ) . '</th>';
     40                $table .= '<th>' . __( 'Version', 'wporg' ) . '</th>';
     41                $table .= '</tr>';
     42                $table .= '</thead>';
     43                $table .= '<tbody>';
     44
     45                foreach ( $sites as $site ) {
     46                        $locale = WP_I18n_Team_Crawler::get_locale( $site->slug, $site->wp_locale );
     47                        $class  = '';
     48
     49                        if ( $locale['version'] ) {
     50                                $class = 'version';
     51
     52                                $one_lower  = WP_CORE_LATEST_RELEASE - 0.1;
     53
     54                                if ( $locale['version'] == WP_CORE_LATEST_RELEASE ) {
     55                                        $class .= ' latest';
     56
     57                                        $latest++;
     58                                }
     59                                else if ( substr( $locale['version'], 0, 3 ) == substr( WP_CORE_LATEST_RELEASE, 0, 3 ) ) {
     60                                        $class .= ' minor-behind';
     61
     62                                        $minor_behind++;
     63                                }
     64                                else if ( substr( $locale['version'], 0, 3 ) == substr( $one_lower, 0, 3 ) ) {
     65                                        $class .= ' major-behind-one';
     66
     67                                        $major_behind_one++;
     68                                }
     69                                else {
     70                                        $class .= ' major-behind-many';
     71
     72                                        $major_behind_many++;
     73                                }
     74                        }
     75
     76                        if ( $locale['url'] ) {
     77                                $locale_code = '<a href="' . $locale['url'] . '">' . $site->slug . '</a>';
     78
     79                                if ( $locale['version'] ) {
     80                                        $version = $locale['version'];
     81                                }
     82                                else {
     83                                        $version = __( 'None', 'wporg' );
     84
     85                                        $no_downloads++;
     86                                }
     87                        }
     88                        else {
     89                                $locale_code = $site->slug;
     90                                $version     = __( 'No site', 'wporg' );
     91
     92                                $no_sites++;
     93                        }
     94
     95                        $table .= '<tr class="' . $class . '">';
     96                        $table .= '<td>' . $site->english_name . '</td>';
     97                        $table .= '<td>' . $site->native_name . '</td>';
     98                        $table .= '<td>' . $locale_code . '</td>';
     99                        $table .= '<td>' . $site->wp_locale . '</td>';
     100                        $table .= '<td>' . $version . '</td>';
     101                        $table .= '</tr>';
     102                }
     103
     104                $table .= '</tbody>';
     105                $table .= '</table>';
     106
     107                $html  = '<p>';
     108                $html .= sprintf(
     109                        __( '%s locales up-to-date. %s locales behind by one minor version. %s locales behind by one major version. %s locales behind by two or more major versions. %s locales do not yet have a package available.', 'wporg' ),
     110                        '<strong class="i18n-label latest">' . $latest . '</strong>',
     111                        '<strong class="i18n-label minor-behind">' . $minor_behind . '</strong>',
     112                        '<strong class="i18n-label major-behind-one">' . $major_behind_one . '</strong>',
     113                        '<strong class="i18n-label major-behind-many">' . $major_behind_many . '</strong>',
     114                        '<strong class="i18n-label">' . ( $no_sites + $no_downloads ) . '</strong>'
     115                );
     116                $html .= '</p>';
     117
     118                $html = '<div class="translators-info">' . $html . $table . '</div>';
     119
     120                return $html;
     121        }
     122
     123}
     124
     125$GLOBALS['wp_i18n_teams'] = new WP_I18n_Teams();