Changeset 9616 for sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/inc/class-plugin.php
- Timestamp:
- 03/22/2020 12:05:56 PM (5 years ago)
- 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 9 9 class Plugin { 10 10 11 public const CACHE_GROUP = 'wporg-translate'; 12 11 13 /** 12 14 * @var Plugin The singleton instance. … … 44 46 add_filter( 'gp_locale_glossary_path_prefix', [ $this, 'set_locale_glossary_path_prefix' ] ); 45 47 46 add_filter( 'cron_schedules', [ $this, 'register_cron_schedules' ] );47 add_action( 'init', [ $this, 'register_cron_events' ] );48 48 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' ] );52 49 } 53 50 … … 180 177 181 178 /** 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 /**214 179 * Calculates the translation status of the WordPress project per locale. 215 180 */ 216 public function update_translation_status_cache() {181 public static function get_translation_status() { 217 182 global $wpdb; 218 183 219 184 if ( ! isset( $wpdb->project_translation_status ) ) { 220 185 return; 186 } 187 188 $cached = wp_cache_get( 'translation-status', self::CACHE_GROUP ); 189 if ( false !== $cached ) { 190 return $cached; 221 191 } 222 192 … … 229 199 ), OBJECT_K ); 230 200 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; 236 204 } 237 205 … … 239 207 * Updates contributors count per locale. 240 208 */ 241 public function update_contributors_count_cache() {209 public static function get_contributors_count() { 242 210 global $wpdb; 243 211 212 $cached = wp_cache_get( 'contributors-count', self::CACHE_GROUP ); 213 if ( false !== $cached ) { 214 return $cached; 215 } 216 244 217 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(); 249 222 $db_counts = $wpdb->get_results( 250 223 "SELECT `locale`, COUNT( DISTINCT user_id ) as `count` FROM {$wpdb->user_translations_count} WHERE `accepted` > 0 GROUP BY `locale`", 251 224 OBJECT_K 252 225 ); 253 254 if ( ! $db_counts || ! $locales ) {255 return;256 }257 226 258 227 $counts = array(); … … 265 234 } 266 235 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; 268 239 } 269 240 … … 276 247 * @return array List of GlotPress locales. 277 248 */ 278 p rivatefunction get_existing_locales() {249 public static function get_existing_locales() { 279 250 global $wpdb; 280 251 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( 282 258 $wpdb->prepare( 283 259 "SELECT locale FROM {$wpdb->gp_translation_sets} WHERE `project_id` = %d and slug = %s", … … 286 262 ) 287 263 ); 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; 300 268 } 301 269 }
Note: See TracChangeset
for help on using the changeset viewer.