Making WordPress.org

Changeset 12827


Ignore:
Timestamp:
08/10/2023 03:35:32 AM (23 months ago)
Author:
dd32
Message:

API: Translations: Cache the specific locales a plugin/theme is translated into separate from the actual translations of the item.

This allows us to cache the specific data in PHP APCu cache and significantly reduce the number of objects that need to be queried.

Location:
sites/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/translations/lib.php

    r12197 r12827  
    146146    wp_cache_add_global_groups( $translations_cache_group );
    147147
     148    // Filter the requested languages down to only valid translated locales.
     149    $translated_languages = find_translated_locales_for( $type, $domain );
     150    $languages            = array_filter( $languages, function( $language ) use( $translated_languages ) {
     151        return (
     152            // Skip any invalid language data requested
     153            is_string( $language ) &&
     154            // and only query for languages this item is translated into
     155            in_array( strtolower( $language ), $translated_languages, true )
     156        );
     157    } );
     158
     159    if ( ! $languages ) {
     160        return array();
     161    }
     162
    148163    $return = array();
    149164    foreach ( $languages as $language ) {
    150         // Skip if an invalid language was provided to the API.
    151         if ( ! is_string( $language ) ) {
    152             continue;
    153         }
    154 
    155165        // Disable LP's for en_US for now, can re-enable later if we have non-en_US native items
    156166        if ( 'en_US' == $language ) {
     
    287297    return $translations;
    288298}
     299
     300/**
     301 * Find the languages an item is actually translated into.
     302 *
     303 * Used by find_latest_translations().
     304 *
     305 * @param string $type   Type of item. Expects 'core', 'plugin', or 'theme'.
     306 * @param string $domain Item slug.
     307 * @return array Array of lower-case language codes.
     308 */
     309function find_translated_locales_for( $type, $domain ) {
     310    global $wpdb;
     311
     312    $cache_group     = 'translations-query';
     313    $cache_key       = "{$type}:{$domain}";
     314    $cache_time      = 86400; // 24hrs
     315    $apcu_cache_time = 60;
     316
     317    wp_cache_add_global_groups( $cache_group );
     318
     319    $languages = apcu_fetch( "{$cache_group}:{$cache_key}", $found );
     320    if ( ! $found ) {
     321        $languages = wp_cache_get( $cache_key, $cache_group, false, $found );
     322        if ( $found ) {
     323            apcu_store( "{$cache_group}:{$cache_key}", $translated_languages, $apcu_cache_time );
     324        }
     325    }
     326
     327    if ( ! $found ) {
     328        $languages = $wpdb->get_col( $wpdb->prepare(
     329            "SELECT DISTINCT `language`
     330            FROM `language_packs`
     331            WHERE `type` = %s AND `domain` = %s AND `active` = 1",
     332            $type,
     333            $domain
     334        ) );
     335
     336        $languages = array_map( 'strtolower', $languages );
     337
     338        apcu_store( "{$cache_group}:{$cache_key}", $languages, $apcu_cache_time );
     339        wp_cache_add( $cache_key, $translated_languages, $cache_group, $cache_time );
     340    }
     341
     342    return $languages ?: array();
     343}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-language-pack.php

    r12197 r12827  
    584584        wp_cache_delete( "{$type}:{$language}:{$domain}", 'update-check-translations' );
    585585        wp_cache_delete( "{$type}:{$domain}:{$version}", 'translations-query' );
     586        wp_cache_delete( "{$type}:{$domain}", 'translations-query' );
    586587
    587588        return true;
Note: See TracChangeset for help on using the changeset viewer.