admin
5 years ago
api
6 years ago
database
6 years ago
deprecated
6 years ago
donors
5 years ago
emails
6 years ago
forms
6 years ago
frontend
6 years ago
gateways
6 years ago
libraries
6 years ago
payments
6 years ago
actions.php
6 years ago
ajax-functions.php
6 years ago
class-give-async-process.php
6 years ago
class-give-background-updater.php
6 years ago
class-give-cache-setting.php
6 years ago
class-give-cache.php
6 years ago
class-give-cli-commands.php
6 years ago
class-give-comment.php
6 years ago
class-give-cron.php
6 years ago
class-give-donate-form.php
6 years ago
class-give-donor.php
6 years ago
class-give-email-access.php
6 years ago
class-give-license-handler.php
6 years ago
class-give-logging.php
6 years ago
class-give-readme-parser.php
6 years ago
class-give-roles.php
6 years ago
class-give-scripts.php
6 years ago
class-give-session.php
6 years ago
class-give-stats.php
6 years ago
class-give-template-loader.php
6 years ago
class-give-tooltips.php
6 years ago
class-give-translation.php
6 years ago
class-notices.php
6 years ago
country-functions.php
6 years ago
currencies-list.php
6 years ago
currency-functions.php
6 years ago
error-tracking.php
6 years ago
filters.php
6 years ago
formatting.php
6 years ago
install.php
6 years ago
login-register.php
6 years ago
misc-functions.php
5 years ago
plugin-compatibility.php
6 years ago
post-types.php
6 years ago
price-functions.php
6 years ago
process-donation.php
6 years ago
setting-functions.php
6 years ago
shortcodes.php
6 years ago
template-functions.php
6 years ago
user-functions.php
6 years ago
class-give-translation.php
430 lines
| 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 a group already exists.', '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 within a group already exists.', 'give' ), $args ); |
| 154 | throw new Exception( $error->get_error_message( 'TEXT_ID_WITHIN_GROUP_ALREADY_EXIST' ) ); |
| 155 | |
| 156 | } |
| 157 | |
| 158 | // Add text. |
| 159 | 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 | 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 | 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 | 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 | public static function add_label_translation( $id, $group = '', $text = '' ) { |
| 277 | return self::get_text( |
| 278 | array( |
| 279 | 'id' => "{$id}_label", |
| 280 | 'group' => $group, |
| 281 | 'text' => $text, |
| 282 | ) |
| 283 | ); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Get tooltip translation by group ( if any ) |
| 288 | * |
| 289 | * @since 2.0 |
| 290 | * @access public |
| 291 | * |
| 292 | * @param string $id |
| 293 | * @param string $group |
| 294 | * @param string $text |
| 295 | * |
| 296 | * @return string |
| 297 | */ |
| 298 | public static function add_tooltip_translation( $id, $group = '', $text = '' ) { |
| 299 | return self::get_text( |
| 300 | array( |
| 301 | 'id' => "{$id}_label", |
| 302 | 'group' => $group, |
| 303 | 'text' => $text, |
| 304 | ) |
| 305 | ); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Get label by group ( if any ) |
| 310 | * |
| 311 | * @since 2.0 |
| 312 | * @access public |
| 313 | * |
| 314 | * @param string $id |
| 315 | * @param string $group |
| 316 | * |
| 317 | * @return string |
| 318 | */ |
| 319 | public static function get_label( $id, $group = '' ) { |
| 320 | return self::get_text( |
| 321 | array( |
| 322 | 'id' => "{$id}_label", |
| 323 | 'group' => $group, |
| 324 | 'type' => 'label', |
| 325 | ) |
| 326 | ); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Get tooltip by group ( if any ) |
| 331 | * |
| 332 | * @since 2.0 |
| 333 | * @access public |
| 334 | * |
| 335 | * @param string $id |
| 336 | * @param string $group |
| 337 | * |
| 338 | * @return string |
| 339 | */ |
| 340 | public static function get_tooltip( $id, $group = '' ) { |
| 341 | return self::get_text( |
| 342 | array( |
| 343 | 'id' => "{$id}_tooltip", |
| 344 | 'group' => $group, |
| 345 | 'type' => 'tooltip', |
| 346 | ) |
| 347 | ); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Get text by group |
| 352 | * |
| 353 | * @since 2.0 |
| 354 | * @access public |
| 355 | * |
| 356 | * @param array $args |
| 357 | * |
| 358 | * @return string |
| 359 | */ |
| 360 | public static function get_text( $args = array() ) { |
| 361 | $text = ''; |
| 362 | |
| 363 | // Bailout. |
| 364 | if ( empty( $args ) ) { |
| 365 | return $text; |
| 366 | } |
| 367 | |
| 368 | // Setup args. |
| 369 | $args = wp_parse_args( |
| 370 | $args, |
| 371 | array( |
| 372 | 'id' => '', |
| 373 | 'group' => '', |
| 374 | 'type' => 'text', |
| 375 | ) |
| 376 | ); |
| 377 | |
| 378 | // Check if text exist. |
| 379 | if ( |
| 380 | empty( $args['id'] ) || |
| 381 | ( empty( $args['group'] ) && ! array_key_exists( $args['id'], self::$text_configs ) ) || |
| 382 | ( ! empty( $args['group'] ) && ! empty( self::$text_configs[ $args['group'] ] ) && ! array_key_exists( $args['id'], self::$text_configs[ $args['group'] ] ) ) |
| 383 | ) { |
| 384 | return $text; |
| 385 | } |
| 386 | |
| 387 | // Get text value. |
| 388 | if ( |
| 389 | ! empty( $args['group'] ) && |
| 390 | array_key_exists( $args['group'], self::$text_configs ) |
| 391 | ) { |
| 392 | $text = self::$text_configs[ $args['group'] ][ $args['id'] ]['text']; |
| 393 | |
| 394 | // Get translated text if exist. |
| 395 | if ( |
| 396 | ! empty( self::$text_translations ) && |
| 397 | ! empty( self::$text_translations[ $args['group'] ] ) && |
| 398 | array_key_exists( $args['id'], self::$text_translations[ $args['group'] ] ) |
| 399 | ) { |
| 400 | $text = self::$text_translations[ $args['group'] ][ $args['id'] ]; |
| 401 | } |
| 402 | } elseif ( |
| 403 | empty( $args['group'] ) && |
| 404 | array_key_exists( $args['id'], self::$text_configs ) |
| 405 | ) { |
| 406 | $text = self::$text_configs[ $args['id'] ]['text']; |
| 407 | |
| 408 | // Get translated text if exist. |
| 409 | if ( |
| 410 | ! empty( self::$text_translations ) && |
| 411 | array_key_exists( $args['id'], self::$text_translations ) |
| 412 | ) { |
| 413 | $text = self::$text_translations[ $args['id'] ]; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Filter the give text |
| 419 | * |
| 420 | * @since 2.0 |
| 421 | */ |
| 422 | $text = apply_filters( 'give_text', $text, $args, self::$text_configs, self::$text_translations ); |
| 423 | |
| 424 | return $text; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // Setup translations. |
| 429 | Give_Translations::get_instance()->setup(); |
| 430 |