Making WordPress.org


Ignore:
Timestamp:
03/31/2016 08:25:15 PM (9 years ago)
Author:
ocean90
Message:

Translate: Display stats, contributors and language packs on theme project pages.

See #1388.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/routes/wp-directory.php

    r2858 r2864  
    11<?php
    22
    3 class WPorg_GP_Route_WP_Plugins extends GP_Route {
    4 
    5     /**
    6      * Prints stats about sub-project of a specific project.
    7      *
    8      * @param string $project_slug Slug of a project.
    9      */
    10     public function get_plugin_projects( $project_slug ) {
     3class WPorg_GP_Route_WP_Directory extends GP_Route {
     4
     5    /**
     6     * Prints stats about contributors of a specific project.
     7     *
     8     * @param GP_Project $project The project.
     9     * @return array|false False if project not found, otherwise array with contributors.
     10     */
     11    public function get_contributors( $project ) {
    1112        global $wpdb;
    12 
    13         $project_path = 'wp-plugins/' . $project_slug;
    14         $project = GP::$project->by_path( $project_path );
    15         if ( ! $project ) {
    16             return $this->die_with_404();
    17         }
    18 
    19         $rows = $wpdb->get_results( "
    20             SELECT
    21                 path, locale, locale_slug,
    22                 (100 * stats.current/stats.all) as percent_complete,
    23                 stats.waiting+stats.fuzzy as waiting_strings,
    24                 stats.untranslated as untranslated
    25             FROM {$wpdb->project_translation_status} stats
    26                 LEFT JOIN {$wpdb->gp_projects} p ON stats.project_id = p.id
    27             WHERE
    28                 p.parent_project_id = '{$project->id}'
    29         " );
    30 
    31         // Split out into $[Locale][Project] = %
    32         $translation_locale_statuses = array();
    33         $sub_projects = array();
    34         foreach ( $rows as $set ) {
    35 
    36             // Find unique locale key.
    37             $locale_key = $set->locale;
    38             if ( 'default' != $set->locale_slug ) {
    39                 $locale_key = $set->locale . '/' . $set->locale_slug;
    40             }
    41             $sub_project = str_replace( "$project_path/", '', $set->path );
    42             $sub_projects[ $sub_project ] = true;
    43 
    44             /*
    45              * > 50% round down, so that a project with all strings except 1 translated shows 99%, instead of 100%.
    46              * < 50% round up, so that a project with just a few strings shows 1%, instead of 0%.
    47              */
    48             $percent_complete = (float) $set->percent_complete;
    49             $translation_locale_statuses[ $locale_key ][ $sub_project ] = ( $percent_complete > 50 ) ? floor( $percent_complete ) : ceil( $percent_complete );
    50 
    51             // Increment the amount of waiting and untranslated strings.
    52             if ( ! isset( $translation_locale_statuses[ $locale_key ]['waiting'] ) ) {
    53                 $translation_locale_statuses[ $locale_key ]['waiting'] = 0;
    54             }
    55             if ( ! isset( $translation_locale_statuses[ $locale_key ]['untranslated'] ) ) {
    56                 $translation_locale_statuses[ $locale_key ]['untranslated'] = 0;
    57             }
    58             $translation_locale_statuses[ $locale_key ]['waiting'] += (int) $set->waiting_strings;
    59             $translation_locale_statuses[ $locale_key ]['untranslated'] += (int) $set->untranslated;
    60 
    61 
    62             ksort( $translation_locale_statuses[ $locale_key ], SORT_NATURAL );
    63         }
    64 
    65         // Check if the plugin has at least one code project. These won't be created if a plugin
    66         // has no text domain defined.
    67         $sub_projects = array_keys( $sub_projects );
    68         $has_error = ( ! in_array( 'dev', $sub_projects ) && ! in_array( 'stable', $sub_projects ) );
    69 
    70         unset( $project_path, $locale_key, $rows, $set, $sub_project, $sub_projects );
    71 
    72         // Calculate a list of [Locale] = % subtotals
    73         $translation_locale_complete = array();
    74         foreach ( $translation_locale_statuses as $locale => $sets ) {
    75             unset( $sets['waiting'], $sets['untranslated'] );
    76             $translation_locale_complete[ $locale ] = round( array_sum( $sets ) / count( $sets ), 3 );
    77         }
    78         unset( $locale, $sets );
    79 
    80 
    81         // Sort by translation completeness, least number of waiting strings, and locale slug.
    82         uksort( $translation_locale_complete, function ( $a, $b ) use ( $translation_locale_complete, $translation_locale_statuses ) {
    83             if ( $translation_locale_complete[ $a ] < $translation_locale_complete[ $b ] ) {
    84                 return 1;
    85             } elseif ( $translation_locale_complete[ $a ] == $translation_locale_complete[ $b ] ) {
    86                 if ( $translation_locale_statuses[ $a ]['waiting'] != $translation_locale_statuses[ $b ]['waiting'] ) {
    87                     return strnatcmp( $translation_locale_statuses[ $a ]['waiting'], $translation_locale_statuses[ $b ]['waiting'] );
    88                 } else {
    89                     return strnatcmp( $a, $b );
    90                 }
    91             } else {
    92                 return -1;
    93             }
    94         } );
    95 
    96         if ( function_exists( 'wporg_get_plugin_icon' ) ) {
    97             $project->icon = wporg_get_plugin_icon( $project->slug, 64 );
    98         } else {
    99             $project->icon = '<div class="default-icon"><span class="dashicons dashicons-admin-plugins"></span></div>';
    100         }
    101 
    102         $this->tmpl( 'projects-wp-plugins', get_defined_vars() );
    103     }
    104 
    105     /**
    106      * Prints stats about contributors of a specific project.
    107      *
    108      * @param string $project_slug Slug of a project.
    109      */
    110     public function get_plugin_contributors( $project_slug ) {
    111         global $wpdb;
    112 
    113         $project_path = 'wp-plugins/' . $project_slug;
    114         $project = GP::$project->by_path( $project_path );
    115         if ( ! $project ) {
    116             return $this->die_with_404();
    117         }
    118 
    119         if ( function_exists( 'wporg_get_plugin_icon' ) ) {
    120             $project->icon = wporg_get_plugin_icon( $project->slug, 64 );
    121         } else {
    122             $project->icon = '<div class="default-icon"><span class="dashicons dashicons-admin-plugins"></span></div>';
    123         }
    12413
    12514        $contributors_by_locale = array();
     
    15746        unset( $translation_editors );
    15847
     48        foreach( $this->get_translation_contributors_by_locale( $project->id ) as $row ) {
     49            if ( ! isset( $contributors_by_locale[ $row->locale ] ) ) {
     50                $contributors_by_locale[ $row->locale ] = $default_value;
     51            }
     52
     53            if ( isset( $contributors_by_locale[ $row->locale ]['editors'][ $row->user_id ] ) ) {
     54                continue;
     55            }
     56
     57            if ( isset( $contributors_by_locale[ $row->locale ]['contributors'][ $row->user_id ] ) ) {
     58                continue;
     59            }
     60
     61            $user = get_user_by( 'id', $row->user_id );
     62            if ( ! $user ) {
     63                continue;
     64            }
     65
     66            $contributors_by_locale[ $row->locale ]['contributors'][ $row->user_id ] = (object) array(
     67                'nicename'     => $user->user_nicename,
     68                'display_name' => $this->_encode( $user->display_name ),
     69            );
     70
     71            $contributors_by_locale[ $row->locale ]['count']++;
     72        }
     73
    15974        $sub_projects = $wpdb->get_col( $wpdb->prepare( "
    16075            SELECT id
     
    191106        }
    192107
    193         $chart_data = $this->get_plugin_contributors_chart_data( $project->id, $sub_projects );
    194 
    195         $this->tmpl( 'projects-wp-plugins-contributors', get_defined_vars() );
     108        return $contributors_by_locale;
    196109    }
    197110
     
    199112     * Generates the chart data for contributors activity.
    200113     *
    201      * @param  int    $project_id   The ID of a project. Used to store data as meta.
    202      * @param  array $sub_projects Optional. IDs of sub-projects.
     114     * @param GP_Project $project_id The project.
    203115     * @return array The data to build a chart via Chartist.js.
    204116     */
    205     private function get_plugin_contributors_chart_data( $project_id, $sub_projects = null ) {
    206         $chart_data = gp_get_meta( 'wp-plugins', $project_id, 'contributors-chart-data' );
    207         if ( $chart_data ) {
    208             if ( $chart_data['last_updated'] + DAY_IN_SECONDS > time() ) {
    209                 return $chart_data;
    210             }
    211         }
    212 
     117    protected function get_contributors_chart_data( $project ) {
    213118        global $wpdb;
    214119
    215         if ( ! $sub_projects ) {
    216             $sub_projects = $wpdb->get_col( $wpdb->prepare( "
    217                 SELECT id
    218                 FROM {$wpdb->gp_projects}
    219                 WHERE parent_project_id = %d
    220             ", $$project_id ) );
    221         }
    222 
     120        $sub_projects = $wpdb->get_col( $wpdb->prepare( "
     121            SELECT id
     122            FROM {$wpdb->gp_projects}
     123            WHERE parent_project_id = %d
     124        ", $project->id ) );
     125
     126        $project_ids = array_merge( array( $project->id ), $sub_projects );
    223127        $translation_set_ids = $wpdb->get_col( "
    224             SELECT `id` FROM {$wpdb->gp_translation_sets} WHERE `project_id` IN (" . implode( ',', $sub_projects ) . ")
     128            SELECT `id` FROM {$wpdb->gp_translation_sets} WHERE `project_id` IN (" . implode( ',', $project_ids ) . ")
    225129        " );
    226130
     
    277181        }
    278182
    279         $last_updated = time();
    280         $chart_data = compact( 'labels', 'series', 'last_updated' );
    281 
    282         gp_update_meta( $project_id, 'contributors-chart-data', $chart_data, 'wp-plugins' );
     183        $chart_data = compact( 'labels', 'series' );
    283184
    284185        return $chart_data;
     
    288189     * Prints stats about language packs of a specific project.
    289190     *
    290      * @param string $project_slug Slug of a project.
    291      */
    292     public function get_plugin_language_packs( $project_slug ) {
    293         $project_path = 'wp-plugins/' . $project_slug;
    294         $project = GP::$project->by_path( $project_path );
    295         if ( ! $project ) {
    296             return $this->die_with_404();
    297         }
    298 
    299         if ( function_exists( 'wporg_get_plugin_icon' ) ) {
    300             $project->icon = wporg_get_plugin_icon( $project->slug, 64 );
    301         } else {
    302             $project->icon = '<div class="default-icon"><span class="dashicons dashicons-admin-plugins"></span></div>';
    303         }
    304 
     191     * @param string $type Type of the language pack, plugin or theme.
     192     * @param string $slug Slug of a project.
     193     */
     194    public function get_language_packs( $type, $slug ) {
    305195        $http_context = stream_context_create( array(
    306196            'http' => array(
     
    308198            ),
    309199        ) );
    310         $json = file_get_contents( "https://api.wordpress.org/translations/plugins/1.0/?slug={$project_slug}", null, $http_context );
     200        if ( 'plugin' === $type ) {
     201            $type = 'plugins';
     202        } else {
     203            $type = 'themes';
     204        }
     205        $json = file_get_contents( "https://api.wordpress.org/translations/$type/1.0/?slug={$slug}", null, $http_context );
    311206        $language_packs = $json && '{' == $json[0] ? json_decode( $json ) : null;
    312207
    313         $this->tmpl( 'projects-wp-plugins-language-packs', get_defined_vars() );
     208        return $language_packs;
    314209    }
    315210
Note: See TracChangeset for help on using the changeset viewer.