| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Allows to download translations from TranslationsPress |
| 5 |
* This is a modified version of the library available at https://github.com/WP-Translations/t15s-registry |
| 6 |
* This version aims to be compatible with PHP 5.2, and supports only plugins. |
| 7 |
* |
| 8 |
* @since 2.6 |
| 9 |
*/ |
| 10 |
class PLL_T15S { |
| 11 |
|
| 12 |
const TRANSIENT_KEY_PLUGIN = 't15s-registry-plugins'; |
| 13 |
|
| 14 |
private $type = 'plugin'; |
| 15 |
private $slug = ''; |
| 16 |
private $api_url = ''; |
| 17 |
|
| 18 |
/** |
| 19 |
* Adds a new project to load translations for. |
| 20 |
* |
| 21 |
* @since 2.6 |
| 22 |
* |
| 23 |
* @param string $slug Project directory slug. |
| 24 |
* @param string $api_url Full GlotPress API URL for the project. |
| 25 |
*/ |
| 26 |
public function __construct( $slug, $api_url ) { |
| 27 |
$this->slug = $slug; |
| 28 |
$this->api_url = $api_url; |
| 29 |
|
| 30 |
add_action( 'init', array( __CLASS__, 'register_clean_translations_cache' ), 9999 ); |
| 31 |
add_filter( 'translations_api', array( $this, 'translations_api' ), 10, 3 ); |
| 32 |
add_filter( 'site_transient_update_' . $this->type . 's', array( $this, 'site_transient_update_plugins' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Short-circuits translations API requests for private projects. |
| 37 |
* |
| 38 |
* @since 2.6 |
| 39 |
* |
| 40 |
* @param bool|array $result The result object. Default false. |
| 41 |
* @param string $requested_type The type of translations being requested. |
| 42 |
* @param object $args Translation API arguments. |
| 43 |
* @return bool|array |
| 44 |
*/ |
| 45 |
public function translations_api( $result, $requested_type, $args ) { |
| 46 |
if ( $this->type . 's' === $requested_type && $this->slug === $args['slug'] ) { |
| 47 |
return self::get_translations( $this->type, $args['slug'], $this->api_url ); |
| 48 |
} |
| 49 |
|
| 50 |
return $result; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Filters the translations transients to include the private plugin or theme. |
| 55 |
* |
| 56 |
* @see wp_get_translation_updates() |
| 57 |
* |
| 58 |
* @since 2.6 |
| 59 |
* |
| 60 |
* @param bool|array $value The transient value. |
| 61 |
*/ |
| 62 |
public function site_transient_update_plugins( $value ) { |
| 63 |
if ( ! $value ) { |
| 64 |
$value = new stdClass(); |
| 65 |
} |
| 66 |
|
| 67 |
if ( ! isset( $value->translations ) ) { |
| 68 |
$value->translations = array(); |
| 69 |
} |
| 70 |
|
| 71 |
$translations = self::get_translations( $this->type, $this->slug, $this->api_url ); |
| 72 |
|
| 73 |
if ( ! isset( $translations['translations'] ) ) { |
| 74 |
return $value; |
| 75 |
} |
| 76 |
|
| 77 |
$installed_translations = wp_get_installed_translations( $this->type . 's' ); |
| 78 |
|
| 79 |
foreach ( (array) $translations['translations'] as $translation ) { |
| 80 |
if ( in_array( $translation['language'], get_available_languages() ) ) { |
| 81 |
if ( isset( $installed_translations[ $this->slug ][ $translation['language'] ] ) && $translation['updated'] ) { |
| 82 |
$local = new DateTime( $installed_translations[ $this->slug ][ $translation['language'] ]['PO-Revision-Date'] ); |
| 83 |
$remote = new DateTime( $translation['updated'] ); |
| 84 |
|
| 85 |
if ( $local >= $remote ) { |
| 86 |
continue; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
$translation['type'] = $this->type; |
| 91 |
$translation['slug'] = $this->slug; |
| 92 |
|
| 93 |
$value->translations[] = $translation; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return $value; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Registers actions for clearing translation caches. |
| 102 |
* |
| 103 |
* @since 2.6 |
| 104 |
*/ |
| 105 |
public static function register_clean_translations_cache() { |
| 106 |
add_action( 'set_site_transient_update_plugins', array( __CLASS__, 'clean_translations_cache' ) ); |
| 107 |
add_action( 'delete_site_transient_update_plugins', array( __CLASS__, 'clean_translations_cache' ) ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Clears existing translation cache. |
| 112 |
* |
| 113 |
* @since 2.6 |
| 114 |
*/ |
| 115 |
public static function clean_translations_cache() { |
| 116 |
$translations = get_site_transient( self::TRANSIENT_KEY_PLUGIN ); |
| 117 |
|
| 118 |
if ( ! is_object( $translations ) ) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
/* |
| 123 |
* Don't delete the cache if the transient gets changed multiple times |
| 124 |
* during a single request. Set cache lifetime to maximum 15 seconds. |
| 125 |
*/ |
| 126 |
$cache_lifespan = 15; |
| 127 |
$time_not_changed = isset( $translations->_last_checked ) && ( time() - $translations->_last_checked ) > $cache_lifespan; |
| 128 |
|
| 129 |
if ( ! $time_not_changed ) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
delete_site_transient( self::TRANSIENT_KEY_PLUGIN ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Gets the translations for a given project. |
| 138 |
* |
| 139 |
* @since 2.6 |
| 140 |
* |
| 141 |
* @param string $type Project type. Either plugin or theme. |
| 142 |
* @param string $slug Project directory slug. |
| 143 |
* @param string $url Full GlotPress API URL for the project. |
| 144 |
* @return array Translation data. |
| 145 |
*/ |
| 146 |
private static function get_translations( $type, $slug, $url ) { |
| 147 |
$translations = get_site_transient( self::TRANSIENT_KEY_PLUGIN ); |
| 148 |
|
| 149 |
if ( ! is_object( $translations ) ) { |
| 150 |
$translations = new stdClass(); |
| 151 |
} |
| 152 |
|
| 153 |
if ( isset( $translations->{$slug} ) && is_array( $translations->{$slug} ) ) { |
| 154 |
return $translations->{$slug}; |
| 155 |
} |
| 156 |
|
| 157 |
$result = json_decode( wp_remote_retrieve_body( wp_remote_get( $url, array( 'timeout' => 3 ) ) ), true ); |
| 158 |
|
| 159 |
// Nothing found. |
| 160 |
if ( ! is_array( $result ) ) { |
| 161 |
$result = array(); |
| 162 |
} |
| 163 |
|
| 164 |
$translations->{$slug} = $result; |
| 165 |
$translations->_last_checked = time(); |
| 166 |
|
| 167 |
set_site_transient( self::TRANSIENT_KEY_PLUGIN, $translations ); |
| 168 |
return $result; |
| 169 |
} |
| 170 |
} |
| 171 |
|