PluginProbe
Polylang / 3.1.4
Polylang v3.1.4
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
← All changes | install/t15s.php +79 -12 2.7.13.1.4 View file →
@@ -1,5 +1,8 @@
1 1 <?php
2 +/**
3 + * @package Polylang
4 + */
2 5
3 6 /**
4 7 * Allows to download translations from TranslationsPress
5 8 * This is a modified version of the library available at https://github.com/WP-Translations/t15s-registry
@@ -7,16 +10,44 @@
7 10 *
8 11 * @since 2.6
9 12 */
10 13 class PLL_T15S {
14 + /**
15 + * Transient key
16 + *
17 + * @var string
18 + */
19 + const TRANSIENT_KEY_PLUGIN = 't15s-registry-plugins';
11 20
12 - const TRANSIENT_KEY_PLUGIN = 't15s-registry-plugins';
21 + /**
22 + * Project directory slug
23 + *
24 + * @var string
25 + */
26 + private $slug = '';
13 27
14 - private $type = 'plugin';
15 - private $slug = '';
28 + /**
29 + * Full GlotPress API URL for the project.
30 + *
31 + * @var string
32 + */
16 33 private $api_url = '';
17 34
18 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 + /**
19 50 * Adds a new project to load translations for.
20 51 *
21 52 * @since 2.6
22 53 *
@@ -28,9 +59,9 @@
28 59 $this->api_url = $api_url;
29 60
30 61 add_action( 'init', array( __CLASS__, 'register_clean_translations_cache' ), 9999 );
31 62 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' ) );
63 + add_filter( 'site_transient_update_plugins', array( $this, 'site_transient_update_plugins' ) );
33 64 }
34 65
35 66 /**
36 67 * Short-circuits translations API requests for private projects.
@@ -42,10 +73,10 @@
42 73 * @param object $args Translation API arguments.
43 74 * @return bool|array
44 75 */
45 76 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 );
77 + if ( 'plugins' === $requested_type && $this->slug === $args['slug'] ) {
78 + return self::get_translations( $args['slug'], $this->api_url );
48 79 }
49 80
50 81 return $result;
51 82 }
@@ -57,8 +88,9 @@
57 88 *
58 89 * @since 2.6
59 90 *
60 91 * @param bool|array $value The transient value.
92 + * @return bool|array
61 93 */
62 94 public function site_transient_update_plugins( $value ) {
63 95 if ( ! $value ) {
64 96 $value = new stdClass();
@@ -67,18 +99,18 @@
67 99 if ( ! isset( $value->translations ) ) {
68 100 $value->translations = array();
69 101 }
70 102
71 - $translations = self::get_translations( $this->type, $this->slug, $this->api_url );
103 + $translations = self::get_translations( $this->slug, $this->api_url );
72 104
73 105 if ( ! isset( $translations['translations'] ) ) {
74 106 return $value;
75 107 }
76 108
77 - $installed_translations = wp_get_installed_translations( $this->type . 's' );
109 + $installed_translations = self::get_installed_translations();
78 110
79 111 foreach ( (array) $translations['translations'] as $translation ) {
80 - if ( in_array( $translation['language'], get_available_languages() ) ) {
112 + if ( in_array( $translation['language'], self::get_available_languages() ) ) {
81 113 if ( isset( $installed_translations[ $this->slug ][ $translation['language'] ] ) && $translation['updated'] ) {
82 114 $local = new DateTime( $installed_translations[ $this->slug ][ $translation['language'] ]['PO-Revision-Date'] );
83 115 $remote = new DateTime( $translation['updated'] );
84 116
@@ -86,9 +118,9 @@
86 118 continue;
87 119 }
88 120 }
89 121
90 - $translation['type'] = $this->type;
122 + $translation['type'] = 'plugin';
91 123 $translation['slug'] = $this->slug;
92 124
93 125 $value->translations[] = $translation;
94 126 }
@@ -100,8 +132,10 @@
100 132 /**
101 133 * Registers actions for clearing translation caches.
102 134 *
103 135 * @since 2.6
136 + *
137 + * @return void
104 138 */
105 139 public static function register_clean_translations_cache() {
106 140 add_action( 'set_site_transient_update_plugins', array( __CLASS__, 'clean_translations_cache' ) );
107 141 add_action( 'delete_site_transient_update_plugins', array( __CLASS__, 'clean_translations_cache' ) );
@@ -110,8 +144,10 @@
110 144 /**
111 145 * Clears existing translation cache.
112 146 *
113 147 * @since 2.6
148 + *
149 + * @return void
114 150 */
115 151 public static function clean_translations_cache() {
116 152 $translations = get_site_transient( self::TRANSIENT_KEY_PLUGIN );
117 153
@@ -137,14 +173,13 @@
137 173 * Gets the translations for a given project.
138 174 *
139 175 * @since 2.6
140 176 *
141 - * @param string $type Project type. Either plugin or theme.
142 177 * @param string $slug Project directory slug.
143 178 * @param string $url Full GlotPress API URL for the project.
144 179 * @return array Translation data.
145 180 */
146 - private static function get_translations( $type, $slug, $url ) {
181 + private static function get_translations( $slug, $url ) {
147 182 $translations = get_site_transient( self::TRANSIENT_KEY_PLUGIN );
148 183
149 184 if ( ! is_object( $translations ) ) {
150 185 $translations = new stdClass();
@@ -165,6 +200,38 @@
165 200 $translations->_last_checked = time();
166 201
167 202 set_site_transient( self::TRANSIENT_KEY_PLUGIN, $translations );
168 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;
169 236 }
170 237 }