PluginProbe
Polylang / 2.8.2
Polylang v2.8.2
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / install / t15s.php

t15s.php in Polylang 2.8.2, at install/t15s.php

233 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 */
93 public function site_transient_update_plugins( $value ) {
94 if ( ! $value ) {
95 $value = new stdClass();
96 }
97
98 if ( ! isset( $value->translations ) ) {
99 $value->translations = array();
100 }
101
102 $translations = self::get_translations( $this->slug, $this->api_url );
103
104 if ( ! isset( $translations['translations'] ) ) {
105 return $value;
106 }
107
108 $installed_translations = self::get_installed_translations();
109
110 foreach ( (array) $translations['translations'] as $translation ) {
111 if ( in_array( $translation['language'], self::get_available_languages() ) ) {
112 if ( isset( $installed_translations[ $this->slug ][ $translation['language'] ] ) && $translation['updated'] ) {
113 $local = new DateTime( $installed_translations[ $this->slug ][ $translation['language'] ]['PO-Revision-Date'] );
114 $remote = new DateTime( $translation['updated'] );
115
116 if ( $local >= $remote ) {
117 continue;
118 }
119 }
120
121 $translation['type'] = 'plugin';
122 $translation['slug'] = $this->slug;
123
124 $value->translations[] = $translation;
125 }
126 }
127
128 return $value;
129 }
130
131 /**
132 * Registers actions for clearing translation caches.
133 *
134 * @since 2.6
135 */
136 public static function register_clean_translations_cache() {
137 add_action( 'set_site_transient_update_plugins', array( __CLASS__, 'clean_translations_cache' ) );
138 add_action( 'delete_site_transient_update_plugins', array( __CLASS__, 'clean_translations_cache' ) );
139 }
140
141 /**
142 * Clears existing translation cache.
143 *
144 * @since 2.6
145 */
146 public static function clean_translations_cache() {
147 $translations = get_site_transient( self::TRANSIENT_KEY_PLUGIN );
148
149 if ( ! is_object( $translations ) ) {
150 return;
151 }
152
153 /*
154 * Don't delete the cache if the transient gets changed multiple times
155 * during a single request. Set cache lifetime to maximum 15 seconds.
156 */
157 $cache_lifespan = 15;
158 $time_not_changed = isset( $translations->_last_checked ) && ( time() - $translations->_last_checked ) > $cache_lifespan;
159
160 if ( ! $time_not_changed ) {
161 return;
162 }
163
164 delete_site_transient( self::TRANSIENT_KEY_PLUGIN );
165 }
166
167 /**
168 * Gets the translations for a given project.
169 *
170 * @since 2.6
171 *
172 * @param string $slug Project directory slug.
173 * @param string $url Full GlotPress API URL for the project.
174 * @return array Translation data.
175 */
176 private static function get_translations( $slug, $url ) {
177 $translations = get_site_transient( self::TRANSIENT_KEY_PLUGIN );
178
179 if ( ! is_object( $translations ) ) {
180 $translations = new stdClass();
181 }
182
183 if ( isset( $translations->{$slug} ) && is_array( $translations->{$slug} ) ) {
184 return $translations->{$slug};
185 }
186
187 $result = json_decode( wp_remote_retrieve_body( wp_remote_get( $url, array( 'timeout' => 3 ) ) ), true );
188
189 // Nothing found.
190 if ( ! is_array( $result ) ) {
191 $result = array();
192 }
193
194 $translations->{$slug} = $result;
195 $translations->_last_checked = time();
196
197 set_site_transient( self::TRANSIENT_KEY_PLUGIN, $translations );
198 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 }
232 }
233