Making WordPress.org


Ignore:
Timestamp:
03/22/2020 12:05:56 PM (7 years ago)
Author:
ocean90
Message:

Translate: Remove cron schedules for cache updates, update data on the fly.

Prevents missing data before the next schedule is run.

File:
1 edited

Legend:

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

    r9355 r9616  
    99class Plugin {
    1010
     11        public const CACHE_GROUP = 'wporg-translate';
     12
    1113        /**
    1214         * @var Plugin The singleton instance.
    … …  
    4446                add_filter( 'gp_locale_glossary_path_prefix', [ $this, 'set_locale_glossary_path_prefix' ] );
    4547
    46                 add_filter( 'cron_schedules', [ $this, 'register_cron_schedules' ] );
    47                 add_action( 'init', [ $this, 'register_cron_events' ] );
    4848                add_action( 'init', [ $this, 'respect_robots_txt' ], 9 );
    49                 add_action( 'wporg_translate_update_existing_locales_cache', [ $this, 'update_existing_locales_cache' ] );
    50                 add_action( 'wporg_translate_update_translation_status_cache', [ $this, 'update_translation_status_cache' ] );
    51                 add_action( 'wporg_translate_update_contributors_count_cache', [ $this, 'update_contributors_count_cache' ] );
    5249        }
    5350
    … …  
    180177
    181178        /**
    182          * Filters the non-default cron schedules.
    183          *
    184          * @param array $schedules An array of non-default cron schedules.
    185          * @return array  An array of non-default cron schedules.
    186          */
    187         public function register_cron_schedules( $schedules ) {
    188                 $schedules['15_minutes'] = array(
    189                         'interval' => 15 * MINUTE_IN_SECONDS,
    190                         'display'  => 'Every 15 minutes',
    191                 );
    192 
    193                 return $schedules;
    194         }
    195 
    196         /**
    197          * Registers CLI commands if WP-CLI is loaded.
    198          */
    199         public function register_cron_events() {
    200                 if ( ! wp_next_scheduled( 'wporg_translate_update_existing_locales_cache' ) ) {
    201                         wp_schedule_event( time(), '15_minutes', 'wporg_translate_update_existing_locales_cache' );
    202                 }
    203 
    204                 if ( ! wp_next_scheduled( 'wporg_translate_update_translation_status_cache' ) ) {
    205                         wp_schedule_event( time() + 3 * MINUTE_IN_SECONDS, '15_minutes', 'wporg_translate_update_translation_status_cache' );
    206                 }
    207 
    208                 if ( ! wp_next_scheduled( 'wporg_translate_update_contributors_count_cache' ) ) {
    209                         wp_schedule_event( time() + 6 * MINUTE_IN_SECONDS, '15_minutes', 'wporg_translate_update_contributors_count_cache' );
    210                 }
    211         }
    212 
    213         /**
    214179         * Calculates the translation status of the WordPress project per locale.
    215180         */
    216         public function update_translation_status_cache() {
     181        public static function get_translation_status() {
    217182                global $wpdb;
    218183
    219184                if ( ! isset( $wpdb->project_translation_status ) ) {
    220185                        return;
     186                }
     187
     188                $cached = wp_cache_get( 'translation-status', self::CACHE_GROUP );
     189                if ( false !== $cached ) {
     190                        return $cached;
    221191                }
    222192
    … …  
    229199                ), OBJECT_K );
    230200
    231                 if ( ! $translation_status ) {
    232                         return;
    233                 }
    234 
    235                 wp_cache_set( 'translation-status', $translation_status, 'wporg-translate' );
     201                wp_cache_set( 'translation-status', $translation_status, 'wporg-translate', 15 * MINUTE_IN_SECONDS );
     202
     203                return $translation_status;
    236204        }
    237205
    … …  
    239207         * Updates contributors count per locale.
    240208         */
    241         public function update_contributors_count_cache() {
     209        public static function get_contributors_count() {
    242210                global $wpdb;
    243211
     212                $cached = wp_cache_get( 'contributors-count', self::CACHE_GROUP );
     213                if ( false !== $cached ) {
     214                        return $cached;
     215                }
     216
    244217                if ( ! isset( $wpdb->user_translations_count ) ) {
    245                         return;
    246                 }
    247 
    248                 $locales   = $this->get_existing_locales();
     218                        return [];
     219                }
     220
     221                $locales   = self::get_existing_locales();
    249222                $db_counts = $wpdb->get_results(
    250223                        "SELECT `locale`, COUNT( DISTINCT user_id ) as `count` FROM {$wpdb->user_translations_count} WHERE `accepted` > 0 GROUP BY `locale`",
    251224                        OBJECT_K
    252225                );
    253 
    254                 if ( ! $db_counts || ! $locales ) {
    255                         return;
    256                 }
    257226
    258227                $counts = array();
    … …  
    265234                }
    266235
    267                 wp_cache_set( 'contributors-count', $counts, 'wporg-translate' );
     236                wp_cache_set( 'contributors-count', $counts, self::CACHE_GROUP, 15 * MINUTE_IN_SECONDS );
     237
     238                return $counts;
    268239        }
    269240
    … …  
    276247         * @return array List of GlotPress locales.
    277248         */
    278         private function get_existing_locales() {
     249        public static function get_existing_locales() {
    279250                global $wpdb;
    280251
    281                 return $wpdb->get_col(
     252                $cached = wp_cache_get( 'existing-locales', self::CACHE_GROUP );
     253                if ( false !== $cached ) {
     254                        return $cached;
     255                }
     256
     257                $existing_locales = $wpdb->get_col(
    282258                        $wpdb->prepare(
    283259                                "SELECT locale FROM {$wpdb->gp_translation_sets} WHERE `project_id` = %d and slug = %s",
    … …  
    286262                        )
    287263                );
    288         }
    289 
    290         /**
    291          * Updates cache for existing locales.
    292          */
    293         public function update_existing_locales_cache() {
    294                 $existing_locales = $this->get_existing_locales();
    295                 if ( ! $existing_locales ) {
    296                         return;
    297                 }
    298 
    299                 wp_cache_set( 'existing-locales', $existing_locales, 'wporg-translate' );
     264
     265                wp_cache_set( 'existing-locales', $existing_locales, self::CACHE_GROUP, HOUR_IN_SECONDS );
     266
     267                return $existing_locales;
    300268        }
    301269}
Note: See TracChangeset for help on using the changeset viewer.