| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Allows to download translations from TranslationsPress |
| 8 | 5 | * This is a modified version of the library available at https://github.com/WP-Translations/t15s-registry |
| @@ -10,44 +7,16 @@ | ||
| 10 | 7 | * |
| 11 | 8 | * @since 2.6 |
| 12 | 9 | */ |
| 13 | 10 | class PLL_T15S { |
| 14 | - /** | |
| 15 | - * Transient key | |
| 16 | - * | |
| 17 | - * @var string | |
| 18 | - */ | |
| 11 | + | |
| 19 | 12 | const TRANSIENT_KEY_PLUGIN = 't15s-registry-plugins'; |
| 20 | 13 | |
| 21 | - /** | |
| 22 | - * Project directory slug | |
| 23 | - * | |
| 24 | - * @var string | |
| 25 | - */ | |
| 26 | - private $slug = ''; | |
| 27 | - | |
| 28 | - /** | |
| 29 | - * Full GlotPress API URL for the project. | |
| 30 | - * | |
| 31 | - * @var string | |
| 32 | - */ | |
| 14 | + private $type = 'plugin'; | |
| 15 | + private $slug = ''; | |
| 33 | 16 | private $api_url = ''; |
| 34 | 17 | |
| 35 | 18 | /** |
| 36 | - * Installed translations. | |
| 37 | - * | |
| 38 | - * @var array | |
| 39 | - */ | |
| 40 | - private static $installed_translations; | |
| 41 | - | |
| 42 | - /** | |
| 43 | - * Available languages. | |
| 44 | - * | |
| 45 | - * @var array | |
| 46 | - */ | |
| 47 | - private static $available_languages; | |
| 48 | - | |
| 49 | - /** | |
| 50 | 19 | * Adds a new project to load translations for. |
| 51 | 20 | * |
| 52 | 21 | * @since 2.6 |
| 53 | 22 | * |
| @@ -59,9 +28,9 @@ | ||
| 59 | 28 | $this->api_url = $api_url; |
| 60 | 29 | |
| 61 | 30 | add_action( 'init', array( __CLASS__, 'register_clean_translations_cache' ), 9999 ); |
| 62 | 31 | add_filter( 'translations_api', array( $this, 'translations_api' ), 10, 3 ); |
| 63 | - add_filter( 'site_transient_update_plugins', array( $this, 'site_transient_update_plugins' ) ); | |
| 32 | + add_filter( 'site_transient_update_' . $this->type . 's', array( $this, 'site_transient_update_plugins' ) ); | |
| 64 | 33 | } |
| 65 | 34 | |
| 66 | 35 | /** |
| 67 | 36 | * Short-circuits translations API requests for private projects. |
| @@ -73,10 +42,10 @@ | ||
| 73 | 42 | * @param object $args Translation API arguments. |
| 74 | 43 | * @return bool|array |
| 75 | 44 | */ |
| 76 | 45 | public function translations_api( $result, $requested_type, $args ) { |
| 77 | - if ( 'plugins' === $requested_type && $this->slug === $args['slug'] ) { | |
| 78 | - return self::get_translations( $args['slug'], $this->api_url ); | |
| 46 | + if ( $this->type . 's' === $requested_type && $this->slug === $args['slug'] ) { | |
| 47 | + return self::get_translations( $this->type, $args['slug'], $this->api_url ); | |
| 79 | 48 | } |
| 80 | 49 | |
| 81 | 50 | return $result; |
| 82 | 51 | } |
| @@ -98,18 +67,18 @@ | ||
| 98 | 67 | if ( ! isset( $value->translations ) ) { |
| 99 | 68 | $value->translations = array(); |
| 100 | 69 | } |
| 101 | 70 | |
| 102 | - $translations = self::get_translations( $this->slug, $this->api_url ); | |
| 71 | + $translations = self::get_translations( $this->type, $this->slug, $this->api_url ); | |
| 103 | 72 | |
| 104 | 73 | if ( ! isset( $translations['translations'] ) ) { |
| 105 | 74 | return $value; |
| 106 | 75 | } |
| 107 | 76 | |
| 108 | - $installed_translations = self::get_installed_translations(); | |
| 77 | + $installed_translations = wp_get_installed_translations( $this->type . 's' ); | |
| 109 | 78 | |
| 110 | 79 | foreach ( (array) $translations['translations'] as $translation ) { |
| 111 | - if ( in_array( $translation['language'], self::get_available_languages() ) ) { | |
| 80 | + if ( in_array( $translation['language'], get_available_languages() ) ) { | |
| 112 | 81 | if ( isset( $installed_translations[ $this->slug ][ $translation['language'] ] ) && $translation['updated'] ) { |
| 113 | 82 | $local = new DateTime( $installed_translations[ $this->slug ][ $translation['language'] ]['PO-Revision-Date'] ); |
| 114 | 83 | $remote = new DateTime( $translation['updated'] ); |
| 115 | 84 | |
| @@ -117,9 +86,9 @@ | ||
| 117 | 86 | continue; |
| 118 | 87 | } |
| 119 | 88 | } |
| 120 | 89 | |
| 121 | - $translation['type'] = 'plugin'; | |
| 90 | + $translation['type'] = $this->type; | |
| 122 | 91 | $translation['slug'] = $this->slug; |
| 123 | 92 | |
| 124 | 93 | $value->translations[] = $translation; |
| 125 | 94 | } |
| @@ -168,13 +137,14 @@ | ||
| 168 | 137 | * Gets the translations for a given project. |
| 169 | 138 | * |
| 170 | 139 | * @since 2.6 |
| 171 | 140 | * |
| 141 | + * @param string $type Project type. Either plugin or theme. | |
| 172 | 142 | * @param string $slug Project directory slug. |
| 173 | 143 | * @param string $url Full GlotPress API URL for the project. |
| 174 | 144 | * @return array Translation data. |
| 175 | 145 | */ |
| 176 | - private static function get_translations( $slug, $url ) { | |
| 146 | + private static function get_translations( $type, $slug, $url ) { | |
| 177 | 147 | $translations = get_site_transient( self::TRANSIENT_KEY_PLUGIN ); |
| 178 | 148 | |
| 179 | 149 | if ( ! is_object( $translations ) ) { |
| 180 | 150 | $translations = new stdClass(); |
| @@ -195,38 +165,6 @@ | ||
| 195 | 165 | $translations->_last_checked = time(); |
| 196 | 166 | |
| 197 | 167 | set_site_transient( self::TRANSIENT_KEY_PLUGIN, $translations ); |
| 198 | 168 | return $result; |
| 199 | - } | |
| 200 | - | |
| 201 | - /** | |
| 202 | - * Returns installed translations. | |
| 203 | - * | |
| 204 | - * Used to cache the result of wp_get_installed_translations() as it is very expensive. | |
| 205 | - * | |
| 206 | - * @since 2.8 | |
| 207 | - * | |
| 208 | - * @return array | |
| 209 | - */ | |
| 210 | - private static function get_installed_translations() { | |
| 211 | - if ( null === self::$installed_translations ) { | |
| 212 | - self::$installed_translations = wp_get_installed_translations( 'plugins' ); | |
| 213 | - } | |
| 214 | - return self::$installed_translations; | |
| 215 | - } | |
| 216 | - | |
| 217 | - /** | |
| 218 | - * Returns available languages. | |
| 219 | - * | |
| 220 | - * Used to cache the result of get_available_languages() as it is very expensive. | |
| 221 | - * | |
| 222 | - * @since 2.8 | |
| 223 | - * | |
| 224 | - * @return array | |
| 225 | - */ | |
| 226 | - private static function get_available_languages() { | |
| 227 | - if ( null === self::$available_languages ) { | |
| 228 | - self::$available_languages = get_available_languages(); | |
| 229 | - } | |
| 230 | - return self::$available_languages; | |
| 231 | 169 | } |
| 232 | 170 | } |