Making WordPress.org

Changeset 6319


Ignore:
Timestamp:
01/08/2018 10:15:21 PM (7 years ago)
Author:
ocean90
Message:

Translate: Replace WP-CLI commands for updating caches with cron events.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/inc
Files:
1 deleted
1 edited

Legend:

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

    r5895 r6319  
    4444        add_filter( 'gp_locale_glossary_path_prefix', [ $this, 'set_locale_glossary_path_prefix' ] );
    4545
    46         if ( defined( 'WP_CLI' ) && WP_CLI ) {
    47             $this->register_cli_commands();
    48         }
     46        add_filter( 'cron_schedules', [ $this, 'register_cron_schedules' ] );
     47        add_action( 'init', [ $this, 'register_cron_events' ] );
     48        add_action( 'wporg_translate_update_existing_locales_cache', [ $this, 'update_existing_locales_cache' ] );
     49        add_action( 'wporg_translate_update_translation_status_cache', [ $this, 'update_translation_status_cache' ] );
     50        add_action( 'wporg_translate_update_contributors_count_cache', [ $this, 'update_contributors_count_cache' ] );
    4951    }
    5052
     
    159161
    160162    /**
     163     * Filters the non-default cron schedules.
     164     *
     165     * @param array $schedules An array of non-default cron schedules.
     166     * @return array  An array of non-default cron schedules.
     167     */
     168    public function register_cron_schedules( $schedules ) {
     169        $schedules['15_minutes'] = array(
     170            'interval' => 15 * MINUTE_IN_SECONDS,
     171            'display'  => 'Every 15 minutes',
     172        );
     173
     174        return $schedules;
     175    }
     176
     177    /**
    161178     * Registers CLI commands if WP-CLI is loaded.
    162179     */
    163     function register_cli_commands() {
    164         WP_CLI::add_command( 'wporg-translate update-cache', __NAMESPACE__ . '\CLI\Update_Caches' );
     180    public function register_cron_events() {
     181        if ( ! wp_next_scheduled( 'wporg_translate_update_existing_locales_cache' ) ) {
     182            wp_schedule_event( time(), '15_minutes', 'wporg_translate_update_existing_locales_cache' );
     183        }
     184
     185        if ( ! wp_next_scheduled( 'wporg_translate_update_translation_status_cache' ) ) {
     186            wp_schedule_event( time() + 3 * MINUTE_IN_SECONDS, '15_minutes', 'wporg_translate_update_translation_status_cache' );
     187        }
     188
     189        if ( ! wp_next_scheduled( 'wporg_translate_update_contributors_count_cache' ) ) {
     190            wp_schedule_event( time() + 6 * MINUTE_IN_SECONDS, '15_minutes', 'wporg_translate_update_contributors_count_cache' );
     191        }
     192    }
     193
     194    /**
     195     * Calculates the translation status of the WordPress project per locale.
     196     */
     197    public function update_translation_status_cache() {
     198        global $wpdb;
     199
     200        if ( ! isset( $wpdb->project_translation_status ) ) {
     201            return;
     202        }
     203
     204        $translation_status = $wpdb->get_results( $wpdb->prepare(
     205            "SELECT `locale`, `all` AS `all_count`, `waiting` AS `waiting_count`, `current` AS `current_count`, `fuzzy` AS `fuzzy_count`
     206            FROM {$wpdb->project_translation_status}
     207            WHERE `project_id` = %d AND `locale_slug` = %s",
     208            2, 'default' // 2 = wp/dev
     209        ), OBJECT_K );
     210
     211        if ( ! $translation_status ) {
     212            return;
     213        }
     214
     215        wp_cache_set( 'translation-status', $translation_status, 'wporg-translate' );
     216    }
     217
     218    /**
     219     * Updates contributors count per locale.
     220     */
     221    public function update_contributors_count_cache() {
     222        global $wpdb;
     223
     224        if ( ! isset( $wpdb->user_translations_count ) ) {
     225            return;
     226        }
     227
     228        $locales   = GP::$translation_set->existing_locales();
     229        $db_counts = $wpdb->get_results(
     230            "SELECT `locale`, COUNT( DISTINCT user_id ) as `count` FROM {$wpdb->user_translations_count} WHERE `accepted` > 0 GROUP BY `locale`",
     231            OBJECT_K
     232        );
     233
     234        if ( ! $db_counts || ! $locales ) {
     235            return;
     236        }
     237
     238        $counts = array();
     239        foreach ( $locales as $locale ) {
     240            if ( isset( $db_counts[ $locale ] ) ) {
     241                $counts[ $locale ] = (int) $db_counts[ $locale ]->count;
     242            } else {
     243                $counts[ $locale ] = 0;
     244            }
     245        }
     246
     247        wp_cache_set( 'contributors-count', $counts, 'wporg-translate' );
     248    }
     249
     250    /**
     251     * Updates cache for existing locales.
     252     */
     253    public function update_existing_locales_cache() {
     254        $existing_locales = GP::$translation_set->existing_locales();
     255
     256        if ( ! $existing_locales ) {
     257            return;
     258        }
     259
     260        wp_cache_set( 'existing-locales', $existing_locales, 'wporg-translate' );
    165261    }
    166262}
Note: See TracChangeset for help on using the changeset viewer.