|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Translations |
|
5
|
|
|
* |
|
6
|
|
|
* @package Give |
|
7
|
|
|
* @subpackage Classes/Give_Stats |
|
8
|
|
|
* @copyright Copyright (c) 2017, Give |
|
9
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
|
10
|
|
|
* @since 2.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
class Give_Translations { |
|
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Instance. |
|
15
|
|
|
* |
|
16
|
|
|
* @since 2.0 |
|
17
|
|
|
* @access private |
|
18
|
|
|
* @var |
|
19
|
|
|
*/ |
|
20
|
|
|
private static $instance; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Text config. |
|
24
|
|
|
* |
|
25
|
|
|
* @since 2.0 |
|
26
|
|
|
* @access private |
|
27
|
|
|
* @var |
|
28
|
|
|
*/ |
|
29
|
|
|
private static $text_configs = array(); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Translated texts. |
|
33
|
|
|
* |
|
34
|
|
|
* @since 2.0 |
|
35
|
|
|
* @access private |
|
36
|
|
|
* @var |
|
37
|
|
|
*/ |
|
38
|
|
|
private static $text_translations = array(); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Singleton pattern. |
|
42
|
|
|
* |
|
43
|
|
|
* @since 2.0 |
|
44
|
|
|
* @access private |
|
45
|
|
|
*/ |
|
46
|
|
|
private function __construct() { |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get instance. |
|
52
|
|
|
* |
|
53
|
|
|
* @since 2.0 |
|
54
|
|
|
* @access public |
|
55
|
|
|
* @return static |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function get_instance() { |
|
58
|
|
|
if ( null === static::$instance ) { |
|
59
|
|
|
self::$instance = new static(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return self::$instance; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Setup |
|
67
|
|
|
* |
|
68
|
|
|
* @since 2.0 |
|
69
|
|
|
* @access public |
|
70
|
|
|
*/ |
|
71
|
|
|
public function setup() { |
|
72
|
|
|
self::setup_hooks(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Setup hooks |
|
77
|
|
|
* |
|
78
|
|
|
* @since 2.0 |
|
79
|
|
|
* @access public |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setup_hooks() { |
|
82
|
|
|
add_action( 'init', array( $this, 'load_translated_texts' ), 999 ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Load translated texts. |
|
87
|
|
|
* |
|
88
|
|
|
* @since 2.0 |
|
89
|
|
|
* @access public |
|
90
|
|
|
*/ |
|
91
|
|
|
public function load_translated_texts() { |
|
92
|
|
|
/** |
|
93
|
|
|
* Filter the translated texts. |
|
94
|
|
|
* |
|
95
|
|
|
* @since 2.0 |
|
96
|
|
|
*/ |
|
97
|
|
|
self::$text_translations = apply_filters( |
|
98
|
|
|
'give_translated_texts', |
|
99
|
|
|
self::$text_translations |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Add text by group ( if any ) |
|
105
|
|
|
* |
|
106
|
|
|
* @since 2.0 |
|
107
|
|
|
* @access public |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $args |
|
110
|
|
|
* |
|
111
|
|
|
* @return bool|WP_Error false on success otherwise WP_Error object |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function add_text( $args = array() ) { |
|
114
|
|
|
$error = false; |
|
115
|
|
|
|
|
116
|
|
|
// Set text params. |
|
117
|
|
|
$args = wp_parse_args( |
|
118
|
|
|
$args, |
|
119
|
|
|
array( |
|
120
|
|
|
'text' => '', |
|
121
|
|
|
'id' => '', |
|
122
|
|
|
'group' => '', |
|
123
|
|
|
'type' => 'text', |
|
124
|
|
|
) |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
try { |
|
128
|
|
|
// Check for errors. |
|
129
|
|
|
if ( empty( $args['text'] ) ) { |
|
130
|
|
|
/* @var WP_Error $error */ |
|
131
|
|
|
$error = new WP_Error( 'EMPTY_TEXT', __( 'Empty string is not allowed.', 'give' ), $args ); |
|
132
|
|
|
throw new Exception( $error->get_error_message( 'EMPTY_TEXT' ) ); |
|
133
|
|
|
|
|
134
|
|
|
} elseif ( empty( $args['id'] ) ) { |
|
135
|
|
|
/* @var WP_Error $error */ |
|
136
|
|
|
$error = new WP_Error( 'EMPTY_ID', __( 'Empty ID is not allowed.', 'give' ), $args ); |
|
137
|
|
|
throw new Exception( $error->get_error_message( 'EMPTY_ID' ) ); |
|
138
|
|
|
|
|
139
|
|
|
} elseif ( |
|
140
|
|
|
empty( $args['group'] ) && |
|
141
|
|
|
array_key_exists( $args['id'], self::$text_configs ) |
|
142
|
|
|
) { |
|
143
|
|
|
/* @var WP_Error $error */ |
|
144
|
|
|
$error = new WP_Error( 'TEXT_ID_ALREADY_EXIST', __( 'Text ID without group already exist.', 'give' ), $args ); |
|
145
|
|
|
throw new Exception( $error->get_error_message( 'TEXT_ID_ALREADY_EXIST' ) ); |
|
146
|
|
|
|
|
147
|
|
|
} elseif ( |
|
148
|
|
|
! empty( $args['group'] ) && |
|
149
|
|
|
! empty( self::$text_configs[ $args['group'] ] ) && |
|
150
|
|
|
array_key_exists( $args['id'], self::$text_configs[ $args['group'] ] ) |
|
151
|
|
|
) { |
|
152
|
|
|
/* @var WP_Error $error */ |
|
153
|
|
|
$error = new WP_Error( 'TEXT_ID_WITHIN_GROUP_ALREADY_EXIST', __( 'Text ID with in group already exist.', 'give' ), $args ); |
|
154
|
|
|
throw new Exception( $error->get_error_message( 'TEXT_ID_WITHIN_GROUP_ALREADY_EXIST' ) ); |
|
155
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
// Add text. |
|
159
|
|
View Code Duplication |
if ( ! empty( $args['group'] ) ) { |
|
|
|
|
|
|
160
|
|
|
self::$text_configs[ $args['group'] ][ $args['id'] ] = $args; |
|
161
|
|
|
} else { |
|
162
|
|
|
self::$text_configs[ $args['id'] ] = $args; |
|
163
|
|
|
} |
|
164
|
|
|
} catch ( Exception $e ) { |
|
165
|
|
|
error_log( $e->getMessage() ); |
|
166
|
|
|
}// End try(). |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Filter the texts |
|
170
|
|
|
* |
|
171
|
|
|
* @since 2.0 |
|
172
|
|
|
*/ |
|
173
|
|
|
self::$text_configs = apply_filters( 'give_texts', self::$text_configs ); |
|
174
|
|
|
|
|
175
|
|
|
return $error; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Add label by group ( if any ) |
|
180
|
|
|
* |
|
181
|
|
|
* @since 2.0 |
|
182
|
|
|
* @access public |
|
183
|
|
|
* |
|
184
|
|
|
* @param array $args |
|
185
|
|
|
* |
|
186
|
|
|
* @return string |
|
187
|
|
|
*/ |
|
188
|
|
View Code Duplication |
public static function add_label( $args = array() ) { |
|
|
|
|
|
|
189
|
|
|
// Set text params. |
|
190
|
|
|
$args = wp_parse_args( |
|
191
|
|
|
$args, |
|
192
|
|
|
array( |
|
193
|
|
|
'text' => '', |
|
194
|
|
|
'id' => '', |
|
195
|
|
|
'group' => '', |
|
196
|
|
|
) |
|
197
|
|
|
); |
|
198
|
|
|
|
|
199
|
|
|
$args['type'] = 'label'; |
|
200
|
|
|
$args['id'] = "{$args['id']}_label"; |
|
201
|
|
|
|
|
202
|
|
|
return self::add_text( $args ); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Add tooltip by group ( if any ) |
|
207
|
|
|
* |
|
208
|
|
|
* @since 2.0 |
|
209
|
|
|
* @access public |
|
210
|
|
|
* |
|
211
|
|
|
* @param array $args |
|
212
|
|
|
* |
|
213
|
|
|
* @return string |
|
214
|
|
|
*/ |
|
215
|
|
View Code Duplication |
public static function add_tooltip( $args = array() ) { |
|
|
|
|
|
|
216
|
|
|
// Set text params. |
|
217
|
|
|
$args = wp_parse_args( |
|
218
|
|
|
$args, |
|
219
|
|
|
array( |
|
220
|
|
|
'text' => '', |
|
221
|
|
|
'id' => '', |
|
222
|
|
|
'group' => '', |
|
223
|
|
|
) |
|
224
|
|
|
); |
|
225
|
|
|
|
|
226
|
|
|
$args['type'] = 'tooltip'; |
|
227
|
|
|
$args['id'] = "{$args['id']}_tooltip"; |
|
228
|
|
|
|
|
229
|
|
|
return self::add_text( $args ); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Add translation by group ( if any ) |
|
234
|
|
|
* |
|
235
|
|
|
* @since 2.0 |
|
236
|
|
|
* @access public |
|
237
|
|
|
* |
|
238
|
|
|
* @param array 4args |
|
239
|
|
|
* |
|
240
|
|
|
* @return string |
|
241
|
|
|
*/ |
|
242
|
|
|
public static function add_translation( $args = array() ) { |
|
243
|
|
|
$args = wp_parse_args( |
|
244
|
|
|
$args, |
|
245
|
|
|
array( |
|
246
|
|
|
'id' => '', |
|
247
|
|
|
'group' => '', |
|
248
|
|
|
'text' => '', |
|
249
|
|
|
) |
|
250
|
|
|
); |
|
251
|
|
|
|
|
252
|
|
|
// Bailout. |
|
253
|
|
|
if ( empty( $args['id'] ) ) { |
|
254
|
|
|
return; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
View Code Duplication |
if ( ! empty( $args['group'] ) ) { |
|
|
|
|
|
|
258
|
|
|
self::$text_translations[ $args['group'] ][ $args['id'] ] = $args['text']; |
|
259
|
|
|
} else { |
|
260
|
|
|
self::$text_translations[ $args['id'] ] = $args['text']; |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Get label translation by group ( if any ) |
|
266
|
|
|
* |
|
267
|
|
|
* @since 2.0 |
|
268
|
|
|
* @access public |
|
269
|
|
|
* |
|
270
|
|
|
* @param string $id |
|
271
|
|
|
* @param string $group |
|
272
|
|
|
* @param string $text |
|
273
|
|
|
* |
|
274
|
|
|
* @return string |
|
275
|
|
|
*/ |
|
276
|
|
View Code Duplication |
public static function add_label_translation( $id, $group = '', $text = '' ) { |
|
|
|
|
|
|
277
|
|
|
return self::get_text( array( |
|
278
|
|
|
'id' => "{$id}_label", |
|
279
|
|
|
'group' => $group, |
|
280
|
|
|
'text' => $text, |
|
281
|
|
|
) ); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Get tooltip translation by group ( if any ) |
|
286
|
|
|
* |
|
287
|
|
|
* @since 2.0 |
|
288
|
|
|
* @access public |
|
289
|
|
|
* |
|
290
|
|
|
* @param string $id |
|
291
|
|
|
* @param string $group |
|
292
|
|
|
* @param string $text |
|
293
|
|
|
* |
|
294
|
|
|
* @return string |
|
295
|
|
|
*/ |
|
296
|
|
View Code Duplication |
public static function add_tooltip_translation( $id, $group = '', $text = '' ) { |
|
|
|
|
|
|
297
|
|
|
return self::get_text( array( |
|
298
|
|
|
'id' => "{$id}_label", |
|
299
|
|
|
'group' => $group, |
|
300
|
|
|
'text' => $text, |
|
301
|
|
|
) ); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Get label by group ( if any ) |
|
306
|
|
|
* |
|
307
|
|
|
* @since 2.0 |
|
308
|
|
|
* @access public |
|
309
|
|
|
* |
|
310
|
|
|
* @param string $id |
|
311
|
|
|
* @param string $group |
|
312
|
|
|
* |
|
313
|
|
|
* @return string |
|
314
|
|
|
*/ |
|
315
|
|
|
public static function get_label( $id, $group = '' ) { |
|
316
|
|
|
return self::get_text( array( |
|
317
|
|
|
'id' => "{$id}_label", |
|
318
|
|
|
'group' => $group, |
|
319
|
|
|
'type' => 'label', |
|
320
|
|
|
) ); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Get tooltip by group ( if any ) |
|
325
|
|
|
* |
|
326
|
|
|
* @since 2.0 |
|
327
|
|
|
* @access public |
|
328
|
|
|
* |
|
329
|
|
|
* @param string $id |
|
330
|
|
|
* @param string $group |
|
331
|
|
|
* |
|
332
|
|
|
* @return string |
|
333
|
|
|
*/ |
|
334
|
|
|
public static function get_tooltip( $id, $group = '' ) { |
|
335
|
|
|
return self::get_text( array( |
|
336
|
|
|
'id' => "{$id}_tooltip", |
|
337
|
|
|
'group' => $group, |
|
338
|
|
|
'type' => 'tooltip', |
|
339
|
|
|
) ); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Get text by group |
|
344
|
|
|
* |
|
345
|
|
|
* @since 2.0 |
|
346
|
|
|
* @access public |
|
347
|
|
|
* |
|
348
|
|
|
* @param array $args |
|
349
|
|
|
* |
|
350
|
|
|
* @return string |
|
351
|
|
|
*/ |
|
352
|
|
|
public static function get_text( $args = array() ) { |
|
353
|
|
|
$text = ''; |
|
354
|
|
|
|
|
355
|
|
|
// Bailout. |
|
356
|
|
|
if ( empty( $args ) ) { |
|
357
|
|
|
return $text; |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
// Setup args. |
|
361
|
|
|
$args = wp_parse_args( |
|
362
|
|
|
$args, |
|
363
|
|
|
array( |
|
364
|
|
|
'id' => '', |
|
365
|
|
|
'group' => '', |
|
366
|
|
|
'type' => 'text', |
|
367
|
|
|
) |
|
368
|
|
|
); |
|
369
|
|
|
|
|
370
|
|
|
// Check if text exist. |
|
371
|
|
|
if ( |
|
372
|
|
|
empty( $args['id'] ) || |
|
373
|
|
|
( empty( $args['group'] ) && ! array_key_exists( $args['id'], self::$text_configs ) ) || |
|
374
|
|
|
( ! empty( $args['group'] ) && ! empty( self::$text_configs[ $args['group'] ] ) && ! array_key_exists( $args['id'], self::$text_configs[ $args['group'] ] ) ) |
|
375
|
|
|
) { |
|
376
|
|
|
return $text; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
// Get text value. |
|
380
|
|
|
if ( |
|
381
|
|
|
! empty( $args['group'] ) && |
|
382
|
|
|
array_key_exists( $args['group'], self::$text_configs ) |
|
383
|
|
|
) { |
|
384
|
|
|
$text = self::$text_configs[ $args['group'] ][ $args['id'] ]['text']; |
|
385
|
|
|
|
|
386
|
|
|
// Get translated text if exist. |
|
387
|
|
|
if ( |
|
388
|
|
|
! empty( self::$text_translations ) && |
|
389
|
|
|
! empty( self::$text_translations[ $args['group'] ] ) && |
|
390
|
|
|
array_key_exists( $args['id'], self::$text_translations[ $args['group'] ] ) |
|
391
|
|
|
) { |
|
392
|
|
|
$text = self::$text_translations[ $args['group'] ][ $args['id'] ]; |
|
393
|
|
|
} |
|
394
|
|
|
} elseif ( |
|
395
|
|
|
empty( $args['group'] ) && |
|
396
|
|
|
array_key_exists( $args['id'], self::$text_configs ) |
|
397
|
|
|
) { |
|
398
|
|
|
$text = self::$text_configs[ $args['id'] ]['text']; |
|
399
|
|
|
|
|
400
|
|
|
// Get translated text if exist. |
|
401
|
|
|
if ( |
|
402
|
|
|
! empty( self::$text_translations ) && |
|
403
|
|
|
array_key_exists( $args['id'], self::$text_translations ) |
|
404
|
|
|
) { |
|
405
|
|
|
$text = self::$text_translations[ $args['id'] ]; |
|
406
|
|
|
} |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
/** |
|
410
|
|
|
* Filter the give text |
|
411
|
|
|
* |
|
412
|
|
|
* @since 2.0 |
|
413
|
|
|
*/ |
|
414
|
|
|
$text = apply_filters( 'give_text', $text, $args, self::$text_configs, self::$text_translations ); |
|
415
|
|
|
|
|
416
|
|
|
return $text; |
|
417
|
|
|
} |
|
418
|
|
|
} |
|
419
|
|
|
|
|
420
|
|
|
// Setup translations. |
|
421
|
|
|
Give_Translations::get_instance()->setup(); |
|
422
|
|
|
|