| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Allows to download translations from TranslationsPress |
| 8 |
* This is a modified version of the library available at https://github.com/WP-Translations/t15s-registry |
| 9 |
* This version aims to be compatible with PHP 5.2, and supports only plugins. |
| 10 |
* |
| 11 |
* @since 2.6 |
| 12 |
*/ |
| 13 |
class PLL_T15S { |
| 14 |
/** |
| 15 |
* Transient key |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
const TRANSIENT_KEY_PLUGIN = 't15s-registry-plugins'; |
| 20 |
|
| 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 |
*/ |
| 33 |
private $api_url = ''; |
| 34 |
|
| 35 |
/** |
| 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 |
* Adds a new project to load translations for. |
| 51 |
* |
| 52 |
* @since 2.6 |
| 53 |
* |
| 54 |
* @param string $slug Project directory slug. |
| 55 |
* @param string $api_url Full GlotPress API URL for the project. |
| 56 |
*/ |
| 57 |
public function __construct( $slug, $api_url ) { |
| 58 |
$this->slug = $slug; |
| 59 |
$this->api_url = $api_url; |
| 60 |
|
| 61 |
add_action( 'init', array( __CLASS__, 'register_clean_translations_cache' ), 9999 ); |
| 62 |
add_filter( 'translations_api', array( $this, 'translations_api' ), 10, 3 ); |
| 63 |
add_filter( 'site_transient_update_plugins', array( $this, 'site_transient_update_plugins' ) ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Short-circuits translations API requests for private projects. |
| 68 |
* |
| 69 |
* @since 2.6 |
| 70 |
* |
| 71 |
* @param bool|array $result The result object. Default false. |
| 72 |
* @param string $requested_type The type of translations being requested. |
| 73 |
* @param object $args Translation API arguments. |
| 74 |
* @return bool|array |
| 75 |
*/ |
| 76 |
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 ); |
| 79 |
} |
| 80 |
|
| 81 |
return $result; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Filters the translations transients to include the private plugin or theme. |
| 86 |
* |
| 87 |
* @see wp_get_translation_updates() |
| 88 |
* |
| 89 |
* @since 2.6 |
| 90 |
* |
| 91 |
* @param bool|array $value The transient value. |
| 92 |
* @return bool|array |
| 93 |
*/ |
| 94 |
public function site_transient_update_plugins( $value ) { |
| 95 |
if ( ! $value ) { |
| 96 |
$value = new stdClass(); |
| 97 |
} |
| 98 |
|
| 99 |
if ( ! isset( $value->translations ) ) { |
| 100 |
$value->translations = array(); |
| 101 |
} |
| 102 |
|
| 103 |
$translations = self::get_translations( $this->slug, $this->api_url ); |
| 104 |
|
| 105 |
if ( ! isset( $translations['translations'] ) ) { |
| 106 |
return $value; |
| 107 |
} |
| 108 |
|
| 109 |
$installed_translations = self::get_installed_translations(); |
| 110 |
|
| 111 |
foreach ( (array) $translations['translations'] as $translation ) { |
| 112 |
if ( in_array( $translation['language'], self::get_available_languages() ) ) { |
| 113 |
if ( isset( $installed_translations[ $this->slug ][ $translation['language'] ] ) && $translation['updated'] ) { |
| 114 |
$local = new DateTime( $installed_translations[ $this->slug ][ $translation['language'] ]['PO-Revision-Date'] ); |
| 115 |
$remote = new DateTime( $translation['updated'] ); |
| 116 |
|
| 117 |
if ( $local >= $remote ) { |
| 118 |
continue; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
$translation['type'] = 'plugin'; |
| 123 |
$translation['slug'] = $this->slug; |
| 124 |
|
| 125 |
$value->translations[] = $translation; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return $value; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Registers actions for clearing translation caches. |
| 134 |
* |
| 135 |
* @since 2.6 |
| 136 |
* |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
public static function register_clean_translations_cache() { |
| 140 |
add_action( 'set_site_transient_update_plugins', array( __CLASS__, 'clean_translations_cache' ) ); |
| 141 |
add_action( 'delete_site_transient_update_plugins', array( __CLASS__, 'clean_translations_cache' ) ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Clears existing translation cache. |
| 146 |
* |
| 147 |
* @since 2.6 |
| 148 |
* |
| 149 |
* @return void |
| 150 |
*/ |
| 151 |
public static function clean_translations_cache() { |
| 152 |
$translations = get_site_transient( self::TRANSIENT_KEY_PLUGIN ); |
| 153 |
|
| 154 |
if ( ! is_object( $translations ) ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
* Don't delete the cache if the transient gets changed multiple times |
| 160 |
* during a single request. Set cache lifetime to maximum 15 seconds. |
| 161 |
*/ |
| 162 |
$cache_lifespan = 15; |
| 163 |
$time_not_changed = isset( $translations->_last_checked ) && ( time() - $translations->_last_checked ) > $cache_lifespan; |
| 164 |
|
| 165 |
if ( ! $time_not_changed ) { |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
delete_site_transient( self::TRANSIENT_KEY_PLUGIN ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Gets the translations for a given project. |
| 174 |
* |
| 175 |
* @since 2.6 |
| 176 |
* |
| 177 |
* @param string $slug Project directory slug. |
| 178 |
* @param string $url Full GlotPress API URL for the project. |
| 179 |
* @return array Translation data. |
| 180 |
*/ |
| 181 |
private static function get_translations( $slug, $url ) { |
| 182 |
$translations = get_site_transient( self::TRANSIENT_KEY_PLUGIN ); |
| 183 |
|
| 184 |
if ( ! is_object( $translations ) ) { |
| 185 |
$translations = new stdClass(); |
| 186 |
} |
| 187 |
|
| 188 |
if ( isset( $translations->{$slug} ) && is_array( $translations->{$slug} ) ) { |
| 189 |
return $translations->{$slug}; |
| 190 |
} |
| 191 |
|
| 192 |
$result = json_decode( wp_remote_retrieve_body( wp_remote_get( $url, array( 'timeout' => 3 ) ) ), true ); |
| 193 |
|
| 194 |
// Nothing found. |
| 195 |
if ( ! is_array( $result ) ) { |
| 196 |
$result = array(); |
| 197 |
} |
| 198 |
|
| 199 |
$translations->{$slug} = $result; |
| 200 |
$translations->_last_checked = time(); |
| 201 |
|
| 202 |
set_site_transient( self::TRANSIENT_KEY_PLUGIN, $translations ); |
| 203 |
return $result; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Returns installed translations. |
| 208 |
* |
| 209 |
* Used to cache the result of wp_get_installed_translations() as it is very expensive. |
| 210 |
* |
| 211 |
* @since 2.8 |
| 212 |
* |
| 213 |
* @return array |
| 214 |
*/ |
| 215 |
private static function get_installed_translations() { |
| 216 |
if ( null === self::$installed_translations ) { |
| 217 |
self::$installed_translations = wp_get_installed_translations( 'plugins' ); |
| 218 |
} |
| 219 |
return self::$installed_translations; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Returns available languages. |
| 224 |
* |
| 225 |
* Used to cache the result of get_available_languages() as it is very expensive. |
| 226 |
* |
| 227 |
* @since 2.8 |
| 228 |
* |
| 229 |
* @return array |
| 230 |
*/ |
| 231 |
private static function get_available_languages() { |
| 232 |
if ( null === self::$available_languages ) { |
| 233 |
self::$available_languages = get_available_languages(); |
| 234 |
} |
| 235 |
return self::$available_languages; |
| 236 |
} |
| 237 |
} |
| 238 |
|