Changeset 9616
- Timestamp:
- 03/22/2020 12:05:56 PM (5 years ago)
- Location:
- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/inc
- Files:
-
- 3 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 } -
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/inc/routes/class-index.php
r3002 r9616 5 5 use GP_Locales; 6 6 use GP_Route; 7 use WordPressdotorg\GlotPress\Routes\Plugin; 7 8 8 9 /** … … 13 14 class Index extends GP_Route { 14 15 15 private $cache_group = 'wporg-translate';16 17 16 /** 18 17 * Prints all exisiting locales as cards. 19 *20 * Note: Cache gets refreshed via `WPorg_GP_CLI_Update_Caches`.21 18 */ 22 19 public function get_locales() { 23 $existing_locales = wp_cache_get( 'existing-locales', $this->cache_group ); 24 if ( false === $existing_locales ) { 25 $existing_locales = array(); 26 } 20 $existing_locales = Plugin::get_existing_locales(); 27 21 28 22 $locales = array(); … … 33 27 unset( $existing_locales ); 34 28 35 $contributors_count = wp_cache_get( 'contributors-count', $this->cache_group ); 36 if ( false === $contributors_count ) { 37 $contributors_count = array(); 38 } 39 40 $translation_status = wp_cache_get( 'translation-status', $this->cache_group ); 41 if ( false === $translation_status ) { 42 $translation_status = array(); 43 } 29 $contributors_count = Plugin::get_contributors_count(); 30 $translation_status = Plugin::get_translation_status(); 44 31 45 32 $this->tmpl( 'index-locales', get_defined_vars() ); -
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/inc/routes/class-locale.php
r9299 r9616 8 8 use stdClass; 9 9 use WordPressdotorg\GlotPress\Rosetta_Roles\Plugin as Rosetta_Roles; 10 use WordPressdotorg\GlotPress\Routes\Plugin; 10 11 11 12 /** … … 129 130 ); 130 131 131 $contributors_count = wp_cache_get( 'contributors-count', 'wporg-translate' ); 132 if ( false === $contributors_count ) { 133 $contributors_count = array(); 134 } 132 $contributors_count = Plugin::get_contributors_count(); 135 133 136 134 $variants = $this->get_locale_variants( $locale_slug ); … … 183 181 $project_icon = $this->get_project_icon( $project, $sub_project, 64 ); 184 182 185 $contributors_count = wp_cache_get( 'contributors-count', 'wporg-translate' ); 186 if ( false === $contributors_count ) { 187 $contributors_count = array(); 188 } 183 $contributors_count = Plugin::get_contributors_count(); 189 184 190 185 $sub_project_statuses = array();
Note: See TracChangeset
for help on using the changeset viewer.