Making WordPress.org

Changeset 3519


Ignore:
Timestamp:
06/21/2016 10:06:58 AM (10 years ago)
Author:
tellyworth
Message:

Plugin directory: include all available translations in pseudo-meta for search.

Also add some defensive code, better testing and bugfixes.

See #1691, #1692

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
2 edited

Legend:

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

    r3511 r3519  
    568568                remove_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ) );
    569569
    570                 if ( $post_id <= 200 ) {
    571 
    572                         // This should probably be a list of available translations for the plugin readme.
    573                         $locales_to_sync = array(
     570                if ( empty( $this->i18n_meta[ $post_id ] ) ) {
     571
     572                        $locales_to_sync = array(
     573                                // Default locales to translate, just in case we can't determine the available ones
    574574                                'fr_FR',
    575575                                'es_ES',
    576576                        );
     577                        $post = get_post( $post_id );
     578                        if ( $post ) {
     579                                $translations = Plugin_I18n::find_all_translations_for_plugin( $post->post_name, 'stable-readme', 10 ); // at least 10% translated
     580                                if ( $translations )
     581                                        $locales_to_sync = $translations;
     582                        }
    577583
    578584                        global $locale;
    … …  
    580586
    581587                        foreach ( $locales_to_sync as $locale ) {
    582                                 $this->i18n_meta[ $post_id ][ 'title_' . $locale ]   = $this->translate_post_title( get_the_title( $post_id ), $post_id );
    583                                 $this->i18n_meta[ $post_id ][ 'excerpt_' . $locale ] = $this->translate_post_excerpt( get_the_excerpt( $post_id ), $post_id );
     588                                $the_title = $this->translate_post_title( get_the_title( $post_id ), $post_id );
     589                                if ( $the_title && $the_title != get_the_title( $post_id ) ) {
     590                                        $this->i18n_meta[ $post_id ][ 'title_' . $locale ] = $the_title;
     591                                }
     592
     593                                $the_excerpt = $this->translate_post_excerpt( get_the_excerpt( $post_id ), $post_id );
     594                                if ( $the_excerpt && $the_excerpt != get_the_excerpt( $post_id ) ) {
     595                                        $this->i18n_meta[ $post_id ][ 'excerpt_' . $locale ] = $the_excerpt;
     596                                }
    584597
    585598                                // Split up the content to translate it in sections.
    586                                 $content  = '';
     599                                $the_content = array();
    587600                                $sections = $this->split_post_content_into_pages( get_the_content( $post_id ) );
    588601                                foreach ( $sections as $section => $section_content ) {
    589                                         $content .= $this->translate_post_content( $section_content, $section, $post_id );
    590                                 }
    591                                 $this->i18n_meta[ $post_id ][ 'content_' . $locale ] = $content;
     602                                        $translated_section = $this->translate_post_content( $section_content, $section, $post_id );
     603                                        if ( $translated_section && $translated_section != $section_content ) {
     604                                                $the_content[] = $translated_section;
     605                                        }
     606                                }
     607                                if ( !empty( $the_content ) )
     608                                        $this->i18n_meta[ $post_id ][ 'content_' . $locale ] = implode( $the_content );
    592609                        }
    593610
    594611                        $locale = $_locale;
    595612
     613                }
     614
     615                if ( is_array( $this->i18n_meta[ $post_id ] ) )
    596616                        $meta = array_merge( $meta, array_keys( $this->i18n_meta[ $post_id ] ) );
    597                 }
    598617
    599618                add_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ), 10, 2 );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-i18n.php

    r3511 r3519  
    420420                return $content;
    421421        }
     422
     423        /**
     424         * Returns a list of translation locales for a given plugin slug and branch.
     425         *
     426         * @param string $slug    Plugin slug.
     427         * @param string $branch  Branch - 'stable-readme' for example.
     428         * @param string $min_percent     Only return locales where percent_translated is >= this value.
     429         * @return array
     430         */
     431        public function find_all_translations_for_plugin( $slug, $branch, $min_percent = 0 ) {
     432
     433                // This naively hits the API. It could probably be re-written to query the DB instead.
     434                $api_url = esc_url_raw( 'https://translate.wordpress.org/api/projects/wp-plugins/' . $slug . '/' . $branch, array( 'https' ) );
     435
     436                $out = array();
     437                if ( $json = file_get_contents( $api_url ) ) {
     438                        if ( $data = json_decode( $json ) ) {
     439                                if ( isset( $data->translation_sets ) ) {
     440                                        foreach ( $data->translation_sets as $translation ) {
     441                                                if ( $translation->percent_translated >= $min_percent )
     442                                                        $out[] = $translation->wp_locale;
     443                                        }
     444                                }
     445                        }
     446                }
     447
     448                return $out;
     449        }
     450
    422451}
Note: See TracChangeset for help on using the changeset viewer.