Completed
Pull Request — 2.x (#4187)
by Scott Kingsley
06:00
created

PodsI18n::default_strings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 67
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 42
nc 1
nop 0
dl 0
loc 67
rs 9.2815
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package Pods
4
 * @since   2.7
5
 */
6
final class PodsI18n {
7
8
	/**
9
	 * @var PodsI18n Singleton instance
10
	 */
11
	private static $instance = null;
12
13
	/**
14
	 * @var array Key/value pairs with label/translation
15
	 */
16
	private static $strings = array();
17
18
	/**
19
	 * @var mixed Current language locale
20
	 */
21
	private static $current_language = null;
22
23
	/**
24
	 * @var mixed Current language data
25
	 */
26
	private static $current_language_data = null;
27
28
	/**
29
	 * Singleton handling for a basic pods_i18n() request
30
	 *
31
	 * @since 2.7
32
	 */
33
	private function __construct() {
34
		self::$instance = $this;
35
36
		// Hook all enqueue scripts actions
37
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
38
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
39
		add_action( 'login_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
40
	}
41
42
	/**
43
	 * Singleton handling for a basic pods_i18n() request
44
	 *
45
	 * @return \PodsI18n
46
	 *
47
	 * @since 2.7
48
	 */
49
	public static function get_instance() {
50
51
		// Initialize if the class hasn't been setup yet for some reason
52
		if ( ! is_object( self::$instance ) ) {
53
			self::$instance = new self();
54
		}
55
56
		return self::$instance;
57
	}
58
59
	/**
60
	 * @since 2.7
61
	 */
62
	public function enqueue_scripts() {
63
64
		// Register our i18n script for JS
65
		wp_register_script( 'pods-i18n', PODS_URL . 'ui/js/pods-i18n.js', array(), PODS_VERSION, true );
66
67
		self::localize_assets();
68
	}
69
70
	/**
71
	 * Localize assets:
72
	 *     * Build localizations strings from the defaults and those provided via filter
73
	 *     * Provide a global JavaScript object with the assembled localization strings via `wp_localize_script`
74
	 *
75
	 * @since 2.7
76
	 */
77
	private static function localize_assets() {
78
79
		/**
80
		 * Add strings to the localization
81
		 * Setting the key of your string to the original (non translated) value is mandatory
82
		 * Note: Existing keys in this class will overwrite the ones of this filter!
83
		 *
84
		 * @since 2.7
85
		 * @see   default_strings()
86
		 *
87
		 * @param array
88
		 *
89
		 * @return array format: 'Untranslated string' => 'Translated string with use of WP translate functions'
90
		 */
91
		$strings_extra = apply_filters( 'pods_localized_strings', array() );
92
93
		self::$strings = array_merge( $strings_extra, self::default_strings() );
94
95
		foreach ( self::$strings as $key => $str ) {
96
			self::register( $key, $str );
97
		}
98
99
		// Some other stuff we need to pass through
100
		$i18n_base = array(
101
			'debug' => ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG == true ) ? true : false,
102
		);
103
		// Add localization to our i18n script
104
		wp_localize_script( 'pods-i18n', 'podsLocalizedStrings', array_merge( self::$strings, $i18n_base ) );
105
	}
106
107
	/**
108
	 * Register function that creates the references and combines these with the translated strings
109
	 *
110
	 * @param string $string_key
111
	 * @param string $translation
112
	 *
113
	 * @since 2.7
114
	 */
115
	private static function register( $string_key, $translation ) {
116
117
		/**
118
		 * Converts string into reference object variable
119
		 * Uses the same logic as JS to create the same references
120
		 */
121
		$ref = '__' . $string_key;
122
123
		// Add it to the strings localized
124
		self::$strings[ $ref ] = $translation;
125
126
		// Remove the old key
127
		unset( self::$strings[ $string_key ] );
128
	}
129
130
	/**
131
	 * Register our labels to use in JS
132
	 * We need to register them as normal string to convert to JS references
133
	 * And we need to register the translations to attach to these references, these may not be variables!
134
	 *
135
	 * @return array Key/value pairs with label/translation
136
	 *
137
	 * @since 2.7
138
	 */
139
	private static function default_strings() {
140
141
		return array(
142
143
			'%s is required.' =>
144
				__( '%s is required.', 'pods' ),
145
146
			'This field is required.' =>
147
				__( 'This field is required.', 'pods' ),
148
149
			'Add' =>
150
				__( 'Add', 'pods' ),
151
152
			'Add New' =>
153
				__( 'Add New', 'pods' ),
154
155
			'Add New Record' =>
156
				__( 'Add New Record', 'pods' ),
157
158
			'Added!' =>
159
				__( 'Added!', 'pods' ),
160
161
			'Added! Choose another or <a href="#">close this box</a>' =>
162
				__( 'Added! Choose another or <a href="#">close this box</a>', 'pods' ),
163
164
			'Copy' =>
165
				__( 'Copy', 'pods' ),
166
167
			'Reorder' =>
168
				__( 'Reorder', 'pods' ),
169
170
			'Remove' =>
171
				__( 'Remove', 'pods' ),
172
173
			'Download' =>
174
				__( 'Download', 'pods' ),
175
176
			'View' =>
177
				__( 'View', 'pods' ),
178
179
			'Edit' =>
180
				__( 'Edit', 'pods' ),
181
182
			'Navigating away from this page will discard any changes you have made.' =>
183
				__( 'Navigating away from this page will discard any changes you have made.', 'pods' ),
184
185
			'Unable to process request, please try again.' =>
186
				__( 'Unable to process request, please try again.', 'pods' ),
187
188
			'Error uploading file: ' =>
189
				__( 'Error uploading file: ', 'pods' ),
190
191
			'Allowed Files' =>
192
				__( 'Allowed Files', 'pods' ),
193
194
			'The Title' =>
195
				__( 'The Title', 'pods' ),
196
197
			'Select from existing' =>
198
				__( 'Select from existing', 'pods' ),
199
200
			'Icon' =>
201
				__( 'Icon', 'pods' ),
202
203
		);
204
205
	}
206
207
	/**
208
	 * Get current locale information from Multilingual plugins
209
	 *
210
	 * @since 2.7
211
	 *
212
	 * @param array $args (optional) {
213
	 *     @type bool $refresh Rerun get_current_language() logic?
214
	 * }
215
	 *
216
	 * @return string
217
	 */
218
	public function get_current_language( $args = array() ) {
219
220
		$args = wp_parse_args( $args, array(
221
			'refresh' => false,
222
		) );
223
224
		if ( ! $args['refresh'] && ! empty( self::$current_language ) ) {
225
			return self::$current_language;
226
		}
227
228
		$this->get_current_language_data( $args );
229
		return self::$current_language;
230
	}
231
232
	/**
233
	 * Get current language information from Multilingual plugins
234
	 *
235
	 * @since 2.6.6
236
	 * @since 2.7 Moved to this class from PodsAPI
237
	 *
238
	 * @param array $args (optional) {
239
	 *     @type bool $refresh Rerun logic?
240
	 * }
241
	 *
242
	 * @return array
243
	 */
244
	public function get_current_language_data( $args = array() ) {
245
246
		$args = wp_parse_args( $args, array(
247
			'refresh' => false,
248
		) );
249
250
		if ( ! $args['refresh'] && ! empty( self::$current_language_data ) ) {
251
			return self::$current_language_data;
252
		}
253
254
		/**
255
		 * @var $sitepress SitePress object
256
		 * @var $polylang  Polylang object
257
		 */
258
		/*
259
		 * @todo wpml-comp Remove global object usage
260
		 */
261
		global $sitepress, $polylang;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
262
263
		$lang_data        = false;
264
		$translator       = false;
265
		$current_language = false;
266
267
		// Multilingual support
268
		if ( did_action( 'wpml_loaded' ) && apply_filters( 'wpml_setting', true, 'auto_adjust_ids' ) ) {
269
			// WPML support
270
			$translator = 'WPML';
271
272
			// Get the global current language (if set)
273
			$wpml_language = apply_filters( 'wpml_current_language', null );
274
			$current_language = ( $wpml_language != 'all' ) ? $wpml_language : '';
275
276
		} elseif ( ( function_exists( 'PLL' ) || is_object( $polylang ) ) && function_exists( 'pll_current_language' ) ) {
277
			// Polylang support
278
			$translator = 'PLL';
279
280
			// Get the global current language (if set)
281
			$current_language = pll_current_language( 'slug' );
282
		}
283
284
		/**
285
		 * Admin functions that overwrite the current language
286
		 *
287
		 * @since 2.6.6
288
		 */
289
		if ( is_admin() && ! empty( $translator ) ) {
290
			if ( $translator == 'PLL' ) {
291
				/**
292
				 * Polylang support
293
				 * Get the current user's preferred language.
294
				 * This is a user meta setting that will overwrite the language returned from pll_current_language()
295
				 * @see polylang/admin/admin-base.php -> init_user()
296
				 */
297
				$current_language = get_user_meta( get_current_user_id(), 'pll_filter_content', true );
298
			}
299
300
			// Get current language based on the object language if available
301
			if ( function_exists( 'get_current_screen' ) ) {
302
				$current_screen = get_current_screen();
303
304
				/**
305
				 * Overwrite the current language if needed for post types
306
				 */
307
				if ( isset( $current_screen->base ) && ( $current_screen->base == 'post' || $current_screen->base == 'edit' ) ) {
308
					if ( ! empty( $_GET['post'] ) ) {
309
						/**
310
						 * WPML support
311
						 * In WPML the current language is always set to default on an edit screen
312
						 * We need to overwrite this when the current object is not-translatable to enable relationships with different languages
313
						 */
314
						if (   $translator == 'WPML'
315
						       && ! apply_filters( 'wpml_is_translated_post_type', false, ( get_post_type( $_GET['post'] ) ) )
316
						) {
317
							// Overwrite the current language to nothing if this is a NOT-translatable post_type
318
							$current_language = '';
319
						}
320
321
						/**
322
						 * Polylang support (1.5.4+)
323
						 * In polylang the preferred language could be anything.
324
						 * We only want the related objects if they are not translatable OR the same language as the current object
325
						 */
326
						if (   $translator == 'PLL'
327
						       && function_exists( 'pll_get_post_language' )
328
						       && pll_is_translated_post_type( get_post_type( $_GET['post'] ) )
329
						) {
330
							// Overwrite the current language if this is a translatable post_type
331
							$current_language = pll_get_post_language( (int) $_GET['post'] );
332
						}
333
					}
334
335
					/**
336
					 * Polylang support (1.0.1+)
337
					 * In polylang the preferred language could be anything.
338
					 * When we're adding a new object and language is set we only want the related objects if they are not translatable OR the same language
339
					 */
340 View Code Duplication
					if (   $translator == 'PLL'
341
					       && ! empty( $_GET['new_lang'] )
342
					       && ! empty( $_GET['post_type'] )
343
					       && pll_is_translated_post_type( sanitize_text_field( $_GET['post_type'] ) )
344
					) {
345
						$current_language = $_GET['new_lang'];
346
					}
347
348
					/**
349
					 * Overwrite the current language if needed for taxonomies
350
					 */
351
				} elseif ( isset( $current_screen->base ) && ( $current_screen->base == 'term' || $current_screen->base == 'edit-tags' ) ) {
352
					// @todo MAYBE: Similar function like get_post_type for taxonomies so we don't need to check for $_GET['taxonomy']
353
					if ( ! empty( $_GET['taxonomy'] ) ) {
354
						/*
355
						 * @todo wpml-comp API call for taxonomy needed!
356
						 * Suggested API call:
357
						 * add_filter( 'wpml_is_translated_taxonomy', $_GET['taxonomy'], 10, 2 );
358
						 */
359
						/**
360
						 * WPML support
361
						 * In WPML the current language is always set to default on an edit screen
362
						 * We need to overwrite this when the current object is not-translatable to enable relationships with different languages
363
						 */
364
						if (   $translator == 'WPML'
365
						       && method_exists( $sitepress, 'is_translated_taxonomy')
366
						       && ! $sitepress->is_translated_taxonomy( $_GET['taxonomy'] )
367
						) {
368
							// Overwrite the current language to nothing if this is a NOT-translatable taxonomy
369
							$current_language = '';
370
						}
371
372
						/**
373
						 * Polylang support (1.5.4+)
374
						 * In polylang the preferred language could be anything.
375
						 * We only want the related objects if they are not translatable OR the same language as the current object
376
						 */
377
						if (   $translator == 'PLL'
378
						       && ! empty( $_GET['tag_ID'] )
379
						       && function_exists( 'pll_get_term_language' )
380
						       && pll_is_translated_taxonomy( sanitize_text_field( $_GET['taxonomy'] ) )
381
						) {
382
							// Overwrite the current language if this is a translatable taxonomy
383
							$current_language = pll_get_term_language( (int) $_GET['tag_ID'] );
384
						}
385
					}
386
387
					/**
388
					 * Polylang support (1.0.1+)
389
					 * In polylang the preferred language could be anything.
390
					 * When we're adding a new object and language is set we only want the related objects if they are not translatable OR the same language
391
					 */
392 View Code Duplication
					if (   $translator == 'PLL'
393
					       && ! empty( $_GET['new_lang'] )
394
					       && ! empty( $_GET['taxonomy'] )
395
					       && pll_is_translated_taxonomy( sanitize_text_field( $_GET['taxonomy'] ) )
396
					) {
397
						$current_language = $_GET['new_lang'];
398
					}
399
				}
400
			}
401
		}
402
403
		$current_language = pods_sanitize( sanitize_text_field( $current_language ) );
404
405
		if ( ! empty( $current_language ) ) {
406
			// We need to return language data
407
			$lang_data = array(
408
				'language' => $current_language,
409
				't_id'     => 0,
410
				'tt_id'    => 0,
411
				'term'     => null,
412
			);
413
414
			/**
415
			 * Polylang support
416
			 * Get the language taxonomy object for the current language
417
			 */
418
			if ( $translator == 'PLL' ) {
419
				$current_language_t = false;
420
421
				// Get the language term object
422
				if ( function_exists( 'PLL' ) && isset( PLL()->model ) && method_exists( PLL()->model, 'get_language' ) ) {
423
					// Polylang 1.8 and newer
424
					$current_language_t = PLL()->model->get_language( $current_language );
425
				} elseif ( is_object( $polylang ) && isset( $polylang->model ) && method_exists( $polylang->model, 'get_language' ) ) {
426
					// Polylang 1.2 - 1.7.x
427
					$current_language_t = $polylang->model->get_language( $current_language );
428
				} elseif ( is_object( $polylang ) && method_exists( $polylang, 'get_language' ) ) {
429
					// Polylang 1.1.x and older
430
					$current_language_t = $polylang->get_language( $current_language );
431
				}
432
433
				// If the language object exists, add it!
434
				if ( $current_language_t && ! empty( $current_language_t->term_id ) ) {
435
					$lang_data['t_id']  = (int) $current_language_t->term_id;
436
					$lang_data['tt_id'] = (int) $current_language_t->term_taxonomy_id;
437
					$lang_data['tl_t_id'] = (int) $current_language_t->tl_term_id;
438
					$lang_data['tl_tt_id'] = (int) $current_language_t->tl_term_taxonomy_id;
439
					$lang_data['term']  = $current_language_t;
440
				}
441
			}
442
		}
443
444
		/**
445
		 * Override language data used by Pods.
446
		 *
447
		 * @since 2.6.6
448
		 *
449
		 * @param array|false    $lang_data {
450
		 *      Language data
451
		 *
452
		 *      @type string       $language  Language slug
453
		 *      @type int          $t_id      Language term_id
454
		 *      @type int          $tt_id     Language term_taxonomy_id
455
		 *      @type WP_Term      $term      Language term object
456
		 * }
457
		 * @param string|boolean $translator Language plugin used
458
		 */
459
		$lang_data = apply_filters( 'pods_get_current_language', $lang_data, $translator );
460
461
		self::$current_language = $lang_data['language'];
462
		self::$current_language_data = $lang_data;
463
464
		return $lang_data;
465
466
	}
467
468
}
469