Making WordPress.org

Ticket #1691: 1691.3.patch

File 1691.3.patch, 6.8 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                // @todo Do we need caching or is advanced post cache enough?
     45                $posts = get_posts( array(
     46                        'post_type'   => 'plugin_translated',
     47                        'name'        => $locale,
     48                        'post_parent' => $post_id,
     49                        'post_status' => array( 'publish' ),
     50                        'numberposts' => 1,
     51                ) );
     52
     53                if ( ! $posts ) {
     54                        return null;
     55                }
     56
     57                $plugin = reset( $posts );
     58                return $plugin;
     59        }
     60
     61        /**
     62         * Translates the title of a plugin.
     63         *
     64         * @param string $title   The post title.
     65         * @param int    $post_id The post ID.
     66         * @return string Filtered title.
     67         */
     68        static function translate_title( $title, $post_id ) {
     69                $post = get_post( $post_id );
     70                if ( ! $post || 'plugin' !== $post->post_type ) {
     71                        return $title;
     72                }
     73
     74                $translated_plugin = self::get_translated_plugin_post( $post->ID );
     75                if ( ! $translated_plugin || ! $translated_plugin->post_title ) {
     76                        return $title;
     77                }
     78
     79                return $translated_plugin->post_title;
     80        }
     81
     82        /**
     83         * Translates the title of a plugin.
     84         *
     85         * @param string $content The post content.
     86         * @return string Filtered content.
     87         */
     88        static function translate_content( $content ) {
     89                $post = get_post();
     90                if ( ! $post || 'plugin' !== $post->post_type ) {
     91                        return $content;
     92                }
     93
     94                $translated_plugin = self::get_translated_plugin_post( $post->ID );
     95                if ( ! $translated_plugin || ! $translated_plugin->post_content ) {
     96                        return $content;
     97                }
     98
     99                return $translated_plugin->post_content;
     100        }
     101
     102        /**
     103         * Translates the excerpt of a plugin.
     104         *
     105         * @param string $excerpt The post excerpt.
     106         * @param int    $post_id The post ID.
     107         * @return string Filtered excerpt.
     108         */
     109        static function translate_excerpt( $excerpt, $post_id ) {
     110                $post = get_post( $post_id );
     111                if ( ! $post || 'plugin' !== $post->post_type ) {
     112                        return $excerpt;
     113                }
     114
     115                $translated_plugin = self::get_translated_plugin_post( $post->ID );
     116                if ( ! $translated_plugin || ! $translated_plugin->post_excerpt ) {
     117                        return $excerpt;
     118                }
     119
     120                return $translated_plugin->post_excerpt;
     121        }
     122
     123        /**
     124         * Translates the screenshot descriptions of a plugin.
     125         *
     126         * @param null|array|string $value     The value get_metadata() should return - a single metadata value,
     127         *                                     or an array of values.
     128         * @param int               $object_id Object ID.
     129         * @param string            $meta_key  Meta key.
     130         * @param bool              $single    Whether to return only the first value of the specified $meta_key.
     131         * @return mixed Will be an array if $single is false. Will be value of meta data
     132         *               field if $single is true.
     133         */
     134        static function translate_screenshot_descriptions( $value, $object_id, $meta_key, $single ) {
     135                if ( 'screenshots' !== $meta_key ) {
     136                        return $value;
     137                }
     138
     139                $post = get_post( $object_id );
     140                if ( ! $post || 'plugin' !== $post->post_type ) {
     141                        return $value;
     142                }
     143
     144                $translated_plugin = self::get_translated_plugin_post( $post->ID );
     145                if ( ! $translated_plugin ) {
     146                        return $value;
     147                }
     148
     149                $translated_value = get_post_meta( $translated_plugin->ID, 'screenshots', false );
     150                if ( ! $translated_value ) {
     151                        return $value;
     152                }
     153
     154                return $translated_value;
     155        }
     156
     157        /**
    30158         * A private method to hold a list of the strings contained within the Database.
    31159         *
    32160         * 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( 'wporg_plugins_content', array( __NAMESPACE__ . '\i18n', 'translate_content' ) ); // @todo get_the_content
     229                        add_filter( 'get_the_excerpt', array( __NAMESPACE__ . '\i18n', 'translate_excerpt', 10, 2 ) );
     230                        add_filter( 'get_post_metadata', array( __NAMESPACE__ . '\i18n', 'translate_screenshot_descriptions' ), 10, 4 );
    219231                }
    220232
    221233                // Instantiate our copy of the Jetpack_Search class.
  • trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/template-parts/plugin-single.php

     
    1111use WordPressdotorg\Plugin_Directory\Plugin_Directory;
    1212use WordPressdotorg\Plugin_Directory\Template;
    1313
    14 $content = call_user_func( array( Plugin_Directory::instance(), 'split_post_content_into_pages' ), get_the_content() );
     14$content = apply_filters( 'wporg_plugins_content', get_the_content() );
     15$content = call_user_func( array( Plugin_Directory::instance(), 'split_post_content_into_pages' ), $content );
    1516
    1617$widget_args = array(
    1718        'before_title' => '<h4 class="widget-title">',
     
    5960                ?>
    6061
    6162        </div><!-- .entry-meta -->
    62 </article><!-- #post-## -->
    63  No newline at end of file
     63</article><!-- #post-## -->