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