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