Test Failed
Push — issues/1953 ( fc402a )
by Ravinder
05:33
created

Give_Translations::add_tooltip_translation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
13
	/**
14
	 * Instance.
15
	 *
16
	 * @since  2.0
17
	 * @access static
18
	 * @var
19
	 */
20
	static private $instance;
21
22
	/**
23
	 * Text config.
24
	 *
25
	 * @since  2.0
26
	 * @access static
27
	 * @var
28
	 */
29
	static private $text_configs = array();
30
31
	/**
32
	 * Translated texts.
33
	 *
34
	 * @since  2.0
35
	 * @access static
36
	 * @var
37
	 */
38
	static private $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 static
55
	 * @return static
56
	 */
57
	static function get_instance() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
			array_merge( self::$text_translations, get_option( 'give_text_translations', array() ) )
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
			} else if ( 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
			} else if (
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
			} else if (
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'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
		}
167
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
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 View Code Duplication
	public static function add_label( $args = array() ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
	public static function add_tooltip( $args = array() ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
		if ( ! empty( $args['group'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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( array( 'id' => "{$id}_label", 'group' => $group, 'text' => $text ) );
279
	}
280
281
	/**
282
	 * Get tooltip translation by group ( if any )
283
	 *
284
	 * @since  2.0
285
	 * @access public
286
	 *
287
	 * @param string $id
288
	 * @param string $group
289
	 * @param string $text
290
	 *
291
	 * @return string
292
	 */
293
	public static function add_tooltip_translation( $id, $group = '', $text = '' ) {
294
		return self::get_text( array( 'id' => "{$id}_label", 'group' => $group, 'text' => $text ) );
295
	}
296
297
	/**
298
	 * Get label by group ( if any )
299
	 *
300
	 * @since  2.0
301
	 * @access public
302
	 *
303
	 * @param string $id
304
	 * @param string $group
305
	 *
306
	 * @return string
307
	 */
308
	public static function get_label( $id, $group = '' ) {
309
		return self::get_text( array( 'id' => "{$id}_label", 'group' => $group, 'type' => 'label' ) );
310
	}
311
312
	/**
313
	 * Get tooltip by group ( if any )
314
	 *
315
	 * @since  2.0
316
	 * @access public
317
	 *
318
	 * @param string $id
319
	 * @param string $group
320
	 *
321
	 * @return string
322
	 */
323
	public static function get_tooltip( $id, $group = '' ) {
324
		return self::get_text( array( 'id' => "{$id}_tooltip", 'group' => $group, 'type' => 'tooltip' ) );
325
	}
326
327
	/**
328
	 * Get text by group
329
	 *
330
	 * @since  2.0
331
	 * @access public
332
	 *
333
	 * @param array $args
334
	 *
335
	 * @return string
336
	 */
337
	public static function get_text( $args = array() ) {
338
		$text = '';
339
340
		// Bailout.
341
		if ( empty( $args ) ) {
342
			return $text;
343
		}
344
345
		// Setup args.
346
		$args = wp_parse_args(
347
			$args,
348
			array(
349
				'id'    => '',
350
				'group' => '',
351
				'type'  => 'text',
352
			)
353
		);
354
355
		// Check if text exist.
356
		if (
357
			empty( $args['id'] ) ||
358
			( empty( $args['group'] ) && ! array_key_exists( $args['id'], self::$text_configs ) ) ||
359
			( ! empty( $args['group'] ) && ! empty( self::$text_configs[ $args['group'] ] ) && ! array_key_exists( $args['id'], self::$text_configs[ $args['group'] ] ) )
360
		) {
361
			return $text;
362
		}
363
364
		// Get text value.
365
		if (
366
			! empty( $args['group'] ) &&
367
			array_key_exists( $args['group'], self::$text_configs )
368
		) {
369
			$text = self::$text_configs[ $args['group'] ][ $args['id'] ]['text'];
370
371
			// Get translated text if exist.
372
			if (
373
				! empty( self::$text_translations ) &&
374
				! empty( self::$text_translations[ $args['group'] ] ) &&
375
				array_key_exists( $args['id'], self::$text_translations[ $args['group'] ] )
376
			) {
377
				$text = self::$text_translations[ $args['group'] ][ $args['id'] ];
378
			}
379
		} else if (
380
			empty( $args['group'] ) &&
381
			array_key_exists( $args['id'], self::$text_configs )
382
		) {
383
			$text = self::$text_configs[ $args['id'] ]['text'];
384
385
			// Get translated text if exist.
386
			if (
387
				! empty( self::$text_translations ) &&
388
				array_key_exists( $args['id'], self::$text_translations )
389
			) {
390
				$text = self::$text_translations[ $args['id'] ];
391
			}
392
		}
393
394
		/**
395
		 * Filter the give text
396
		 *
397
		 * @since 2.0
398
		 */
399
		$text = apply_filters( 'give_text', $text, $args, self::$text_configs, self::$text_translations );
400
401
		return $text;
402
	}
403
}
404
405
// Setup translations.
406
Give_Translations::get_instance()->setup();