Making WordPress.org


Ignore:
Timestamp:
03/07/2024 06:39:00 AM (13 months ago)
Author:
akirk
Message:

Translate Events: add languages and contributors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-events/includes/stats-calculator.php

    r13279 r13298  
    55use Exception;
    66use WP_Post;
     7use WP_User;
     8use GP_Locale;
     9use GP_Locales;
    710
    811class Stats_Row {
     
    1013    public int $reviewed;
    1114    public int $users;
    12 
    13     public function __construct( $created, $reviewed, $users ) {
     15    public ?GP_Locale $language = null;
     16
     17    public function __construct( $created, $reviewed, $users, ?GP_Locale $language = null ) {
    1418        $this->created  = $created;
    1519        $this->reviewed = $reviewed;
    1620        $this->users    = $users;
     21        $this->language = $language;
    1722    }
    1823}
     
    5055     */
    5156    public function rows(): array {
     57        uasort(
     58            $this->rows,
     59            function ( $a, $b ) {
     60                if ( ! $a->language && ! $b->language ) {
     61                    return 0;
     62                }
     63                if ( ! $a->language ) {
     64                    return -1;
     65                }
     66                if ( ! $b->language ) {
     67                    return 1;
     68                }
     69
     70                return strcasecmp( $a->language->english_name, $b->language->english_name );
     71            }
     72        );
     73
    5274        return $this->rows;
    5375    }
     
    101123            }
    102124
     125            $lang = GP_Locales::by_slug( $row->locale );
     126            if ( ! $lang ) {
     127                $lang = null;
     128            }
     129
    103130            $stats_row = new Stats_Row(
    104131                $row->created,
    105132                $row->total - $row->created,
    106133                $row->users,
     134                $lang
    107135            );
    108136
     
    115143
    116144        return $stats;
     145    }
     146
     147    /**
     148     * Get contributors for an event.
     149     */
     150    public function get_contributors( WP_Post $event ): array {
     151        global $wpdb, $gp_table_prefix;
     152
     153        // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
     154        // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
     155        // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
     156        // phpcs thinks we're doing a schema change but we aren't.
     157        // phpcs:disable WordPress.DB.DirectDatabaseQuery.SchemaChange
     158        $rows = $wpdb->get_results(
     159            $wpdb->prepare(
     160                "
     161                select user_id, group_concat( distinct locale ) as locales
     162                from {$gp_table_prefix}event_actions
     163                where event_id = %d
     164                group by user_id
     165            ",
     166                array(
     167                    $event->ID,
     168                )
     169            )
     170        );
     171        // phpcs:enable
     172
     173        $users = array();
     174        foreach ( $rows as $row ) {
     175            $user          = new WP_User( $row->user_id );
     176            $user->locales = explode( ',', $row->locales );
     177            $users[]       = $user;
     178        }
     179
     180        uasort(
     181            $users,
     182            function ( $a, $b ) {
     183                return strcasecmp( $a->display_name, $b->display_name );
     184            }
     185        );
     186
     187        return $users;
    117188    }
    118189
Note: See TracChangeset for help on using the changeset viewer.