-
Notifications
You must be signed in to change notification settings - Fork 22
/
i18n-module.php
executable file
·280 lines (239 loc) · 7.3 KB
/
i18n-module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* This class defines a promo box and checks your translation site's API for stats about it, then shows them to the user.
*/
class bctt_i18n {
/**
* Your translation site's logo
*
* @var string
*/
private $glotpress_logo;
/**
* Your translation site's name
*
* @var string
*/
private $glotpress_name;
/**
* Your translation site's URL
*
* @var string
*/
private $glotpress_url;
/**
* Hook where you want to show the promo box
*
* @var string
*/
private $hook;
/**
* Will contain the site's locale
*
* @access private
* @var string
*/
private $locale;
/**
* Will contain the locale's name, obtained from yoru translation site
*
* @access private
* @var string
*/
private $locale_name;
/**
* Will contain the percentage translated for the plugin translation project in the locale
*
* @access private
* @var int
*/
private $percent_translated;
/**
* Name of your plugin
*
* @var string
*/
private $plugin_name;
/**
* Project slug for the project on your translation site
*
* @var string
*/
private $project_slug;
/**
* URL to point to for registration links
*
* @var string
*/
private $register_url;
/**
* Your plugins textdomain
*
* @var string
*/
private $textdomain;
/**
* Indicates whether there's a translation available at all.
*
* @access private
* @var bool
*/
private $translation_exists;
/**
* Indicates whether the translation's loaded.
*
* @access private
* @var bool
*/
private $translation_loaded;
/**
* Class constructor
*
* @param array $args Contains the settings for the class.
*/
public function __construct( $args ) {
if ( ! is_admin() ) {
return;
}
$this->locale = get_locale();
if ( 'en_US' === $this->locale ) {
return;
}
$this->init( $args );
if ( ! $this->hide_promo() ) {
add_action( $this->hook, array( $this, 'promo' ) );
}
}
/**
* This is where you decide where to display the messages and where you set the plugin specific variables.
*
* @access private
*
* @param array $args
*/
private function init( $args ) {
foreach ( $args as $key => $arg ) {
$this->$key = $arg;
}
}
/**
* Check whether the promo should be hidden or not
*
* @access private
*
* @return bool
*/
private function hide_promo() {
$hide_promo = get_transient( 'yoast_i18n_' . $this->project_slug . '_promo_hide' );
if ( ! $hide_promo ) {
if ( filter_input( INPUT_GET, 'remove_i18n_promo', FILTER_VALIDATE_INT ) === 1 ) {
// No expiration time, so this would normally not expire, but it wouldn't be copied to other sites etc.
set_transient( 'yoast_i18n_' . $this->project_slug . '_promo_hide', true );
$hide_promo = true;
}
}
return $hide_promo;
}
/**
* Generates a promo message
*
* @access private
*
* @return bool|string $message
*/
private function promo_message() {
$message = false;
if ( $this->translation_exists && $this->translation_loaded && $this->percent_translated < 90 ) {
$message = __( 'As you can see, there is a translation of this plugin in %1$s. This translation is currently %3$d%% complete. We need your help to make it complete and to fix any errors. Please register at %4$s to help complete the translation to %1$s!', 'better-click-to-tweet' );
} else if ( ! $this->translation_loaded && $this->translation_exists ) {
$message = __( 'You\'re using WordPress in %1$s. While %2$s has been translated to %1$s for %3$d%%, it\'s not been shipped with the plugin yet. You can help! Register at %4$s to help complete the translation to %1$s!', 'better-click-to-tweet' );
} else if ( ! $this->translation_exists ) {
$message = __( 'You\'re using WordPress in a language we don\'t support yet. We\'d love for %2$s to be translated in that language too, but unfortunately, it isn\'t right now. You can change that! Register at %4$s to help translate it!', 'better-click-to-tweet' );
}
$registration_link = sprintf( '<a href="https://translate.wordpress.org/projects/wp-plugins/better-click-to-tweet/">%1$s</a>', esc_html( $this->glotpress_name ) );
$message = sprintf( $message, esc_html( $this->locale_name ), esc_html( $this->plugin_name ), $this->percent_translated, $registration_link );
return $message;
}
/**
* Outputs a promo box
*/
public function promo() {
$this->translation_details();
$message = $this->promo_message();
if ( $message ) {
echo '<div id="i18n_promo_box" style="border:1px solid #ccc;background-color:#fff;padding:1em 2em;max-width:100%;min-height:220px;">';
echo '<a href="' . esc_url( add_query_arg( array( 'remove_i18n_promo' => '1' ) ) ) . '" style="color:#333;text-decoration:none;font-weight:bold;font-size:16px;border:1px solid #ccc;padding:1px 4px;" class="alignright">X</a>';
if ( isset( $this->glotpress_logo ) && '' != $this->glotpress_logo ) {
echo '<a href="https://translate.wordpress.org/projects/wp-plugins/better-click-to-tweet/"><img style="float: right;margin: 15px 5px 5px 5px;padding: 0 1em;width: 200px;" src="' . esc_url( $this->glotpress_logo ) . '" alt="' . esc_attr( $this->glotpress_name ) . '"/></a>';
}
echo '<h2>' . sprintf( __( 'Translation of %s', 'better-click-to-tweet' ), esc_html( $this->plugin_name ) ) . '</h2>';
echo '<p>' . esc_html( $message ) . '</p>';
echo '<p><a href="https://translate.wordpress.org/projects/wp-plugins/better-click-to-tweet/">' . __( 'Register now »', 'better-click-to-tweet' ) . '</a></p>';
echo '</div>';
}
}
/**
* Try to find the transient for the translation set or retrieve them.
*
* @access private
*
* @return object|null
*/
private function find_or_initialize_translation_details() {
$set = get_transient( 'yoast_i18n_' . $this->project_slug . '_' . $this->locale );
if ( ! $set ) {
$set = $this->retrieve_translation_details();
set_transient( 'yoast_i18n_' . $this->project_slug . '_' . $this->locale, $set, DAY_IN_SECONDS );
}
return $set;
}
/**
* Try to get translation details from cache, otherwise retrieve them, then parse them.
*
* @access private
*/
private function translation_details() {
$set = $this->find_or_initialize_translation_details();
$this->translation_exists = ! is_null( $set );
$this->translation_loaded = is_textdomain_loaded( 'better-click-to-tweet' );
$this->parse_translation_set( $set );
}
/**
* Retrieve the translation details from Yoast Translate
*
* @access private
*
* @return object|null
*/
private function retrieve_translation_details() {
$api_url = trailingslashit( $this->glotpress_url ) . 'api/projects/' . $this->project_slug;
$resp = wp_remote_get( $api_url );
$body = wp_remote_retrieve_body( $resp );
unset( $resp );
if ( $body ) {
$body = json_decode( $body );
foreach ( $body->translation_sets as $set ) {
if ( $this->locale == $set->wp_locale ) {
return $set;
}
}
}
return null;
}
/**
* Set the needed private variables based on the results from Yoast Translate
*
* @param object $set The translation set
*
* @access private
*/
private function parse_translation_set( $set ) {
if ( $this->translation_exists && is_object( $set ) ) {
$this->locale_name = $set->name;
$this->percent_translated = $set->percent_translated;
} else {
$this->locale_name = '';
$this->percent_translated = '';
}
}
}