Making WordPress.org

Ticket #1691: 1691.patch

File 1691.patch, 4.4 KB (added by ocean90, 9 years ago)
  • trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-i18n.php

     
    99class i18n {
    1010
    1111        /**
    12          * Translate a Term Name.
     12         * Translates a term name.
    1313         *
    1414         * @param \WP_Term $term The Term object to translate.
    1515         * @return \WP_Term The term object with a translated `name` field.
     
    2727        }
    2828
    2929        /**
     30         * Retrieves the translated version of a plugin.
     31         *
     32         * @param int $post_id The post ID of a plugin.
     33         * @return \WP_Post|null WP_Post object on success, null on failure.
     34         */
     35        static function get_translated_plugin_post( $post_id, $locale = null ) {
     36                if ( null === $locale ) {
     37                        $locale = get_locale();
     38                }
     39
     40                if ( ! $locale || 'en_US' === $locale ) {
     41                        return null;
     42                }
     43
     44                $posts = get_posts( array(
     45                        'post_type'   => 'plugin_translated',
     46                        'name'        => $locale,
     47                        'post_parent' => $post_id,
     48                        'post_status' => array( 'publish' ),
     49                        'numberposts' => 1,
     50                ) );
     51
     52                if ( ! $posts ) {
     53                        return null;
     54                }
     55
     56                $plugin = reset( $posts );
     57                return $plugin;
     58        }
     59
     60        /**
     61         * Translates the title of a plugin.
     62         *
     63         * @param string $title   The post title.
     64         * @param int    $post_id The post ID.
     65         * @return string Filtered title.
     66         */
     67        static function translate_title( $title, $post_id ) {
     68                $post = get_post( $post_id );
     69                if ( ! $post || 'plugin' !== $post->post_type ) {
     70                        return $title;
     71                }
     72
     73                $translated_plugin = self::get_translated_plugin_post( $post->ID );
     74                if ( ! $translated_plugin ) {
     75                        return $title;
     76                }
     77
     78                return $translated_plugin->post_title;
     79        }
     80
     81        /**
     82         * Translates the title of a plugin.
     83         *
     84         * @param string $content The post content.
     85         * @return string Filtered content.
     86         */
     87        static function translate_content( $content ) {
     88                $post = get_post();
     89                if ( ! $post || 'plugin' !== $post->post_type ) {
     90                        return $content;
     91                }
     92
     93                $translated_plugin = self::get_translated_plugin_post( $post->ID );
     94                if ( ! $translated_plugin ) {
     95                        return $content;
     96                }
     97
     98                return $translated_plugin->post_content;
     99        }
     100
     101        /**
     102         * Translates the excerpt of a plugin.
     103         *
     104         * @param string $excerpt The post excerpt.
     105         * @param int    $post_id The post ID.
     106         * @return string Filtered excerpt.
     107         */
     108        static function translate_excerpt( $excerpt, $post_id ) {
     109                $post = get_post( $post_id );
     110                if ( ! $post || 'plugin' !== $post->post_type ) {
     111                        return $excerpt;
     112                }
     113
     114                $translated_plugin = self::get_translated_plugin_post( $post->ID );
     115                if ( ! $translated_plugin ) {
     116                        return $excerpt;
     117                }
     118
     119                return $translated_plugin->post_excerpt;
     120        }
     121
     122        /**
    30123         * A private method to hold a list of the strings contained within the Database.
    31124         *
    32125         * This function is never called, and only exists so that out pot tools can detect the strings.
  • trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php

     
    9191                                'read_private_posts' => 'do_not_allow',
    9292                                'delete_posts'       => 'do_not_allow',
    9393                                'create_posts'       => 'do_not_allow',
    94                         )
     94                        ),
    9595                ) );
    9696
     97                register_post_type( 'plugin_translated', array(
     98                        'public'       => false,
     99                        'rewrite'      => false,
     100                        'query_var'    => false,
     101                        'can_export'   => false,
     102                        'hierarchical' => true,
     103                ) );
     104
    97105                register_taxonomy( 'plugin_section', 'plugin', array(
    98106                        'hierarchical'      => true,
    99107                        'query_var'         => 'plugin_section',
     
    216224
    217225                if ( 'en_US' != get_locale() ) {
    218226                        add_filter( 'get_term', array( __NAMESPACE__ . '\i18n', 'translate_term' ) );
     227                        add_filter( 'the_title', array( __NAMESPACE__ . '\i18n', 'translate_title' ), 10, 2 );
     228                        add_filter( 'the_content', array( __NAMESPACE__ . '\i18n', 'translate_content' ) ); // @todo get_the_content
     229                        add_filter( 'get_the_excerpt', array( __NAMESPACE__ . '\i18n', 'translate_excerpt', 10, 2 ) );
    219230                }
    220231
    221232                // Instantiate our copy of the Jetpack_Search class.