Making WordPress.org

Changeset 1399


Ignore:
Timestamp:
03/13/2015 09:48:54 PM (11 years ago)
Author:
ocean90
Message:

Rosetta: Update page for download stats.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/downloads/rosetta-downloads.php

    r889 r1399  
    11<?php
    2 /* Plugin Name: Rosetta Downloads
    3  * Author: Nacin
     2/**
     3 * Plugin Name: Rosetta Downloads
     4 * Plugin URI: https://wordpress.org/
     5 * Description: Dashboard page for download stats.
     6 * Author: Nacin, Dominik Schilling
     7 * Version: 1.0
    48 */
    59
    610class Rosetta_Downloads {
    711
    8     function __construct() {
     12    /**
     13     * Class Constructor.
     14     */
     15    public function __construct() {
     16        add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
     17    }
     18
     19    /**
     20     * Attaches hooks once plugins are loaded.
     21     */
     22    public function plugins_loaded() {
    923        add_action( 'admin_menu', array( $this, 'admin_menu' ) );
    1024    }
    1125
    12     function admin_menu() {
    13         add_dashboard_page( 'Download Stats', 'Download Stats', 'read', 'download-stats', array( $this, 'render_download_stats' ) );
     26    /**
     27     * Registers "Download Stats" page.
     28     */
     29    public function admin_menu() {
     30        add_dashboard_page(
     31            __( 'Download Stats', 'rosetta' ),
     32            __( 'Download Stats', 'rosetta' ),
     33            'read',
     34            'download-stats',
     35            array( $this, 'render_download_stats' )
     36        );
    1437    }
    1538
    16     function get_counts() {
     39    /**
     40     * Returns download counts for release packages of the current stable branch.
     41     *
     42     * @return array Download counts per locale.
     43     */
     44    function get_release_download_counts() {
    1745        global $wpdb;
    18         $orderby = isset( $_GET['desc'] ) ? 'SUM(`downloads`) DESC' : '`locale`';
    19         $stable_branch = implode( '.', array_map( 'absint', explode( '.', WP_CORE_STABLE_BRANCH ) ) );
    20         return $wpdb->get_results( "SELECT `locale`, SUM(`downloads`) FROM `download_counts` WHERE `release` LIKE '{$stable_branch}%' AND `release` NOT LIKE '%-%' GROUP BY `locale` ORDER BY $orderby", ARRAY_N );
     46
     47        $cache = wp_cache_get( 'downloadcounts:release', 'rosetta' );
     48        if ( false !== $cache ) {
     49            return $cache;
     50        }
     51
     52        $results = $wpdb->get_results( $wpdb->prepare( "
     53            SELECT
     54                `locale`,
     55                SUM(`downloads`) AS downloads
     56            FROM `download_counts`
     57            WHERE
     58                `release` LIKE %s AND
     59                `release` NOT LIKE '%%-%%'
     60            GROUP BY `locale`
     61        ", WP_CORE_STABLE_BRANCH . '%' ) );
     62
     63        if ( ! $results ) {
     64            return array();
     65        }
     66
     67        $counts = wp_list_pluck( $results, 'downloads', 'locale' );
     68
     69        wp_cache_add( 'downloadcounts:release', $counts, 'rosetta', 60 );
     70
     71        return $counts;
    2172    }
    2273
     74    /**
     75     * Returns download counts for language packs of the current stable branch.
     76     *
     77     * @return array Download counts per locale.
     78     */
     79    function get_translation_download_counts() {
     80        global $wpdb;
     81
     82        $cache = wp_cache_get( 'downloadcounts:translation', 'rosetta' );
     83        if ( false !== $cache ) {
     84            return $cache;
     85        }
     86
     87        $results = $wpdb->get_results( $wpdb->prepare( "
     88            SELECT `locale`, SUM(`downloads`) AS downloads
     89            FROM `translation_download_counts`
     90            WHERE
     91                `version` LIKE %s AND
     92                `type` = 'core' AND
     93                `domain` = 'default'
     94            GROUP BY `locale`
     95        ", WP_CORE_STABLE_BRANCH . '%' ) );
     96
     97        if ( ! $results ) {
     98            return array();
     99        }
     100
     101        $counts = wp_list_pluck( $results, 'downloads', 'locale' );
     102
     103        wp_cache_add( 'downloadcounts:translation', $counts, 'rosetta', 60 );
     104
     105        return $counts;
     106    }
     107
     108    /**
     109     * Renders the "Download Stats" page.
     110     */
    23111    function render_download_stats() {
    24         echo '<div class="wrap">';
    25         screen_icon();
    26         echo '<h2>' . __( 'Download Stats', 'rosetta' ) . '</h2>';
    27         echo '<table cellpadding=3 cellspacing=2>';
     112        $total_release_counts = $total_translation_counts = 0;
     113        $this_locale = get_locale();
     114        $release_counts = $this->get_release_download_counts();
     115        $translation_counts = $this->get_translation_download_counts();
     116        $locales = array_unique( array_merge( array_keys( $release_counts ), array_keys( $translation_counts ) ) );
     117        $rows = array();
    28118
    29         echo '<p>' . sprintf( __( 'This page shows the <a href="%s">Download Counter</a> number &mdash; total downloads of WordPress %s &mdash; broken down by locale.', 'rosetta' ), 'https://wordpress.org/download/counter/', esc_html( WP_CORE_STABLE_BRANCH ) ) . '</p>';
     119        foreach ( $locales as $locale ) {
     120            $release_count = isset( $release_counts[ $locale ] ) ? $release_counts[ $locale ] : 0;
     121            $translation_count = isset( $translation_counts[ $locale ] ) ? $translation_counts[ $locale ] : 0;
     122            $total_release_counts += $release_count;
     123            $total_translation_counts += $translation_count;
    30124
    31         $rows = array();
    32         $total = 0;
    33         $this_locale = get_locale();
    34         foreach ( $this->get_counts() as $value ) {
    35             list( $locale, $sum ) = $value;
    36             $total += $sum;
    37             $sum = number_format_i18n( $sum );
    38             $highlight = $locale == $this_locale ? 'background:#ffffe0;border:1px solid #e6db55;font-weight:bold;' : '';
    39             $row = "<tr><td>$locale</td><td style='{$highlight}text-align:right'>$sum</td></tr>";
     125            $highlight = ( $locale == $this_locale ) ? ' style="background:#ffffe0;font-weight:bold"' : '';
     126            $row = sprintf(
     127                '<tr%s><td>%s</td><td style="text-align:right">%s</td><td style="text-align:right">%s</td></tr>',
     128                $highlight,
     129                $locale,
     130                $release_count ? number_format_i18n( $release_count ) : '&mdash;',
     131                $translation_count ? number_format_i18n( $translation_count ) : '&mdash;'
     132            );
     133
    40134            if ( $locale == 'en' ) {
    41135                $rows = array_merge( array( $row ), $rows );
     
    44138            }
    45139        }
    46         echo "<tr><td style='width:100px'><strong>" . _x( 'All', 'locales', 'rosetta' ) . "</strong></td><td style='text-align:right'><strong>" . number_format_i18n( $total ) . "</strong></td></tr>";
    47         echo implode( "\n", $rows );
    48         echo '</table>';
     140        ?>
     141        <div class="wrap">
     142            <h2><?php _e( 'Download Stats', 'rosetta' ); ?></h2>
     143            <p>
     144                <?php
     145                printf(
     146                    __( 'This page shows the <a href="%s">Download Counter</a> number &mdash; total downloads of WordPress %s &mdash; broken down by locale.', 'rosetta' ),
     147                    'https://wordpress.org/download/counter/',
     148                    esc_html( WP_CORE_STABLE_BRANCH )
     149                );
     150                ?>
     151            </p>
     152
     153            <table class="widefat fixed striped" style="max-width:400px">
     154                <thead>
     155                    <tr>
     156                        <th scope="col" style="width:80px"><?php _e( 'Locale', 'rosetta' ); ?></th>
     157                        <th scope="col" style="text-align:right"><?php _e( 'Release Package', 'rosetta' ); ?></th>
     158                        <th scope="col" style="text-align:right"><?php _e( 'Language Pack', 'rosetta' ); ?> <abbr title="Since March 11, 2015.">*</abbr></th>
     159                    </tr>
     160                </thead>
     161
     162                <tbody>
     163                    <tr>
     164                        <td>
     165                            <strong><?php _ex( 'All', 'locales', 'rosetta' ); ?></strong>
     166                        </td>
     167                        <td style="text-align:right">
     168                            <strong><?php echo number_format_i18n( $total_release_counts ); ?></strong>
     169                        </td>
     170                        <td style="text-align:right">
     171                            <strong><?php echo number_format_i18n( $total_translation_counts ); ?></strong>
     172                        </td>
     173                    </tr>
     174                    <?php echo implode( "\n", $rows ); ?>
     175                </tbody>
     176
     177                <tfoot>
     178                    <tr>
     179                        <th scope="col" style="width:80px"><?php _e( 'Locale', 'rosetta' ); ?></th>
     180                        <th scope="col" style="text-align:right"><?php _e( 'Release Package', 'rosetta' ); ?></th>
     181                        <th scope="col" style="text-align:right"><?php _e( 'Language Pack', 'rosetta' ); ?> <abbr title="Since March 11, 2015.">*</abbr></th>
     182                    </tr>
     183                </tfoot>
     184            </table>
     185        </div>
     186        <?php
    49187    }
     188}
    50189
    51 
    52 }
    53190new Rosetta_Downloads;
Note: See TracChangeset for help on using the changeset viewer.