Making WordPress.org

Changeset 13774


Ignore:
Timestamp:
06/04/2024 02:53:48 PM (2 years ago)
Author:
amieiro
Message:

Translation. Add the option to show statistics from the start date we want.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-stats-print.php

    r12114 r13774  
    33namespace WordPressdotorg\GlotPress\Customizations\CLI;
    44
     5use DateTime;
    56use WP_CLI;
    67use WP_CLI_Command;
     
    89class Stats_Print extends WP_CLI_Command  {
    910
     11        /**
     12         * Prints statistics starting from a specified date.
     13         *
     14         * ## OPTIONS
     15         *
     16         * [--start_date=<start_date>]
     17         * : The start date for the statistics in 'Y-m-d' format.
     18         *   If not provided, defaults to one week before the current date.
     19         *
     20         * ## EXAMPLES
     21         *
     22         *     # Print statistics starting from one week before the current date.
     23         *     wp wporg-translate show-stats --url=translate.wordpress.org
     24         *
     25         *     # Print statistics starting from 2023-10-25.
     26         *     wp wporg-translate show-stats --start_date=2023-10-25 --url=translate.wordpress.org
     27         *
     28         * @param array $args       Positional arguments.
     29         * @param array $assoc_args Associative arguments.
     30         */
    1031        public function __invoke( $args, $assoc_args ) {
     32                if ( !isset( $assoc_args['start_date'] ) ) {
     33                        $start_date = date( 'Y-m-d', strtotime( '-1 week' ) );
     34                } else {
     35                        $start_date     = $assoc_args['start_date'];
     36                        $start_datetime = DateTime::createFromFormat( 'Y-m-d', $start_date ) ;
     37                        $is_valid_date  = $start_datetime && $start_datetime->format( 'Y-m-d' ) === $start_date;
     38                        if ( ! $is_valid_date ) {
     39                                WP_CLI::error( 'The --start_date parameter must be in the format Y-m-d. E.g.: 2023-10-24' );
     40                                return;
     41                        }
     42                }
    1143                $stats = new Stats();
    12                 $stats( true );
     44                $stats( true, $start_date );
    1345        }
    1446}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-stats.php

    r12798 r13774  
    1414
    1515use DateTime;
    16 use Exception;
    17 use GP;
    1816use GP_Locale;
    19 use GP_Locales;
    2017use WP_CLI;
    21 use WP_CLI_Command;
    2218use WP_Query;
    2319use WordPressdotorg\GlotPress\Routes\Plugin;
     
    163159         * Prints the Polyglots stats or stores them on a page.
    164160         *
    165          * @param bool $echo_the_values Whether it should print the info in the CLI or stores it on a page.
     161         * @param bool        $echo_the_values Whether it should print the info in the CLI or stores it on a page.
     162         * @param string|null $old_date        The date to compare the stats with. Format: 'Y-m-d'.
    166163         *
    167164         * @return void
    168165         */
    169         public function __invoke( bool $echo_the_values = false ) {
     166        public function __invoke( bool $echo_the_values = false, string $old_date = null ): void {
    170167                global $wpdb;
    171168
     
    190187                $this->print_managers_stats();
    191188                $this->print_most_active_translators();
    192                 $this->store_stats();
    193                 $this->print_stats_comparison( gmdate( 'Y-m-d' ) );
     189                // Don't store the stats if we execute the command in the CLI, to avoid storing the same stats twice.
     190                if ( ! $echo_the_values ) {
     191                        $this->store_stats();
     192                }
     193                $this->print_stats_comparison( gmdate( 'Y-m-d' ), $old_date );
    194194
    195195                $this->update_page();
     
    360360         *
    361361         * @param string $current_date The date for which we display the stats.
    362          * @param string $old_date The date to compare the stats with.
     362         * @param string $old_date     The date to compare the stats with.
    363363         *
    364364         * @return void
     
    375375                        return;
    376376                }
    377                 if ( ! $this->echo_the_values ) {
    378                         $this->stats_comparison = $this->create_gutenberg_heading( 'Summary for weekly stats' );
    379                 } else {
    380                         $this->print_wpcli_heading( 'Summary for weekly stats' );
     377
     378                $current_datetime = DateTime::createFromFormat( 'Y-m-d', $current_date );
     379                $old_datetime     = DateTime::createFromFormat( 'Y-m-d', $old_date );
     380                if ( ! $current_datetime || ! $old_datetime ) {
     381                        return;
     382                }
     383                $interval        = $current_datetime->diff( $old_datetime );
     384                $days_difference = $interval->days;
     385
     386                if ( ! $this->echo_the_values ) {
     387                        $this->stats_comparison = $this->create_gutenberg_heading( "Summary for the last $days_difference days" );
     388                } else {
     389                        $this->print_wpcli_heading( "Summary for the last $days_difference days" );
    381390                }
    382391                $stats_diff = new \stdClass();
     
    395404                $code .= 'Requests: There are ' . $current_date_data->requests_unresolved . ' unresolved editor requests out of ' . $current_date_data->requests_total . ' (' . $this->prefix_num( $stats_diff->requests_unresolved ) . ') total and ' . $current_date_data->locale_requests_unresolved . ' unresolved locale requests out of ' . $current_date_data->locale_requests_total . ' (' . $this->prefix_num( $stats_diff->locale_requests_unresolved ) . ') total.' . PHP_EOL . PHP_EOL;
    396405
    397                 $code .= 'Translators: There are ' . $current_date_data->translators_gtes . ' (' . $this->prefix_num( $stats_diff->translators_gtes ) . ') GTEs, ' . $current_date_data->translators_ptes . ' (' . $this->prefix_num( $stats_diff->translators_ptes ) . ') PTEs and ' . $current_date_data->translators_contributors . ' (' . $this->prefix_num( $stats_diff->translators_contributors ) . ') translation contributors.' . PHP_EOL;
    398                 $code .= '(A wordpress.org account could have multiple roles over different locale)' . PHP_EOL . PHP_EOL;
    399 
    400                 $code .= 'Site language: ' . $current_date_data->wp_translated_sites_pct . '% (' . $this->prefix_num( round( $stats_diff->wp_translated_sites_pct, 3 ) ) . '%) of WordPress sites are running a translated WordPress site. ' . PHP_EOL;
     406                $code .= 'Translators: There are ' . number_format_i18n( $current_date_data->translators_gtes ) . ' (' . $this->prefix_num( $stats_diff->translators_gtes ) . ') GTEs, ' . number_format_i18n( $current_date_data->translators_ptes ) . ' (' . $this->prefix_num( $stats_diff->translators_ptes ) . ') PTEs and ' . number_format_i18n( $current_date_data->translators_contributors ) . ' (' . $this->prefix_num( $stats_diff->translators_contributors ) . ') translation contributors.' . PHP_EOL;
     407                $code .= '(A wordpress.org account could have multiple roles over different locale).' . PHP_EOL . PHP_EOL;
     408
     409                $code .= 'Site language: ' . $current_date_data->wp_translated_sites_pct . '% (' . $this->prefix_num( round( $stats_diff->wp_translated_sites_pct, 3 ), 3 ) . '%) of WordPress sites are running a translated WordPress site. ' . PHP_EOL;
    401410                if ( ! $this->echo_the_values ) {
    402411                        $this->stats_comparison .= $this->create_gutenberg_code( $code );
     
    421430         * Prefix numbers greater than zero with plus sign and plus_minus sign if number is zero.
    422431         *
    423          * @param int $num Number to be prefixed.
     432         * @param float $number          Number to be prefixed.
     433         * @param int   $number_decimals Number of decimals to be displayed.
    424434         *
    425435         * @return string Prefixed number
    426436         */
    427         private function prefix_num( $num ) {
    428                 if ( 0 === $num ) {
     437        private function prefix_num( $number, $number_decimals = 0 ) {
     438                if ( 0 === $number ) {
    429439                        return '±0';
    430440                }
    431                 return $num > 0 ? sprintf( '+%d', $num ) : $num;
     441                return $number > 0 ? sprintf( '+%s', number_format_i18n( $number, $number_decimals ) ) : number_format_i18n( $number, $number_decimals );
    432442        }
    433443
Note: See TracChangeset for help on using the changeset viewer.