Making WordPress.org


Ignore:
Timestamp:
02/02/2018 09:39:39 PM (7 years ago)
Author:
obenland
Message:

Plugins: Translate plugin strings in bulk.

Rather than checking for each translatable string in the database, now we'll grab all originals and their translations, cache them, and then determine which one we want.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-i18n.php

    r6429 r6529  
    173173        global $wpdb;
    174174
    175         // Try to get a single original with the whole content first (title, etc), if passed, or get them all otherwise.
    176         if ( ! empty( $key ) && ! empty( $str ) ) {
    177             $originals = $this->search_gp_original( $slug, $branch, $key, $str );
    178 
    179             // Do not cache this as originals, search_gp_original() does its own caching.
    180             if ( ! empty( $originals ) ) {
    181                 return array( $originals );
    182             }
    183         }
    184 
    185175        $cache_suffix = 'originals';
    186176
     
    296286
    297287    /**
     288     * Searches GlotPress "originals" for the passed string.
     289     *
     290     * @param string $slug               Plugin slug
     291     * @param string $branch             dev|stable
     292     * @param array  $originals          List of IDs of the original strings.
     293     * @param int    $translation_set_id Unique ID for translation set.
     294     * @return array Plugin translations
     295     */
     296    public function get_gp_translations( $slug, $branch, $originals, $translation_set_id ) {
     297        global $wpdb;
     298
     299        $cache_suffix = "translations:{$translation_set_id}";
     300        $translations = $this->cache_get( $slug, $branch, $cache_suffix );
     301
     302        if ( false === $translations ) {
     303            $translations = [];
     304
     305            $raw_translations = $wpdb->get_results( $wpdb->prepare(
     306                'SELECT original_id, translation_0 FROM ' . GLOTPRESS_TABLE_PREFIX . 'translations WHERE original_id IN (' . implode( ', ', wp_list_pluck( $originals, 'id' ) ) . ') AND translation_set_id = %d AND status = %s',
     307                $translation_set_id, 'current'
     308            ) );
     309
     310            foreach ( $raw_translations as $translation ) {
     311                $translations[ $translation->original_id ] = $translation->translation_0;
     312            }
     313
     314            $this->cache_set( $slug, $branch, $translations, $cache_suffix );
     315        }
     316
     317        return $translations;
     318    }
     319
     320    /**
    298321     * Somewhat emulated equivalent of __() for content translation drawn directly from the GlotPress DB.
    299322     *
     
    376399        }
    377400
    378         $raw_translations = $wpdb->get_results( $wpdb->prepare(
    379             'SELECT original_id, translation_0 FROM ' . GLOTPRESS_TABLE_PREFIX . 'translations WHERE original_id IN (' . implode( ', ', wp_list_pluck( $originals, 'id' ) ) . ') AND translation_set_id = %d AND status = %s',
    380             $translation_set_id, 'current'
    381         ) );
    382 
    383         $translations = [];
    384         foreach ( $raw_translations as $translation ) {
    385             $translations[ $translation->original_id ] = $translation->translation_0;
    386         }
     401        $translations = $this->get_gp_translations( $slug, $branch, $originals, $translation_set_id );
    387402
    388403        foreach ( $originals as $original ) {
Note: See TracChangeset for help on using the changeset viewer.