| 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 | /** |